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_read_only.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
20 * @brief This file contains the implementation of plugin dao read only
23 #include <dpl/wrt-dao-ro/plugin_dao_read_only.h>
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>
37 typedef DPL::DB::ORM::wrt::PluginProperties::Row PluginRow;
39 PluginRow getPluginRow(DbPluginHandle pluginHandle)
41 LogDebug("Getting plugin row. Handle: " << pluginHandle);
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>(
49 PluginProperties::Select::RowList rows = select->GetRowList();
51 ThrowMsg(PluginDAOReadOnly::Exception::PluginNotExist,
52 "Cannot find plugin. Handle: " + pluginHandle);
56 Catch(DPL::DB::SqlConnection::Exception::Base) {
57 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
58 "Failed in GetPluginRow");
63 PluginDAOReadOnly::PluginDAOReadOnly(DbPluginHandle pluginHandle) :
64 m_pluginHandle(pluginHandle)
66 if (!isPluginInstalled(m_pluginHandle)) {
67 LogError("Plugin " << m_pluginHandle << " not installed.");
68 Throw(PluginDAOReadOnly::Exception::PluginNotExist);
71 checkInstallationCompleted();
74 PluginDAOReadOnly::PluginDAOReadOnly(const std::string &libraryName)
76 LogDebug("PluginDAOReadOnly ( " << libraryName << " )");
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)));
84 PluginProperties::Select::RowList rows = select->GetRowList();
86 m_pluginHandle = rows.front().Get_PluginPropertiesId();
88 ThrowMsg(PluginDAOReadOnly::Exception::PluginNotExist,
89 "Cannot find plugin: [" + libraryName + "]");
91 LogDebug(" >> Handle for this plugin: " << m_pluginHandle);
93 checkInstallationCompleted();
95 Catch(DPL::DB::SqlConnection::Exception::Base) {
96 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
97 "Failed to connect to database");
101 void PluginDAOReadOnly::checkInstallationCompleted()
103 if (getInstallationStateForHandle(m_pluginHandle)
104 != PluginDAOReadOnly::INSTALLATION_COMPLETED) {
105 LogError("Plugin " << m_pluginHandle << " installation not completed");
106 Throw(PluginDAOReadOnly::Exception::PluginInstallationNotCompleted);
110 bool PluginDAOReadOnly::isPluginInstalled(const std::string &libraryName)
112 LogDebug("Check if Library is installed. LibraryName: " << libraryName);
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)));
120 PluginProperties::Select::RowList rows = select->GetRowList();
122 bool flag = !rows.empty();
123 LogDebug(" >> Plugin " << libraryName <<
124 (flag ? " found." : " not found."));
128 Catch(DPL::DB::SqlConnection::Exception::Base) {
129 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
130 "Failed in isPluginInstalled");
134 PluginHandleList PluginDAOReadOnly::getPluginHandleList()
136 LogDebug("Getting plugin handle list.");
138 using namespace DPL::DB::ORM;
139 using namespace DPL::DB::ORM::wrt;
140 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
142 PluginHandleList ret =
143 select->GetValueList<PluginProperties::PluginPropertiesId>();
145 std::ostringstream handles;
147 handles << *it << " ";
148 LogDebug(" >> PluginHandle list retrieved: (" << handles << ")");
152 Catch(DPL::DB::SqlConnection::Exception::Base) {
153 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
154 "Failed in GetPluginHandleList");
158 DbPluginHandle PluginDAOReadOnly::getPluginHandle() const
160 return m_pluginHandle;
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); \
172 std::string PluginDAOReadOnly::getLibraryPath() const
174 LogDebug("Getting plugin library path. Handle: " << m_pluginHandle);
175 PluginRow row = getPluginRow(m_pluginHandle);
176 RETURN_STD_STRING(row.Get_PluginLibraryPath(), "library path")
179 std::string PluginDAOReadOnly::getLibraryName() const
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);
188 #undef RETURN_STD_STRING
190 PluginHandleSetPtr PluginDAOReadOnly::getLibraryDependencies() const
194 using namespace DPL::DB::ORM;
195 using namespace DPL::DB::ORM::wrt;
196 PluginHandleSetPtr dependencies(new PluginHandleSet);
198 WRT_DB_SELECT(select, PluginDependencies, &WrtDatabase::interface())
200 Equals<PluginDependencies::PluginPropertiesId>(m_pluginHandle));
202 PluginDependencies::Select::RowList rows = select->GetRowList();
207 dependencies->insert(it->Get_RequiredPluginPropertiesId());
213 Catch(DPL::DB::SqlConnection::Exception::Base) {
214 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
215 "Failed in GetLibraryDependencies");
219 DbPluginHandle PluginDAOReadOnly::getPluginHandleForImplementedObject(
220 const std::string& objectName)
222 LogDebug("GetPluginHandle for object: " << objectName);
226 DbPluginHandle pluginHandle = INVALID_PLUGIN_HANDLE;
228 using namespace DPL::DB::ORM;
229 using namespace DPL::DB::ORM::wrt;
231 WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
233 Equals<PluginImplementedObjects::PluginObject>(
234 DPL::FromUTF8String(objectName)));
236 PluginImplementedObjects::Select::RowList rows = select->GetRowList();
239 pluginHandle = rows.front().Get_PluginPropertiesId();
241 LogWarning("PluginHandle for object not found");
245 Catch(DPL::DB::SqlConnection::Exception::Base) {
246 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
247 "Failed in GetPluginHandleForImplementedObject");
251 ImplementedObjectsList PluginDAOReadOnly::getImplementedObjectsForPluginHandle(
252 DbPluginHandle handle)
254 LogDebug("getImplementedObjects for pluginHandle: " << handle);
258 ImplementedObjectsList objectList;
259 using namespace DPL::DB::ORM;
260 using namespace DPL::DB::ORM::wrt;
262 WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
264 Equals<PluginImplementedObjects::PluginPropertiesId>(handle));
266 PluginImplementedObjects::Select::RowList rows = select->GetRowList();
271 objectList.push_back(DPL::ToUTF8String(it->Get_PluginObject()));
274 LogWarning("PluginHandle for object not found");
278 Catch(DPL::DB::SqlConnection::Exception::Base) {
279 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
280 "Failed in GetPluginHandleForImplementedObject");
284 PluginObjectsDAO::ObjectsPtr PluginDAOReadOnly::getRequiredObjectsForPluginHandle(
285 DbPluginHandle handle)
289 using namespace DPL::DB::ORM;
290 using namespace DPL::DB::ORM::wrt;
292 PluginObjectsDAO::ObjectsPtr objects =
293 PluginObjectsDAO::ObjectsPtr(new PluginObjectsDAO::Objects);
295 WRT_DB_SELECT(select, PluginRequiredObjects, &WrtDatabase::interface())
297 Equals<PluginRequiredObjects::PluginPropertiesId>(handle));
299 PluginRequiredObjects::Select::RowList rows = select->GetRowList();
304 objects->insert(DPL::ToUTF8String(it->Get_PluginObject()));
310 Catch(DPL::DB::SqlConnection::Exception::Base) {
311 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
312 "Failed in GetRequiredObjectsForPluginHandle");
316 PluginHandleSetPtr PluginDAOReadOnly::getPluginHandleByStatus(
317 PluginInstallationState state)
321 using namespace DPL::DB::ORM;
322 using namespace DPL::DB::ORM::wrt;
324 PluginHandleSetPtr handleSet(new PluginHandleSet);
326 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
328 Equals<PluginProperties::InstallationState>(ToInt(state)));
330 PluginProperties::Select::RowList rows = select->GetRowList();
335 handleSet->insert(it->Get_PluginPropertiesId());
341 Catch(DPL::DB::SqlConnection::Exception::Base) {
342 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
343 "Failed in GetPluginHandleByStatus");
347 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::getInstallationStatus() const
349 PluginRow row = getPluginRow(m_pluginHandle);
350 return ToState(row.Get_InstallationState());
353 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::getInstallationStateForHandle(
354 DbPluginHandle handle)
358 using namespace DPL::DB::ORM;
359 using namespace DPL::DB::ORM::wrt;
361 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
363 Equals<PluginProperties::PluginPropertiesId>(handle));
365 PluginProperties::Select::RowList rows = select->GetRowList();
368 return ToState(rows.front().Get_InstallationState());
370 LogError("Data in DB are invalid. Missing field");
371 return UNKNOWN_ERROR;
373 Catch(DPL::DB::SqlConnection::Exception::Base) {
374 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
375 "Failed in GetStatusForHandle");
379 bool PluginDAOReadOnly::isPluginInstalled(DbPluginHandle pluginHandle)
382 using namespace DPL::DB::ORM;
383 using namespace DPL::DB::ORM::wrt;
384 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
386 Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
388 PluginProperties::Select::RowList rows = select->GetRowList();
390 bool flag = !rows.empty();
394 Catch(DPL::DB::SqlConnection::Exception::Base) {
395 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
396 "Failed in isPluginInstalled");