Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules / widget_dao / dao / plugin_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    plugin_dao_read_only.cpp
18  * @author  Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version 1.0
20  * @brief   This file contains the implementation of plugin dao read only
21  */
22 #include <stddef.h>
23 #include <dpl/wrt-dao-ro/plugin_dao_read_only.h>
24
25 #include <sstream>
26 #include <dpl/log/wrt_log.h>
27 #include <dpl/foreach.h>
28 #include <dpl/wrt-dao-ro/webruntime_database.h>
29 #include <dpl/db/orm.h>
30 #include <orm_generator_wrt.h>
31 #include <dpl/wrt-dao-ro/WrtDatabase.h>
32
33 namespace WrtDB {
34 namespace {
35 typedef DPL::DB::ORM::wrt::PluginProperties::Row PluginRow;
36
37 PluginRow getPluginRow(DbPluginHandle pluginHandle)
38 {
39     WrtLogD("Getting plugin row. Handle: %i", pluginHandle);
40     Try {
41         using namespace DPL::DB::ORM;
42         using namespace DPL::DB::ORM::wrt;
43         WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
44         select->Where(Equals<PluginProperties::PluginPropertiesId>(
45                           pluginHandle));
46
47         PluginProperties::Select::RowList rows = select->GetRowList();
48         if (rows.empty()) {
49             ThrowMsg(PluginDAOReadOnly::Exception::PluginNotExist,
50                      "Cannot find plugin. Handle: " + pluginHandle);
51         }
52         return rows.front();
53     }
54     Catch(DPL::DB::SqlConnection::Exception::Base) {
55         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
56                    "Failed in GetPluginRow");
57     }
58 }
59 }
60
61 PluginDAOReadOnly::PluginDAOReadOnly(DbPluginHandle pluginHandle) :
62     m_pluginHandle(pluginHandle)
63 {
64     if (!isPluginInstalled(m_pluginHandle)) {
65         WrtLogE("Plugin %i not installed.", m_pluginHandle);
66         Throw(PluginDAOReadOnly::Exception::PluginNotExist);
67     }
68 }
69
70 PluginDAOReadOnly::PluginDAOReadOnly(const std::string &libraryName)
71 {
72     WrtLogD("PluginDAOReadOnly ( %s )", libraryName.c_str());
73     Try {
74         using namespace DPL::DB::ORM;
75         using namespace DPL::DB::ORM::wrt;
76         WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
77         select->Where(Equals<PluginProperties::PluginLibraryName>(
78                           DPL::FromUTF8String(libraryName)));
79
80         PluginProperties::Select::RowList rows = select->GetRowList();
81         if (!rows.empty()) {
82             m_pluginHandle = rows.front().Get_PluginPropertiesId();
83         } else {
84             ThrowMsg(PluginDAOReadOnly::Exception::PluginNotExist,
85                      "Cannot find plugin: [" + libraryName + "]");
86         }
87         WrtLogD(" >> Handle for this plugin: %i", m_pluginHandle);
88     }
89     Catch(DPL::DB::SqlConnection::Exception::Base) {
90         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
91                    "Failed to connect to database");
92     }
93 }
94
95 bool PluginDAOReadOnly::isPluginInstalled(const std::string &libraryName)
96 {
97     WrtLogD("Check if Library is installed. LibraryName: %s",
98         libraryName.c_str());
99     Try {
100         using namespace DPL::DB::ORM;
101         using namespace DPL::DB::ORM::wrt;
102         WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
103         select->Where(Equals<PluginProperties::PluginLibraryName>(
104                           DPL::FromUTF8String(libraryName)));
105
106         PluginProperties::Select::RowList rows = select->GetRowList();
107
108         bool flag = !rows.empty();
109         WrtLogD(" >> Plugin %s %s", libraryName.c_str(),
110                  (flag ? " found." : " not found."));
111
112         return flag;
113     }
114     Catch(DPL::DB::SqlConnection::Exception::Base) {
115         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
116                    "Failed in isPluginInstalled");
117     }
118 }
119
120 PluginHandleList PluginDAOReadOnly::getPluginHandleList()
121 {
122     WrtLogD("Getting plugin handle list.");
123     Try {
124         using namespace DPL::DB::ORM;
125         using namespace DPL::DB::ORM::wrt;
126         WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
127
128         PluginHandleList ret =
129             select->GetValueList<PluginProperties::PluginPropertiesId>();
130
131         std::ostringstream handles;
132         FOREACH(it, ret)
133         handles << *it << " ";
134         WrtLogD(" >> PluginHandle list retrieved: (%s)", handles.str().c_str());
135
136         return ret;
137     }
138     Catch(DPL::DB::SqlConnection::Exception::Base) {
139         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
140                    "Failed in GetPluginHandleList");
141     }
142 }
143
144 PluginHandleList PluginDAOReadOnly::getRootPluginHandleList()
145 {
146     WrtLogD("Getting root plugin handle list.");
147     Try {
148         using namespace DPL::DB::ORM;
149         using namespace DPL::DB::ORM::wrt;
150
151         WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
152         PluginHandleList handleList = select->GetValueList<PluginProperties::PluginPropertiesId>();
153
154         WRT_DB_SELECT(select_2nd, PluginDependencies, &WrtDatabase::interface())
155         PluginDependencies::Select::RowList dependenciesRows = select_2nd->GetRowList();
156
157         if (!dependenciesRows.empty())
158         {
159             FOREACH(it, dependenciesRows)
160             {
161                 handleList.remove(it->Get_PluginPropertiesId());
162             }
163         }
164
165         return handleList;
166     }
167     Catch(DPL::DB::SqlConnection::Exception::Base) {
168         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
169                    "Failed in getRootPluginHandleList");
170     }
171 }
172
173 DbPluginHandle PluginDAOReadOnly::getPluginHandle() const
174 {
175     return m_pluginHandle;
176 }
177
178 //"value" cannot be null, as in registerPlugin it is always set
179 #define RETURN_STD_STRING(in, what)                 \
180     std::string ret = "";                           \
181     if (!!in) {                                       \
182         ret = DPL::ToUTF8String(*in); }             \
183     WrtLogD(" >> Plugin %s: %s", what, ret.c_str()); \
184     return ret;
185
186 std::string PluginDAOReadOnly::getLibraryPath() const
187 {
188     WrtLogD("Getting plugin library path. Handle: %i", m_pluginHandle);
189     PluginRow row = getPluginRow(m_pluginHandle);
190     RETURN_STD_STRING(row.Get_PluginLibraryPath(), "library path")
191 }
192
193 std::string PluginDAOReadOnly::getLibraryName() const
194 {
195     WrtLogD("Getting plugin library name. Handle: %i", m_pluginHandle);
196     PluginRow row = getPluginRow(m_pluginHandle);
197     std::string ret = DPL::ToUTF8String(row.Get_PluginLibraryName());
198     WrtLogD(" >> Plugin library name: %s", ret.c_str());
199     return ret;
200 }
201
202 #undef RETURN_STD_STRING
203
204 PluginHandleSetPtr PluginDAOReadOnly::getLibraryDependencies() const
205 {
206     Try
207     {
208         using namespace DPL::DB::ORM;
209         using namespace DPL::DB::ORM::wrt;
210         PluginHandleSetPtr dependencies(new PluginHandleSet);
211
212         WRT_DB_SELECT(select, PluginDependencies, &WrtDatabase::interface())
213         select->Where(
214             Equals<PluginDependencies::PluginPropertiesId>(m_pluginHandle));
215
216         PluginDependencies::Select::RowList rows = select->GetRowList();
217
218         if (!rows.empty()) {
219             FOREACH(it, rows)
220             {
221                 dependencies->insert(it->Get_RequiredPluginPropertiesId());
222             }
223         }
224
225         return dependencies;
226     }
227     Catch(DPL::DB::SqlConnection::Exception::Base) {
228         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
229                    "Failed in GetLibraryDependencies");
230     }
231 }
232
233 PluginHandleSetPtr PluginDAOReadOnly::getLibraryDependentPlugins() const
234 {
235     Try
236     {
237         using namespace DPL::DB::ORM;
238         using namespace DPL::DB::ORM::wrt;
239         PluginHandleSetPtr dependencies(new PluginHandleSet);
240
241         WRT_DB_SELECT(select, PluginDependencies, &WrtDatabase::interface())
242         select->Where(
243             Equals<PluginDependencies::RequiredPluginPropertiesId>(m_pluginHandle));
244
245         PluginDependencies::Select::RowList rows = select->GetRowList();
246
247         if (!rows.empty()) {
248             FOREACH(it, rows)
249             {
250                 dependencies->insert(it->Get_PluginPropertiesId());
251             }
252         }
253
254         return dependencies;
255     }
256     Catch(DPL::DB::SqlConnection::Exception::Base) {
257         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
258                    "Failed in GetLibraryDependencies");
259     }
260 }
261
262 DbPluginHandle PluginDAOReadOnly::getPluginHandleForImplementedObject(
263     const std::string& objectName)
264 {
265     WrtLogD("GetPluginHandle for object: %s", objectName.c_str());
266
267     Try
268     {
269         DbPluginHandle pluginHandle = INVALID_PLUGIN_HANDLE;
270
271         using namespace DPL::DB::ORM;
272         using namespace DPL::DB::ORM::wrt;
273
274         WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
275         select->Where(
276             Equals<PluginImplementedObjects::PluginObject>(
277                 DPL::FromUTF8String(objectName)));
278
279         PluginImplementedObjects::Select::RowList rows = select->GetRowList();
280
281         if (!rows.empty()) {
282             pluginHandle = rows.front().Get_PluginPropertiesId();
283         } else {
284             WrtLogW("PluginHandle for object not found");
285         }
286         return pluginHandle;
287     }
288     Catch(DPL::DB::SqlConnection::Exception::Base) {
289         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
290                    "Failed in GetPluginHandleForImplementedObject");
291     }
292 }
293
294 ImplementedObjectsList PluginDAOReadOnly::getImplementedObjects()
295 {
296     WrtLogD("getImplementedObjects");
297
298     Try
299     {
300         ImplementedObjectsList objectList;
301         using namespace DPL::DB::ORM;
302         using namespace DPL::DB::ORM::wrt;
303
304         WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
305         std::list<DPL::String> valueList = select->GetValueList<PluginImplementedObjects::PluginObject>();
306
307         if (!valueList.empty()) {
308             FOREACH(it, valueList)
309             {
310                 objectList.push_back(DPL::ToUTF8String(*it));
311             }
312         } else {
313             WrtLogW("PluginHandle for object not found");
314         }
315         return objectList;
316     }
317     Catch(DPL::DB::SqlConnection::Exception::Base) {
318         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
319                    "Failed in GetPluginHandleForImplementedObject");
320     }
321 }
322
323 ImplementedObjectsList PluginDAOReadOnly::getImplementedObjectsForPluginHandle(
324     DbPluginHandle handle)
325 {
326     WrtLogD("getImplementedObjects for pluginHandle: %i", handle);
327
328     Try
329     {
330         ImplementedObjectsList objectList;
331         using namespace DPL::DB::ORM;
332         using namespace DPL::DB::ORM::wrt;
333
334         WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
335         select->Where(
336             Equals<PluginImplementedObjects::PluginPropertiesId>(handle));
337
338         PluginImplementedObjects::Select::RowList rows = select->GetRowList();
339
340         if (!rows.empty()) {
341             FOREACH(it, rows)
342             {
343                 objectList.push_back(DPL::ToUTF8String(it->Get_PluginObject()));
344             }
345         } else {
346             WrtLogW("PluginHandle for object not found");
347         }
348         return objectList;
349     }
350     Catch(DPL::DB::SqlConnection::Exception::Base) {
351         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
352                    "Failed in GetPluginHandleForImplementedObject");
353     }
354 }
355
356 PluginObjectsDAO::ObjectsPtr PluginDAOReadOnly::
357     getRequiredObjectsForPluginHandle(
358     DbPluginHandle handle)
359 {
360     Try
361     {
362         using namespace DPL::DB::ORM;
363         using namespace DPL::DB::ORM::wrt;
364
365         PluginObjectsDAO::ObjectsPtr objects =
366             PluginObjectsDAO::ObjectsPtr(new PluginObjectsDAO::Objects);
367
368         WRT_DB_SELECT(select, PluginRequiredObjects, &WrtDatabase::interface())
369         select->Where(
370             Equals<PluginRequiredObjects::PluginPropertiesId>(handle));
371
372         PluginRequiredObjects::Select::RowList rows = select->GetRowList();
373
374         if (!rows.empty()) {
375             FOREACH(it, rows)
376             {
377                 objects->insert(DPL::ToUTF8String(it->Get_PluginObject()));
378             }
379         }
380
381         return objects;
382     }
383     Catch(DPL::DB::SqlConnection::Exception::Base) {
384         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
385                    "Failed in GetRequiredObjectsForPluginHandle");
386     }
387 }
388
389 PluginHandleSetPtr PluginDAOReadOnly::getPluginHandleByStatus(
390     PluginInstallationState state)
391 {
392     Try
393     {
394         using namespace DPL::DB::ORM;
395         using namespace DPL::DB::ORM::wrt;
396
397         PluginHandleSetPtr handleSet(new PluginHandleSet);
398
399         WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
400         select->Where(
401             Equals<PluginProperties::InstallationState>(ToInt(state)));
402
403         PluginProperties::Select::RowList rows = select->GetRowList();
404
405         if (!rows.empty()) {
406             FOREACH(it, rows)
407             {
408                 handleSet->insert(it->Get_PluginPropertiesId());
409             }
410         }
411
412         return handleSet;
413     }
414     Catch(DPL::DB::SqlConnection::Exception::Base) {
415         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
416                    "Failed in GetPluginHandleByStatus");
417     }
418 }
419
420 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::
421     getInstallationStatus() const
422 {
423     PluginRow row = getPluginRow(m_pluginHandle);
424     return ToState(row.Get_InstallationState());
425 }
426
427 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::
428     getInstallationStateForHandle(
429     DbPluginHandle handle)
430 {
431     Try
432     {
433         using namespace DPL::DB::ORM;
434         using namespace DPL::DB::ORM::wrt;
435
436         WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
437         select->Where(
438             Equals<PluginProperties::PluginPropertiesId>(handle));
439
440         PluginProperties::Select::RowList rows = select->GetRowList();
441
442         if (!rows.empty()) {
443             return ToState(rows.front().Get_InstallationState());
444         }
445         WrtLogE("Data in DB are invalid. Missing field");
446         return UNKNOWN_ERROR;
447     }
448     Catch(DPL::DB::SqlConnection::Exception::Base) {
449         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
450                    "Failed in GetStatusForHandle");
451     }
452 }
453
454 std::time_t PluginDAOReadOnly::getLibraryTimestamp() const
455 {
456     WrtLogD("Getting plugin library timestamp. Handle: %i", m_pluginHandle);
457     PluginRow row = getPluginRow(m_pluginHandle);
458     std::time_t ret = row.Get_LibraryTimestamp();
459     WrtLogD(" >> Plugin library timestamp: %li", ret);
460     return ret;
461 }
462
463 bool PluginDAOReadOnly::isPluginInstalled(DbPluginHandle pluginHandle)
464 {
465     Try {
466         using namespace DPL::DB::ORM;
467         using namespace DPL::DB::ORM::wrt;
468         WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
469         select->Where(
470             Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
471
472         PluginProperties::Select::RowList rows = select->GetRowList();
473
474         bool flag = !rows.empty();
475
476         return flag;
477     }
478     Catch(DPL::DB::SqlConnection::Exception::Base) {
479         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
480                    "Failed in isPluginInstalled");
481     }
482 }
483 } // namespace WrtDB