WrtDAO clean up - unused function with widget handle usage
[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 pkgName) :
60     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(pkgName))
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::setPkgName(const DPL::OptionalString& pkgName)
100 {
101     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
102     {
103         using namespace DPL::DB::ORM;
104         wrt::ScopedTransaction transaction(&WrtDatabase::interface());
105
106         isWidgetInstalled(getHandle());
107
108         wrt::WidgetInfo::Row row;
109         row.Set_pkgname(pkgName);
110
111         WRT_DB_UPDATE(update, wrt::WidgetInfo, &WrtDatabase::interface())
112         update->Where(
113             Equals<wrt::WidgetInfo::app_id>(getHandle()));
114
115         update->Values(row);
116         update->Execute();
117         transaction.Commit();
118     }
119     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
120 }
121
122 void WidgetDAO::registerWidget(
123         const WidgetPkgName & widgetPkgname,
124         const WidgetRegisterInfo &widgetRegInfo,
125         const IWacSecurity &wacSecurity)
126 {
127     LogDebug("Registering widget");
128     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
129     {
130         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
131         registerWidgetInternal(widgetPkgname, widgetRegInfo, wacSecurity);
132         transaction.Commit();
133     }
134     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
135 }
136
137 void WidgetDAO::registerWidget(
138         WrtDB::DbWidgetHandle handle,
139         const WidgetRegisterInfo &widgetRegInfo,
140         const IWacSecurity &wacSecurity)
141 {
142     LogDebug("Registering widget");
143     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
144     {
145         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
146         registerWidgetInternal(generateTizenId(), widgetRegInfo, wacSecurity, handle);
147         transaction.Commit();
148     }
149     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
150 }
151
152 DbWidgetHandle WidgetDAO::registerWidget(
153             const WidgetRegisterInfo &pWidgetRegisterInfo,
154             const IWacSecurity &wacSecurity)
155 {
156     //make it more precise due to very fast tests
157     struct timeval tv;
158     gettimeofday(&tv, NULL);
159     srand(time(NULL) + tv.tv_usec);
160     DbWidgetHandle widgetHandle;
161     do {
162         widgetHandle = rand();
163     } while (isWidgetInstalled(widgetHandle));
164
165     registerWidget(widgetHandle, pWidgetRegisterInfo, wacSecurity);
166     return widgetHandle;
167 }
168
169 WidgetPkgName WidgetDAO::registerWidgetGenerateTizenId(
170             const WidgetRegisterInfo &pWidgetRegisterInfo,
171             const IWacSecurity &wacSecurity)
172 {
173     WidgetPkgName widgetPkgName = generateTizenId();
174     registerWidget(widgetPkgName, pWidgetRegisterInfo, wacSecurity);
175     return widgetPkgName;
176 }
177
178 void WidgetDAO::registerWidgetInternal(
179         const WidgetPkgName & widgetName,
180         const WidgetRegisterInfo &widgetRegInfo,
181         const IWacSecurity &wacSecurity,
182         const DPL::Optional<DbWidgetHandle> handle)
183 {
184     //Register into WidgetInfo has to be first
185     //as all other tables depend upon that
186     DbWidgetHandle widgetHandle = registerWidgetInfo(widgetName, widgetRegInfo, wacSecurity, handle);
187
188     registerWidgetExtendedInfo(widgetHandle, widgetRegInfo);
189
190     registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo);
191
192     registerWidgetIcons(widgetHandle, widgetRegInfo);
193
194     registerWidgetStartFile(widgetHandle, widgetRegInfo);
195
196     PropertyDAO::RegisterProperties(widgetHandle, widgetRegInfo);
197
198     registerWidgetFeatures(widgetHandle, widgetRegInfo);
199
200     registerWidgetWindowModes(widgetHandle, widgetRegInfo);
201
202     registerWidgetWarpInfo(widgetHandle, widgetRegInfo);
203
204     registerWidgetCertificates(widgetHandle, wacSecurity);
205
206     CertificateChainList list;
207     wacSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR);
208     registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR, list);
209
210     list.clear();
211     wacSecurity.getCertificateChainList(list, SIGNATURE_AUTHOR);
212     registerCertificatesChains(widgetHandle, SIGNATURE_AUTHOR, list);
213
214     registerWidgetSettings(widgetHandle, widgetRegInfo);
215
216     registerAppService(widgetHandle, widgetRegInfo);
217
218     registerEncryptedResouceInfo(widgetHandle, widgetRegInfo);
219
220     registerExternalLocations(widgetHandle, widgetRegInfo.externalLocations);
221 }
222
223 void WidgetDAO::registerOrUpdateWidget(
224         const WidgetPkgName & widgetName,
225         const WidgetRegisterInfo &widgetRegInfo,
226         const IWacSecurity &wacSecurity)
227 {
228     LogDebug("Reregistering widget");
229     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
230     {
231         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
232
233         unregisterWidgetInternal(widgetName);
234         registerWidgetInternal(widgetName, widgetRegInfo, wacSecurity);
235         transaction.Commit();
236     }
237     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to reregister widget")
238 }
239
240 #define DO_INSERT(row, table)                              \
241     {                                                      \
242         WRT_DB_INSERT(insert, table, &WrtDatabase::interface()) \
243         insert->Values(row);                               \
244         insert->Execute();                                 \
245     }
246
247 void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle,
248         const WidgetRegisterInfo &regInfo)
249 {
250     //Try and transaction not needed
251     using namespace DPL::DB::ORM;
252     using namespace DPL::DB::ORM::wrt;
253
254
255     WidgetExtendedInfo::Row row;
256     row.Set_app_id(widgetHandle);
257     //    row.Set_share_href    (DPL::FromUTF8String(regInfo.shareHref));
258     row.Set_signature_type(regInfo.signatureType);
259     row.Set_test_widget(regInfo.isTestWidget);
260     row.Set_install_time(regInfo.installedTime);
261     row.Set_splash_img_src(regInfo.configInfo.splashImgSrc);
262     row.Set_background_page(regInfo.configInfo.backgroundPage);
263
264
265     DO_INSERT(row, WidgetExtendedInfo)
266 }
267
268 DbWidgetHandle WidgetDAO::registerWidgetInfo(
269         const WidgetPkgName & widgetName,
270         const WidgetRegisterInfo &regInfo,
271         const IWacSecurity &wacSecurity,
272         const DPL::Optional<DbWidgetHandle> handle)
273 {
274     using namespace DPL::DB::ORM;
275     using namespace DPL::DB::ORM::wrt;
276     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
277
278     // TODO in wrt_db all Columns in WidgetInfo have DEFAULT VALUE set.
279     // Because of that, "Optional" is not used there
280
281     WidgetInfo::Row row;
282     if(!!handle)
283     {
284         row.Set_app_id(*handle);
285     }
286
287     if (regInfo.webAppType == APP_TYPE_UNKNOWN && regInfo.type !=
288             APP_TYPE_UNKNOWN) {
289         // TODO : regInfo.type is temporary code for security.
290         //        This code will be removed.
291         row.Set_widget_type(regInfo.type.appType);
292     } else {
293         row.Set_widget_type(regInfo.webAppType.appType);
294     }
295     row.Set_widget_id(widgetConfigurationInfo.widget_id);
296     row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale);
297     row.Set_widget_version(widgetConfigurationInfo.version);
298     row.Set_widget_width(widgetConfigurationInfo.width);
299     row.Set_widget_height(widgetConfigurationInfo.height);
300     row.Set_author_name(widgetConfigurationInfo.authorName);
301     row.Set_author_email(widgetConfigurationInfo.authorEmail);
302     row.Set_author_href(widgetConfigurationInfo.authorHref);
303     row.Set_base_folder(DPL::FromUTF8String(regInfo.baseFolder));
304     row.Set_webkit_plugins_required(widgetConfigurationInfo.flashNeeded);
305     row.Set_recognized(wacSecurity.isRecognized());
306     row.Set_wac_signed(wacSecurity.isWacSigned());
307     row.Set_distributor_signed(wacSecurity.isDistributorSigned());
308     row.Set_pkgname(widgetName);
309     {
310         std::stringstream tmp;
311         tmp << regInfo.minVersion;
312         row.Set_min_version(DPL::FromUTF8String(tmp.str()));
313     }
314     row.Set_back_supported(widgetConfigurationInfo.backSupported);
315     row.Set_access_network(widgetConfigurationInfo.accessNetwork);
316     row.Set_pkg_type(regInfo.packagingType.pkgType);
317
318     Try
319     {
320         DO_INSERT(row, WidgetInfo);
321     }
322     Catch(DPL::DB::SqlConnection::Exception::Base)
323     {
324         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
325                    "Failed to register widget info.");
326     }
327
328     if(!handle)
329     {
330         //get autoincremented value of widgetHandle
331         WRT_DB_SELECT(select, WidgetInfo, &WrtDatabase::interface())
332         select->Where(Equals<WidgetInfo::pkgname>(widgetName));
333         return select->GetSingleValue<WidgetInfo::app_id>();
334     }
335     else
336     {
337         return *handle;
338     }
339 }
340
341 void WidgetDAO::registerWidgetLocalizedInfo(DbWidgetHandle widgetHandle,
342         const WidgetRegisterInfo &regInfo)
343 {
344     using namespace DPL::DB::ORM;
345     using namespace DPL::DB::ORM::wrt;
346
347     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
348
349     FOREACH(it, widgetConfigurationInfo.localizedDataSet)
350     {
351         const DPL::String& locale = it->first;
352         const ConfigParserData::LocalizedData& data = it->second;
353
354         LocalizedWidgetInfo::Row row;
355         row.Set_app_id(widgetHandle);
356         row.Set_widget_locale(locale);
357         row.Set_widget_name(data.name);
358         row.Set_widget_shortname(data.shortName);
359         row.Set_widget_description(data.description);
360         row.Set_widget_license(data.license);
361         row.Set_widget_license_file(data.licenseFile);
362         row.Set_widget_license_href(data.licenseHref);
363
364         DO_INSERT(row, LocalizedWidgetInfo)
365     }
366 }
367
368 void WidgetDAO::registerWidgetIcons(DbWidgetHandle widgetHandle,
369         const WidgetRegisterInfo &regInfo)
370 {
371     using namespace DPL::DB::ORM;
372     using namespace DPL::DB::ORM::wrt;
373
374
375     FOREACH(i, regInfo.localizationData.icons)
376     {
377         wrt::WidgetIcon::icon_id::ColumnType icon_id;
378         {
379             wrt::WidgetIcon::Row row;
380             row.Set_app_id(widgetHandle);
381             row.Set_icon_src(i->src);
382             row.Set_icon_width(i->width);
383             row.Set_icon_height(i->height);
384
385             WRT_DB_INSERT(insert, wrt::WidgetIcon, &WrtDatabase::interface())
386             insert->Values(row);
387             icon_id = insert->Execute();
388         }
389
390         FOREACH(j, i->availableLocales)
391         {
392             WidgetLocalizedIcon::Row row;
393             row.Set_app_id(widgetHandle);
394             row.Set_icon_id(icon_id);
395             row.Set_widget_locale(*j);
396             WRT_DB_SELECT(select, WidgetLocalizedIcon, &WrtDatabase::interface())
397             select->Where(And(Equals<WidgetLocalizedIcon::app_id>(widgetHandle),
398                               Equals<WidgetLocalizedIcon::widget_locale>(*j)));
399             WidgetLocalizedIcon::Select::RowList rows = select->GetRowList();
400
401             bool flag = !rows.empty();
402
403             if(flag == true)
404             {
405                 // already default icon value of same locale exists
406                 WRT_DB_UPDATE(update, WidgetLocalizedIcon, &WrtDatabase::interface())
407                 update->Where(And(Equals<WidgetLocalizedIcon::app_id>(widgetHandle),
408                                   Equals<WidgetLocalizedIcon::widget_locale>(*j)));
409                 update->Values(row);
410                 update->Execute();
411             }else{
412                 // any icon value of same locale doesn't exist
413                 DO_INSERT(row, WidgetLocalizedIcon)
414             }
415         }
416     }
417 }
418
419 void WidgetDAO::registerWidgetStartFile(DbWidgetHandle widgetHandle,
420         const WidgetRegisterInfo &regInfo)
421 {
422     using namespace DPL::DB::ORM;
423     using namespace DPL::DB::ORM::wrt;
424
425
426     FOREACH(i, regInfo.localizationData.startFiles)
427     {
428         WidgetStartFile::start_file_id::ColumnType startFileID;
429         {
430             WidgetStartFile::Row row;
431             row.Set_app_id(widgetHandle);
432             row.Set_src(i->path);
433
434             WRT_DB_INSERT(insert, WidgetStartFile, &WrtDatabase::interface())
435             insert->Values(row);
436             startFileID = insert->Execute();
437         }
438
439         FOREACH(j, i->propertiesForLocales)
440         {
441             WidgetLocalizedStartFile::Row row;
442             row.Set_app_id(widgetHandle);
443             row.Set_start_file_id(startFileID);
444             row.Set_widget_locale(j->first);
445             row.Set_type(j->second.type);
446             row.Set_encoding(j->second.encoding);
447
448             DO_INSERT(row, WidgetLocalizedStartFile)
449         }
450     }
451 }
452
453 void WidgetDAO::registerWidgetFeatures(DbWidgetHandle widgetHandle,
454         const WidgetRegisterInfo &regInfo)
455 {
456     using namespace DPL::DB::ORM;
457     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
458
459
460     FOREACH(pWidgetFeature, widgetConfigurationInfo.featuresList)
461     {
462         wrt::WidgetFeature::Row widgetFeature;
463         widgetFeature.Set_app_id(widgetHandle);
464         widgetFeature.Set_name(pWidgetFeature->name);
465         widgetFeature.Set_required(pWidgetFeature->required);
466         widgetFeature.Set_rejected(false);
467
468         wrt::WidgetFeature::widget_feature_id::ColumnType widgetFeatureID;
469         {
470             WRT_DB_INSERT(insert, wrt::WidgetFeature, &WrtDatabase::interface())
471             insert->Values(widgetFeature);
472             widgetFeatureID = insert->Execute();
473         }
474
475         // Insert into table FeatureParam
476         wrt::FeatureParam::Row featureParam;
477         featureParam.Set_widget_feature_id(widgetFeatureID);
478
479         FOREACH(iter, pWidgetFeature->paramsList)
480         {
481             featureParam.Set_name(iter->name);
482             featureParam.Set_value(iter->value);
483
484             DO_INSERT(featureParam, wrt::FeatureParam)
485         }
486     }
487 }
488
489 void WidgetDAO::updateFeatureRejectStatus(const DbWidgetFeature &widgetFeature){
490     // This function could be merged with registerWidgetFeature but it requires desing change:
491     // 1. Check "ace step" in installer must be done before "update database step"
492     // And:
493     // ConfigurationParserData shouldn't be called "ParserData" any more.
494     using namespace DPL::DB::ORM;
495
496     wrt::ScopedTransaction transaction(&WrtDatabase::interface());
497     WRT_DB_SELECT(select, wrt::WidgetFeature, &WrtDatabase::interface())
498     select->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
499                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
500
501     auto row = select->GetSingleRow();
502     row.Set_rejected(widgetFeature.rejected);
503
504     WRT_DB_UPDATE(update, wrt::WidgetFeature, &WrtDatabase::interface())
505     update->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
506                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
507     update->Values(row);
508     update->Execute();
509     transaction.Commit();
510 }
511
512 void WidgetDAO::registerWidgetWindowModes(DbWidgetHandle widgetHandle,
513         const WidgetRegisterInfo &regInfo)
514 {
515     using namespace DPL::DB::ORM;
516     using namespace DPL::DB::ORM::wrt;
517     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
518
519
520     FOREACH(i, widgetConfigurationInfo.windowModes)
521     {
522         wrt::WidgetWindowModes::Row windowMode;
523         windowMode.Set_app_id(widgetHandle);
524         windowMode.Set_window_mode(*i);
525
526         DO_INSERT(windowMode, wrt::WidgetWindowModes)
527     }
528 }
529
530 void WidgetDAO::registerWidgetWarpInfo(DbWidgetHandle widgetHandle,
531         const WidgetRegisterInfo &regInfo)
532 {
533     using namespace DPL::DB::ORM;
534     using namespace DPL::DB::ORM::wrt;
535     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
536
537
538     FOREACH(AccIt, widgetConfigurationInfo.accessInfoSet)
539     {
540         WidgetWARPInfo::Row row;
541         row.Set_app_id(widgetHandle);
542         row.Set_iri(AccIt->m_strIRI);
543         row.Set_subdomain_access(static_cast <int>(
544                                      AccIt->m_bSubDomainAccess));
545
546         DO_INSERT(row, WidgetWARPInfo)
547     }
548 }
549
550 void WidgetDAO::registerWidgetCertificates(DbWidgetHandle widgetHandle,
551         const IWacSecurity &wacSecurity)
552 {
553     using namespace DPL::DB::ORM;
554     using namespace DPL::DB::ORM::wrt;
555
556
557     FOREACH(it, wacSecurity.getCertificateList())
558     {
559         WidgetCertificateFingerprint::Row row;
560         row.Set_app_id(widgetHandle);
561         row.Set_owner(it->owner);
562         row.Set_chainid(it->chainId);
563         row.Set_type(it->type);
564         row.Set_md5_fingerprint(DPL::FromUTF8String(it->strMD5Fingerprint));
565         row.Set_sha1_fingerprint(DPL::FromUTF8String(it->strSHA1Fingerprint));
566         row.Set_common_name(it->strCommonName);
567
568         DO_INSERT(row, WidgetCertificateFingerprint)
569     }
570 }
571
572 void WidgetDAO::registerCertificatesChains(DbWidgetHandle widgetHandle,
573         CertificateSource certificateSource,
574         const CertificateChainList &certificateChainList)
575 {
576     using namespace DPL::DB::ORM;
577     using namespace DPL::DB::ORM::wrt;
578     FOREACH(certChain, certificateChainList)
579     {
580         WidgetCertificate::Row row;
581         row.Set_app_id(widgetHandle);
582         row.Set_cert_source(certificateSource);
583         row.Set_encoded_chain(DPL::FromASCIIString(*certChain));
584
585         DO_INSERT(row, WidgetCertificate);
586     }
587 }
588
589 void WidgetDAO::registerWidgetSettings(DbWidgetHandle widgetHandle,
590         const WidgetRegisterInfo &regInfo)
591 {
592     using namespace DPL::DB::ORM;
593     using namespace DPL::DB::ORM::wrt;
594
595     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
596
597     FOREACH(pWidgetSetting, widgetConfigurationInfo.settingsList)
598     {
599         SettingsList::Row row;
600         row.Set_appId(widgetHandle);
601         row.Set_settingName(pWidgetSetting->m_name);
602         row.Set_settingValue(pWidgetSetting->m_value);
603
604         DO_INSERT(row, SettingsList)
605     }
606 }
607
608 void WidgetDAO::registerAppService(DbWidgetHandle widgetHandle,
609                                    const WidgetRegisterInfo &regInfo)
610 {
611     using namespace DPL::DB::ORM;
612     using namespace DPL::DB::ORM::wrt;
613     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
614
615     FOREACH(ASIt, widgetConfigurationInfo.appServiceList)
616     {
617         ApplicationServiceInfo::Row row;
618         row.Set_app_id(widgetHandle);
619         row.Set_src(ASIt->m_src);
620         row.Set_operation(ASIt->m_operation);
621         row.Set_scheme(ASIt->m_scheme);
622         row.Set_mime(ASIt->m_mime);
623
624         DO_INSERT(row, ApplicationServiceInfo)
625     }
626 }
627
628 void WidgetDAO::registerEncryptedResouceInfo(DbWidgetHandle widgetHandle,
629         const WidgetRegisterInfo &regInfo)
630 {
631     using namespace DPL::DB::ORM;
632     using namespace DPL::DB::ORM::wrt;
633
634     FOREACH(it, regInfo.encryptedFiles)
635     {
636         EncryptedResourceList::Row row;
637         row.Set_app_id(widgetHandle);
638         row.Set_resource(it->fileName);
639         row.Set_size(it->fileSize);
640
641         DO_INSERT(row, EncryptedResourceList)
642     }
643 }
644
645 void WidgetDAO::registerExternalLocations(DbWidgetHandle widgetHandle,
646                                           const ExternalLocationList & externals)
647 {
648     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
649     {
650         using namespace DPL::DB::ORM;
651         using namespace DPL::DB::ORM::wrt;
652         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
653         LogDebug("Inserting external files for widgetHandle: " << widgetHandle);
654         FOREACH(it, externals)
655         {
656             WidgetExternalLocations::Row row;
657             row.Set_app_id(widgetHandle);
658             row.Set_path(DPL::FromUTF8String(*it));
659
660             DO_INSERT(row, WidgetExternalLocations)
661         }
662         transaction.Commit();
663     }
664     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register external files");
665 }
666
667 void WidgetDAO::unregisterAllExternalLocations()
668 {
669     using namespace DPL::DB::ORM;
670     using namespace DPL::DB::ORM::wrt;
671     LogDebug("Deleting external files for widgetHandle: " << m_widgetHandle);
672     WRT_DB_DELETE(del, WidgetExternalLocations, &WrtDatabase::interface());
673     del->Where(Equals<WidgetExternalLocations::app_id>(m_widgetHandle));
674     del->Execute();
675 }
676
677 void WidgetDAO::unregisterWidget(const WidgetPkgName & pkgName)
678 {
679     LogDebug("Unregistering widget from DB. PkgName: " << pkgName);
680     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
681     {
682         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
683         unregisterWidgetInternal(pkgName);
684         transaction.Commit();
685     }
686     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
687 }
688
689 void WidgetDAO::unregisterWidget(WrtDB::DbWidgetHandle handle)
690 {
691     LogDebug("Unregistering widget from DB. Handle: " << handle);
692     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
693     {
694         using namespace DPL::DB::ORM;
695         using namespace DPL::DB::ORM::wrt;
696         ScopedTransaction transaction(&WrtDatabase::interface());
697
698         // Delete from table Widget Info
699         WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
700         del->Where(Equals<WidgetInfo::app_id>(handle));
701         del->Execute();
702
703         transaction.Commit();
704     }
705     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
706 }
707
708 void WidgetDAO::unregisterWidgetInternal(
709         const WidgetPkgName & pkgName)
710 {
711     using namespace DPL::DB::ORM;
712     using namespace DPL::DB::ORM::wrt;
713
714     DbWidgetHandle handle = getHandle(pkgName);
715
716     // Delete from table Widget Info
717     WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
718     del->Where(Equals<WidgetInfo::app_id>(handle));
719     del->Execute();
720
721     // Deleting in other tables is done via "delete cascade" in SQL
722 }
723
724 #undef DO_INSERT
725
726 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
727 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
728
729 } // namespace WrtDB