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