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