adf11e9b5193c224ddd6a1e45910949a332bdde1
[framework/web/wrt-commons.git] / modules / widget_dao / dao / feature_dao_read_only.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    feature_dao_read_only.cpp
18  * @author  Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version 1.0
20  * @brief   This file contains the implementation of feature dao read only
21  */
22
23 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
24 #include <sstream>
25 #include <dpl/log/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>
31
32 namespace WrtDB {
33
34 FeatureDAOReadOnly::FeatureDAOReadOnly(FeatureHandle featureHandle) :
35     m_featureHandle(featureHandle)
36 {
37     if (!isFeatureInstalled(m_featureHandle)) {
38         std::ostringstream exc;
39         exc << "Feature " << m_featureHandle << " not installed.";
40         LogError(exc.str());
41         ThrowMsg(FeatureDAOReadOnly::Exception::FeatureNotExist, exc.str());
42     }
43 }
44
45 FeatureDAOReadOnly::FeatureDAOReadOnly(const std::string &featureName)
46 {
47     LogDebug("FeatureDAOReadOnly ( " << featureName << " )");
48     Try {
49         using namespace DPL::DB::ORM;
50         using namespace DPL::DB::ORM::wrt;
51         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
52         select->Where(Equals<FeaturesList::FeatureName>(
53                           DPL::FromUTF8String(featureName)));
54
55         m_featureHandle = select->GetSingleValue<FeaturesList::FeatureUUID>();
56         LogDebug(" >> FeatureHandle retrieved: " << m_featureHandle);
57     }
58     Catch(DPL::DB::SqlConnection::Exception::Base){
59         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
60                    "Failure during creating FeatureDAOReadOnly");
61     }
62 }
63
64 #define GET_PLUGIN_DATA(func)                                           \
65     Try {                                                               \
66         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface()); \
67         LogDebug(# func << ". FeatureHandle: " << m_featureHandle);     \
68         std::string ret = PluginDAOReadOnly(GetPluginHandle()).func();  \
69         transaction.Commit();                                           \
70         return ret;                                                     \
71     }                                                                   \
72     Catch(DPL::DB::SqlConnection::Exception::Base) {                        \
73         std::ostringstream failure("Failure during ");                  \
74         failure << # func;                                              \
75         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,        \
76                 failure.str());                                         \
77     }
78
79 std::string FeatureDAOReadOnly::GetInstallURI() const
80 {
81     GET_PLUGIN_DATA(getInstallURI)
82 }
83
84 std::string FeatureDAOReadOnly::GetKeyCn() const
85 {
86     GET_PLUGIN_DATA(getKeyCn)
87 }
88
89 std::string FeatureDAOReadOnly::GetRootKey() const
90 {
91     GET_PLUGIN_DATA(getRootKey)
92 }
93
94 std::string FeatureDAOReadOnly::GetRootKeyFingerprint() const
95 {
96     GET_PLUGIN_DATA(getRootKeyFingerprint)
97 }
98
99 std::string FeatureDAOReadOnly::GetLibraryPath() const
100 {
101     GET_PLUGIN_DATA(getLibraryPath)
102 }
103
104 std::string FeatureDAOReadOnly::GetLibraryName() const
105 {
106     GET_PLUGIN_DATA(getLibraryName)
107 }
108
109 #undef GET_PLUGIN_DATA
110
111 FeatureHandle FeatureDAOReadOnly::GetFeatureHandle()  const
112 {
113     return m_featureHandle;
114 }
115
116 std::string FeatureDAOReadOnly::GetName() const
117 {
118     LogDebug("Getting Feature Name. Handle: " << m_featureHandle);
119     Try {
120         using namespace DPL::DB::ORM;
121         using namespace DPL::DB::ORM::wrt;
122         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
123         select->Where(Equals<FeaturesList::FeatureUUID>(m_featureHandle));
124
125         std::string ret = DPL::ToUTF8String(
126                 select->GetSingleValue< FeaturesList::FeatureName>());
127
128         LogDebug(" >> Feature name: " << ret);
129         return ret;
130     }
131     Catch(DPL::DB::SqlConnection::Exception::Base){
132         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
133                    "Failure during getting GetName");
134     }
135 }
136
137 DbPluginHandle FeatureDAOReadOnly::GetPluginHandle() const
138 {
139     LogDebug("Getting Plugin handle. FHandle: " << m_featureHandle);
140     Try {
141         using namespace DPL::DB::ORM;
142         using namespace DPL::DB::ORM::wrt;
143         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
144         select->Where(Equals<FeaturesList::FeatureUUID>(m_featureHandle));
145
146         DbPluginHandle pluginHandle =
147             select->GetSingleValue< FeaturesList::PluginPropertiesId>();
148
149         LogDebug(" >> Plugin Handle: " << pluginHandle);
150         return pluginHandle;
151     }
152     Catch(DPL::DB::SqlConnection::Exception::Base){
153         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
154                    "Failure during getting Plugin Handle");
155     }
156 }
157
158 FeatureHandleList FeatureDAOReadOnly::GetHandleList()
159 {
160     LogDebug("Getting FeatureHandle list.");
161     Try {
162         using namespace DPL::DB::ORM;
163         using namespace DPL::DB::ORM::wrt;
164         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
165
166         FeatureHandleList ret =
167             select->GetValueList<FeaturesList::FeatureUUID>();
168
169         std::ostringstream handles;
170         FOREACH(it, ret)
171         handles << *it << " ";
172         LogDebug(" >> FeatureHandle list retrieved: (" << handles << ")");
173
174         return ret;
175     }
176     Catch(DPL::DB::SqlConnection::Exception::Base){
177         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
178                    "Failure during getting FeatureHandle list");
179     }
180 }
181
182 bool FeatureDAOReadOnly::isFeatureInstalled(const std::string &featureName)
183 {
184     LogDebug("Check if Feature is installed. Name: " << featureName);
185     Try {
186         using namespace DPL::DB::ORM;
187         using namespace DPL::DB::ORM::wrt;
188         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
189         select->Where(Equals<FeaturesList::FeatureName>(
190                           DPL::FromUTF8String(featureName)));
191
192         FeaturesList::Select::RowList rows = select->GetRowList();
193
194         bool flag = !rows.empty();
195         LogDebug(" >> Feature " << featureName <<
196                  (flag ? " found." : " not found."));
197
198         return flag;
199     }
200     Catch(DPL::DB::SqlConnection::Exception::Base) {
201         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
202                    "Failure during checking if Feature is installed");
203     }
204 }
205
206 bool FeatureDAOReadOnly::isFeatureInstalled(FeatureHandle handle)
207 {
208     LogDebug("Check if Feature is installed. Handle: " << handle);
209     Try
210     {
211         using namespace DPL::DB::ORM;
212         using namespace DPL::DB::ORM::wrt;
213         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
214         select->Where(Equals<FeaturesList::FeatureUUID>(handle));
215
216         FeaturesList::Select::RowList rows = select->GetRowList();
217
218         bool flag = !rows.empty();
219         LogDebug(" >> Feature " << handle <<
220                  (flag ? " found." : " not found."));
221
222         return flag;
223     }
224     Catch(DPL::DB::SqlConnection::Exception::Base){
225         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
226                    "Failure during checking if Feature is installed");
227     }
228 }
229
230 bool FeatureDAOReadOnly::isDeviceCapabilityInstalled(
231         const std::string &deviceCapName)
232 {
233     LogDebug("Check if DeviceCap is installed. Name: " << deviceCapName);
234     Try {
235         using namespace DPL::DB::ORM;
236         using namespace DPL::DB::ORM::wrt;
237         WRT_DB_SELECT(select, DeviceCapabilities, &WrtDatabase::interface())
238         select->Where(Equals<DeviceCapabilities::DeviceCapName>(
239                           DPL::FromUTF8String(deviceCapName)));
240
241         DeviceCapabilities::Select::RowList rows = select->GetRowList();
242
243         bool flag = !rows.empty();
244         LogDebug(" >> Device Cap " << deviceCapName <<
245                  (flag ? "found." : "not found."));
246
247         return flag;
248     }
249     Catch(DPL::DB::SqlConnection::Exception::Base){
250         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
251                    "Failure during checking if DeviceCap is installed");
252     }
253 }
254
255 FeatureDAOReadOnly::DeviceCapabilitiesList
256 FeatureDAOReadOnly::GetDeviceCapabilities() const
257 {
258     Try {
259         LogDebug("Get DeviceCap. FeatureHandle: " << m_featureHandle);
260         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
261
262         using namespace DPL::DB::ORM;
263         using namespace DPL::DB::ORM::wrt;
264         WRT_DB_SELECT(selectDevCP, FeatureDeviceCapProxy, &WrtDatabase::interface())
265         selectDevCP->Where(Equals<FeatureDeviceCapProxy::FeatureUUID>(
266                                m_featureHandle));
267
268         DeviceCapabilitiesList devCap;
269
270         std::list<int> deviceIDs =
271             selectDevCP->GetValueList<FeatureDeviceCapProxy::DeviceCapID>();
272         FOREACH(devCId, deviceIDs)
273         {
274             WRT_DB_SELECT(selectDevC, DeviceCapabilities, &WrtDatabase::interface())
275             selectDevC->Where(Equals<DeviceCapabilities::DeviceCapID>(*devCId));
276
277             DPL::String devNames =
278                 selectDevC->GetSingleValue<DeviceCapabilities::DeviceCapName>();
279
280             devCap.insert(DPL::ToUTF8String(devNames));
281         }
282
283         transaction.Commit();
284         return devCap;
285     }
286     Catch(DPL::DB::SqlConnection::Exception::Base){
287         ReThrowMsg(FeatureDAOReadOnly::Exception::DatabaseError,
288                    "Failure during getting DeviceCapabilities names");
289     }
290 }
291
292 FeatureHandleListPtr FeatureDAOReadOnly::GetFeatureHandleListForPlugin(
293         DbPluginHandle pluginHandle)
294 {
295     LogDebug("Getting FeatureHandle list for pluginHandle: " << pluginHandle);
296     Try {
297         using namespace DPL::DB::ORM;
298         using namespace DPL::DB::ORM::wrt;
299
300         WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
301         select->Where(Equals<FeaturesList::PluginPropertiesId>(pluginHandle));
302
303         FeatureHandleListPtr handles(new FeatureHandleList);
304         FeatureHandleList ret =
305             select->GetValueList<FeaturesList::FeatureUUID>();
306
307         FOREACH(it, ret)
308         {
309             LogDebug("feature handle: " << *it);
310             handles->push_back(*it);
311         }
312
313         return handles;
314     }
315     Catch(DPL::DB::SqlConnection::Exception::Base){
316         ReThrowMsg(
317                 FeatureDAOReadOnly::Exception::DatabaseError,
318                 "Failure during getting FeatureHandle Set for plugin handle");
319     }
320 }
321
322 } // namespace WrtDB