Tizen 2.0 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 #include <stddef.h>
24 #include <dpl/wrt-dao-rw/plugin_dao.h>
25 #include <dpl/log/log.h>
26 #include <dpl/foreach.h>
27 #include <orm_generator_wrt.h>
28 #include <dpl/wrt-dao-ro/webruntime_database.h>
29 #include <dpl/wrt-dao-ro/WrtDatabase.h>
30
31 namespace WrtDB {
32 PluginDAO::PluginDAO(DbPluginHandle pluginHandle) :
33     PluginDAOReadOnly(pluginHandle)
34 {}
35
36 PluginDAO::PluginDAO(const std::string &libraryName) :
37     PluginDAOReadOnly(libraryName)
38 {}
39
40 DbPluginHandle PluginDAO::registerPlugin(const PluginMetafileData& metafile,
41                                          const std::string& pluginPath)
42 {
43     LogDebug("Registering plugin. Path: " << pluginPath);
44
45     Try {
46         DPL::DB::ORM::wrt::ScopedTransaction transaction(
47             &WrtDatabase::interface());
48         DbPluginHandle handle;
49
50         if (isPluginInstalled(metafile.m_libraryName)) {
51             handle = PluginDAO(metafile.m_libraryName).getPluginHandle();
52             LogInfo(" >> Library " << metafile.m_libraryName <<
53                     " is already registered. Handle: " << handle);
54         } else {
55             LogDebug("Register Plugin: " << metafile.m_libraryName);
56
57             using namespace DPL::DB::ORM;
58             using namespace DPL::DB::ORM::wrt;
59
60             typedef PluginProperties::Row PluginPropertiesRow;
61
62             PluginPropertiesRow row;
63             row.Set_PluginLibraryName(
64                 DPL::FromUTF8String(metafile.m_libraryName));
65             row.Set_InstallationState(INSTALLATION_IN_PROGRESS);
66             row.Set_PluginLibraryPath(
67                 DPL::FromUTF8String(pluginPath));
68
69             WRT_DB_INSERT(insert, PluginProperties, &WrtDatabase::interface())
70             insert->Values(row);
71             handle = static_cast<WrtDB::DbWidgetHandle>(insert->Execute());
72             LogDebug(" >> Plugin Registered. Handle: " << handle);
73         }
74         transaction.Commit();
75         return handle;
76     }
77     Catch(DPL::DB::SqlConnection::Exception::Base)
78     {
79         ReThrowMsg(PluginDAO::Exception::DatabaseError,
80                    "Failed in RegisterPlugin");
81     }
82 }
83
84 void PluginDAO::registerPluginImplementedObject(const std::string& objectName,
85                                                 DbPluginHandle pluginHandle)
86 {
87     LogDebug("Registering plugin object: " << objectName);
88
89     Try {
90         DPL::DB::ORM::wrt::ScopedTransaction transaction(
91             &WrtDatabase::interface());
92
93         LogDebug("Register Object: " << objectName);
94
95         using namespace DPL::DB::ORM;
96         using namespace DPL::DB::ORM::wrt;
97
98         typedef PluginImplementedObjects::Row PluginObjectsRow;
99
100         PluginObjectsRow row;
101         row.Set_PluginObject(DPL::FromUTF8String(objectName));
102         row.Set_PluginPropertiesId(pluginHandle);
103
104         WRT_DB_INSERT(insert, PluginImplementedObjects, &WrtDatabase::interface())
105         insert->Values(row);
106         insert->Execute();
107         transaction.Commit();
108     }
109     Catch(DPL::DB::SqlConnection::Exception::Base)
110     {
111         ReThrowMsg(PluginDAO::Exception::DatabaseError,
112                    "Failed in RegisterPluginObject");
113     }
114 }
115
116 void PluginDAO::registerPluginRequiredObject(const std::string& objectName,
117                                              DbPluginHandle pluginHandle)
118 {
119     LogDebug("Registering plugin object: " << objectName);
120
121     Try {
122         DPL::DB::ORM::wrt::ScopedTransaction transaction(
123             &WrtDatabase::interface());
124
125         LogDebug("Register Object: " << objectName);
126
127         using namespace DPL::DB::ORM;
128         using namespace DPL::DB::ORM::wrt;
129
130         typedef PluginRequiredObjects::Row PluginObjectsRow;
131
132         PluginObjectsRow row;
133         row.Set_PluginPropertiesId(pluginHandle);
134         row.Set_PluginObject(DPL::FromUTF8String(objectName));
135
136         WRT_DB_INSERT(insert, PluginRequiredObjects, &WrtDatabase::interface())
137         insert->Values(row);
138         insert->Execute();
139         transaction.Commit();
140     }
141     Catch(DPL::DB::SqlConnection::Exception::Base)
142     {
143         ReThrowMsg(PluginDAO::Exception::DatabaseError,
144                    "Failed in RegisterPluginObject");
145     }
146 }
147
148 void PluginDAO::registerPluginLibrariesDependencies(
149     DbPluginHandle pluginHandle,
150     const PluginHandleSetPtr& dependencies)
151 {
152     LogDebug("Registering plugin library dependencies: " << pluginHandle);
153
154     Try {
155         DPL::DB::ORM::wrt::ScopedTransaction transaction(
156             &WrtDatabase::interface());
157
158         using namespace DPL::DB::ORM;
159         using namespace DPL::DB::ORM::wrt;
160
161         typedef PluginDependencies::Row PluginDependeciesRow;
162         PluginDependeciesRow row;
163
164         FOREACH(it, *dependencies)
165         {
166             row.Set_PluginPropertiesId(pluginHandle);
167             row.Set_RequiredPluginPropertiesId(*it);
168
169             WRT_DB_INSERT(insert, PluginDependencies, &WrtDatabase::interface())
170             insert->Values(row);
171             insert->Execute();
172             transaction.Commit();
173         }
174     }
175     Catch(DPL::DB::SqlConnection::Exception::Base)
176     {
177         ReThrowMsg(PluginDAO::Exception::DatabaseError,
178                    "Failed in RegisterPluginObject");
179     }
180 }
181
182 void PluginDAO::setPluginInstallationStatus(DbPluginHandle pluginHandle,
183                                             PluginInstallationState state)
184 {
185     Try {
186         LogDebug(
187             "Set installation state: " << state << " handle " << pluginHandle);
188
189         using namespace DPL::DB::ORM;
190         using namespace DPL::DB::ORM::wrt;
191         ScopedTransaction transaction(&WrtDatabase::interface());
192
193         typedef wrt::PluginProperties::Row PluginPropertiesRow;
194
195         PluginPropertiesRow row;
196         row.Set_InstallationState(state);
197
198         WRT_DB_UPDATE(update, PluginProperties, &WrtDatabase::interface())
199         update->Where(
200             Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
201
202         update->Values(row);
203         update->Execute();
204         transaction.Commit();
205     }
206     Catch(DPL::DB::SqlConnection::Exception::Base)
207     {
208         ReThrowMsg(PluginDAO::Exception::DatabaseError,
209                    "Failed in RegisterLibraryDependencies");
210     }
211 }
212
213 void PluginDAO::unregisterPlugin(DbPluginHandle pluginHandle)
214 {
215     LogDebug("unregisterPlugin plugin. Handle: " << pluginHandle);
216
217     Try {
218         DPL::DB::ORM::wrt::ScopedTransaction transaction(
219             &WrtDatabase::interface());
220
221         if (!isPluginInstalled(pluginHandle)) {
222             LogInfo("PluginHandle is invalid. Handle: " << pluginHandle);
223             return;
224         } else {
225             using namespace DPL::DB::ORM;
226             using namespace DPL::DB::ORM::wrt;
227
228             WRT_DB_DELETE(del, PluginProperties, &WrtDatabase::interface())
229             del->Where(
230                 Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
231             del->Execute();
232
233             transaction.Commit();
234             LogDebug(" >> Plugin Unregistered. Handle: " << pluginHandle);
235         }
236     }
237     Catch(DPL::DB::SqlConnection::Exception::Base)
238     {
239         ReThrowMsg(PluginDAO::Exception::DatabaseError,
240                    "Failed in UnregisterPlugin");
241     }
242 }
243 } // namespace WrtDB