Tizen 2.0 Release
[framework/web/wrt-commons.git] / modules / widget_dao / dao / feature_dao.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * This file contains the definition of feature dao class.
18  *
19  * @file    widget_dao.cpp
20  * @author  Jaroslaw Osmanski (j.osmanski@samsung.com)
21  * @author  Pawel Sikorski (p.sikorski@samsung.com)
22  * @version 1.0
23  * @brief   This file contains the definition of feature configuration.
24  */
25 #include <stddef.h>
26 #include <dpl/wrt-dao-rw/feature_dao.h>
27 #include <dpl/foreach.h>
28 #include <dpl/db/orm.h>
29 #include <orm_generator_wrt.h>
30 #include <dpl/wrt-dao-ro/webruntime_database.h>
31
32 namespace WrtDB {
33 namespace FeatureDAO {
34
35 FeatureHandle RegisterFeature(const PluginMetafileData::Feature &feature,
36                               const DbPluginHandle pluginHandle)
37 {
38     Try
39     {
40         LogDebug("Registering Feature " << feature.m_name);
41         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
42
43         if (FeatureDAOReadOnly::isFeatureInstalled(feature.m_name)) {
44             LogError(" >> Feature " << feature.m_name <<
45                      " is already registered.");
46             transaction.Commit();
47             return -1;
48         }
49
50         using namespace DPL::DB::ORM;
51         using namespace DPL::DB::ORM::wrt;
52
53         //register feature
54         {
55             LogInfo("    |-- Registering feature " << feature.m_name);
56
57             FeaturesList::Row row;
58             row.Set_FeatureName(DPL::FromUTF8String(feature.m_name));
59             row.Set_PluginPropertiesId(pluginHandle);
60
61             WRT_DB_INSERT(insert, FeaturesList, &WrtDatabase::interface())
62             insert->Values(row);
63             insert->Execute();
64         }
65
66         FeatureHandle featureHandle =
67                 FeatureDAOReadOnly(feature.m_name).GetFeatureHandle();
68
69         //register device capabilities
70         // Device Capabilities is unused in current version
71         FOREACH(itdev, feature.m_deviceCapabilities)
72         {
73             int deviceCapID;
74
75             if (FeatureDAOReadOnly::isDeviceCapabilityInstalled(*itdev)) {
76                 LogInfo("    |    |--DeviceCap " << *itdev <<
77                         " already installed!");
78
79                 WRT_DB_SELECT(select, DeviceCapabilities, &WrtDatabase::interface())
80
81                 select->Where(Equals<DeviceCapabilities::DeviceCapName>(
82                                   DPL::FromUTF8String(*itdev)));
83
84                 deviceCapID =
85                     select->GetSingleValue<DeviceCapabilities::DeviceCapID>();
86             } else {
87                 LogInfo("    |    |--Register DeviceCap: " << *itdev);
88
89                 DeviceCapabilities::Row row;
90                 row.Set_DeviceCapName(DPL::FromUTF8String(*itdev));
91
92                 WRT_DB_INSERT(insert, DeviceCapabilities, &WrtDatabase::interface())
93                 insert->Values(row);
94                 deviceCapID = insert->Execute();
95             }
96
97             FeatureDeviceCapProxy::Row row;
98             row.Set_FeatureUUID(featureHandle);
99             row.Set_DeviceCapID(deviceCapID);
100
101             WRT_DB_INSERT(insert, FeatureDeviceCapProxy, &WrtDatabase::interface())
102             insert->Values(row);
103             insert->Execute();
104         }
105
106         transaction.Commit();
107
108         return featureHandle;
109     }
110     Catch(DPL::DB::SqlConnection::Exception::Base){
111         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
112                    "Failure during Registering Feature");
113     }
114 }
115
116 void UnregisterFeature(FeatureHandle featureHandle)
117 {
118     Try
119     {
120         LogDebug("Unregistering Feature " << featureHandle);
121         DPL::DB::ORM::wrt::ScopedTransaction transaction(
122                 &WrtDatabase::interface());
123
124         if (FeatureDAOReadOnly::isFeatureInstalled(featureHandle)) {
125             LogError("Feature handle is invalid");
126             return;
127         }
128
129         using namespace DPL::DB::ORM;
130         using namespace DPL::DB::ORM::wrt;
131
132         // Unregister DeviceCapabilities
133         FeatureDAOReadOnly::DeviceCapabilitiesList capabilitiesList =
134             FeatureDAOReadOnly(featureHandle).GetDeviceCapabilities();
135
136         FOREACH(it, capabilitiesList) {
137             WRT_DB_DELETE(del, DeviceCapabilities, &WrtDatabase::interface())
138             del->Where(
139                 Equals<DeviceCapabilities::DeviceCapName>(
140                     DPL::FromUTF8String(*it)));
141             del->Execute();
142         }
143
144         // Unregister Feature
145         WRT_DB_DELETE(del, FeaturesList, &WrtDatabase::interface())
146         del->Where(Equals<FeaturesList::FeatureUUID>(featureHandle));
147         del->Execute();
148         transaction.Commit();
149
150         return;
151     }
152     Catch(DPL::DB::SqlConnection::Exception::Base){
153         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
154                    "Fail to unregister Feature");
155     }
156 }
157
158
159 } // namespace FeatureDAO
160 } // namespace WrtDB