tizen 2.4 release
[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 #include <stddef.h>
23 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
24 #include <sstream>
25 #include <dpl/log/wrt_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         WrtLogE("%s", exc.str().c_str());
40         ThrowMsg(FeatureDAOReadOnly::Exception::FeatureNotExist, exc.str());
41     }
42 }
43
44 FeatureDAOReadOnly::FeatureDAOReadOnly(const std::string &featureName)
45 {
46     WrtLogD("FeatureDAOReadOnly ( %s )", featureName.c_str());
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         WrtLogD(" >> FeatureHandle retrieved: %i", 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         WrtLogD(#func ". FeatureHandle: %i", 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     WrtLogD("Getting Feature Name. Handle: %i", 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         WrtLogD(" >> Feature name: %s", ret.c_str());
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     WrtLogD("Getting Plugin handle. FHandle: %i", 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         WrtLogD(" >> Plugin Handle: %i", 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     WrtLogD("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         WrtLogD(" >> FeatureHandle list retrieved: (%s)",
153             handles.str().c_str());
154
155         return ret;
156     }
157     Catch(DPL::DB::SqlConnection::Exception::Base){
158         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
159                    "Failure during getting FeatureHandle list");
160     }
161 }
162
163 bool FeatureDAOReadOnly::isFeatureInstalled(const std::string &featureName)
164 {
165     WrtLogD("Check if Feature is installed. Name: %s", featureName.c_str());
166     Try {
167         using namespace DPL::DB::ORM;
168         using namespace DPL::DB::ORM::wrt;
169         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
170         select->Where(Equals<FeaturesList::FeatureName>(
171                           DPL::FromUTF8String(featureName)));
172
173         FeaturesList::Select::RowList rows = select->GetRowList();
174
175         bool flag = !rows.empty();
176         WrtLogD(" >> Feature %s %s", featureName.c_str(),
177                  (flag ? " found." : " not found."));
178
179         return flag;
180     }
181     Catch(DPL::DB::SqlConnection::Exception::Base) {
182         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
183                    "Failure during checking if Feature is installed");
184     }
185 }
186
187 bool FeatureDAOReadOnly::isFeatureInstalled(FeatureHandle handle)
188 {
189     WrtLogD("Check if Feature is installed. Handle: %i", handle);
190     Try
191     {
192         using namespace DPL::DB::ORM;
193         using namespace DPL::DB::ORM::wrt;
194         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
195         select->Where(Equals<FeaturesList::FeatureUUID>(handle));
196
197         FeaturesList::Select::RowList rows = select->GetRowList();
198
199         bool flag = !rows.empty();
200         WrtLogD(" >> Feature %i %s",
201             handle, (flag ? " found." : " not found."));
202
203         return flag;
204     }
205     Catch(DPL::DB::SqlConnection::Exception::Base){
206         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
207                    "Failure during checking if Feature is installed");
208     }
209 }
210
211 bool FeatureDAOReadOnly::isDeviceCapabilityInstalled(
212     const std::string &deviceCapName)
213 {
214     WrtLogD("Check if DeviceCap is installed. Name: %s", deviceCapName.c_str());
215     Try {
216         using namespace DPL::DB::ORM;
217         using namespace DPL::DB::ORM::wrt;
218         WRT_DB_SELECT(select, DeviceCapabilities, &WrtDatabase::interface())
219         select->Where(Equals<DeviceCapabilities::DeviceCapName>(
220                           DPL::FromUTF8String(deviceCapName)));
221
222         DeviceCapabilities::Select::RowList rows = select->GetRowList();
223
224         bool flag = !rows.empty();
225         WrtLogD(" >> Device Cap %s %s", deviceCapName.c_str(),
226                  (flag ? "found." : "not found."));
227
228         return flag;
229     }
230     Catch(DPL::DB::SqlConnection::Exception::Base){
231         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
232                    "Failure during checking if DeviceCap is installed");
233     }
234 }
235
236 FeatureDAOReadOnly::DeviceCapabilitiesList
237 FeatureDAOReadOnly::GetDeviceCapabilities() const
238 {
239     Try {
240         WrtLogD("Get DeviceCap. FeatureHandle: %i", m_featureHandle);
241         DPL::DB::ORM::wrt::ScopedTransaction transaction(
242             &WrtDatabase::interface());
243
244         using namespace DPL::DB::ORM;
245         using namespace DPL::DB::ORM::wrt;
246         WRT_DB_SELECT(selectDevCP,
247                       FeatureDeviceCapProxy,
248                       &WrtDatabase::interface())
249         selectDevCP->Where(Equals<FeatureDeviceCapProxy::FeatureUUID>(
250                                m_featureHandle));
251
252         DeviceCapabilitiesList devCap;
253
254         std::list<int> deviceIDs =
255             selectDevCP->GetValueList<FeatureDeviceCapProxy::DeviceCapID>();
256         FOREACH(devCId, deviceIDs)
257         {
258             WRT_DB_SELECT(selectDevC,
259                           DeviceCapabilities,
260                           &WrtDatabase::interface())
261             selectDevC->Where(Equals<DeviceCapabilities::DeviceCapID>(*devCId));
262
263             DPL::String devNames =
264                 selectDevC->GetSingleValue<DeviceCapabilities::DeviceCapName>();
265
266             devCap.insert(DPL::ToUTF8String(devNames));
267         }
268
269         transaction.Commit();
270         return devCap;
271     }
272     Catch(DPL::DB::SqlConnection::Exception::Base){
273         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
274                    "Failure during getting DeviceCapabilities names");
275     }
276 }
277
278 FeatureHandleListPtr FeatureDAOReadOnly::GetFeatureHandleListForPlugin(
279     DbPluginHandle pluginHandle)
280 {
281     WrtLogD("Getting FeatureHandle list for pluginHandle: %i", pluginHandle);
282     Try {
283         using namespace DPL::DB::ORM;
284         using namespace DPL::DB::ORM::wrt;
285
286         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
287         select->Where(Equals<FeaturesList::PluginPropertiesId>(pluginHandle));
288
289         FeatureHandleListPtr handles(new FeatureHandleList);
290         FeatureHandleList ret =
291             select->GetValueList<FeaturesList::FeatureUUID>();
292
293         FOREACH(it, ret)
294         {
295             WrtLogD("feature handle: %i", *it);
296             handles->push_back(*it);
297         }
298
299         return handles;
300     }
301     Catch(DPL::DB::SqlConnection::Exception::Base){
302         ReThrowMsg(
303             FeatureDAOReadOnly::Exception::DatabaseError,
304             "Failure during getting FeatureHandle Set for plugin handle");
305     }
306 }
307
308 FeatureDAOReadOnly::NameMap
309 FeatureDAOReadOnly::GetNames()
310 {
311     Try {
312         using namespace DPL::DB::ORM;
313         using namespace DPL::DB::ORM::wrt;
314         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
315
316         NameMap nameMap;
317
318         FeaturesList::Select::RowList rows = select->GetRowList();
319         FOREACH(rowIt, rows)
320         {
321             nameMap.insert(std::pair<FeatureHandle,
322                                      std::string>(rowIt->Get_FeatureUUID(),
323                                                   DPL::ToUTF8String(rowIt->
324                                                                         Get_FeatureName())));
325         }
326
327         return nameMap;
328     }
329     Catch(DPL::DB::SqlConnection::Exception::Base){
330         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
331                    "Failure during getting GetNames");
332     }
333 }
334
335 FeatureDAOReadOnly::DeviceCapabilitiesMap
336 FeatureDAOReadOnly::GetDevCapWithFeatureHandle()
337 {
338     Try {
339         using namespace DPL::DB::ORM;
340         using namespace DPL::DB::ORM::wrt;
341
342         DECLARE_COLUMN_TYPE_LIST()
343         SELECTED_COLUMN(FeatureDeviceCapProxy, FeatureUUID)
344         SELECTED_COLUMN(DeviceCapabilities, DeviceCapName)
345         DECLARE_COLUMN_TYPE_LIST_END(DevCapNameList)
346
347         WRT_DB_SELECT(select, FeatureDeviceCapProxy, &WrtDatabase::interface())
348         select->Join<DevCapNameList>(Equal<FeatureDeviceCapProxy::DeviceCapID,
349                                            DeviceCapabilities::DeviceCapID>());
350
351         DeviceCapabilitiesMap devCap;
352
353         std::list< CustomRow<DevCapNameList> > rowList =
354             select->GetCustomRowList< DevCapNameList, CustomRow<DevCapNameList> >();
355         FOREACH(rowIt, rowList)
356         {
357             FeatureHandle featureHandle =
358                 (*rowIt).GetColumnData<FeatureDeviceCapProxy::FeatureUUID>();
359             std::string devName =
360                 DPL::ToUTF8String((*rowIt).GetColumnData<DeviceCapabilities::
361                                                              DeviceCapName>());
362             devCap.insert(std::pair<FeatureHandle, std::string>(featureHandle,
363                                                                 devName));
364         }
365
366         return devCap;
367     }
368     Catch(DPL::DB::SqlConnection::Exception::Base){
369         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
370                    "Failure during getting DeviceCapabilities names");
371     }
372 }
373
374 DeviceCapabilitySet FeatureDAOReadOnly::GetDeviceCapability(
375     const DPL::String &apifeature)
376 {
377     // This could be done with one simply sql query but support for join is
378     // needed in orm.
379     Try {
380         using namespace DPL::DB::ORM;
381         using namespace DPL::DB::ORM::wrt;
382
383         int featureUUID;
384         FeatureDeviceCapProxy::Select::RowList rows;
385         DeviceCapabilitySet result;
386
387         {
388             WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
389             select->Where(Equals<FeaturesList::FeatureName>(apifeature));
390             featureUUID = select->GetSingleValue<FeaturesList::FeatureUUID>();
391         }
392
393         {
394             WRT_DB_SELECT(select,
395                           FeatureDeviceCapProxy,
396                           &WrtDatabase::interface())
397             select->Where(Equals<FeatureDeviceCapProxy::FeatureUUID>(
398                               featureUUID));
399             rows = select->GetRowList();
400         }
401
402         FOREACH(it, rows){
403             WRT_DB_SELECT(select, DeviceCapabilities, &WrtDatabase::interface())
404             select->Where(Equals<DeviceCapabilities::DeviceCapID>(
405                               it->Get_DeviceCapID()));
406             result.insert(select->
407                               GetSingleValue<DeviceCapabilities::DeviceCapName>());
408         }
409
410         return result;
411     } Catch(DPL::DB::SqlConnection::Exception::Base){
412         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
413                    "Failed to get device capability");
414     }
415 }
416
417 FeatureDAOReadOnly::FeatureMap
418 FeatureDAOReadOnly::GetFeatures(const std::list<std::string>& featureNames)
419 {
420     Try {
421         using namespace DPL::DB::ORM;
422         using namespace DPL::DB::ORM::wrt;
423
424         std::set<typename FeaturesList::FeatureName::ColumnType> nameList;
425         FOREACH(nm, featureNames) {
426             nameList.insert(DPL::FromUTF8String(*nm));
427         }
428
429         FeaturesList::Select select{ &WrtDatabase::interface() };
430         select.Where(In<FeaturesList::FeatureName>(nameList));
431
432         FeatureMap featureMap;
433         FeatureData featureData;
434         FeaturesList::Select::RowList rows = select.GetRowList();
435         FOREACH(rowIt, rows) {
436             featureData.featureName = DPL::ToUTF8String(
437                     rowIt->Get_FeatureName());
438             featureData.pluginHandle = rowIt->Get_PluginPropertiesId();
439             featureMap.insert(std::pair<FeatureHandle, FeatureData>(
440                                   rowIt->Get_FeatureUUID(), featureData));
441         }
442
443         return featureMap;
444     }
445     Catch(DPL::DB::SqlConnection::Exception::Base){
446         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
447                    "Failure during getting GetFeatures");
448     }
449 }
450 } // namespace WrtDB