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 DbPluginHandle PluginDAOReadOnly::getPluginHandle() const
159 return m_pluginHandle;
162 //"value" cannot be null, as in registerPlugin it is always set
163 #define RETURN_STD_STRING(in, what) \
164 std::string ret = ""; \
165 if (!in.IsNull()) { \
166 ret = DPL::ToUTF8String(*in); } \
167 LogDebug(" >> Plugin " << what << ": " << ret); \
170 std::string PluginDAOReadOnly::getLibraryPath() const
172 LogDebug("Getting plugin library path. Handle: " << m_pluginHandle);
173 PluginRow row = getPluginRow(m_pluginHandle);
174 RETURN_STD_STRING(row.Get_PluginLibraryPath(), "library path")
177 std::string PluginDAOReadOnly::getLibraryName() const
179 LogDebug("Getting plugin library name. Handle: " << m_pluginHandle);
180 PluginRow row = getPluginRow(m_pluginHandle);
181 std::string ret = DPL::ToUTF8String(row.Get_PluginLibraryName());
182 LogDebug(" >> Plugin library name: " << ret);
186 #undef RETURN_STD_STRING
188 PluginHandleSetPtr PluginDAOReadOnly::getLibraryDependencies() const
192 using namespace DPL::DB::ORM;
193 using namespace DPL::DB::ORM::wrt;
194 PluginHandleSetPtr dependencies(new PluginHandleSet);
196 WRT_DB_SELECT(select, PluginDependencies, &WrtDatabase::interface())
198 Equals<PluginDependencies::PluginPropertiesId>(m_pluginHandle));
200 PluginDependencies::Select::RowList rows = select->GetRowList();
205 dependencies->insert(it->Get_RequiredPluginPropertiesId());
211 Catch(DPL::DB::SqlConnection::Exception::Base) {
212 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
213 "Failed in GetLibraryDependencies");
217 DbPluginHandle PluginDAOReadOnly::getPluginHandleForImplementedObject(
218 const std::string& objectName)
220 LogDebug("GetPluginHandle for object: " << objectName);
224 DbPluginHandle pluginHandle = INVALID_PLUGIN_HANDLE;
226 using namespace DPL::DB::ORM;
227 using namespace DPL::DB::ORM::wrt;
229 WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
231 Equals<PluginImplementedObjects::PluginObject>(
232 DPL::FromUTF8String(objectName)));
234 PluginImplementedObjects::Select::RowList rows = select->GetRowList();
237 pluginHandle = rows.front().Get_PluginPropertiesId();
239 LogWarning("PluginHandle for object not found");
243 Catch(DPL::DB::SqlConnection::Exception::Base) {
244 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
245 "Failed in GetPluginHandleForImplementedObject");
249 ImplementedObjectsList PluginDAOReadOnly::getImplementedObjectsForPluginHandle(
250 DbPluginHandle handle)
252 LogDebug("getImplementedObjects for pluginHandle: " << handle);
256 ImplementedObjectsList objectList;
257 using namespace DPL::DB::ORM;
258 using namespace DPL::DB::ORM::wrt;
260 WRT_DB_SELECT(select, PluginImplementedObjects, &WrtDatabase::interface())
262 Equals<PluginImplementedObjects::PluginPropertiesId>(handle));
264 PluginImplementedObjects::Select::RowList rows = select->GetRowList();
269 objectList.push_back(DPL::ToUTF8String(it->Get_PluginObject()));
272 LogWarning("PluginHandle for object not found");
276 Catch(DPL::DB::SqlConnection::Exception::Base) {
277 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
278 "Failed in GetPluginHandleForImplementedObject");
282 PluginObjectsDAO::ObjectsPtr PluginDAOReadOnly::
283 getRequiredObjectsForPluginHandle(
284 DbPluginHandle handle)
288 using namespace DPL::DB::ORM;
289 using namespace DPL::DB::ORM::wrt;
291 PluginObjectsDAO::ObjectsPtr objects =
292 PluginObjectsDAO::ObjectsPtr(new PluginObjectsDAO::Objects);
294 WRT_DB_SELECT(select, PluginRequiredObjects, &WrtDatabase::interface())
296 Equals<PluginRequiredObjects::PluginPropertiesId>(handle));
298 PluginRequiredObjects::Select::RowList rows = select->GetRowList();
303 objects->insert(DPL::ToUTF8String(it->Get_PluginObject()));
309 Catch(DPL::DB::SqlConnection::Exception::Base) {
310 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
311 "Failed in GetRequiredObjectsForPluginHandle");
315 PluginHandleSetPtr PluginDAOReadOnly::getPluginHandleByStatus(
316 PluginInstallationState state)
320 using namespace DPL::DB::ORM;
321 using namespace DPL::DB::ORM::wrt;
323 PluginHandleSetPtr handleSet(new PluginHandleSet);
325 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
327 Equals<PluginProperties::InstallationState>(ToInt(state)));
329 PluginProperties::Select::RowList rows = select->GetRowList();
334 handleSet->insert(it->Get_PluginPropertiesId());
340 Catch(DPL::DB::SqlConnection::Exception::Base) {
341 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
342 "Failed in GetPluginHandleByStatus");
346 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::
347 getInstallationStatus() const
349 PluginRow row = getPluginRow(m_pluginHandle);
350 return ToState(row.Get_InstallationState());
353 PluginDAOReadOnly::PluginInstallationState PluginDAOReadOnly::
354 getInstallationStateForHandle(
355 DbPluginHandle handle)
359 using namespace DPL::DB::ORM;
360 using namespace DPL::DB::ORM::wrt;
362 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
364 Equals<PluginProperties::PluginPropertiesId>(handle));
366 PluginProperties::Select::RowList rows = select->GetRowList();
369 return ToState(rows.front().Get_InstallationState());
371 LogError("Data in DB are invalid. Missing field");
372 return UNKNOWN_ERROR;
374 Catch(DPL::DB::SqlConnection::Exception::Base) {
375 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
376 "Failed in GetStatusForHandle");
380 bool PluginDAOReadOnly::isPluginInstalled(DbPluginHandle pluginHandle)
383 using namespace DPL::DB::ORM;
384 using namespace DPL::DB::ORM::wrt;
385 WRT_DB_SELECT(select, PluginProperties, &WrtDatabase::interface())
387 Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
389 PluginProperties::Select::RowList rows = select->GetRowList();
391 bool flag = !rows.empty();
395 Catch(DPL::DB::SqlConnection::Exception::Base) {
396 ReThrowMsg(PluginDAOReadOnly::Exception::DatabaseError,
397 "Failed in isPluginInstalled");