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