merge with master
[platform/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/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 DbPluginHandle PluginDAOReadOnly::getPluginHandle() const
158 {
159     return m_pluginHandle;
160 }
161
162 //"value" cannot be null, as in registerPlugin it is always set
163 #define RETURN_STD_STRING(in, what)                 \
164     std::string ret = "";                           \
165     if (!in.IsNull()) {                             \
166         ret = DPL::ToUTF8String(*in); }             \
167     LogDebug(" >> Plugin " << what << ": " << ret); \
168     return ret;
169
170 std::string PluginDAOReadOnly::getLibraryPath() const
171 {
172     LogDebug("Getting plugin library path. Handle: " << m_pluginHandle);
173     PluginRow row = getPluginRow(m_pluginHandle);
174     RETURN_STD_STRING(row.Get_PluginLibraryPath(), "library path")
175 }
176
177 std::string PluginDAOReadOnly::getLibraryName() const
178 {
179     LogDebug("Getting plugin library name. Handle: " << m_pluginHandle);
180     PluginRow row = getPluginRow(m_pluginHandle);
181     std::string ret = DPL::ToUTF8String(row.Get_PluginLibraryName());
182     LogDebug(" >> Plugin library name: " << ret);
183     return ret;
184 }
185
186 #undef RETURN_STD_STRING
187
188 PluginHandleSetPtr PluginDAOReadOnly::getLibraryDependencies() const
189 {
190     Try
191     {
192         using namespace DPL::DB::ORM;
193         using namespace DPL::DB::ORM::wrt;
194         PluginHandleSetPtr dependencies(new PluginHandleSet);
195
196         WRT_DB_SELECT(select, PluginDependencies, &WrtDatabase::interface())
197         select->Where(
198             Equals<PluginDependencies::PluginPropertiesId>(m_pluginHandle));
199
200         PluginDependencies::Select::RowList rows = select->GetRowList();
201
202         if (!rows.empty()) {
203             FOREACH(it, rows)
204             {
205                 dependencies->insert(it->Get_RequiredPluginPropertiesId());
206             }
207         }
208
209         return dependencies;
210     }
211     Catch(DPL::DB::SqlConnection::Exception::Base) {
212         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
213                    "Failed in GetLibraryDependencies");
214     }
215 }
216
217 DbPluginHandle PluginDAOReadOnly::getPluginHandleForImplementedObject(
218     const std::string& objectName)
219 {
220     LogDebug("GetPluginHandle for object: " << objectName);
221
222     Try
223     {
224         DbPluginHandle pluginHandle = INVALID_PLUGIN_HANDLE;
225
226         using namespace DPL::DB::ORM;
227         using namespace DPL::DB::ORM::wrt;
228
229         WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
230         select->Where(
231             Equals<PluginImplementedObjects::PluginObject>(
232                 DPL::FromUTF8String(objectName)));
233
234         PluginImplementedObjects::Select::RowList rows = select->GetRowList();
235
236         if (!rows.empty()) {
237             pluginHandle = rows.front().Get_PluginPropertiesId();
238         } else {
239             LogWarning("PluginHandle for object not found");
240         }
241         return pluginHandle;
242     }
243     Catch(DPL::DB::SqlConnection::Exception::Base) {
244         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
245                    "Failed in GetPluginHandleForImplementedObject");
246     }
247 }
248
249 ImplementedObjectsList PluginDAOReadOnly::getImplementedObjectsForPluginHandle(
250     DbPluginHandle handle)
251 {
252     LogDebug("getImplementedObjects for pluginHandle: " << handle);
253
254     Try
255     {
256         ImplementedObjectsList objectList;
257         using namespace DPL::DB::ORM;
258         using namespace DPL::DB::ORM::wrt;
259
260         WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
261         select->Where(
262             Equals<PluginImplementedObjects::PluginPropertiesId>(handle));
263
264         PluginImplementedObjects::Select::RowList rows = select->GetRowList();
265
266         if (!rows.empty()) {
267             FOREACH(it, rows)
268             {
269                 objectList.push_back(DPL::ToUTF8String(it->Get_PluginObject()));
270             }
271         } else {
272             LogWarning("PluginHandle for object not found");
273         }
274         return objectList;
275     }
276     Catch(DPL::DB::SqlConnection::Exception::Base) {
277         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
278                    "Failed in GetPluginHandleForImplementedObject");
279     }
280 }
281
282 PluginObjectsDAO::ObjectsPtr PluginDAOReadOnly::
283     getRequiredObjectsForPluginHandle(
284     DbPluginHandle handle)
285 {
286     Try
287     {
288         using namespace DPL::DB::ORM;
289         using namespace DPL::DB::ORM::wrt;
290
291         PluginObjectsDAO::ObjectsPtr objects =
292             PluginObjectsDAO::ObjectsPtr(new PluginObjectsDAO::Objects);
293
294         WRT_DB_SELECT(select, PluginRequiredObjects, &WrtDatabase::interface())
295         select->Where(
296             Equals<PluginRequiredObjects::PluginPropertiesId>(handle));
297
298         PluginRequiredObjects::Select::RowList rows = select->GetRowList();
299
300         if (!rows.empty()) {
301             FOREACH(it, rows)
302             {
303                 objects->insert(DPL::ToUTF8String(it->Get_PluginObject()));
304             }
305         }
306
307         return objects;
308     }
309     Catch(DPL::DB::SqlConnection::Exception::Base) {
310         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
311                    "Failed in GetRequiredObjectsForPluginHandle");
312     }
313 }
314
315 PluginHandleSetPtr PluginDAOReadOnly::getPluginHandleByStatus(
316     PluginInstallationState state)
317 {
318     Try
319     {
320         using namespace DPL::DB::ORM;
321         using namespace DPL::DB::ORM::wrt;
322
323         PluginHandleSetPtr handleSet(new PluginHandleSet);
324
325         WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
326         select->Where(
327             Equals<PluginProperties::InstallationState>(ToInt(state)));
328
329         PluginProperties::Select::RowList rows = select->GetRowList();
330
331         if (!rows.empty()) {
332             FOREACH(it, rows)
333             {
334                 handleSet->insert(it->Get_PluginPropertiesId());
335             }
336         }
337
338         return handleSet;
339     }
340     Catch(DPL::DB::SqlConnection::Exception::Base) {
341         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
342                    "Failed in GetPluginHandleByStatus");
343     }
344 }
345
346 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::
347     getInstallationStatus() const
348 {
349     PluginRow row = getPluginRow(m_pluginHandle);
350     return ToState(row.Get_InstallationState());
351 }
352
353 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::
354     getInstallationStateForHandle(
355     DbPluginHandle handle)
356 {
357     Try
358     {
359         using namespace DPL::DB::ORM;
360         using namespace DPL::DB::ORM::wrt;
361
362         WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
363         select->Where(
364             Equals<PluginProperties::PluginPropertiesId>(handle));
365
366         PluginProperties::Select::RowList rows = select->GetRowList();
367
368         if (!rows.empty()) {
369             return ToState(rows.front().Get_InstallationState());
370         }
371         LogError("Data in DB are invalid. Missing field");
372         return UNKNOWN_ERROR;
373     }
374     Catch(DPL::DB::SqlConnection::Exception::Base) {
375         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
376                    "Failed in GetStatusForHandle");
377     }
378 }
379
380 bool PluginDAOReadOnly::isPluginInstalled(DbPluginHandle pluginHandle)
381 {
382     Try {
383         using namespace DPL::DB::ORM;
384         using namespace DPL::DB::ORM::wrt;
385         WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
386         select->Where(
387             Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
388
389         PluginProperties::Select::RowList rows = select->GetRowList();
390
391         bool flag = !rows.empty();
392
393         return flag;
394     }
395     Catch(DPL::DB::SqlConnection::Exception::Base) {
396         ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
397                    "Failed in isPluginInstalled");
398     }
399 }
400 } // namespace WrtDB