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>
35 typedef DPL::DB::ORM::wrt::PluginProperties::Row PluginRow;
37 PluginRow getPluginRow(DbPluginHandle pluginHandle)
39 LogDebug("Getting plugin row. Handle: " << pluginHandle);
41 using namespace DPL::DB::ORM;
42 using namespace DPL::DB::ORM::wrt;
43 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
44 select->Where(Equals<PluginProperties::PluginPropertiesId>(
47 PluginProperties::Select::RowList rows = select->GetRowList();
49 ThrowMsg(PluginDAOReadOnly::Exception::PluginNotExist,
50 "Cannot find plugin. Handle: " + pluginHandle);
54 Catch(DPL::DB::SqlConnection::Exception::Base) {
55 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
56 "Failed in GetPluginRow");
61 PluginDAOReadOnly::PluginDAOReadOnly(DbPluginHandle pluginHandle) :
62 m_pluginHandle(pluginHandle)
64 if (!isPluginInstalled(m_pluginHandle)) {
65 LogError("Plugin " << m_pluginHandle << " not installed.");
66 Throw(PluginDAOReadOnly::Exception::PluginNotExist);
69 checkInstallationCompleted();
72 PluginDAOReadOnly::PluginDAOReadOnly(const std::string &libraryName)
74 LogDebug("PluginDAOReadOnly ( " << libraryName << " )");
76 using namespace DPL::DB::ORM;
77 using namespace DPL::DB::ORM::wrt;
78 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
79 select->Where(Equals<PluginProperties::PluginLibraryName>(
80 DPL::FromUTF8String(libraryName)));
82 PluginProperties::Select::RowList rows = select->GetRowList();
84 m_pluginHandle = rows.front().Get_PluginPropertiesId();
86 ThrowMsg(PluginDAOReadOnly::Exception::PluginNotExist,
87 "Cannot find plugin: [" + libraryName + "]");
89 LogDebug(" >> Handle for this plugin: " << m_pluginHandle);
91 checkInstallationCompleted();
93 Catch(DPL::DB::SqlConnection::Exception::Base) {
94 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
95 "Failed to connect to database");
99 void PluginDAOReadOnly::checkInstallationCompleted()
101 if (getInstallationStateForHandle(m_pluginHandle)
102 != PluginDAOReadOnly::INSTALLATION_COMPLETED)
104 LogError("Plugin " << m_pluginHandle << " installation not completed");
105 Throw(PluginDAOReadOnly::Exception::PluginInstallationNotCompleted);
109 bool PluginDAOReadOnly::isPluginInstalled(const std::string &libraryName)
111 LogDebug("Check if Library is installed. LibraryName: " << libraryName);
113 using namespace DPL::DB::ORM;
114 using namespace DPL::DB::ORM::wrt;
115 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
116 select->Where(Equals<PluginProperties::PluginLibraryName>(
117 DPL::FromUTF8String(libraryName)));
119 PluginProperties::Select::RowList rows = select->GetRowList();
121 bool flag = !rows.empty();
122 LogDebug(" >> Plugin " << libraryName <<
123 (flag ? " found." : " not found."));
127 Catch(DPL::DB::SqlConnection::Exception::Base) {
128 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
129 "Failed in isPluginInstalled");
133 PluginHandleList PluginDAOReadOnly::getPluginHandleList()
135 LogDebug("Getting plugin handle list.");
137 using namespace DPL::DB::ORM;
138 using namespace DPL::DB::ORM::wrt;
139 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
141 PluginHandleList ret =
142 select->GetValueList<PluginProperties::PluginPropertiesId>();
144 std::ostringstream handles;
146 handles << *it << " ";
147 LogDebug(" >> PluginHandle list retrieved: (" << handles << ")");
151 Catch(DPL::DB::SqlConnection::Exception::Base) {
152 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
153 "Failed in GetPluginHandleList");
157 PluginHandleList PluginDAOReadOnly::getRootPluginHandleList()
159 LogDebug("Getting root plugin handle list.");
161 using namespace DPL::DB::ORM;
162 using namespace DPL::DB::ORM::wrt;
164 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
165 PluginHandleList handleList = select->GetValueList<PluginProperties::PluginPropertiesId>();
167 WRT_DB_SELECT(select_2nd, PluginDependencies, &WrtDatabase::interface())
168 PluginDependencies::Select::RowList dependenciesRows = select_2nd->GetRowList();
170 if (!dependenciesRows.empty())
172 FOREACH(it, dependenciesRows)
174 handleList.remove(it->Get_PluginPropertiesId());
180 Catch(DPL::DB::SqlConnection::Exception::Base) {
181 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
182 "Failed in getRootPluginHandleList");
186 DbPluginHandle PluginDAOReadOnly::getPluginHandle() const
188 return m_pluginHandle;
191 //"value" cannot be null, as in registerPlugin it is always set
192 #define RETURN_STD_STRING(in, what) \
193 std::string ret = ""; \
194 if (!in.IsNull()) { \
195 ret = DPL::ToUTF8String(*in); } \
196 LogDebug(" >> Plugin " << what << ": " << ret); \
199 std::string PluginDAOReadOnly::getLibraryPath() const
201 LogDebug("Getting plugin library path. Handle: " << m_pluginHandle);
202 PluginRow row = getPluginRow(m_pluginHandle);
203 RETURN_STD_STRING(row.Get_PluginLibraryPath(), "library path")
206 std::string PluginDAOReadOnly::getLibraryName() const
208 LogDebug("Getting plugin library name. Handle: " << m_pluginHandle);
209 PluginRow row = getPluginRow(m_pluginHandle);
210 std::string ret = DPL::ToUTF8String(row.Get_PluginLibraryName());
211 LogDebug(" >> Plugin library name: " << ret);
215 #undef RETURN_STD_STRING
217 PluginHandleSetPtr PluginDAOReadOnly::getLibraryDependencies() const
221 using namespace DPL::DB::ORM;
222 using namespace DPL::DB::ORM::wrt;
223 PluginHandleSetPtr dependencies(new PluginHandleSet);
225 WRT_DB_SELECT(select, PluginDependencies, &WrtDatabase::interface())
227 Equals<PluginDependencies::PluginPropertiesId>(m_pluginHandle));
229 PluginDependencies::Select::RowList rows = select->GetRowList();
234 dependencies->insert(it->Get_RequiredPluginPropertiesId());
240 Catch(DPL::DB::SqlConnection::Exception::Base) {
241 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
242 "Failed in GetLibraryDependencies");
246 DbPluginHandle PluginDAOReadOnly::getPluginHandleForImplementedObject(
247 const std::string& objectName)
249 LogDebug("GetPluginHandle for object: " << objectName);
253 DbPluginHandle pluginHandle = INVALID_PLUGIN_HANDLE;
255 using namespace DPL::DB::ORM;
256 using namespace DPL::DB::ORM::wrt;
258 WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
260 Equals<PluginImplementedObjects::PluginObject>(
261 DPL::FromUTF8String(objectName)));
263 PluginImplementedObjects::Select::RowList rows = select->GetRowList();
266 pluginHandle = rows.front().Get_PluginPropertiesId();
268 LogWarning("PluginHandle for object not found");
272 Catch(DPL::DB::SqlConnection::Exception::Base) {
273 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
274 "Failed in GetPluginHandleForImplementedObject");
278 ImplementedObjectsList PluginDAOReadOnly::getImplementedObjects()
280 LogDebug("getImplementedObjects");
284 ImplementedObjectsList objectList;
285 using namespace DPL::DB::ORM;
286 using namespace DPL::DB::ORM::wrt;
288 WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
289 std::list<DPL::String> valueList = select->GetValueList<PluginImplementedObjects::PluginObject>();
291 if (!valueList.empty()) {
292 FOREACH(it, valueList)
294 objectList.push_back(DPL::ToUTF8String(*it));
297 LogWarning("PluginHandle for object not found");
301 Catch(DPL::DB::SqlConnection::Exception::Base) {
302 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
303 "Failed in GetPluginHandleForImplementedObject");
307 ImplementedObjectsList PluginDAOReadOnly::getImplementedObjectsForPluginHandle(
308 DbPluginHandle handle)
310 LogDebug("getImplementedObjects for pluginHandle: " << handle);
314 ImplementedObjectsList objectList;
315 using namespace DPL::DB::ORM;
316 using namespace DPL::DB::ORM::wrt;
318 WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
320 Equals<PluginImplementedObjects::PluginPropertiesId>(handle));
322 PluginImplementedObjects::Select::RowList rows = select->GetRowList();
327 objectList.push_back(DPL::ToUTF8String(it->Get_PluginObject()));
330 LogWarning("PluginHandle for object not found");
334 Catch(DPL::DB::SqlConnection::Exception::Base) {
335 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
336 "Failed in GetPluginHandleForImplementedObject");
340 PluginObjectsDAO::ObjectsPtr PluginDAOReadOnly::
341 getRequiredObjectsForPluginHandle(
342 DbPluginHandle handle)
346 using namespace DPL::DB::ORM;
347 using namespace DPL::DB::ORM::wrt;
349 PluginObjectsDAO::ObjectsPtr objects =
350 PluginObjectsDAO::ObjectsPtr(new PluginObjectsDAO::Objects);
352 WRT_DB_SELECT(select, PluginRequiredObjects, &WrtDatabase::interface())
354 Equals<PluginRequiredObjects::PluginPropertiesId>(handle));
356 PluginRequiredObjects::Select::RowList rows = select->GetRowList();
361 objects->insert(DPL::ToUTF8String(it->Get_PluginObject()));
367 Catch(DPL::DB::SqlConnection::Exception::Base) {
368 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
369 "Failed in GetRequiredObjectsForPluginHandle");
373 PluginHandleSetPtr PluginDAOReadOnly::getPluginHandleByStatus(
374 PluginInstallationState state)
378 using namespace DPL::DB::ORM;
379 using namespace DPL::DB::ORM::wrt;
381 PluginHandleSetPtr handleSet(new PluginHandleSet);
383 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
385 Equals<PluginProperties::InstallationState>(ToInt(state)));
387 PluginProperties::Select::RowList rows = select->GetRowList();
392 handleSet->insert(it->Get_PluginPropertiesId());
398 Catch(DPL::DB::SqlConnection::Exception::Base) {
399 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
400 "Failed in GetPluginHandleByStatus");
404 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::
405 getInstallationStatus() const
407 PluginRow row = getPluginRow(m_pluginHandle);
408 return ToState(row.Get_InstallationState());
411 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::
412 getInstallationStateForHandle(
413 DbPluginHandle handle)
417 using namespace DPL::DB::ORM;
418 using namespace DPL::DB::ORM::wrt;
420 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
422 Equals<PluginProperties::PluginPropertiesId>(handle));
424 PluginProperties::Select::RowList rows = select->GetRowList();
427 return ToState(rows.front().Get_InstallationState());
429 LogError("Data in DB are invalid. Missing field");
430 return UNKNOWN_ERROR;
432 Catch(DPL::DB::SqlConnection::Exception::Base) {
433 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
434 "Failed in GetStatusForHandle");
438 bool PluginDAOReadOnly::isPluginInstalled(DbPluginHandle pluginHandle)
441 using namespace DPL::DB::ORM;
442 using namespace DPL::DB::ORM::wrt;
443 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
445 Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
447 PluginProperties::Select::RowList rows = select->GetRowList();
449 bool flag = !rows.empty();
453 Catch(DPL::DB::SqlConnection::Exception::Base) {
454 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
455 "Failed in isPluginInstalled");