tizen 2.4 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 FeatureHandle RegisterFeature(const PluginMetafileData::Feature &feature,
35                               const DbPluginHandle pluginHandle)
36 {
37     Try
38     {
39         WrtLogD("Registering Feature %s", feature.m_name.c_str());
40         DPL::DB::ORM::wrt::ScopedTransaction transaction(
41             &WrtDatabase::interface());
42
43         if (FeatureDAOReadOnly::isFeatureInstalled(feature.m_name)) {
44             WrtLogE(" >> Feature %s is already registered.",
45                 feature.m_name.c_str());
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             WrtLogD("    |-- Registering feature %s", feature.m_name.c_str());
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                 WrtLogD("    |    |--DeviceCap %s already installed!",
77                     itdev->c_str());
78
79                 WRT_DB_SELECT(select,
80                               DeviceCapabilities,
81                               &WrtDatabase::interface())
82
83                 select->Where(Equals<DeviceCapabilities::DeviceCapName>(
84                                   DPL::FromUTF8String(*itdev)));
85
86                 deviceCapID =
87                     select->GetSingleValue<DeviceCapabilities::DeviceCapID>();
88             } else {
89                 WrtLogD("    |    |--Register DeviceCap: %s", itdev->c_str());
90
91                 DeviceCapabilities::Row row;
92                 row.Set_DeviceCapName(DPL::FromUTF8String(*itdev));
93
94                 WRT_DB_INSERT(insert,
95                               DeviceCapabilities,
96                               &WrtDatabase::interface())
97                 insert->Values(row);
98                 deviceCapID = static_cast<int>(insert->Execute());
99             }
100
101             FeatureDeviceCapProxy::Row row;
102             row.Set_FeatureUUID(featureHandle);
103             row.Set_DeviceCapID(deviceCapID);
104
105             WRT_DB_INSERT(insert,
106                           FeatureDeviceCapProxy,
107                           &WrtDatabase::interface())
108             insert->Values(row);
109             insert->Execute();
110         }
111
112         transaction.Commit();
113
114         return featureHandle;
115     }
116     Catch(DPL::DB::SqlConnection::Exception::Base){
117         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
118                    "Failure during Registering Feature");
119     }
120 }
121
122 void UnregisterFeature(FeatureHandle featureHandle)
123 {
124     Try
125     {
126         WrtLogD("Unregistering Feature %i", featureHandle);
127         DPL::DB::ORM::wrt::ScopedTransaction transaction(
128             &WrtDatabase::interface());
129
130         if (!FeatureDAOReadOnly::isFeatureInstalled(featureHandle)) {
131             WrtLogE("Feature handle is invalid");
132             return;
133         }
134
135         using namespace DPL::DB::ORM;
136         using namespace DPL::DB::ORM::wrt;
137
138         // Unregister DeviceCapabilities
139         FeatureDAOReadOnly::DeviceCapabilitiesList capabilitiesList =
140             FeatureDAOReadOnly(featureHandle).GetDeviceCapabilities();
141
142         FOREACH(it, capabilitiesList) {
143             WRT_DB_DELETE(del, DeviceCapabilities, &WrtDatabase::interface())
144             del->Where(
145                 Equals<DeviceCapabilities::DeviceCapName>(
146                     DPL::FromUTF8String(*it)));
147             del->Execute();
148         }
149
150         // Unregister Feature
151         WRT_DB_DELETE(del, FeaturesList, &WrtDatabase::interface())
152         del->Where(Equals<FeaturesList::FeatureUUID>(featureHandle));
153         del->Execute();
154         transaction.Commit();
155
156         return;
157     }
158     Catch(DPL::DB::SqlConnection::Exception::Base){
159         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
160                    "Fail to unregister Feature");
161     }
162 }
163 } // namespace FeatureDAO
164 } // namespace WrtDB