tizen 2.4 release
[framework/web/wrt-commons.git] / modules / widget_dao / dao / plugin_dao.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.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @author  Grzegorz Krawczyk (g.krawczyk@samsung.com)
20  * @version 1.0
21  * @brief   This file contains the definition of plugin dao class.
22  */
23
24 #include <sys/stat.h>
25
26 #include <ctime>
27 #include <stddef.h>
28 #include <dpl/wrt-dao-rw/plugin_dao.h>
29 #include <dpl/log/wrt_log.h>
30 #include <dpl/foreach.h>
31 #include <orm_generator_wrt.h>
32 #include <dpl/wrt-dao-ro/webruntime_database.h>
33 #include <dpl/wrt-dao-ro/WrtDatabase.h>
34
35 namespace WrtDB {
36 PluginDAO::PluginDAO(DbPluginHandle pluginHandle) :
37     PluginDAOReadOnly(pluginHandle)
38 {}
39
40 PluginDAO::PluginDAO(const std::string &libraryName) :
41     PluginDAOReadOnly(libraryName)
42 {}
43
44 DbPluginHandle PluginDAO::registerPlugin(const PluginMetafileData& metafile,
45                                          const std::string& pluginPath)
46 {
47     WrtLogD("Registering plugin. Path: %s", pluginPath.c_str());
48
49     Try {
50         DPL::DB::ORM::wrt::ScopedTransaction transaction(
51             &WrtDatabase::interface());
52         DbPluginHandle handle;
53
54         if (isPluginInstalled(metafile.m_libraryName)) {
55             handle = PluginDAO(metafile.m_libraryName).getPluginHandle();
56             WrtLogD(" >> Library %s is already registered. Handle: %i",
57                     metafile.m_libraryName.c_str(), handle);
58         } else {
59             WrtLogD("Register Plugin: %s", metafile.m_libraryName.c_str());
60
61             using namespace DPL::DB::ORM;
62             using namespace DPL::DB::ORM::wrt;
63
64             typedef PluginProperties::Row PluginPropertiesRow;
65
66             PluginPropertiesRow row;
67             row.Set_PluginLibraryName(
68                 DPL::FromUTF8String(metafile.m_libraryName));
69             row.Set_InstallationState(INSTALLATION_IN_PROGRESS);
70             row.Set_PluginLibraryPath(
71                 DPL::FromUTF8String(pluginPath));
72
73             struct stat st;
74             memset(&st, 0, sizeof(struct stat));
75             if (-1 == stat(pluginPath.c_str(), &st)) {
76                 ThrowMsg(PluginDAO::Exception::DatabaseError,
77                    "Failed in RegisterPlugin");
78             }
79             row.Set_LibraryTimestamp(st.st_mtime);
80
81             WRT_DB_INSERT(insert, PluginProperties, &WrtDatabase::interface());
82             insert->Values(row);
83             handle = static_cast<WrtDB::DbWidgetHandle>(insert->Execute());
84             WrtLogD(" >> Plugin Registered. Handle: %i", handle);
85         }
86         transaction.Commit();
87         return handle;
88     }
89     Catch(DPL::DB::SqlConnection::Exception::Base)
90     {
91         ReThrowMsg(PluginDAO::Exception::DatabaseError,
92                    "Failed in RegisterPlugin");
93     }
94 }
95
96 void PluginDAO::registerPluginImplementedObject(const std::string& objectName,
97                                                 DbPluginHandle pluginHandle)
98 {
99     WrtLogD("Registering plugin object: %s", objectName.c_str());
100
101     Try {
102         DPL::DB::ORM::wrt::ScopedTransaction transaction(
103             &WrtDatabase::interface());
104
105         WrtLogD("Register Object: %s", objectName.c_str());
106
107         using namespace DPL::DB::ORM;
108         using namespace DPL::DB::ORM::wrt;
109
110         typedef PluginImplementedObjects::Row PluginObjectsRow;
111
112         PluginObjectsRow row;
113         row.Set_PluginObject(DPL::FromUTF8String(objectName));
114         row.Set_PluginPropertiesId(pluginHandle);
115
116         WRT_DB_INSERT(insert, PluginImplementedObjects, &WrtDatabase::interface())
117         insert->Values(row);
118         insert->Execute();
119         transaction.Commit();
120     }
121     Catch(DPL::DB::SqlConnection::Exception::Base)
122     {
123         ReThrowMsg(PluginDAO::Exception::DatabaseError,
124                    "Failed in RegisterPluginObject");
125     }
126 }
127
128 void PluginDAO::registerPluginRequiredObject(const std::string& objectName,
129                                              DbPluginHandle pluginHandle)
130 {
131     WrtLogD("Registering plugin object: %s", objectName.c_str());
132
133     Try {
134         DPL::DB::ORM::wrt::ScopedTransaction transaction(
135             &WrtDatabase::interface());
136
137         WrtLogD("Register Object: %s", objectName.c_str());
138
139         using namespace DPL::DB::ORM;
140         using namespace DPL::DB::ORM::wrt;
141
142         typedef PluginRequiredObjects::Row PluginObjectsRow;
143
144         PluginObjectsRow row;
145         row.Set_PluginPropertiesId(pluginHandle);
146         row.Set_PluginObject(DPL::FromUTF8String(objectName));
147
148         WRT_DB_INSERT(insert, PluginRequiredObjects, &WrtDatabase::interface())
149         insert->Values(row);
150         insert->Execute();
151         transaction.Commit();
152     }
153     Catch(DPL::DB::SqlConnection::Exception::Base)
154     {
155         ReThrowMsg(PluginDAO::Exception::DatabaseError,
156                    "Failed in RegisterPluginObject");
157     }
158 }
159
160 void PluginDAO::registerPluginLibrariesDependencies(
161     DbPluginHandle pluginHandle,
162     const PluginHandleSetPtr& dependencies)
163 {
164     WrtLogD("Registering plugin library dependencies: %i", pluginHandle);
165
166     Try {
167         DPL::DB::ORM::wrt::ScopedTransaction transaction(
168             &WrtDatabase::interface());
169
170         using namespace DPL::DB::ORM;
171         using namespace DPL::DB::ORM::wrt;
172
173         typedef PluginDependencies::Row PluginDependeciesRow;
174         PluginDependeciesRow row;
175
176         FOREACH(it, *dependencies)
177         {
178             row.Set_PluginPropertiesId(pluginHandle);
179             row.Set_RequiredPluginPropertiesId(*it);
180
181             WRT_DB_INSERT(insert, PluginDependencies, &WrtDatabase::interface())
182             insert->Values(row);
183             insert->Execute();
184             transaction.Commit();
185         }
186     }
187     Catch(DPL::DB::SqlConnection::Exception::Base)
188     {
189         ReThrowMsg(PluginDAO::Exception::DatabaseError,
190                    "Failed in RegisterPluginObject");
191     }
192 }
193
194 void PluginDAO::unregisterPluginLibrariesDependencies(DbPluginHandle plugin)
195 {
196     WrtLogD("Unregistering plugin dependencies: %i", plugin);
197
198     Try {
199         DPL::DB::ORM::wrt::ScopedTransaction transaction(
200             &WrtDatabase::interface());
201
202         using namespace DPL::DB::ORM;
203         using namespace DPL::DB::ORM::wrt;
204
205         WRT_DB_DELETE(deleteStmt, PluginDependencies, &WrtDatabase::interface());
206         deleteStmt->Where(Equals<PluginDependencies::PluginPropertiesId>(plugin));
207         deleteStmt->Execute();
208         transaction.Commit();
209     }
210     Catch(DPL::DB::SqlConnection::Exception::Base)
211     {
212         ReThrowMsg(PluginDAO::Exception::DatabaseError,
213                    "Failed in unregisterPluginLibrariesDependencies");
214     }
215 }
216
217 void PluginDAO::setPluginInstallationStatus(DbPluginHandle pluginHandle,
218                                             PluginInstallationState state)
219 {
220     Try {
221         WrtLogD("Set installation state: %i handle %i", state, pluginHandle);
222
223         using namespace DPL::DB::ORM;
224         using namespace DPL::DB::ORM::wrt;
225         ScopedTransaction transaction(&WrtDatabase::interface());
226
227         typedef wrt::PluginProperties::Row PluginPropertiesRow;
228
229         PluginPropertiesRow row;
230         row.Set_InstallationState(state);
231
232         WRT_DB_UPDATE(update, PluginProperties, &WrtDatabase::interface())
233         update->Where(
234             Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
235
236         update->Values(row);
237         update->Execute();
238         transaction.Commit();
239     }
240     Catch(DPL::DB::SqlConnection::Exception::Base)
241     {
242         ReThrowMsg(PluginDAO::Exception::DatabaseError,
243                    "Failed in RegisterLibraryDependencies");
244     }
245 }
246
247 void PluginDAO::unregisterPlugin(DbPluginHandle pluginHandle)
248 {
249     WrtLogD("unregisterPlugin plugin. Handle: %i", pluginHandle);
250
251     Try {
252         DPL::DB::ORM::wrt::ScopedTransaction transaction(
253             &WrtDatabase::interface());
254
255         if (!isPluginInstalled(pluginHandle)) {
256             WrtLogD("PluginHandle is invalid. Handle: %i", pluginHandle);
257             return;
258         } else {
259             using namespace DPL::DB::ORM;
260             using namespace DPL::DB::ORM::wrt;
261
262             WRT_DB_DELETE(del, PluginProperties, &WrtDatabase::interface())
263             del->Where(
264                 Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
265             del->Execute();
266
267             transaction.Commit();
268             WrtLogD(" >> Plugin Unregistered. Handle: %i", pluginHandle);
269         }
270     }
271     Catch(DPL::DB::SqlConnection::Exception::Base)
272     {
273         ReThrowMsg(PluginDAO::Exception::DatabaseError,
274                    "Failed in UnregisterPlugin");
275     }
276 }
277 } // namespace WrtDB