Update wrt-commons_0.2.53
[framework/web/wrt-commons.git] / modules / widget_dao / dao / feature_dao_read_only.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  * @file    feature_dao_read_only.cpp
18  * @author  Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version 1.0
20  * @brief   This file contains the implementation of feature dao read only
21  */
22
23 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
24 #include <sstream>
25 #include <dpl/log/log.h>
26 #include <dpl/foreach.h>
27 #include <dpl/db/orm.h>
28 #include <orm_generator_wrt.h>
29 #include <dpl/wrt-dao-ro/webruntime_database.h>
30 #include <dpl/wrt-dao-ro/plugin_dao_read_only.h>
31
32 namespace WrtDB {
33
34 FeatureDAOReadOnly::FeatureDAOReadOnly(FeatureHandle featureHandle) :
35     m_featureHandle(featureHandle)
36 {
37     if (!isFeatureInstalled(m_featureHandle)) {
38         std::ostringstream exc;
39         exc << "Feature " << m_featureHandle << " not installed.";
40         LogError(exc.str());
41         ThrowMsg(FeatureDAOReadOnly::Exception::FeatureNotExist, exc.str());
42     }
43 }
44
45 FeatureDAOReadOnly::FeatureDAOReadOnly(const std::string &featureName)
46 {
47     LogDebug("FeatureDAOReadOnly ( " << featureName << " )");
48     Try {
49         using namespace DPL::DB::ORM;
50         using namespace DPL::DB::ORM::wrt;
51         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
52         select->Where(Equals<FeaturesList::FeatureName>(
53                           DPL::FromUTF8String(featureName)));
54
55         m_featureHandle = select->GetSingleValue<FeaturesList::FeatureUUID>();
56         LogDebug(" >> FeatureHandle retrieved: " << m_featureHandle);
57     }
58     Catch(DPL::DB::SqlConnection::Exception::Base){
59         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
60                    "Failure during creating FeatureDAOReadOnly");
61     }
62 }
63
64 #define GET_PLUGIN_DATA(func)                                           \
65     Try {                                                               \
66         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface()); \
67         LogDebug(# func << ". FeatureHandle: " << m_featureHandle);     \
68         std::string ret = PluginDAOReadOnly(GetPluginHandle()).func();  \
69         transaction.Commit();                                           \
70         return ret;                                                     \
71     }                                                                   \
72     Catch(DPL::DB::SqlConnection::Exception::Base) {                        \
73         std::ostringstream failure("Failure during ");                  \
74         failure << # func;                                              \
75         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,        \
76                 failure.str());                                         \
77     }
78
79 std::string FeatureDAOReadOnly::GetLibraryPath() const
80 {
81     GET_PLUGIN_DATA(getLibraryPath)
82 }
83
84 std::string FeatureDAOReadOnly::GetLibraryName() const
85 {
86     GET_PLUGIN_DATA(getLibraryName)
87 }
88
89 #undef GET_PLUGIN_DATA
90
91 FeatureHandle FeatureDAOReadOnly::GetFeatureHandle()  const
92 {
93     return m_featureHandle;
94 }
95
96 std::string FeatureDAOReadOnly::GetName() const
97 {
98     LogDebug("Getting Feature Name. Handle: " << m_featureHandle);
99     Try {
100         using namespace DPL::DB::ORM;
101         using namespace DPL::DB::ORM::wrt;
102         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
103         select->Where(Equals<FeaturesList::FeatureUUID>(m_featureHandle));
104
105         std::string ret = DPL::ToUTF8String(
106                 select->GetSingleValue< FeaturesList::FeatureName>());
107
108         LogDebug(" >> Feature name: " << ret);
109         return ret;
110     }
111     Catch(DPL::DB::SqlConnection::Exception::Base){
112         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
113                    "Failure during getting GetName");
114     }
115 }
116
117 DbPluginHandle FeatureDAOReadOnly::GetPluginHandle() const
118 {
119     LogDebug("Getting Plugin handle. FHandle: " << m_featureHandle);
120     Try {
121         using namespace DPL::DB::ORM;
122         using namespace DPL::DB::ORM::wrt;
123         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
124         select->Where(Equals<FeaturesList::FeatureUUID>(m_featureHandle));
125
126         DbPluginHandle pluginHandle =
127             select->GetSingleValue< FeaturesList::PluginPropertiesId>();
128
129         LogDebug(" >> Plugin Handle: " << pluginHandle);
130         return pluginHandle;
131     }
132     Catch(DPL::DB::SqlConnection::Exception::Base){
133         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
134                    "Failure during getting Plugin Handle");
135     }
136 }
137
138 FeatureHandleList FeatureDAOReadOnly::GetHandleList()
139 {
140     LogDebug("Getting FeatureHandle list.");
141     Try {
142         using namespace DPL::DB::ORM;
143         using namespace DPL::DB::ORM::wrt;
144         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
145
146         FeatureHandleList ret =
147             select->GetValueList<FeaturesList::FeatureUUID>();
148
149         std::ostringstream handles;
150         FOREACH(it, ret)
151         handles << *it << " ";
152         LogDebug(" >> FeatureHandle list retrieved: (" << handles << ")");
153
154         return ret;
155     }
156     Catch(DPL::DB::SqlConnection::Exception::Base){
157         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
158                    "Failure during getting FeatureHandle list");
159     }
160 }
161
162 bool FeatureDAOReadOnly::isFeatureInstalled(const std::string &featureName)
163 {
164     LogDebug("Check if Feature is installed. Name: " << featureName);
165     Try {
166         using namespace DPL::DB::ORM;
167         using namespace DPL::DB::ORM::wrt;
168         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
169         select->Where(Equals<FeaturesList::FeatureName>(
170                           DPL::FromUTF8String(featureName)));
171
172         FeaturesList::Select::RowList rows = select->GetRowList();
173
174         bool flag = !rows.empty();
175         LogDebug(" >> Feature " << featureName <<
176                  (flag ? " found." : " not found."));
177
178         return flag;
179     }
180     Catch(DPL::DB::SqlConnection::Exception::Base) {
181         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
182                    "Failure during checking if Feature is installed");
183     }
184 }
185
186 bool FeatureDAOReadOnly::isFeatureInstalled(FeatureHandle handle)
187 {
188     LogDebug("Check if Feature is installed. Handle: " << handle);
189     Try
190     {
191         using namespace DPL::DB::ORM;
192         using namespace DPL::DB::ORM::wrt;
193         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
194         select->Where(Equals<FeaturesList::FeatureUUID>(handle));
195
196         FeaturesList::Select::RowList rows = select->GetRowList();
197
198         bool flag = !rows.empty();
199         LogDebug(" >> Feature " << handle <<
200                  (flag ? " found." : " not found."));
201
202         return flag;
203     }
204     Catch(DPL::DB::SqlConnection::Exception::Base){
205         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
206                    "Failure during checking if Feature is installed");
207     }
208 }
209
210 bool FeatureDAOReadOnly::isDeviceCapabilityInstalled(
211         const std::string &deviceCapName)
212 {
213     LogDebug("Check if DeviceCap is installed. Name: " << deviceCapName);
214     Try {
215         using namespace DPL::DB::ORM;
216         using namespace DPL::DB::ORM::wrt;
217         WRT_DB_SELECT(select, DeviceCapabilities, &WrtDatabase::interface())
218         select->Where(Equals<DeviceCapabilities::DeviceCapName>(
219                           DPL::FromUTF8String(deviceCapName)));
220
221         DeviceCapabilities::Select::RowList rows = select->GetRowList();
222
223         bool flag = !rows.empty();
224         LogDebug(" >> Device Cap " << deviceCapName <<
225                  (flag ? "found." : "not found."));
226
227         return flag;
228     }
229     Catch(DPL::DB::SqlConnection::Exception::Base){
230         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
231                    "Failure during checking if DeviceCap is installed");
232     }
233 }
234
235 FeatureDAOReadOnly::DeviceCapabilitiesList
236 FeatureDAOReadOnly::GetDeviceCapabilities() const
237 {
238     Try {
239         LogDebug("Get DeviceCap. FeatureHandle: " << m_featureHandle);
240         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
241
242         using namespace DPL::DB::ORM;
243         using namespace DPL::DB::ORM::wrt;
244         WRT_DB_SELECT(selectDevCP, FeatureDeviceCapProxy, &WrtDatabase::interface())
245         selectDevCP->Where(Equals<FeatureDeviceCapProxy::FeatureUUID>(
246                                m_featureHandle));
247
248         DeviceCapabilitiesList devCap;
249
250         std::list<int> deviceIDs =
251             selectDevCP->GetValueList<FeatureDeviceCapProxy::DeviceCapID>();
252         FOREACH(devCId, deviceIDs)
253         {
254             WRT_DB_SELECT(selectDevC, DeviceCapabilities, &WrtDatabase::interface())
255             selectDevC->Where(Equals<DeviceCapabilities::DeviceCapID>(*devCId));
256
257             DPL::String devNames =
258                 selectDevC->GetSingleValue<DeviceCapabilities::DeviceCapName>();
259
260             devCap.insert(DPL::ToUTF8String(devNames));
261         }
262
263         transaction.Commit();
264         return devCap;
265     }
266     Catch(DPL::DB::SqlConnection::Exception::Base){
267         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
268                    "Failure during getting DeviceCapabilities names");
269     }
270 }
271
272 FeatureHandleListPtr FeatureDAOReadOnly::GetFeatureHandleListForPlugin(
273         DbPluginHandle pluginHandle)
274 {
275     LogDebug("Getting FeatureHandle list for pluginHandle: " << pluginHandle);
276     Try {
277         using namespace DPL::DB::ORM;
278         using namespace DPL::DB::ORM::wrt;
279
280         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
281         select->Where(Equals<FeaturesList::PluginPropertiesId>(pluginHandle));
282
283         FeatureHandleListPtr handles(new FeatureHandleList);
284         FeatureHandleList ret =
285             select->GetValueList<FeaturesList::FeatureUUID>();
286
287         FOREACH(it, ret)
288         {
289             LogDebug("feature handle: " << *it);
290             handles->push_back(*it);
291         }
292
293         return handles;
294     }
295     Catch(DPL::DB::SqlConnection::Exception::Base){
296         ReThrowMsg(
297                 FeatureDAOReadOnly::Exception::DatabaseError,
298                 "Failure during getting FeatureHandle Set for plugin handle");
299     }
300 }
301
302 FeatureDAOReadOnly::NameMap
303 FeatureDAOReadOnly::GetNames()
304 {
305     Try {
306         using namespace DPL::DB::ORM;
307         using namespace DPL::DB::ORM::wrt;
308         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
309
310         NameMap nameMap;
311
312         FeaturesList::Select::RowList rows = select->GetRowList();
313         FOREACH(rowIt, rows)
314         {
315             nameMap.insert(std::pair<FeatureHandle, std::string>(rowIt->Get_FeatureUUID(), DPL::ToUTF8String(rowIt->Get_FeatureName())));
316         }
317
318         return nameMap;
319     }
320     Catch(DPL::DB::SqlConnection::Exception::Base){
321         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
322                    "Failure during getting GetNames");
323     }
324 }
325
326 FeatureDAOReadOnly::DeviceCapabilitiesMap
327 FeatureDAOReadOnly::GetDevCapWithFeatureHandle()
328 {
329     Try {
330         using namespace DPL::DB::ORM;
331         using namespace DPL::DB::ORM::wrt;
332
333         DECLARE_COLUMN_TYPE_LIST()
334         SELECTED_COLUMN(FeatureDeviceCapProxy, FeatureUUID)
335         SELECTED_COLUMN(DeviceCapabilities, DeviceCapName)
336         DECLARE_COLUMN_TYPE_LIST_END(DevCapNameList)
337
338         WRT_DB_SELECT(select, FeatureDeviceCapProxy, &WrtDatabase::interface())
339         select->Join<DevCapNameList>(Equal<FeatureDeviceCapProxy::DeviceCapID, DeviceCapabilities::DeviceCapID>());
340
341         DeviceCapabilitiesMap devCap;
342
343         std::list< CustomRow<DevCapNameList> > rowList = select->GetCustomRowList< DevCapNameList, CustomRow<DevCapNameList> >();
344         FOREACH(rowIt, rowList)
345         {
346             FeatureHandle featureHandle = (*rowIt).GetColumnData<FeatureDeviceCapProxy::FeatureUUID>();
347             std::string devName = DPL::ToUTF8String((*rowIt).GetColumnData<DeviceCapabilities::DeviceCapName>());
348             devCap.insert(std::pair<FeatureHandle, std::string>(featureHandle, devName));
349         }
350
351         return devCap;
352     }
353     Catch(DPL::DB::SqlConnection::Exception::Base){
354         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
355                    "Failure during getting DeviceCapabilities names");
356     }
357 }
358
359 } // namespace WrtDB