Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_mobile / 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 #include <stddef.h>
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 FeatureDAOReadOnly::FeatureDAOReadOnly(FeatureHandle featureHandle) :
34     m_featureHandle(featureHandle)
35 {
36     if (!isFeatureInstalled(m_featureHandle)) {
37         std::ostringstream exc;
38         exc << "Feature " << m_featureHandle << " not installed.";
39         LogError(exc.str());
40         ThrowMsg(FeatureDAOReadOnly::Exception::FeatureNotExist, exc.str());
41     }
42 }
43
44 FeatureDAOReadOnly::FeatureDAOReadOnly(const std::string &featureName)
45 {
46     LogDebug("FeatureDAOReadOnly ( " << featureName << " )");
47     Try {
48         using namespace DPL::DB::ORM;
49         using namespace DPL::DB::ORM::wrt;
50         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
51         select->Where(Equals<FeaturesList::FeatureName>(
52                           DPL::FromUTF8String(featureName)));
53
54         m_featureHandle = select->GetSingleValue<FeaturesList::FeatureUUID>();
55         LogDebug(" >> FeatureHandle retrieved: " << m_featureHandle);
56     }
57     Catch(DPL::DB::SqlConnection::Exception::Base){
58         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
59                    "Failure during creating FeatureDAOReadOnly");
60     }
61 }
62
63 #define GET_PLUGIN_DATA(func)                                           \
64     Try {                                                               \
65         DPL::DB::ORM::wrt::ScopedTransaction transaction( \
66             &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(
241             &WrtDatabase::interface());
242
243         using namespace DPL::DB::ORM;
244         using namespace DPL::DB::ORM::wrt;
245         WRT_DB_SELECT(selectDevCP,
246                       FeatureDeviceCapProxy,
247                       &WrtDatabase::interface())
248         selectDevCP->Where(Equals<FeatureDeviceCapProxy::FeatureUUID>(
249                                m_featureHandle));
250
251         DeviceCapabilitiesList devCap;
252
253         std::list<int> deviceIDs =
254             selectDevCP->GetValueList<FeatureDeviceCapProxy::DeviceCapID>();
255         FOREACH(devCId, deviceIDs)
256         {
257             WRT_DB_SELECT(selectDevC,
258                           DeviceCapabilities,
259                           &WrtDatabase::interface())
260             selectDevC->Where(Equals<DeviceCapabilities::DeviceCapID>(*devCId));
261
262             DPL::String devNames =
263                 selectDevC->GetSingleValue<DeviceCapabilities::DeviceCapName>();
264
265             devCap.insert(DPL::ToUTF8String(devNames));
266         }
267
268         transaction.Commit();
269         return devCap;
270     }
271     Catch(DPL::DB::SqlConnection::Exception::Base){
272         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
273                    "Failure during getting DeviceCapabilities names");
274     }
275 }
276
277 FeatureHandleListPtr FeatureDAOReadOnly::GetFeatureHandleListForPlugin(
278     DbPluginHandle pluginHandle)
279 {
280     LogDebug("Getting FeatureHandle list for pluginHandle: " << pluginHandle);
281     Try {
282         using namespace DPL::DB::ORM;
283         using namespace DPL::DB::ORM::wrt;
284
285         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
286         select->Where(Equals<FeaturesList::PluginPropertiesId>(pluginHandle));
287
288         FeatureHandleListPtr handles(new FeatureHandleList);
289         FeatureHandleList ret =
290             select->GetValueList<FeaturesList::FeatureUUID>();
291
292         FOREACH(it, ret)
293         {
294             LogDebug("feature handle: " << *it);
295             handles->push_back(*it);
296         }
297
298         return handles;
299     }
300     Catch(DPL::DB::SqlConnection::Exception::Base){
301         ReThrowMsg(
302             FeatureDAOReadOnly::Exception::DatabaseError,
303             "Failure during getting FeatureHandle Set for plugin handle");
304     }
305 }
306
307 FeatureDAOReadOnly::NameMap
308 FeatureDAOReadOnly::GetNames()
309 {
310     Try {
311         using namespace DPL::DB::ORM;
312         using namespace DPL::DB::ORM::wrt;
313         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
314
315         NameMap nameMap;
316
317         FeaturesList::Select::RowList rows = select->GetRowList();
318         FOREACH(rowIt, rows)
319         {
320             nameMap.insert(std::pair<FeatureHandle,
321                                      std::string>(rowIt->Get_FeatureUUID(),
322                                                   DPL::ToUTF8String(rowIt->
323                                                                         Get_FeatureName())));
324         }
325
326         return nameMap;
327     }
328     Catch(DPL::DB::SqlConnection::Exception::Base){
329         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
330                    "Failure during getting GetNames");
331     }
332 }
333
334 FeatureDAOReadOnly::DeviceCapabilitiesMap
335 FeatureDAOReadOnly::GetDevCapWithFeatureHandle()
336 {
337     Try {
338         using namespace DPL::DB::ORM;
339         using namespace DPL::DB::ORM::wrt;
340
341         DECLARE_COLUMN_TYPE_LIST()
342         SELECTED_COLUMN(FeatureDeviceCapProxy, FeatureUUID)
343         SELECTED_COLUMN(DeviceCapabilities, DeviceCapName)
344         DECLARE_COLUMN_TYPE_LIST_END(DevCapNameList)
345
346         WRT_DB_SELECT(select, FeatureDeviceCapProxy, &WrtDatabase::interface())
347         select->Join<DevCapNameList>(Equal<FeatureDeviceCapProxy::DeviceCapID,
348                                            DeviceCapabilities::DeviceCapID>());
349
350         DeviceCapabilitiesMap devCap;
351
352         std::list< CustomRow<DevCapNameList> > rowList =
353             select->GetCustomRowList< DevCapNameList, CustomRow<DevCapNameList> >();
354         FOREACH(rowIt, rowList)
355         {
356             FeatureHandle featureHandle =
357                 (*rowIt).GetColumnData<FeatureDeviceCapProxy::FeatureUUID>();
358             std::string devName =
359                 DPL::ToUTF8String((*rowIt).GetColumnData<DeviceCapabilities::
360                                                              DeviceCapName>());
361             devCap.insert(std::pair<FeatureHandle, std::string>(featureHandle,
362                                                                 devName));
363         }
364
365         return devCap;
366     }
367     Catch(DPL::DB::SqlConnection::Exception::Base){
368         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
369                    "Failure during getting DeviceCapabilities names");
370     }
371 }
372
373 DeviceCapabilitySet FeatureDAOReadOnly::GetDeviceCapability(
374     const DPL::String &apifeature)
375 {
376     // This could be done with one simply sql query but support for join is
377     // needed in orm.
378     Try {
379         using namespace DPL::DB::ORM;
380         using namespace DPL::DB::ORM::wrt;
381
382         int featureUUID;
383         FeatureDeviceCapProxy::Select::RowList rows;
384         DeviceCapabilitySet result;
385
386         {
387             WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
388             select->Where(Equals<FeaturesList::FeatureName>(apifeature));
389             featureUUID = select->GetSingleValue<FeaturesList::FeatureUUID>();
390         }
391
392         {
393             WRT_DB_SELECT(select,
394                           FeatureDeviceCapProxy,
395                           &WrtDatabase::interface())
396             select->Where(Equals<FeatureDeviceCapProxy::FeatureUUID>(
397                               featureUUID));
398             rows = select->GetRowList();
399         }
400
401         FOREACH(it, rows){
402             WRT_DB_SELECT(select, DeviceCapabilities, &WrtDatabase::interface())
403             select->Where(Equals<DeviceCapabilities::DeviceCapID>(
404                               it->Get_DeviceCapID()));
405             result.insert(select->
406                               GetSingleValue<DeviceCapabilities::DeviceCapName>());
407         }
408
409         return result;
410     } Catch(DPL::DB::SqlConnection::Exception::Base){
411         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
412                    "Failed to get device capability");
413     }
414 }
415
416 FeatureDAOReadOnly::FeatureMap
417 FeatureDAOReadOnly::GetFeatures(const std::list<std::string>& featureNames)
418 {
419     Try {
420         using namespace DPL::DB::ORM;
421         using namespace DPL::DB::ORM::wrt;
422
423         std::set<typename FeaturesList::FeatureName::ColumnType> nameList;
424         FOREACH(nm, featureNames) {
425             nameList.insert(DPL::FromUTF8String(*nm));
426         }
427
428         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
429         select->Where(In<FeaturesList::FeatureName>(nameList));
430
431         FeatureMap featureMap;
432         FeatureData featureData;
433         FeaturesList::Select::RowList rows = select->GetRowList();
434         FOREACH(rowIt, rows) {
435             featureData.featureName = DPL::ToUTF8String(
436                     rowIt->Get_FeatureName());
437             featureData.pluginHandle = rowIt->Get_PluginPropertiesId();
438             featureMap.insert(std::pair<FeatureHandle, FeatureData>(
439                                   rowIt->Get_FeatureUUID(), featureData));
440         }
441
442         return featureMap;
443     }
444     Catch(DPL::DB::SqlConnection::Exception::Base){
445         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
446                    "Failure during getting GetFeatures");
447     }
448 }
449 } // namespace WrtDB