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 feature_dao_read_only.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
20 * @brief This file contains the implementation of feature dao read only
23 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
25 #include <dpl/log/wrt_log.h>
26 #include <dpl/foreach.h>
27 #include <dpl/db/orm.h>
28 #include <orm_generator_wrt.h>
29 #include <dpl/wrt-dao-ro/webruntime_database.h>
30 #include <dpl/wrt-dao-ro/plugin_dao_read_only.h>
33 FeatureDAOReadOnly::FeatureDAOReadOnly(FeatureHandle featureHandle) :
34 m_featureHandle(featureHandle)
36 if (!isFeatureInstalled(m_featureHandle)) {
37 std::ostringstream exc;
38 exc << "Feature " << m_featureHandle << " not installed.";
39 WrtLogE("%s", exc.str().c_str());
40 ThrowMsg(FeatureDAOReadOnly::Exception::FeatureNotExist, exc.str());
44 FeatureDAOReadOnly::FeatureDAOReadOnly(const std::string &featureName)
46 WrtLogD("FeatureDAOReadOnly ( %s )", featureName.c_str());
48 using namespace DPL::DB::ORM;
49 using namespace DPL::DB::ORM::wrt;
50 WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
51 select->Where(Equals<FeaturesList::FeatureName>(
52 DPL::FromUTF8String(featureName)));
54 m_featureHandle = select->GetSingleValue<FeaturesList::FeatureUUID>();
55 WrtLogD(" >> FeatureHandle retrieved: %i", m_featureHandle);
57 Catch(DPL::DB::SqlConnection::Exception::Base){
58 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
59 "Failure during creating FeatureDAOReadOnly");
63 #define GET_PLUGIN_DATA(func) \
65 DPL::DB::ORM::wrt::ScopedTransaction transaction( \
66 &WrtDatabase::interface()); \
67 WrtLogD(#func ". FeatureHandle: %i", m_featureHandle); \
68 std::string ret = PluginDAOReadOnly(GetPluginHandle()).func(); \
69 transaction.Commit(); \
72 Catch(DPL::DB::SqlConnection::Exception::Base) { \
73 std::ostringstream failure("Failure during "); \
75 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError, \
79 std::string FeatureDAOReadOnly::GetLibraryPath() const
81 GET_PLUGIN_DATA(getLibraryPath)
84 std::string FeatureDAOReadOnly::GetLibraryName() const
86 GET_PLUGIN_DATA(getLibraryName)
89 #undef GET_PLUGIN_DATA
91 FeatureHandle FeatureDAOReadOnly::GetFeatureHandle() const
93 return m_featureHandle;
96 std::string FeatureDAOReadOnly::GetName() const
98 WrtLogD("Getting Feature Name. Handle: %i", m_featureHandle);
100 using namespace DPL::DB::ORM;
101 using namespace DPL::DB::ORM::wrt;
102 WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
103 select->Where(Equals<FeaturesList::FeatureUUID>(m_featureHandle));
105 std::string ret = DPL::ToUTF8String(
106 select->GetSingleValue< FeaturesList::FeatureName>());
108 WrtLogD(" >> Feature name: %s", ret.c_str());
111 Catch(DPL::DB::SqlConnection::Exception::Base){
112 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
113 "Failure during getting GetName");
117 DbPluginHandle FeatureDAOReadOnly::GetPluginHandle() const
119 WrtLogD("Getting Plugin handle. FHandle: %i", m_featureHandle);
121 using namespace DPL::DB::ORM;
122 using namespace DPL::DB::ORM::wrt;
123 WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
124 select->Where(Equals<FeaturesList::FeatureUUID>(m_featureHandle));
126 DbPluginHandle pluginHandle =
127 select->GetSingleValue< FeaturesList::PluginPropertiesId>();
129 WrtLogD(" >> Plugin Handle: %i", pluginHandle);
132 Catch(DPL::DB::SqlConnection::Exception::Base){
133 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
134 "Failure during getting Plugin Handle");
138 FeatureHandleList FeatureDAOReadOnly::GetHandleList()
140 WrtLogD("Getting FeatureHandle list.");
142 using namespace DPL::DB::ORM;
143 using namespace DPL::DB::ORM::wrt;
144 WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
146 FeatureHandleList ret =
147 select->GetValueList<FeaturesList::FeatureUUID>();
149 std::ostringstream handles;
151 handles << *it << " ";
152 WrtLogD(" >> FeatureHandle list retrieved: (%s)",
153 handles.str().c_str());
157 Catch(DPL::DB::SqlConnection::Exception::Base){
158 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
159 "Failure during getting FeatureHandle list");
163 bool FeatureDAOReadOnly::isFeatureInstalled(const std::string &featureName)
165 WrtLogD("Check if Feature is installed. Name: %s", featureName.c_str());
167 using namespace DPL::DB::ORM;
168 using namespace DPL::DB::ORM::wrt;
169 WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
170 select->Where(Equals<FeaturesList::FeatureName>(
171 DPL::FromUTF8String(featureName)));
173 FeaturesList::Select::RowList rows = select->GetRowList();
175 bool flag = !rows.empty();
176 WrtLogD(" >> Feature %s %s", featureName.c_str(),
177 (flag ? " found." : " not found."));
181 Catch(DPL::DB::SqlConnection::Exception::Base) {
182 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
183 "Failure during checking if Feature is installed");
187 bool FeatureDAOReadOnly::isFeatureInstalled(FeatureHandle handle)
189 WrtLogD("Check if Feature is installed. Handle: %i", handle);
192 using namespace DPL::DB::ORM;
193 using namespace DPL::DB::ORM::wrt;
194 WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
195 select->Where(Equals<FeaturesList::FeatureUUID>(handle));
197 FeaturesList::Select::RowList rows = select->GetRowList();
199 bool flag = !rows.empty();
200 WrtLogD(" >> Feature %i %s",
201 handle, (flag ? " found." : " not found."));
205 Catch(DPL::DB::SqlConnection::Exception::Base){
206 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
207 "Failure during checking if Feature is installed");
211 bool FeatureDAOReadOnly::isDeviceCapabilityInstalled(
212 const std::string &deviceCapName)
214 WrtLogD("Check if DeviceCap is installed. Name: %s", deviceCapName.c_str());
216 using namespace DPL::DB::ORM;
217 using namespace DPL::DB::ORM::wrt;
218 WRT_DB_SELECT(select, DeviceCapabilities, &WrtDatabase::interface())
219 select->Where(Equals<DeviceCapabilities::DeviceCapName>(
220 DPL::FromUTF8String(deviceCapName)));
222 DeviceCapabilities::Select::RowList rows = select->GetRowList();
224 bool flag = !rows.empty();
225 WrtLogD(" >> Device Cap %s %s", deviceCapName.c_str(),
226 (flag ? "found." : "not found."));
230 Catch(DPL::DB::SqlConnection::Exception::Base){
231 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
232 "Failure during checking if DeviceCap is installed");
236 FeatureDAOReadOnly::DeviceCapabilitiesList
237 FeatureDAOReadOnly::GetDeviceCapabilities() const
240 WrtLogD("Get DeviceCap. FeatureHandle: %i", m_featureHandle);
241 DPL::DB::ORM::wrt::ScopedTransaction transaction(
242 &WrtDatabase::interface());
244 using namespace DPL::DB::ORM;
245 using namespace DPL::DB::ORM::wrt;
246 WRT_DB_SELECT(selectDevCP,
247 FeatureDeviceCapProxy,
248 &WrtDatabase::interface())
249 selectDevCP->Where(Equals<FeatureDeviceCapProxy::FeatureUUID>(
252 DeviceCapabilitiesList devCap;
254 std::list<int> deviceIDs =
255 selectDevCP->GetValueList<FeatureDeviceCapProxy::DeviceCapID>();
256 FOREACH(devCId, deviceIDs)
258 WRT_DB_SELECT(selectDevC,
260 &WrtDatabase::interface())
261 selectDevC->Where(Equals<DeviceCapabilities::DeviceCapID>(*devCId));
263 DPL::String devNames =
264 selectDevC->GetSingleValue<DeviceCapabilities::DeviceCapName>();
266 devCap.insert(DPL::ToUTF8String(devNames));
269 transaction.Commit();
272 Catch(DPL::DB::SqlConnection::Exception::Base){
273 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
274 "Failure during getting DeviceCapabilities names");
278 FeatureHandleListPtr FeatureDAOReadOnly::GetFeatureHandleListForPlugin(
279 DbPluginHandle pluginHandle)
281 WrtLogD("Getting FeatureHandle list for pluginHandle: %i", pluginHandle);
283 using namespace DPL::DB::ORM;
284 using namespace DPL::DB::ORM::wrt;
286 WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
287 select->Where(Equals<FeaturesList::PluginPropertiesId>(pluginHandle));
289 FeatureHandleListPtr handles(new FeatureHandleList);
290 FeatureHandleList ret =
291 select->GetValueList<FeaturesList::FeatureUUID>();
295 WrtLogD("feature handle: %i", *it);
296 handles->push_back(*it);
301 Catch(DPL::DB::SqlConnection::Exception::Base){
303 FeatureDAOReadOnly::Exception::DatabaseError,
304 "Failure during getting FeatureHandle Set for plugin handle");
308 FeatureDAOReadOnly::NameMap
309 FeatureDAOReadOnly::GetNames()
312 using namespace DPL::DB::ORM;
313 using namespace DPL::DB::ORM::wrt;
314 WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
318 FeaturesList::Select::RowList rows = select->GetRowList();
321 nameMap.insert(std::pair<FeatureHandle,
322 std::string>(rowIt->Get_FeatureUUID(),
323 DPL::ToUTF8String(rowIt->
324 Get_FeatureName())));
329 Catch(DPL::DB::SqlConnection::Exception::Base){
330 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
331 "Failure during getting GetNames");
335 FeatureDAOReadOnly::DeviceCapabilitiesMap
336 FeatureDAOReadOnly::GetDevCapWithFeatureHandle()
339 using namespace DPL::DB::ORM;
340 using namespace DPL::DB::ORM::wrt;
342 DECLARE_COLUMN_TYPE_LIST()
343 SELECTED_COLUMN(FeatureDeviceCapProxy, FeatureUUID)
344 SELECTED_COLUMN(DeviceCapabilities, DeviceCapName)
345 DECLARE_COLUMN_TYPE_LIST_END(DevCapNameList)
347 WRT_DB_SELECT(select, FeatureDeviceCapProxy, &WrtDatabase::interface())
348 select->Join<DevCapNameList>(Equal<FeatureDeviceCapProxy::DeviceCapID,
349 DeviceCapabilities::DeviceCapID>());
351 DeviceCapabilitiesMap devCap;
353 std::list< CustomRow<DevCapNameList> > rowList =
354 select->GetCustomRowList< DevCapNameList, CustomRow<DevCapNameList> >();
355 FOREACH(rowIt, rowList)
357 FeatureHandle featureHandle =
358 (*rowIt).GetColumnData<FeatureDeviceCapProxy::FeatureUUID>();
359 std::string devName =
360 DPL::ToUTF8String((*rowIt).GetColumnData<DeviceCapabilities::
362 devCap.insert(std::pair<FeatureHandle, std::string>(featureHandle,
368 Catch(DPL::DB::SqlConnection::Exception::Base){
369 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
370 "Failure during getting DeviceCapabilities names");
374 DeviceCapabilitySet FeatureDAOReadOnly::GetDeviceCapability(
375 const DPL::String &apifeature)
377 // This could be done with one simply sql query but support for join is
380 using namespace DPL::DB::ORM;
381 using namespace DPL::DB::ORM::wrt;
384 FeatureDeviceCapProxy::Select::RowList rows;
385 DeviceCapabilitySet result;
388 WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
389 select->Where(Equals<FeaturesList::FeatureName>(apifeature));
390 featureUUID = select->GetSingleValue<FeaturesList::FeatureUUID>();
394 WRT_DB_SELECT(select,
395 FeatureDeviceCapProxy,
396 &WrtDatabase::interface())
397 select->Where(Equals<FeatureDeviceCapProxy::FeatureUUID>(
399 rows = select->GetRowList();
403 WRT_DB_SELECT(select, DeviceCapabilities, &WrtDatabase::interface())
404 select->Where(Equals<DeviceCapabilities::DeviceCapID>(
405 it->Get_DeviceCapID()));
406 result.insert(select->
407 GetSingleValue<DeviceCapabilities::DeviceCapName>());
411 } Catch(DPL::DB::SqlConnection::Exception::Base){
412 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
413 "Failed to get device capability");
417 FeatureDAOReadOnly::FeatureMap
418 FeatureDAOReadOnly::GetFeatures(const std::list<std::string>& featureNames)
421 using namespace DPL::DB::ORM;
422 using namespace DPL::DB::ORM::wrt;
424 std::set<typename FeaturesList::FeatureName::ColumnType> nameList;
425 FOREACH(nm, featureNames) {
426 nameList.insert(DPL::FromUTF8String(*nm));
429 FeaturesList::Select select{ &WrtDatabase::interface() };
430 select.Where(In<FeaturesList::FeatureName>(nameList));
432 FeatureMap featureMap;
433 FeatureData featureData;
434 FeaturesList::Select::RowList rows = select.GetRowList();
435 FOREACH(rowIt, rows) {
436 featureData.featureName = DPL::ToUTF8String(
437 rowIt->Get_FeatureName());
438 featureData.pluginHandle = rowIt->Get_PluginPropertiesId();
439 featureMap.insert(std::pair<FeatureHandle, FeatureData>(
440 rowIt->Get_FeatureUUID(), featureData));
445 Catch(DPL::DB::SqlConnection::Exception::Base){
446 ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
447 "Failure during getting GetFeatures");