Update wrt-commons_0.2.53
[framework/web/wrt-commons.git] / modules / widget_dao / dao / widget_dao.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  * This file contains the definition of widget dao class.
18  *
19  * @file    widget_dao.cpp
20  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
21  * @author  Bartosz Janiak (b.janiak@samsung.com)
22  * @author  Yang Jie (jie2.yang@samsung.com)
23  * @author  Koeun Choi(koeun.choi@samsung.com)
24  * @author  Pawel Sikorski(p.sikorski@samsung.com)
25  * @version 1.0
26  * @brief   This file contains the definition of Configuration.
27  */
28
29 #include <dpl/wrt-dao-rw/widget_dao.h>
30
31 #include <sstream>
32 #include <dpl/log/log.h>
33 #include <dpl/foreach.h>
34 #include <dpl/wrt-dao-ro/webruntime_database.h>
35 #include <dpl/wrt-dao-rw/property_dao.h>
36 #include <orm_generator_wrt.h>
37 #include <dpl/wrt-dao-ro/WrtDatabase.h>
38
39 namespace WrtDB {
40
41 //TODO in current solution in each getter there exists a check
42 //"IsWidgetInstalled". Maybe it should be verified, if it could be done
43 //differently  (check in WidgetDAO constructor)
44
45 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN          Try
46
47 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message)   \
48     Catch(DPL::DB::SqlConnection::Exception::Base) {       \
49         LogError(message);                              \
50         ReThrowMsg(WidgetDAO::Exception::DatabaseError, \
51                    message);                            \
52     }
53
54 #define CHECK_WIDGET_EXISTENCE(macro_transaction, macro_handle)          \
55     if (!WidgetDAO::isWidgetInstalled(macro_handle))                      \
56     {                                                                    \
57         macro_transaction.Commit();                                      \
58         LogWarning("Cannot find widget. Handle: " << macro_handle);      \
59         ThrowMsg(WidgetDAO::Exception::WidgetNotExist,                   \
60                  "Cannot find widget. Handle: " << macro_handle);        \
61     }
62
63
64 WidgetDAO::WidgetDAO(DbWidgetHandle widgetHandle) :
65     WidgetDAOReadOnly(widgetHandle)
66 {
67 }
68
69 WidgetDAO::WidgetDAO(DPL::OptionalString widgetGUID) :
70     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(widgetGUID))
71 {
72 }
73
74 WidgetDAO::~WidgetDAO()
75 {
76 }
77
78 void WidgetDAO::removeProperty(
79         const PropertyDAOReadOnly::WidgetPropertyKey &key)
80 {
81     Try {
82         PropertyDAO::RemoveProperty(m_widgetHandle, key);
83     }
84     Catch(PropertyDAOReadOnly::Exception::ReadOnlyProperty){
85         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
86                    "Failure during removing property");
87     }
88 }
89
90 void WidgetDAO::setProperty(
91         const PropertyDAOReadOnly::WidgetPropertyKey &key,
92         const PropertyDAOReadOnly::WidgetPropertyValue &value,
93         bool readOnly)
94 {
95     Try {
96         PropertyDAO::SetProperty(m_widgetHandle, key, value, readOnly);
97     }
98     Catch(PropertyDAOReadOnly::Exception::ReadOnlyProperty){
99         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
100                    "Failure during setting/updating property");
101     }
102 }
103
104 void WidgetDAO::setPkgName(const DPL::OptionalString& pkgName)
105 {
106     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
107     {
108         using namespace DPL::DB::ORM;
109         wrt::ScopedTransaction transaction(&WrtDatabase::interface());
110
111         isWidgetInstalled(getHandle());
112
113         wrt::WidgetInfo::Row row;
114         row.Set_pkgname(pkgName);
115
116         WRT_DB_UPDATE(update, wrt::WidgetInfo, &WrtDatabase::interface())
117         update->Where(
118             Equals<wrt::WidgetInfo::app_id>(getHandle()));
119
120         update->Values(row);
121         update->Execute();
122         transaction.Commit();
123     }
124     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
125 }
126
127 void WidgetDAO::registerWidget(
128         const DbWidgetHandle& widgetHandle,
129         const WidgetRegisterInfo &widgetRegInfo,
130         const IWacSecurity &wacSecurity)
131 {
132     LogDebug("Registering widget");
133     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
134     {
135         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
136
137         //Register into WidgetInfo has to be first
138         //as all other tables depend upon that
139         registerWidgetInfo(widgetHandle, widgetRegInfo, wacSecurity);
140
141         registerWidgetExtendedInfo(widgetHandle, widgetRegInfo);
142
143         registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo);
144
145         registerWidgetIcons(widgetHandle, widgetRegInfo);
146
147         registerWidgetStartFile(widgetHandle, widgetRegInfo);
148
149         PropertyDAO::RegisterProperties(widgetHandle, widgetRegInfo);
150
151         registerWidgetFeatures(widgetHandle, widgetRegInfo);
152
153         registerWidgetWindowModes(widgetHandle, widgetRegInfo);
154
155         registerWidgetWarpInfo(widgetHandle, widgetRegInfo);
156
157         registerWidgetCertificates(widgetHandle, wacSecurity);
158
159         CertificateChainList list;
160         wacSecurity.getCertificateChainList(list);
161         registerLaunchCertificates(widgetHandle,list);
162
163         registerWidgetSettings(widgetHandle, widgetRegInfo);
164
165         registerAppService(widgetHandle, widgetRegInfo);
166
167         transaction.Commit();
168     }
169     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
170 }
171
172 #define DO_INSERT(row, table)                              \
173     {                                                      \
174         WRT_DB_INSERT(insert, table, &WrtDatabase::interface()) \
175         insert->Values(row);                               \
176         insert->Execute();                                 \
177     }
178
179 void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle,
180         const WidgetRegisterInfo &regInfo)
181 {
182     //Try and transaction not needed
183     using namespace DPL::DB::ORM;
184     using namespace DPL::DB::ORM::wrt;
185
186
187     WidgetExtendedInfo::Row row;
188     row.Set_app_id(widgetHandle);
189     //    row.Set_share_href    (DPL::FromUTF8String(regInfo.shareHref));
190     row.Set_signature_type(regInfo.signatureType);
191     row.Set_factory_widget(regInfo.isFactoryWidget);
192     row.Set_test_widget(regInfo.isTestWidget);
193     row.Set_install_time(regInfo.installedTime);
194     row.Set_splash_img_src(regInfo.configInfo.splashImgSrc);
195
196
197     DO_INSERT(row, WidgetExtendedInfo)
198 }
199
200 void WidgetDAO::registerWidgetInfo(
201         const DbWidgetHandle& widgetHandle,
202         const WidgetRegisterInfo &regInfo,
203         const IWacSecurity &wacSecurity)
204 {
205     using namespace DPL::DB::ORM;
206     using namespace DPL::DB::ORM::wrt;
207     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
208
209     // TODO in wrt_db all Columns in WidgetInfo have DEFAULT VALUE set.
210     // Because of that, "Optional" is not used there
211
212     WidgetInfo::Row row;
213     row.Set_app_id(widgetHandle);
214     row.Set_widget_type(regInfo.type.appType);
215     row.Set_widget_id(widgetConfigurationInfo.widget_id);
216     row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale);
217     row.Set_widget_version(widgetConfigurationInfo.version);
218     row.Set_widget_width(widgetConfigurationInfo.width);
219     row.Set_widget_height(widgetConfigurationInfo.height);
220     row.Set_author_name(widgetConfigurationInfo.authorName);
221     row.Set_author_email(widgetConfigurationInfo.authorEmail);
222     row.Set_author_href(widgetConfigurationInfo.authorHref);
223     row.Set_base_folder(DPL::FromUTF8String(regInfo.baseFolder));
224     row.Set_webkit_plugins_required(widgetConfigurationInfo.flashNeeded);
225     row.Set_recognized(wacSecurity.isRecognized());
226     row.Set_wac_signed(wacSecurity.isWacSigned());
227     row.Set_distributor_signed(wacSecurity.isDistributorSigned());
228     {
229         std::stringstream tmp;
230         tmp << regInfo.minVersion;
231         row.Set_min_version(DPL::FromUTF8String(tmp.str()));
232     }
233     row.Set_back_supported(widgetConfigurationInfo.backSupported);
234     row.Set_access_network(widgetConfigurationInfo.accessNetwork);
235     row.Set_pkgname(regInfo.pkgname);
236     row.Set_pkg_type(regInfo.pType.pkgType);
237
238     Try
239     {
240         DO_INSERT(row, WidgetInfo);
241     }
242     Catch(DPL::DB::SqlConnection::Exception::Base)
243     {
244         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
245                    "Failed to register widget info.");
246     }
247 }
248
249 void WidgetDAO::registerWidgetLocalizedInfo(DbWidgetHandle widgetHandle,
250         const WidgetRegisterInfo &regInfo)
251 {
252     using namespace DPL::DB::ORM;
253     using namespace DPL::DB::ORM::wrt;
254
255     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
256
257     FOREACH(it, widgetConfigurationInfo.localizedDataSet)
258     {
259         const DPL::String& locale = it->first;
260         const ConfigParserData::LocalizedData& data = it->second;
261
262         LocalizedWidgetInfo::Row row;
263         row.Set_app_id(widgetHandle);
264         row.Set_widget_locale(locale);
265         row.Set_widget_name(data.name);
266         row.Set_widget_shortname(data.shortName);
267         row.Set_widget_description(data.description);
268         row.Set_widget_license(data.license);
269         row.Set_widget_license_file(data.licenseFile);
270         row.Set_widget_license_href(data.licenseHref);
271
272         DO_INSERT(row, LocalizedWidgetInfo)
273     }
274 }
275
276 void WidgetDAO::registerWidgetIcons(DbWidgetHandle widgetHandle,
277         const WidgetRegisterInfo &regInfo)
278 {
279     using namespace DPL::DB::ORM;
280     using namespace DPL::DB::ORM::wrt;
281
282
283     FOREACH(i, regInfo.localizationData.icons)
284     {
285         wrt::WidgetIcon::icon_id::ColumnType icon_id;
286         {
287             wrt::WidgetIcon::Row row;
288             row.Set_app_id(widgetHandle);
289             row.Set_icon_src(i->src);
290             row.Set_icon_width(i->width);
291             row.Set_icon_height(i->height);
292
293             WRT_DB_INSERT(insert, wrt::WidgetIcon, &WrtDatabase::interface())
294             insert->Values(row);
295             icon_id = insert->Execute();
296         }
297
298         FOREACH(j, i->availableLocales)
299         {
300             WidgetLocalizedIcon::Row row;
301             row.Set_app_id(widgetHandle);
302             row.Set_icon_id(icon_id);
303             row.Set_widget_locale(*j);
304             WRT_DB_SELECT(select, WidgetLocalizedIcon, &WrtDatabase::interface())
305             select->Where(And(Equals<WidgetLocalizedIcon::app_id>(widgetHandle),
306                               Equals<WidgetLocalizedIcon::widget_locale>(*j)));
307             WidgetLocalizedIcon::Select::RowList rows = select->GetRowList();
308
309             bool flag = !rows.empty();
310
311             if(flag == true)
312             {
313                 // already default icon value of same locale exists
314                 WRT_DB_UPDATE(update, WidgetLocalizedIcon, &WrtDatabase::interface())
315                 update->Where(And(Equals<WidgetLocalizedIcon::app_id>(widgetHandle),
316                                   Equals<WidgetLocalizedIcon::widget_locale>(*j)));
317                 update->Values(row);
318                 update->Execute();
319             }else{
320                 // any icon value of same locale doesn't exist
321                 DO_INSERT(row, WidgetLocalizedIcon)
322             }
323         }
324     }
325 }
326
327 void WidgetDAO::registerWidgetStartFile(DbWidgetHandle widgetHandle,
328         const WidgetRegisterInfo &regInfo)
329 {
330     using namespace DPL::DB::ORM;
331     using namespace DPL::DB::ORM::wrt;
332
333
334     FOREACH(i, regInfo.localizationData.startFiles)
335     {
336         WidgetStartFile::start_file_id::ColumnType startFileID;
337         {
338             WidgetStartFile::Row row;
339             row.Set_app_id(widgetHandle);
340             row.Set_src(i->path);
341
342             WRT_DB_INSERT(insert, WidgetStartFile, &WrtDatabase::interface())
343             insert->Values(row);
344             startFileID = insert->Execute();
345         }
346
347         FOREACH(j, i->propertiesForLocales)
348         {
349             WidgetLocalizedStartFile::Row row;
350             row.Set_app_id(widgetHandle);
351             row.Set_start_file_id(startFileID);
352             row.Set_widget_locale(j->first);
353             row.Set_type(j->second.type);
354             row.Set_encoding(j->second.encoding);
355
356             DO_INSERT(row, WidgetLocalizedStartFile)
357         }
358     }
359 }
360
361 void WidgetDAO::registerWidgetFeatures(DbWidgetHandle widgetHandle,
362         const WidgetRegisterInfo &regInfo)
363 {
364     using namespace DPL::DB::ORM;
365     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
366
367
368     FOREACH(pWidgetFeature, widgetConfigurationInfo.featuresList)
369     {
370         wrt::WidgetFeature::Row widgetFeature;
371         widgetFeature.Set_app_id(widgetHandle);
372         widgetFeature.Set_name(pWidgetFeature->name);
373         widgetFeature.Set_required(pWidgetFeature->required);
374         widgetFeature.Set_rejected(false);
375
376         wrt::WidgetFeature::widget_feature_id::ColumnType widgetFeatureID;
377         {
378             WRT_DB_INSERT(insert, wrt::WidgetFeature, &WrtDatabase::interface())
379             insert->Values(widgetFeature);
380             widgetFeatureID = insert->Execute();
381         }
382
383         // Insert into table FeatureParam
384         wrt::FeatureParam::Row featureParam;
385         featureParam.Set_widget_feature_id(widgetFeatureID);
386
387         ConfigParserData::ParamsList::const_iterator iter;
388
389         FOREACH(iter, pWidgetFeature->paramsList)
390         {
391             featureParam.Set_name(iter->name);
392             featureParam.Set_value(iter->value);
393
394             DO_INSERT(featureParam, wrt::FeatureParam)
395         }
396     }
397 }
398
399 void WidgetDAO::updateFeatureRejectStatus(const DbWidgetFeature &widgetFeature){
400     // This function could be merged with registerWidgetFeature but it requires desing change:
401     // 1. Check "ace step" in installer must be done before "update database step"
402     // And:
403     // ConfigurationParserData shouldn't be called "ParserData" any more.
404     using namespace DPL::DB::ORM;
405
406     wrt::ScopedTransaction transaction(&WrtDatabase::interface());
407     WRT_DB_SELECT(select, wrt::WidgetFeature, &WrtDatabase::interface())
408     select->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
409                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
410
411     auto row = select->GetSingleRow();
412     row.Set_rejected(widgetFeature.rejected);
413
414     WRT_DB_UPDATE(update, wrt::WidgetFeature, &WrtDatabase::interface())
415     update->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
416                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
417     update->Values(row);
418     update->Execute();
419     transaction.Commit();
420 }
421
422 void WidgetDAO::registerWidgetWindowModes(DbWidgetHandle widgetHandle,
423         const WidgetRegisterInfo &regInfo)
424 {
425     using namespace DPL::DB::ORM;
426     using namespace DPL::DB::ORM::wrt;
427     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
428
429
430     FOREACH(i, widgetConfigurationInfo.windowModes)
431     {
432         wrt::WidgetWindowModes::Row windowMode;
433         windowMode.Set_app_id(widgetHandle);
434         windowMode.Set_window_mode(*i);
435
436         DO_INSERT(windowMode, wrt::WidgetWindowModes)
437     }
438 }
439
440 void WidgetDAO::registerWidgetWarpInfo(DbWidgetHandle widgetHandle,
441         const WidgetRegisterInfo &regInfo)
442 {
443     using namespace DPL::DB::ORM;
444     using namespace DPL::DB::ORM::wrt;
445     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
446
447
448     FOREACH(AccIt, widgetConfigurationInfo.accessInfoSet)
449     {
450         WidgetWARPInfo::Row row;
451         row.Set_app_id(widgetHandle);
452         row.Set_iri(AccIt->m_strIRI);
453         row.Set_subdomain_access(static_cast <int>(
454                                      AccIt->m_bSubDomainAccess));
455
456         DO_INSERT(row, WidgetWARPInfo)
457     }
458 }
459
460 void WidgetDAO::registerWidgetCertificates(DbWidgetHandle widgetHandle,
461         const IWacSecurity &wacSecurity)
462 {
463     using namespace DPL::DB::ORM;
464     using namespace DPL::DB::ORM::wrt;
465
466
467     FOREACH(it, wacSecurity.getCertificateList())
468     {
469         WidgetCertificateFingerprint::Row row;
470         row.Set_app_id(widgetHandle);
471         row.Set_owner(it->owner);
472         row.Set_chainid(it->chainId);
473         row.Set_type(it->type);
474         row.Set_md5_fingerprint(DPL::FromUTF8String(it->strMD5Fingerprint));
475         row.Set_sha1_fingerprint(DPL::FromUTF8String(it->strSHA1Fingerprint));
476         row.Set_common_name(it->strCommonName);
477
478         DO_INSERT(row, WidgetCertificateFingerprint)
479     }
480 }
481
482 void WidgetDAO::registerLaunchCertificates(DbWidgetHandle widgetHandle,
483         const CertificateChainList &certificateChainList)
484 {
485     using namespace DPL::DB::ORM;
486     using namespace DPL::DB::ORM::wrt;
487     FOREACH(certChain, certificateChainList)
488     {
489         WidgetCertificate::Row row;
490         row.Set_app_id(widgetHandle);
491         row.Set_encoded_chain(DPL::FromASCIIString(*certChain));
492
493         DO_INSERT(row, WidgetCertificate);
494     }
495 }
496
497 void WidgetDAO::registerWidgetSettings(DbWidgetHandle widgetHandle,
498         const WidgetRegisterInfo &regInfo)
499 {
500     using namespace DPL::DB::ORM;
501     using namespace DPL::DB::ORM::wrt;
502
503     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
504
505     FOREACH(pWidgetSetting, widgetConfigurationInfo.settingsList)
506     {
507         SettginsList::Row row;
508         row.Set_appId(widgetHandle);
509         row.Set_settingName(pWidgetSetting->m_name);
510         row.Set_settingValue(pWidgetSetting->m_value);
511
512         DO_INSERT(row, SettginsList)
513     }
514 }
515
516 void WidgetDAO::registerAppService(DbWidgetHandle widgetHandle,
517                                    const WidgetRegisterInfo &regInfo)
518 {
519     using namespace DPL::DB::ORM;
520     using namespace DPL::DB::ORM::wrt;
521     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
522
523     FOREACH(ASIt, widgetConfigurationInfo.appServiceList)
524     {
525         ApplicationServiceInfo::Row row;
526         row.Set_app_id(widgetHandle);
527         row.Set_src(ASIt->m_src);
528         row.Set_operation(ASIt->m_operation);
529         row.Set_scheme(ASIt->m_scheme);
530         row.Set_mime(ASIt->m_mime);
531
532         DO_INSERT(row, ApplicationServiceInfo)
533     }
534 }
535
536 #undef DO_INSERT
537
538 void WidgetDAO::unregisterWidget(DbWidgetHandle widgetHandle)
539 {
540     LogDebug("Unregistering widget from DB. Handle: " << widgetHandle);
541     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
542     {
543         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
544         using namespace DPL::DB::ORM;
545         using namespace DPL::DB::ORM::wrt;
546
547         CHECK_WIDGET_EXISTENCE(transaction, widgetHandle)
548
549         // Delete from table Widget Info
550         {
551             WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
552             del->Where(Equals<WidgetInfo::app_id>(widgetHandle));
553             del->Execute();
554         }
555
556         // Deleting in other tables is done via "delete cascade" in SQL
557
558         transaction.Commit();
559     }
560     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
561 }
562
563 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
564 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
565 #undef CHECK_WIDGET_EXISTENCE
566
567 } // namespace WrtDB