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/wrt_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 WrtLogD("Getting plugin row. Handle: %i", 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 WrtLogE("Plugin %i not installed.", m_pluginHandle);
66 Throw(PluginDAOReadOnly::Exception::PluginNotExist);
70 PluginDAOReadOnly::PluginDAOReadOnly(const std::string &libraryName)
72 WrtLogD("PluginDAOReadOnly ( %s )", libraryName.c_str());
74 using namespace DPL::DB::ORM;
75 using namespace DPL::DB::ORM::wrt;
76 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
77 select->Where(Equals<PluginProperties::PluginLibraryName>(
78 DPL::FromUTF8String(libraryName)));
80 PluginProperties::Select::RowList rows = select->GetRowList();
82 m_pluginHandle = rows.front().Get_PluginPropertiesId();
84 ThrowMsg(PluginDAOReadOnly::Exception::PluginNotExist,
85 "Cannot find plugin: [" + libraryName + "]");
87 WrtLogD(" >> Handle for this plugin: %i", m_pluginHandle);
89 Catch(DPL::DB::SqlConnection::Exception::Base) {
90 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
91 "Failed to connect to database");
95 bool PluginDAOReadOnly::isPluginInstalled(const std::string &libraryName)
97 WrtLogD("Check if Library is installed. LibraryName: %s",
100 using namespace DPL::DB::ORM;
101 using namespace DPL::DB::ORM::wrt;
102 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
103 select->Where(Equals<PluginProperties::PluginLibraryName>(
104 DPL::FromUTF8String(libraryName)));
106 PluginProperties::Select::RowList rows = select->GetRowList();
108 bool flag = !rows.empty();
109 WrtLogD(" >> Plugin %s %s", libraryName.c_str(),
110 (flag ? " found." : " not found."));
114 Catch(DPL::DB::SqlConnection::Exception::Base) {
115 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
116 "Failed in isPluginInstalled");
120 PluginHandleList PluginDAOReadOnly::getPluginHandleList()
122 WrtLogD("Getting plugin handle list.");
124 using namespace DPL::DB::ORM;
125 using namespace DPL::DB::ORM::wrt;
126 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
128 PluginHandleList ret =
129 select->GetValueList<PluginProperties::PluginPropertiesId>();
131 std::ostringstream handles;
133 handles << *it << " ";
134 WrtLogD(" >> PluginHandle list retrieved: (%s)", handles.str().c_str());
138 Catch(DPL::DB::SqlConnection::Exception::Base) {
139 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
140 "Failed in GetPluginHandleList");
144 PluginHandleList PluginDAOReadOnly::getRootPluginHandleList()
146 WrtLogD("Getting root plugin handle list.");
148 using namespace DPL::DB::ORM;
149 using namespace DPL::DB::ORM::wrt;
151 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
152 PluginHandleList handleList = select->GetValueList<PluginProperties::PluginPropertiesId>();
154 WRT_DB_SELECT(select_2nd, PluginDependencies, &WrtDatabase::interface())
155 PluginDependencies::Select::RowList dependenciesRows = select_2nd->GetRowList();
157 if (!dependenciesRows.empty())
159 FOREACH(it, dependenciesRows)
161 handleList.remove(it->Get_PluginPropertiesId());
167 Catch(DPL::DB::SqlConnection::Exception::Base) {
168 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
169 "Failed in getRootPluginHandleList");
173 DbPluginHandle PluginDAOReadOnly::getPluginHandle() const
175 return m_pluginHandle;
178 //"value" cannot be null, as in registerPlugin it is always set
179 #define RETURN_STD_STRING(in, what) \
180 std::string ret = ""; \
182 ret = DPL::ToUTF8String(*in); } \
183 WrtLogD(" >> Plugin %s: %s", what, ret.c_str()); \
186 std::string PluginDAOReadOnly::getLibraryPath() const
188 WrtLogD("Getting plugin library path. Handle: %i", m_pluginHandle);
189 PluginRow row = getPluginRow(m_pluginHandle);
190 RETURN_STD_STRING(row.Get_PluginLibraryPath(), "library path")
193 std::string PluginDAOReadOnly::getLibraryName() const
195 WrtLogD("Getting plugin library name. Handle: %i", m_pluginHandle);
196 PluginRow row = getPluginRow(m_pluginHandle);
197 std::string ret = DPL::ToUTF8String(row.Get_PluginLibraryName());
198 WrtLogD(" >> Plugin library name: %s", ret.c_str());
202 #undef RETURN_STD_STRING
204 PluginHandleSetPtr PluginDAOReadOnly::getLibraryDependencies() const
208 using namespace DPL::DB::ORM;
209 using namespace DPL::DB::ORM::wrt;
210 PluginHandleSetPtr dependencies(new PluginHandleSet);
212 WRT_DB_SELECT(select, PluginDependencies, &WrtDatabase::interface())
214 Equals<PluginDependencies::PluginPropertiesId>(m_pluginHandle));
216 PluginDependencies::Select::RowList rows = select->GetRowList();
221 dependencies->insert(it->Get_RequiredPluginPropertiesId());
227 Catch(DPL::DB::SqlConnection::Exception::Base) {
228 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
229 "Failed in GetLibraryDependencies");
233 PluginHandleSetPtr PluginDAOReadOnly::getLibraryDependentPlugins() const
237 using namespace DPL::DB::ORM;
238 using namespace DPL::DB::ORM::wrt;
239 PluginHandleSetPtr dependencies(new PluginHandleSet);
241 WRT_DB_SELECT(select, PluginDependencies, &WrtDatabase::interface())
243 Equals<PluginDependencies::RequiredPluginPropertiesId>(m_pluginHandle));
245 PluginDependencies::Select::RowList rows = select->GetRowList();
250 dependencies->insert(it->Get_PluginPropertiesId());
256 Catch(DPL::DB::SqlConnection::Exception::Base) {
257 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
258 "Failed in GetLibraryDependencies");
262 DbPluginHandle PluginDAOReadOnly::getPluginHandleForImplementedObject(
263 const std::string& objectName)
265 WrtLogD("GetPluginHandle for object: %s", objectName.c_str());
269 DbPluginHandle pluginHandle = INVALID_PLUGIN_HANDLE;
271 using namespace DPL::DB::ORM;
272 using namespace DPL::DB::ORM::wrt;
274 WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
276 Equals<PluginImplementedObjects::PluginObject>(
277 DPL::FromUTF8String(objectName)));
279 PluginImplementedObjects::Select::RowList rows = select->GetRowList();
282 pluginHandle = rows.front().Get_PluginPropertiesId();
284 WrtLogW("PluginHandle for object not found");
288 Catch(DPL::DB::SqlConnection::Exception::Base) {
289 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
290 "Failed in GetPluginHandleForImplementedObject");
294 ImplementedObjectsList PluginDAOReadOnly::getImplementedObjects()
296 WrtLogD("getImplementedObjects");
300 ImplementedObjectsList objectList;
301 using namespace DPL::DB::ORM;
302 using namespace DPL::DB::ORM::wrt;
304 WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
305 std::list<DPL::String> valueList = select->GetValueList<PluginImplementedObjects::PluginObject>();
307 if (!valueList.empty()) {
308 FOREACH(it, valueList)
310 objectList.push_back(DPL::ToUTF8String(*it));
313 WrtLogW("PluginHandle for object not found");
317 Catch(DPL::DB::SqlConnection::Exception::Base) {
318 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
319 "Failed in GetPluginHandleForImplementedObject");
323 ImplementedObjectsList PluginDAOReadOnly::getImplementedObjectsForPluginHandle(
324 DbPluginHandle handle)
326 WrtLogD("getImplementedObjects for pluginHandle: %i", handle);
330 ImplementedObjectsList objectList;
331 using namespace DPL::DB::ORM;
332 using namespace DPL::DB::ORM::wrt;
334 WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
336 Equals<PluginImplementedObjects::PluginPropertiesId>(handle));
338 PluginImplementedObjects::Select::RowList rows = select->GetRowList();
343 objectList.push_back(DPL::ToUTF8String(it->Get_PluginObject()));
346 WrtLogW("PluginHandle for object not found");
350 Catch(DPL::DB::SqlConnection::Exception::Base) {
351 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
352 "Failed in GetPluginHandleForImplementedObject");
356 PluginObjectsDAO::ObjectsPtr PluginDAOReadOnly::
357 getRequiredObjectsForPluginHandle(
358 DbPluginHandle handle)
362 using namespace DPL::DB::ORM;
363 using namespace DPL::DB::ORM::wrt;
365 PluginObjectsDAO::ObjectsPtr objects =
366 PluginObjectsDAO::ObjectsPtr(new PluginObjectsDAO::Objects);
368 WRT_DB_SELECT(select, PluginRequiredObjects, &WrtDatabase::interface())
370 Equals<PluginRequiredObjects::PluginPropertiesId>(handle));
372 PluginRequiredObjects::Select::RowList rows = select->GetRowList();
377 objects->insert(DPL::ToUTF8String(it->Get_PluginObject()));
383 Catch(DPL::DB::SqlConnection::Exception::Base) {
384 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
385 "Failed in GetRequiredObjectsForPluginHandle");
389 PluginHandleSetPtr PluginDAOReadOnly::getPluginHandleByStatus(
390 PluginInstallationState state)
394 using namespace DPL::DB::ORM;
395 using namespace DPL::DB::ORM::wrt;
397 PluginHandleSetPtr handleSet(new PluginHandleSet);
399 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
401 Equals<PluginProperties::InstallationState>(ToInt(state)));
403 PluginProperties::Select::RowList rows = select->GetRowList();
408 handleSet->insert(it->Get_PluginPropertiesId());
414 Catch(DPL::DB::SqlConnection::Exception::Base) {
415 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
416 "Failed in GetPluginHandleByStatus");
420 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::
421 getInstallationStatus() const
423 PluginRow row = getPluginRow(m_pluginHandle);
424 return ToState(row.Get_InstallationState());
427 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::
428 getInstallationStateForHandle(
429 DbPluginHandle handle)
433 using namespace DPL::DB::ORM;
434 using namespace DPL::DB::ORM::wrt;
436 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
438 Equals<PluginProperties::PluginPropertiesId>(handle));
440 PluginProperties::Select::RowList rows = select->GetRowList();
443 return ToState(rows.front().Get_InstallationState());
445 WrtLogE("Data in DB are invalid. Missing field");
446 return UNKNOWN_ERROR;
448 Catch(DPL::DB::SqlConnection::Exception::Base) {
449 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
450 "Failed in GetStatusForHandle");
454 std::time_t PluginDAOReadOnly::getLibraryTimestamp() const
456 WrtLogD("Getting plugin library timestamp. Handle: %i", m_pluginHandle);
457 PluginRow row = getPluginRow(m_pluginHandle);
458 std::time_t ret = row.Get_LibraryTimestamp();
459 WrtLogD(" >> Plugin library timestamp: %li", ret);
463 bool PluginDAOReadOnly::isPluginInstalled(DbPluginHandle pluginHandle)
466 using namespace DPL::DB::ORM;
467 using namespace DPL::DB::ORM::wrt;
468 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
470 Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
472 PluginProperties::Select::RowList rows = select->GetRowList();
474 bool flag = !rows.empty();
478 Catch(DPL::DB::SqlConnection::Exception::Base) {
479 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
480 "Failed in isPluginInstalled");