2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file plugin_dao.cpp
18 * @author Pawel Sikorski (p.sikorski@samsung.com)
19 * @author Grzegorz Krawczyk (g.krawczyk@samsung.com)
21 * @brief This file contains the definition of plugin dao class.
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>
36 PluginDAO::PluginDAO(DbPluginHandle pluginHandle) :
37 PluginDAOReadOnly(pluginHandle)
40 PluginDAO::PluginDAO(const std::string &libraryName) :
41 PluginDAOReadOnly(libraryName)
44 DbPluginHandle PluginDAO::registerPlugin(const PluginMetafileData& metafile,
45 const std::string& pluginPath)
47 WrtLogD("Registering plugin. Path: %s", pluginPath.c_str());
50 DPL::DB::ORM::wrt::ScopedTransaction transaction(
51 &WrtDatabase::interface());
52 DbPluginHandle handle;
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);
59 WrtLogD("Register Plugin: %s", metafile.m_libraryName.c_str());
61 using namespace DPL::DB::ORM;
62 using namespace DPL::DB::ORM::wrt;
64 typedef PluginProperties::Row PluginPropertiesRow;
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));
74 memset(&st, 0, sizeof(struct stat));
75 if (-1 == stat(pluginPath.c_str(), &st)) {
76 ThrowMsg(PluginDAO::Exception::DatabaseError,
77 "Failed in RegisterPlugin");
79 row.Set_LibraryTimestamp(st.st_mtime);
81 WRT_DB_INSERT(insert, PluginProperties, &WrtDatabase::interface());
83 handle = static_cast<WrtDB::DbWidgetHandle>(insert->Execute());
84 WrtLogD(" >> Plugin Registered. Handle: %i", handle);
89 Catch(DPL::DB::SqlConnection::Exception::Base)
91 ReThrowMsg(PluginDAO::Exception::DatabaseError,
92 "Failed in RegisterPlugin");
96 void PluginDAO::registerPluginImplementedObject(const std::string& objectName,
97 DbPluginHandle pluginHandle)
99 WrtLogD("Registering plugin object: %s", objectName.c_str());
102 DPL::DB::ORM::wrt::ScopedTransaction transaction(
103 &WrtDatabase::interface());
105 WrtLogD("Register Object: %s", objectName.c_str());
107 using namespace DPL::DB::ORM;
108 using namespace DPL::DB::ORM::wrt;
110 typedef PluginImplementedObjects::Row PluginObjectsRow;
112 PluginObjectsRow row;
113 row.Set_PluginObject(DPL::FromUTF8String(objectName));
114 row.Set_PluginPropertiesId(pluginHandle);
116 WRT_DB_INSERT(insert, PluginImplementedObjects, &WrtDatabase::interface())
119 transaction.Commit();
121 Catch(DPL::DB::SqlConnection::Exception::Base)
123 ReThrowMsg(PluginDAO::Exception::DatabaseError,
124 "Failed in RegisterPluginObject");
128 void PluginDAO::registerPluginRequiredObject(const std::string& objectName,
129 DbPluginHandle pluginHandle)
131 WrtLogD("Registering plugin object: %s", objectName.c_str());
134 DPL::DB::ORM::wrt::ScopedTransaction transaction(
135 &WrtDatabase::interface());
137 WrtLogD("Register Object: %s", objectName.c_str());
139 using namespace DPL::DB::ORM;
140 using namespace DPL::DB::ORM::wrt;
142 typedef PluginRequiredObjects::Row PluginObjectsRow;
144 PluginObjectsRow row;
145 row.Set_PluginPropertiesId(pluginHandle);
146 row.Set_PluginObject(DPL::FromUTF8String(objectName));
148 WRT_DB_INSERT(insert, PluginRequiredObjects, &WrtDatabase::interface())
151 transaction.Commit();
153 Catch(DPL::DB::SqlConnection::Exception::Base)
155 ReThrowMsg(PluginDAO::Exception::DatabaseError,
156 "Failed in RegisterPluginObject");
160 void PluginDAO::registerPluginLibrariesDependencies(
161 DbPluginHandle pluginHandle,
162 const PluginHandleSetPtr& dependencies)
164 WrtLogD("Registering plugin library dependencies: %i", pluginHandle);
167 DPL::DB::ORM::wrt::ScopedTransaction transaction(
168 &WrtDatabase::interface());
170 using namespace DPL::DB::ORM;
171 using namespace DPL::DB::ORM::wrt;
173 typedef PluginDependencies::Row PluginDependeciesRow;
174 PluginDependeciesRow row;
176 FOREACH(it, *dependencies)
178 row.Set_PluginPropertiesId(pluginHandle);
179 row.Set_RequiredPluginPropertiesId(*it);
181 WRT_DB_INSERT(insert, PluginDependencies, &WrtDatabase::interface())
184 transaction.Commit();
187 Catch(DPL::DB::SqlConnection::Exception::Base)
189 ReThrowMsg(PluginDAO::Exception::DatabaseError,
190 "Failed in RegisterPluginObject");
194 void PluginDAO::unregisterPluginLibrariesDependencies(DbPluginHandle plugin)
196 WrtLogD("Unregistering plugin dependencies: %i", plugin);
199 DPL::DB::ORM::wrt::ScopedTransaction transaction(
200 &WrtDatabase::interface());
202 using namespace DPL::DB::ORM;
203 using namespace DPL::DB::ORM::wrt;
205 WRT_DB_DELETE(deleteStmt, PluginDependencies, &WrtDatabase::interface());
206 deleteStmt->Where(Equals<PluginDependencies::PluginPropertiesId>(plugin));
207 deleteStmt->Execute();
208 transaction.Commit();
210 Catch(DPL::DB::SqlConnection::Exception::Base)
212 ReThrowMsg(PluginDAO::Exception::DatabaseError,
213 "Failed in unregisterPluginLibrariesDependencies");
217 void PluginDAO::setPluginInstallationStatus(DbPluginHandle pluginHandle,
218 PluginInstallationState state)
221 WrtLogD("Set installation state: %i handle %i", state, pluginHandle);
223 using namespace DPL::DB::ORM;
224 using namespace DPL::DB::ORM::wrt;
225 ScopedTransaction transaction(&WrtDatabase::interface());
227 typedef wrt::PluginProperties::Row PluginPropertiesRow;
229 PluginPropertiesRow row;
230 row.Set_InstallationState(state);
232 WRT_DB_UPDATE(update, PluginProperties, &WrtDatabase::interface())
234 Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
238 transaction.Commit();
240 Catch(DPL::DB::SqlConnection::Exception::Base)
242 ReThrowMsg(PluginDAO::Exception::DatabaseError,
243 "Failed in RegisterLibraryDependencies");
247 void PluginDAO::unregisterPlugin(DbPluginHandle pluginHandle)
249 WrtLogD("unregisterPlugin plugin. Handle: %i", pluginHandle);
252 DPL::DB::ORM::wrt::ScopedTransaction transaction(
253 &WrtDatabase::interface());
255 if (!isPluginInstalled(pluginHandle)) {
256 WrtLogD("PluginHandle is invalid. Handle: %i", pluginHandle);
259 using namespace DPL::DB::ORM;
260 using namespace DPL::DB::ORM::wrt;
262 WRT_DB_DELETE(del, PluginProperties, &WrtDatabase::interface())
264 Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
267 transaction.Commit();
268 WrtLogD(" >> Plugin Unregistered. Handle: %i", pluginHandle);
271 Catch(DPL::DB::SqlConnection::Exception::Base)
273 ReThrowMsg(PluginDAO::Exception::DatabaseError,
274 "Failed in UnregisterPlugin");