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 std::string PluginDAOReadOnly::getInstallURI() const
190 LogDebug("Getting plugin install URI. Handle: " << m_pluginHandle);
191 PluginRow row = getPluginRow(m_pluginHandle);
192 RETURN_STD_STRING(row.Get_InstallURI(), "install URI")
195 std::string PluginDAOReadOnly::getKeyCn() const
197 LogDebug("Getting plugin KeyCn. Handle: " << m_pluginHandle);
198 PluginRow row = getPluginRow(m_pluginHandle);
199 RETURN_STD_STRING(row.Get_KeyCN(), "keyCN")
202 std::string PluginDAOReadOnly::getRootKey() const
204 LogDebug("Getting plugin rootKey. Handle: " << m_pluginHandle);
205 PluginRow row = getPluginRow(m_pluginHandle);
206 RETURN_STD_STRING(row.Get_RootKeyCN(), "rootKey")
209 std::string PluginDAOReadOnly::getRootKeyFingerprint() const
211 LogDebug("Getting plugin rootKeyFingerprint. Handle: " << m_pluginHandle);
212 PluginRow row = getPluginRow(m_pluginHandle);
213 RETURN_STD_STRING(row.Get_RootKeyFingerprint(), "rootKeyFingerprint")
216 #undef RETURN_STD_STRING
218 PluginHandleSetPtr PluginDAOReadOnly::getLibraryDependencies() const
222 using namespace DPL::DB::ORM;
223 using namespace DPL::DB::ORM::wrt;
224 PluginHandleSetPtr dependencies(new PluginHandleSet);
226 WRT_DB_SELECT(select, PluginDependencies, &WrtDatabase::interface())
228 Equals<PluginDependencies::PluginPropertiesId>(m_pluginHandle));
230 PluginDependencies::Select::RowList rows = select->GetRowList();
235 dependencies->insert(it->Get_RequiredPluginPropertiesId());
241 Catch(DPL::DB::SqlConnection::Exception::Base) {
242 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
243 "Failed in GetLibraryDependencies");
247 DbPluginHandle PluginDAOReadOnly::getPluginHandleForImplementedObject(
248 const std::string& objectName)
250 LogDebug("GetPluginHandle for object: " << objectName);
254 DbPluginHandle pluginHandle = INVALID_PLUGIN_HANDLE;
256 using namespace DPL::DB::ORM;
257 using namespace DPL::DB::ORM::wrt;
259 WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
261 Equals<PluginImplementedObjects::PluginObject>(
262 DPL::FromUTF8String(objectName)));
264 PluginImplementedObjects::Select::RowList rows = select->GetRowList();
267 pluginHandle = rows.front().Get_PluginPropertiesId();
269 LogWarning("PluginHandle for object not found");
273 Catch(DPL::DB::SqlConnection::Exception::Base) {
274 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
275 "Failed in GetPluginHandleForImplementedObject");
279 ImplementedObjectsList PluginDAOReadOnly::getImplementedObjectsForPluginHandle(
280 DbPluginHandle handle)
282 LogDebug("getImplementedObjects for pluginHandle: " << handle);
286 ImplementedObjectsList objectList;
287 using namespace DPL::DB::ORM;
288 using namespace DPL::DB::ORM::wrt;
290 WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
292 Equals<PluginImplementedObjects::PluginPropertiesId>(handle));
294 PluginImplementedObjects::Select::RowList rows = select->GetRowList();
299 objectList.push_back(DPL::ToUTF8String(it->Get_PluginObject()));
302 LogWarning("PluginHandle for object not found");
306 Catch(DPL::DB::SqlConnection::Exception::Base) {
307 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
308 "Failed in GetPluginHandleForImplementedObject");
312 PluginObjectsDAO::ObjectsPtr PluginDAOReadOnly::getRequiredObjectsForPluginHandle(
313 DbPluginHandle handle)
317 using namespace DPL::DB::ORM;
318 using namespace DPL::DB::ORM::wrt;
320 PluginObjectsDAO::ObjectsPtr objects =
321 PluginObjectsDAO::ObjectsPtr(new PluginObjectsDAO::Objects);
323 WRT_DB_SELECT(select, PluginRequiredObjects, &WrtDatabase::interface())
325 Equals<PluginRequiredObjects::PluginPropertiesId>(handle));
327 PluginRequiredObjects::Select::RowList rows = select->GetRowList();
332 objects->insert(DPL::ToUTF8String(it->Get_PluginObject()));
338 Catch(DPL::DB::SqlConnection::Exception::Base) {
339 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
340 "Failed in GetRequiredObjectsForPluginHandle");
344 PluginHandleSetPtr PluginDAOReadOnly::getPluginHandleByStatus(
345 PluginInstallationState state)
349 using namespace DPL::DB::ORM;
350 using namespace DPL::DB::ORM::wrt;
352 PluginHandleSetPtr handleSet(new PluginHandleSet);
354 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
356 Equals<PluginProperties::InstallationState>(ToInt(state)));
358 PluginProperties::Select::RowList rows = select->GetRowList();
363 handleSet->insert(it->Get_PluginPropertiesId());
369 Catch(DPL::DB::SqlConnection::Exception::Base) {
370 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
371 "Failed in GetPluginHandleByStatus");
375 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::getInstallationStatus() const
377 PluginRow row = getPluginRow(m_pluginHandle);
378 return ToState(row.Get_InstallationState());
381 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::getInstallationStateForHandle(
382 DbPluginHandle handle)
386 using namespace DPL::DB::ORM;
387 using namespace DPL::DB::ORM::wrt;
389 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
391 Equals<PluginProperties::PluginPropertiesId>(handle));
393 PluginProperties::Select::RowList rows = select->GetRowList();
396 return ToState(rows.front().Get_InstallationState());
398 LogError("Data in DB are invalid. Missing field");
399 return UNKNOWN_ERROR;
401 Catch(DPL::DB::SqlConnection::Exception::Base) {
402 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
403 "Failed in GetStatusForHandle");
407 bool PluginDAOReadOnly::isPluginInstalled(DbPluginHandle pluginHandle)
410 using namespace DPL::DB::ORM;
411 using namespace DPL::DB::ORM::wrt;
412 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
414 Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
416 PluginProperties::Select::RowList rows = select->GetRowList();
418 bool flag = !rows.empty();
422 Catch(DPL::DB::SqlConnection::Exception::Base) {
423 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
424 "Failed in isPluginInstalled");