f22671ea0c35f95f0370a41828dfd6410bd490d7
[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 #include <stddef.h>
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 WidgetDAO::WidgetDAO(DPL::OptionalString widgetGUID) :
55     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(widgetGUID))
56 {
57 }
58
59 WidgetDAO::WidgetDAO(DPL::String tzAppId) :
60     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(tzAppId))
61 {
62 }
63
64 WidgetDAO::WidgetDAO(DbWidgetHandle handle) :
65     WidgetDAOReadOnly(handle)
66 {
67 }
68
69 WidgetDAO::~WidgetDAO()
70 {
71 }
72
73 void WidgetDAO::removeProperty(
74         const PropertyDAOReadOnly::WidgetPropertyKey &key)
75 {
76     Try {
77         PropertyDAO::RemoveProperty(m_widgetHandle, key);
78     }
79     Catch(PropertyDAOReadOnly::Exception::ReadOnlyProperty){
80         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
81                    "Failure during removing property");
82     }
83 }
84
85 void WidgetDAO::setProperty(
86         const PropertyDAOReadOnly::WidgetPropertyKey &key,
87         const PropertyDAOReadOnly::WidgetPropertyValue &value,
88         bool readOnly)
89 {
90     Try {
91         PropertyDAO::SetProperty(m_widgetHandle, key, value, readOnly);
92     }
93     Catch(PropertyDAOReadOnly::Exception::ReadOnlyProperty){
94         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
95                    "Failure during setting/updating property");
96     }
97 }
98
99 void WidgetDAO::setTizenAppId(const DPL::OptionalString& tzAppId)
100 {
101     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
102     {
103         using namespace DPL::DB::ORM;
104         wrt::ScopedTransaction transaction(&WrtDatabase::interface());
105
106         if (!isWidgetInstalled(getHandle())) {
107             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
108                 "Cannot find widget. Handle: " << getHandle());
109         }
110
111         wrt::WidgetInfo::Row row;
112         row.Set_tizen_appid(*tzAppId);
113
114         WRT_DB_UPDATE(update, wrt::WidgetInfo, &WrtDatabase::interface())
115         update->Where(
116             Equals<wrt::WidgetInfo::app_id>(getHandle()));
117
118         update->Values(row);
119         update->Execute();
120         transaction.Commit();
121     }
122     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
123 }
124
125 void WidgetDAO::setSecurityPopupUsage(const SettingsType value)
126 {
127     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
128     {
129         using namespace DPL::DB::ORM;
130         using namespace DPL::DB::ORM::wrt;
131
132         ScopedTransaction transaction(&WrtDatabase::interface());
133         if (!isWidgetInstalled(getHandle())) {
134             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
135                 "Cannot find widget. Handle: " << getHandle());
136         }
137
138         WidgetSecuritySettings::Row row;
139         row.Set_security_popup_usage(value);
140
141         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
142         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
143         update->Values(row);
144         update->Execute();
145
146         transaction.Commit();
147     }
148     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set security popup usage")
149 }
150
151 void WidgetDAO::setGeolocationUsage(const SettingsType value)
152 {
153     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
154     {
155         using namespace DPL::DB::ORM;
156         using namespace DPL::DB::ORM::wrt;
157
158         ScopedTransaction transaction(&WrtDatabase::interface());
159         if (!isWidgetInstalled(getHandle())) {
160             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
161                 "Cannot find widget. Handle: " << getHandle());
162         }
163
164         WidgetSecuritySettings::Row row;
165         row.Set_geolocation_usage(value);
166
167         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
168         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
169         update->Values(row);
170         update->Execute();
171
172         transaction.Commit();
173     }
174     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set geolocation usage")
175 }
176
177 void WidgetDAO::setWebNotificationUsage(const SettingsType value)
178 {
179     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
180     {
181         using namespace DPL::DB::ORM;
182         using namespace DPL::DB::ORM::wrt;
183
184         ScopedTransaction transaction(&WrtDatabase::interface());
185         if (!isWidgetInstalled(getHandle())) {
186             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
187                 "Cannot find widget. Handle: " << getHandle());
188         }
189
190         WidgetSecuritySettings::Row row;
191         row.Set_web_notification_usage(value);
192
193         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
194         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
195         update->Values(row);
196         update->Execute();
197
198         transaction.Commit();
199     }
200     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set web notification usage")
201 }
202
203 void WidgetDAO::setWebDatabaseUsage(const SettingsType value)
204 {
205     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
206     {
207         using namespace DPL::DB::ORM;
208         using namespace DPL::DB::ORM::wrt;
209
210         ScopedTransaction transaction(&WrtDatabase::interface());
211         if (!isWidgetInstalled(getHandle())) {
212             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
213                 "Cannot find widget. Handle: " << getHandle());
214         }
215
216         WidgetSecuritySettings::Row row;
217         row.Set_web_database_usage(value);
218
219         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
220         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
221         update->Values(row);
222         update->Execute();
223
224         transaction.Commit();
225     }
226     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set web database usage")
227 }
228
229 void WidgetDAO::setFileSystemUsage(const SettingsType value)
230 {
231     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
232     {
233         using namespace DPL::DB::ORM;
234         using namespace DPL::DB::ORM::wrt;
235
236         ScopedTransaction transaction(&WrtDatabase::interface());
237         if (!isWidgetInstalled(getHandle())) {
238             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
239                 "Cannot find widget. Handle: " << getHandle());
240         }
241
242         WidgetSecuritySettings::Row row;
243         row.Set_file_system_usage(value);
244
245         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
246         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
247         update->Values(row);
248         update->Execute();
249
250         transaction.Commit();
251     }
252     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set filesystem usage")
253 }
254
255 void WidgetDAO::registerWidget(
256         const TizenAppId & tzAppId,
257         const WidgetRegisterInfo &widgetRegInfo,
258         const IWacSecurity &wacSecurity)
259 {
260     LogDebug("Registering widget");
261     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
262     {
263         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
264         registerWidgetInternal(tzAppId, widgetRegInfo, wacSecurity);
265         transaction.Commit();
266     }
267     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
268 }
269
270 void WidgetDAO::registerWidget(
271         WrtDB::DbWidgetHandle handle,
272         const WidgetRegisterInfo &widgetRegInfo,
273         const IWacSecurity &wacSecurity)
274 {
275     LogDebug("Registering widget");
276     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
277     {
278         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
279         registerWidgetInternal(generateTizenId(), widgetRegInfo, wacSecurity, handle);
280         transaction.Commit();
281     }
282     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
283 }
284
285 DbWidgetHandle WidgetDAO::registerWidget(
286             const WidgetRegisterInfo &pWidgetRegisterInfo,
287             const IWacSecurity &wacSecurity)
288 {
289     //make it more precise due to very fast tests
290     struct timeval tv;
291     gettimeofday(&tv, NULL);
292     srand(time(NULL) + tv.tv_usec);
293     DbWidgetHandle widgetHandle;
294     do {
295         widgetHandle = rand();
296     } while (isWidgetInstalled(widgetHandle));
297
298     registerWidget(widgetHandle, pWidgetRegisterInfo, wacSecurity);
299     return widgetHandle;
300 }
301
302 TizenAppId WidgetDAO::registerWidgetGeneratePkgId(
303             const WidgetRegisterInfo &pWidgetRegisterInfo,
304             const IWacSecurity &wacSecurity)
305 {
306     TizenAppId tzAppId = generatePkgId();
307     registerWidget(tzAppId, pWidgetRegisterInfo, wacSecurity);
308     return tzAppId;
309 }
310
311 void WidgetDAO::registerWidgetInternal(
312         const TizenAppId & tzAppId,
313         const WidgetRegisterInfo &widgetRegInfo,
314         const IWacSecurity &wacSecurity,
315         const DPL::Optional<DbWidgetHandle> handle)
316 {
317     //Register into WidgetInfo has to be first
318     //as all other tables depend upon that
319     DbWidgetHandle widgetHandle = registerWidgetInfo(tzAppId, widgetRegInfo, wacSecurity, handle);
320
321     registerWidgetExtendedInfo(widgetHandle, widgetRegInfo);
322
323     registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo);
324
325     registerWidgetIcons(widgetHandle, widgetRegInfo);
326
327     registerWidgetStartFile(widgetHandle, widgetRegInfo);
328
329     PropertyDAO::RegisterProperties(widgetHandle, widgetRegInfo);
330
331     registerWidgetFeatures(widgetHandle, widgetRegInfo);
332
333     registerWidgetPrivilege(widgetHandle, widgetRegInfo);
334
335     registerWidgetWindowModes(widgetHandle, widgetRegInfo);
336
337     registerWidgetWarpInfo(widgetHandle, widgetRegInfo);
338
339     registerWidgetCertificates(widgetHandle, wacSecurity);
340
341     CertificateChainList list;
342     wacSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR);
343     registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR, list);
344
345     list.clear();
346     wacSecurity.getCertificateChainList(list, SIGNATURE_AUTHOR);
347     registerCertificatesChains(widgetHandle, SIGNATURE_AUTHOR, list);
348
349     registerWidgetSettings(widgetHandle, widgetRegInfo);
350
351     registerAppService(widgetHandle, widgetRegInfo);
352
353     registerEncryptedResouceInfo(widgetHandle, widgetRegInfo);
354
355     registerExternalLocations(widgetHandle, widgetRegInfo.externalLocations);
356
357     registerWidgetSecuritySettings(widgetHandle);
358 }
359
360 void WidgetDAO::registerOrUpdateWidget(
361         const TizenAppId & widgetName,
362         const WidgetRegisterInfo &widgetRegInfo,
363         const IWacSecurity &wacSecurity)
364 {
365     LogDebug("Reregistering widget");
366     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
367     {
368         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
369
370         unregisterWidgetInternal(widgetName);
371         registerWidgetInternal(widgetName, widgetRegInfo, wacSecurity);
372         transaction.Commit();
373     }
374     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to reregister widget")
375 }
376
377 #define DO_INSERT(row, table)                              \
378     {                                                      \
379         WRT_DB_INSERT(insert, table, &WrtDatabase::interface()) \
380         insert->Values(row);                               \
381         insert->Execute();                                 \
382     }
383
384 void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle,
385         const WidgetRegisterInfo &regInfo)
386 {
387     //Try and transaction not needed
388     using namespace DPL::DB::ORM;
389     using namespace DPL::DB::ORM::wrt;
390
391
392     WidgetExtendedInfo::Row row;
393     row.Set_app_id(widgetHandle);
394     //    row.Set_share_href    (DPL::FromUTF8String(regInfo.shareHref));
395     row.Set_signature_type(regInfo.signatureType);
396     row.Set_test_widget(regInfo.isTestWidget);
397     row.Set_install_time(regInfo.installedTime);
398     row.Set_splash_img_src(regInfo.configInfo.splashImgSrc);
399     row.Set_background_page(regInfo.configInfo.backgroundPage);
400     row.Set_installed_path(regInfo.widgetInstalledPath);
401
402     DO_INSERT(row, WidgetExtendedInfo)
403 }
404
405 DbWidgetHandle WidgetDAO::registerWidgetInfo(
406         const TizenAppId & tzAppId,
407         const WidgetRegisterInfo &regInfo,
408         const IWacSecurity &wacSecurity,
409         const DPL::Optional<DbWidgetHandle> handle)
410 {
411     using namespace DPL::DB::ORM;
412     using namespace DPL::DB::ORM::wrt;
413     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
414
415     // TODO in wrt_db all Columns in WidgetInfo have DEFAULT VALUE set.
416     // Because of that, "Optional" is not used there
417
418     WidgetInfo::Row row;
419     if(!!handle)
420     {
421         row.Set_app_id(*handle);
422     }
423
424     if (regInfo.webAppType == APP_TYPE_UNKNOWN && regInfo.type !=
425             APP_TYPE_UNKNOWN) {
426         // TODO : regInfo.type is temporary code for security.
427         //        This code will be removed.
428         row.Set_widget_type(regInfo.type.appType);
429     } else {
430         row.Set_widget_type(regInfo.webAppType.appType);
431     }
432     row.Set_widget_id(widgetConfigurationInfo.widget_id);
433     row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale);
434     row.Set_widget_version(widgetConfigurationInfo.version);
435     row.Set_widget_width(widgetConfigurationInfo.width);
436     row.Set_widget_height(widgetConfigurationInfo.height);
437     row.Set_author_name(widgetConfigurationInfo.authorName);
438     row.Set_author_email(widgetConfigurationInfo.authorEmail);
439     row.Set_author_href(widgetConfigurationInfo.authorHref);
440     row.Set_base_folder(DPL::FromUTF8String(regInfo.baseFolder));
441     row.Set_webkit_plugins_required(widgetConfigurationInfo.flashNeeded);
442     row.Set_recognized(wacSecurity.isRecognized());
443     row.Set_wac_signed(wacSecurity.isWacSigned());
444     row.Set_distributor_signed(wacSecurity.isDistributorSigned());
445     row.Set_tizen_appid(tzAppId);
446     row.Set_tizen_pkgid(regInfo.tzPkgid);
447     {
448         std::stringstream tmp;
449         tmp << regInfo.minVersion;
450         row.Set_min_version(DPL::FromUTF8String(tmp.str()));
451     }
452     row.Set_back_supported(widgetConfigurationInfo.backSupported);
453     row.Set_access_network(widgetConfigurationInfo.accessNetwork);
454     row.Set_pkg_type(regInfo.packagingType.pkgType);
455
456     Try
457     {
458         DO_INSERT(row, WidgetInfo);
459     }
460     Catch(DPL::DB::SqlConnection::Exception::Base)
461     {
462         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
463                    "Failed to register widget info.");
464     }
465
466     if(!handle)
467     {
468         //get autoincremented value of widgetHandle
469         WRT_DB_SELECT(select, WidgetInfo, &WrtDatabase::interface())
470         select->Where(Equals<WidgetInfo::tizen_appid>(tzAppId));
471         return select->GetSingleValue<WidgetInfo::app_id>();
472     }
473     else
474     {
475         return *handle;
476     }
477 }
478
479 void WidgetDAO::registerWidgetLocalizedInfo(DbWidgetHandle widgetHandle,
480         const WidgetRegisterInfo &regInfo)
481 {
482     using namespace DPL::DB::ORM;
483     using namespace DPL::DB::ORM::wrt;
484
485     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
486
487     FOREACH(it, widgetConfigurationInfo.localizedDataSet)
488     {
489         const DPL::String& locale = it->first;
490         const ConfigParserData::LocalizedData& data = it->second;
491
492         LocalizedWidgetInfo::Row row;
493         row.Set_app_id(widgetHandle);
494         row.Set_widget_locale(locale);
495         row.Set_widget_name(data.name);
496         row.Set_widget_shortname(data.shortName);
497         row.Set_widget_description(data.description);
498         row.Set_widget_license(data.license);
499         row.Set_widget_license_file(data.licenseFile);
500         row.Set_widget_license_href(data.licenseHref);
501
502         DO_INSERT(row, LocalizedWidgetInfo)
503     }
504 }
505
506 void WidgetDAO::registerWidgetIcons(DbWidgetHandle widgetHandle,
507         const WidgetRegisterInfo &regInfo)
508 {
509     using namespace DPL::DB::ORM;
510     using namespace DPL::DB::ORM::wrt;
511
512
513     FOREACH(i, regInfo.localizationData.icons)
514     {
515         wrt::WidgetIcon::icon_id::ColumnType icon_id;
516         {
517             wrt::WidgetIcon::Row row;
518             row.Set_app_id(widgetHandle);
519             row.Set_icon_src(i->src);
520             row.Set_icon_width(i->width);
521             row.Set_icon_height(i->height);
522
523             WRT_DB_INSERT(insert, wrt::WidgetIcon, &WrtDatabase::interface())
524             insert->Values(row);
525             icon_id = insert->Execute();
526         }
527
528         FOREACH(j, i->availableLocales)
529         {
530             WidgetLocalizedIcon::Row row;
531             row.Set_app_id(widgetHandle);
532             row.Set_icon_id(icon_id);
533             row.Set_widget_locale(*j);
534             DO_INSERT(row, WidgetLocalizedIcon)
535         }
536     }
537 }
538
539 void WidgetDAO::registerWidgetStartFile(DbWidgetHandle widgetHandle,
540         const WidgetRegisterInfo &regInfo)
541 {
542     using namespace DPL::DB::ORM;
543     using namespace DPL::DB::ORM::wrt;
544
545
546     FOREACH(i, regInfo.localizationData.startFiles)
547     {
548         WidgetStartFile::start_file_id::ColumnType startFileID;
549         {
550             WidgetStartFile::Row row;
551             row.Set_app_id(widgetHandle);
552             row.Set_src(i->path);
553
554             WRT_DB_INSERT(insert, WidgetStartFile, &WrtDatabase::interface())
555             insert->Values(row);
556             startFileID = insert->Execute();
557         }
558
559         FOREACH(j, i->propertiesForLocales)
560         {
561             WidgetLocalizedStartFile::Row row;
562             row.Set_app_id(widgetHandle);
563             row.Set_start_file_id(startFileID);
564             row.Set_widget_locale(j->first);
565             row.Set_type(j->second.type);
566             row.Set_encoding(j->second.encoding);
567
568             DO_INSERT(row, WidgetLocalizedStartFile)
569         }
570     }
571 }
572
573 void WidgetDAO::registerWidgetFeatures(DbWidgetHandle widgetHandle,
574         const WidgetRegisterInfo &regInfo)
575 {
576     using namespace DPL::DB::ORM;
577     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
578
579
580     FOREACH(pWidgetFeature, widgetConfigurationInfo.featuresList)
581     {
582         wrt::WidgetFeature::Row widgetFeature;
583         widgetFeature.Set_app_id(widgetHandle);
584         widgetFeature.Set_name(pWidgetFeature->name);
585         widgetFeature.Set_required(pWidgetFeature->required);
586         widgetFeature.Set_rejected(false);
587
588         wrt::WidgetFeature::widget_feature_id::ColumnType widgetFeatureID;
589         {
590             WRT_DB_INSERT(insert, wrt::WidgetFeature, &WrtDatabase::interface())
591             insert->Values(widgetFeature);
592             widgetFeatureID = insert->Execute();
593         }
594
595         // Insert into table FeatureParam
596         wrt::FeatureParam::Row featureParam;
597         featureParam.Set_widget_feature_id(widgetFeatureID);
598
599         FOREACH(iter, pWidgetFeature->paramsList)
600         {
601             featureParam.Set_name(iter->name);
602             featureParam.Set_value(iter->value);
603
604             DO_INSERT(featureParam, wrt::FeatureParam)
605         }
606     }
607 }
608
609 void WidgetDAO::registerWidgetPrivilege(DbWidgetHandle widgetHandle,
610                                         const WidgetRegisterInfo &regInfo)
611 {
612     using namespace DPL::DB::ORM;
613     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
614
615     FOREACH(it, widgetConfigurationInfo.privilegeList)
616     {
617         wrt::WidgetPrivilege::Row widgetPrivilege;
618         widgetPrivilege.Set_app_id(widgetHandle);
619         widgetPrivilege.Set_name(it->name);
620
621         DO_INSERT(widgetPrivilege, wrt::WidgetPrivilege)
622     }
623 }
624
625 void WidgetDAO::updateFeatureRejectStatus(const DbWidgetFeature &widgetFeature){
626     // This function could be merged with registerWidgetFeature but it requires desing change:
627     // 1. Check "ace step" in installer must be done before "update database step"
628     // And:
629     // ConfigurationParserData shouldn't be called "ParserData" any more.
630     using namespace DPL::DB::ORM;
631
632     wrt::ScopedTransaction transaction(&WrtDatabase::interface());
633     WRT_DB_SELECT(select, wrt::WidgetFeature, &WrtDatabase::interface())
634     select->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
635                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
636
637     auto row = select->GetSingleRow();
638     row.Set_rejected(widgetFeature.rejected);
639
640     WRT_DB_UPDATE(update, wrt::WidgetFeature, &WrtDatabase::interface())
641     update->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
642                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
643     update->Values(row);
644     update->Execute();
645     transaction.Commit();
646 }
647
648 void WidgetDAO::registerWidgetWindowModes(DbWidgetHandle widgetHandle,
649         const WidgetRegisterInfo &regInfo)
650 {
651     using namespace DPL::DB::ORM;
652     using namespace DPL::DB::ORM::wrt;
653     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
654
655
656     FOREACH(i, widgetConfigurationInfo.windowModes)
657     {
658         wrt::WidgetWindowModes::Row windowMode;
659         windowMode.Set_app_id(widgetHandle);
660         windowMode.Set_window_mode(*i);
661
662         DO_INSERT(windowMode, wrt::WidgetWindowModes)
663     }
664 }
665
666 void WidgetDAO::registerWidgetWarpInfo(DbWidgetHandle widgetHandle,
667         const WidgetRegisterInfo &regInfo)
668 {
669     using namespace DPL::DB::ORM;
670     using namespace DPL::DB::ORM::wrt;
671     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
672
673
674     FOREACH(AccIt, widgetConfigurationInfo.accessInfoSet)
675     {
676         WidgetWARPInfo::Row row;
677         row.Set_app_id(widgetHandle);
678         row.Set_iri(AccIt->m_strIRI);
679         row.Set_subdomain_access(static_cast <int>(
680                                      AccIt->m_bSubDomainAccess));
681
682         DO_INSERT(row, WidgetWARPInfo)
683     }
684 }
685
686 void WidgetDAO::registerWidgetCertificates(DbWidgetHandle widgetHandle,
687         const IWacSecurity &wacSecurity)
688 {
689     using namespace DPL::DB::ORM;
690     using namespace DPL::DB::ORM::wrt;
691
692
693     FOREACH(it, wacSecurity.getCertificateList())
694     {
695         WidgetCertificateFingerprint::Row row;
696         row.Set_app_id(widgetHandle);
697         row.Set_owner(it->owner);
698         row.Set_chainid(it->chainId);
699         row.Set_type(it->type);
700         row.Set_md5_fingerprint(DPL::FromUTF8String(it->strMD5Fingerprint));
701         row.Set_sha1_fingerprint(DPL::FromUTF8String(it->strSHA1Fingerprint));
702         row.Set_common_name(it->strCommonName);
703
704         DO_INSERT(row, WidgetCertificateFingerprint)
705     }
706 }
707
708 void WidgetDAO::registerCertificatesChains(DbWidgetHandle widgetHandle,
709         CertificateSource certificateSource,
710         const CertificateChainList &certificateChainList)
711 {
712     using namespace DPL::DB::ORM;
713     using namespace DPL::DB::ORM::wrt;
714     FOREACH(certChain, certificateChainList)
715     {
716         WidgetCertificate::Row row;
717         row.Set_app_id(widgetHandle);
718         row.Set_cert_source(certificateSource);
719         row.Set_encoded_chain(DPL::FromASCIIString(*certChain));
720
721         DO_INSERT(row, WidgetCertificate);
722     }
723 }
724
725 void WidgetDAO::registerWidgetSettings(DbWidgetHandle widgetHandle,
726         const WidgetRegisterInfo &regInfo)
727 {
728     using namespace DPL::DB::ORM;
729     using namespace DPL::DB::ORM::wrt;
730
731     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
732
733     FOREACH(pWidgetSetting, widgetConfigurationInfo.settingsList)
734     {
735         SettingsList::Row row;
736         row.Set_appId(widgetHandle);
737         row.Set_settingName(pWidgetSetting->m_name);
738         row.Set_settingValue(pWidgetSetting->m_value);
739
740         DO_INSERT(row, SettingsList)
741     }
742 }
743
744 void WidgetDAO::registerAppService(DbWidgetHandle widgetHandle,
745                                    const WidgetRegisterInfo &regInfo)
746 {
747     using namespace DPL::DB::ORM;
748     using namespace DPL::DB::ORM::wrt;
749     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
750
751     FOREACH(ASIt, widgetConfigurationInfo.appServiceList)
752     {
753         ApplicationServiceInfo::Row row;
754         row.Set_app_id(widgetHandle);
755         row.Set_src(ASIt->m_src);
756         row.Set_operation(ASIt->m_operation);
757         row.Set_scheme(ASIt->m_scheme);
758         row.Set_mime(ASIt->m_mime);
759
760         DO_INSERT(row, ApplicationServiceInfo)
761     }
762 }
763
764 void WidgetDAO::registerEncryptedResouceInfo(DbWidgetHandle widgetHandle,
765         const WidgetRegisterInfo &regInfo)
766 {
767     using namespace DPL::DB::ORM;
768     using namespace DPL::DB::ORM::wrt;
769
770     FOREACH(it, regInfo.encryptedFiles)
771     {
772         EncryptedResourceList::Row row;
773         row.Set_app_id(widgetHandle);
774         row.Set_resource(it->fileName);
775         row.Set_size(it->fileSize);
776
777         DO_INSERT(row, EncryptedResourceList)
778     }
779 }
780
781 void WidgetDAO::registerExternalLocations(DbWidgetHandle widgetHandle,
782                                           const ExternalLocationList & externals)
783 {
784     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
785     {
786         using namespace DPL::DB::ORM;
787         using namespace DPL::DB::ORM::wrt;
788         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
789         LogDebug("Inserting external files for widgetHandle: " << widgetHandle);
790         FOREACH(it, externals)
791         {
792             WidgetExternalLocations::Row row;
793             row.Set_app_id(widgetHandle);
794             row.Set_path(DPL::FromUTF8String(*it));
795
796             DO_INSERT(row, WidgetExternalLocations)
797         }
798         transaction.Commit();
799     }
800     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register external files");
801 }
802
803 void WidgetDAO::registerWidgetSecuritySettings(DbWidgetHandle widgetHandle)
804 {
805     using namespace DPL::DB::ORM;
806     using namespace DPL::DB::ORM::wrt;
807     WidgetSecuritySettings::Row row;
808     row.Set_app_id(widgetHandle);
809     row.Set_security_popup_usage(SETTINGS_TYPE_ON);
810     row.Set_geolocation_usage(SETTINGS_TYPE_ON);
811     row.Set_web_notification_usage(SETTINGS_TYPE_ON);
812     row.Set_web_database_usage(SETTINGS_TYPE_ON);
813     row.Set_file_system_usage(SETTINGS_TYPE_ON);
814
815     DO_INSERT(row, WidgetSecuritySettings)
816 }
817
818 void WidgetDAO::unregisterAllExternalLocations()
819 {
820     using namespace DPL::DB::ORM;
821     using namespace DPL::DB::ORM::wrt;
822     LogDebug("Deleting external files for widgetHandle: " << m_widgetHandle);
823     WRT_DB_DELETE(del, WidgetExternalLocations, &WrtDatabase::interface());
824     del->Where(Equals<WidgetExternalLocations::app_id>(m_widgetHandle));
825     del->Execute();
826 }
827
828 void WidgetDAO::unregisterWidget(const TizenAppId & tzAppId)
829 {
830     LogDebug("Unregistering widget from DB. tzAppId: " << tzAppId);
831     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
832     {
833         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
834         unregisterWidgetInternal(tzAppId);
835         transaction.Commit();
836     }
837     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
838 }
839
840 void WidgetDAO::unregisterWidget(WrtDB::DbWidgetHandle handle)
841 {
842     LogDebug("Unregistering widget from DB. Handle: " << handle);
843     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
844     {
845         using namespace DPL::DB::ORM;
846         using namespace DPL::DB::ORM::wrt;
847         ScopedTransaction transaction(&WrtDatabase::interface());
848
849         // Delete from table Widget Info
850         WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
851         del->Where(Equals<WidgetInfo::app_id>(handle));
852         del->Execute();
853
854         transaction.Commit();
855     }
856     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
857 }
858
859 void WidgetDAO::unregisterWidgetInternal(
860         const TizenAppId & tzAppId)
861 {
862     using namespace DPL::DB::ORM;
863     using namespace DPL::DB::ORM::wrt;
864
865     DbWidgetHandle handle = getHandle(tzAppId);
866
867     // Delete from table Widget Info
868     WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
869     del->Where(Equals<WidgetInfo::app_id>(handle));
870     del->Execute();
871
872     // Deleting in other tables is done via "delete cascade" in SQL
873 }
874
875 #undef DO_INSERT
876
877 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
878 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
879
880 } // namespace WrtDB