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