tizen beta 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
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 const int STRANGE_FEATURE_PROPERTIES_ID = 0;
36
37 FeatureHandle RegisterFeature(const PluginMetafileData::Feature &feature,
38                               const DbPluginHandle pluginHandle)
39 {
40     Try
41     {
42         LogDebug("Registering Feature " << feature.m_name);
43         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
44
45         if (FeatureDAOReadOnly::isFeatureInstalled(feature.m_name)) {
46             LogError(" >> Feature " << feature.m_name <<
47                      " is already registered.");
48             transaction.Commit();
49             return -1;
50         }
51
52         using namespace DPL::DB::ORM;
53         using namespace DPL::DB::ORM::wrt;
54
55         //register feature
56         {
57             LogInfo("    |-- Registering feature " << feature.m_name);
58
59             FeaturesList::Row row;
60             row.Set_FeatureName(DPL::FromUTF8String(feature.m_name));
61             row.Set_PluginPropertiesId(pluginHandle);
62
63             WRT_DB_INSERT(insert, FeaturesList, &WrtDatabase::interface())
64             insert->Values(row);
65             insert->Execute();
66         }
67
68         FeatureHandle featureHandle =
69                 FeatureDAOReadOnly(feature.m_name).GetFeatureHandle();
70
71         //register device capabilities
72         // Device Capabilities is unused in current version
73         FOREACH(itdev, feature.m_deviceCapabilities)
74         {
75             int deviceCapID;
76
77             if (FeatureDAOReadOnly::isDeviceCapabilityInstalled(*itdev)) {
78                 LogInfo("    |    |--DeviceCap " << *itdev <<
79                         " already installed!");
80
81                 WRT_DB_SELECT(select, DeviceCapabilities, &WrtDatabase::interface())
82
83                 select->Where(Equals<DeviceCapabilities::DeviceCapName>(
84                                   DPL::FromUTF8String(*itdev)));
85
86                 deviceCapID =
87                     select->GetSingleValue<DeviceCapabilities::DeviceCapID>();
88             } else {
89                 LogInfo("    |    |--Register DeviceCap: " << *itdev);
90
91                 DeviceCapabilities::Row row;
92                 row.Set_DeviceCapName(DPL::FromUTF8String(*itdev));
93
94                 WRT_DB_INSERT(insert, DeviceCapabilities, &WrtDatabase::interface())
95                 insert->Values(row);
96                 deviceCapID = insert->Execute();
97             }
98
99             FeatureDeviceCapProxy::Row row;
100             row.Set_FeatureUUID(featureHandle);
101             row.Set_DeviceCapID(deviceCapID);
102
103             WRT_DB_INSERT(insert, FeatureDeviceCapProxy, &WrtDatabase::interface())
104             insert->Values(row);
105             insert->Execute();
106         }
107
108         transaction.Commit();
109
110         return featureHandle;
111     }
112     Catch(DPL::DB::SqlConnection::Exception::Base){
113         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
114                    "Failure during Registering Feature");
115     }
116 }
117
118 FeatureHandle RegisterStrangeFeature(const std::string& featureName)
119 {
120     Try
121     {
122         LogDebug("Registering Feature " << featureName);
123         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
124
125         if (FeatureDAOReadOnly::isFeatureInstalled(featureName)) {
126             LogError(" >> Feature " << featureName <<
127                      " is already registered.");
128             transaction.Commit();
129             return -1;
130         }
131
132         using namespace DPL::DB::ORM;
133         using namespace DPL::DB::ORM::wrt;
134
135         //register feature
136         LogInfo("    |-- Registering feature " << featureName);
137
138         FeaturesList::Row row;
139         row.Set_FeatureName(DPL::FromUTF8String(featureName));
140
141         // PluginPropertiesId '0' is not used as PluginPropertiesId for normal features(calendar, contact....).
142         // PluginPropertiesId for normal features start from '1'
143         row.Set_PluginPropertiesId(STRANGE_FEATURE_PROPERTIES_ID);
144
145         WRT_DB_INSERT(insert, FeaturesList, &WrtDatabase::interface())
146         insert->Values(row);
147         insert->Execute();
148
149         FeatureHandle featureHandle =
150             FeatureDAOReadOnly(featureName).GetFeatureHandle();
151
152         transaction.Commit();
153
154         return featureHandle;
155     }
156     Catch(DPL::DB::SqlConnection::Exception::Base){
157         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
158                    "Failure during Registering Feature");
159     }
160 }
161
162 } // namespace FeatureDAO
163 } // namespace WrtDB