tizen beta release
[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 DbWidgetHandle WidgetDAO::registerWidget(const WidgetRegisterInfo &widgetRegInfo,
128                                        const IWacSecurity &wacSecurity,
129                                        const LanguageTagsList& languageTags)
130 {
131     LogDebug("Registering widget");
132     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
133     {
134         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
135
136         //Register into WidgetInfo has to be first
137         //as all other tables depend upon that
138         DbWidgetHandle widgetHandle =
139                 registerWidgetInfo(widgetRegInfo, wacSecurity);
140
141         registerWidgetExtendedInfo(widgetHandle, widgetRegInfo);
142
143         registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo);
144
145         registerWidgetUserAgentLocales(
146                 widgetHandle, widgetRegInfo, languageTags);
147
148         registerWidgetIcons(widgetHandle, widgetRegInfo);
149
150         registerWidgetStartFile(widgetHandle, widgetRegInfo);
151
152         PropertyDAO::RegisterProperties(widgetHandle, widgetRegInfo);
153
154         registerWidgetFeatures(widgetHandle, widgetRegInfo);
155
156         registerWidgetWindowModes(widgetHandle, widgetRegInfo);
157
158         registerWidgetWarpInfo(widgetHandle, widgetRegInfo);
159
160         registerWidgetCertificates(widgetHandle, wacSecurity);
161
162         CertificateChainList list;
163         wacSecurity.getCertificateChainList(list);
164         registerLaunchCertificates(widgetHandle,list);
165
166         registerWidgetPowderData(widgetHandle, widgetRegInfo);
167
168         registerWidgetSettings(widgetHandle, widgetRegInfo);
169
170         registerAppService(widgetHandle, widgetRegInfo);
171
172         transaction.Commit();
173
174         return widgetHandle;
175     }
176     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
177 }
178
179 #define DO_INSERT(row, table)                              \
180     {                                                      \
181         WRT_DB_INSERT(insert, table, &WrtDatabase::interface()) \
182         insert->Values(row);                               \
183         insert->Execute();                                 \
184     }
185
186 void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle,
187         const WidgetRegisterInfo &regInfo)
188 {
189     //Try and transaction not needed
190     using namespace DPL::DB::ORM;
191     using namespace DPL::DB::ORM::wrt;
192
193
194     WidgetExtendedInfo::Row row;
195     row.Set_app_id(widgetHandle);
196     //    row.Set_share_href    (DPL::FromUTF8String(regInfo.shareHref));
197     row.Set_signature_type(regInfo.signatureType);
198     row.Set_factory_widget(regInfo.isFactoryWidget);
199     row.Set_test_widget(regInfo.isTestWidget);
200     row.Set_install_time(regInfo.installedTime);
201
202     DO_INSERT(row, WidgetExtendedInfo)
203 }
204
205 DbWidgetHandle WidgetDAO::registerWidgetInfo(const WidgetRegisterInfo &regInfo,
206                                    const IWacSecurity &wacSecurity)
207 {
208     using namespace DPL::DB::ORM;
209     using namespace DPL::DB::ORM::wrt;
210     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
211
212     // TODO in wrt_db all Columns in WidgetInfo have DEFAULT VALUE set.
213     // Because of that, "Optional" is not used there
214
215     WidgetInfo::Row row;
216     row.Set_widget_type(regInfo.type.appType);
217     row.Set_widget_id(widgetConfigurationInfo.widget_id);
218     row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale);
219     row.Set_widget_version(widgetConfigurationInfo.version);
220     row.Set_widget_width(widgetConfigurationInfo.width);
221     row.Set_widget_height(widgetConfigurationInfo.height);
222     row.Set_author_name(widgetConfigurationInfo.authorName);
223     row.Set_author_email(widgetConfigurationInfo.authorEmail);
224     row.Set_author_href(widgetConfigurationInfo.authorHref);
225     row.Set_base_folder(DPL::FromUTF8String(regInfo.baseFolder));
226     row.Set_webkit_plugins_required(widgetConfigurationInfo.flashNeeded);
227     row.Set_child_protection(1);
228     row.Set_recognized(wacSecurity.isRecognized());
229     row.Set_wac_signed(wacSecurity.isWacSigned());
230     row.Set_distributor_signed(wacSecurity.isDistributorSigned());
231     {
232         std::stringstream tmp;
233         tmp << widgetConfigurationInfo.minVersionRequired;
234         row.Set_min_version(DPL::FromUTF8String(tmp.str()));
235     }
236     row.Set_back_supported(widgetConfigurationInfo.backSupported);
237     row.Set_access_network(widgetConfigurationInfo.accessNetwork);
238     row.Set_pkgname(regInfo.pkgname);
239
240     wrt::WidgetInfo::app_id::ColumnType appID;
241     {
242         WRT_DB_INSERT(insert, WidgetInfo, &WrtDatabase::interface())
243         insert->Values(row);
244         appID = insert->Execute();
245     }
246     return appID;
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::registerWidgetUserAgentLocales(
277         DbWidgetHandle widgetHandle,
278         const WidgetRegisterInfo &/*regInfo*/,
279         const LanguageTagsList& languageTags)
280 {
281     using namespace DPL::DB::ORM;
282     using namespace DPL::DB::ORM::wrt;
283
284
285     FOREACH(i, languageTags)
286     {
287         wrt::WidgetUserAgentLocales::Row row;
288         row.Set_app_id(widgetHandle);
289         row.Set_language_tag(*i);
290
291         DO_INSERT(row, wrt::WidgetUserAgentLocales)
292     }
293 }
294
295 void WidgetDAO::registerWidgetIcons(DbWidgetHandle widgetHandle,
296         const WidgetRegisterInfo &regInfo)
297 {
298     using namespace DPL::DB::ORM;
299     using namespace DPL::DB::ORM::wrt;
300
301
302     FOREACH(i, regInfo.localizationData.icons)
303     {
304         wrt::WidgetIcon::icon_id::ColumnType icon_id;
305         {
306             wrt::WidgetIcon::Row row;
307             row.Set_app_id(widgetHandle);
308             row.Set_icon_src(i->src);
309             row.Set_icon_width(i->width);
310             row.Set_icon_height(i->height);
311
312             WRT_DB_INSERT(insert, wrt::WidgetIcon, &WrtDatabase::interface())
313             insert->Values(row);
314             icon_id = insert->Execute();
315         }
316
317         FOREACH(j, i->availableLocales)
318         {
319             WidgetLocalizedIcon::Row row;
320             row.Set_app_id(widgetHandle);
321             row.Set_icon_id(icon_id);
322             row.Set_widget_locale(*j);
323             WRT_DB_SELECT(select, WidgetLocalizedIcon, &WrtDatabase::interface())
324             select->Where(And(Equals<WidgetLocalizedIcon::app_id>(widgetHandle),
325                               Equals<WidgetLocalizedIcon::widget_locale>(*j)));
326             WidgetLocalizedIcon::Select::RowList rows = select->GetRowList();
327
328             bool flag = !rows.empty();
329
330             if(flag == true)
331             {
332                 // already default icon value of same locale exists
333                 WRT_DB_UPDATE(update, WidgetLocalizedIcon, &WrtDatabase::interface())
334                 update->Where(And(Equals<WidgetLocalizedIcon::app_id>(widgetHandle),
335                                   Equals<WidgetLocalizedIcon::widget_locale>(*j)));
336                 update->Values(row);
337                 update->Execute();
338             }else{
339                 // any icon value of same locale doesn't exist
340                 DO_INSERT(row, WidgetLocalizedIcon)
341             }
342         }
343     }
344 }
345
346 void WidgetDAO::registerWidgetStartFile(DbWidgetHandle widgetHandle,
347         const WidgetRegisterInfo &regInfo)
348 {
349     using namespace DPL::DB::ORM;
350     using namespace DPL::DB::ORM::wrt;
351
352
353     FOREACH(i, regInfo.localizationData.startFiles)
354     {
355         WidgetStartFile::start_file_id::ColumnType startFileID;
356         {
357             WidgetStartFile::Row row;
358             row.Set_app_id(widgetHandle);
359             row.Set_src(i->path);
360
361             WRT_DB_INSERT(insert, WidgetStartFile, &WrtDatabase::interface())
362             insert->Values(row);
363             startFileID = insert->Execute();
364         }
365
366         FOREACH(j, i->propertiesForLocales)
367         {
368             WidgetLocalizedStartFile::Row row;
369             row.Set_app_id(widgetHandle);
370             row.Set_start_file_id(startFileID);
371             row.Set_widget_locale(j->first);
372             row.Set_type(j->second.type);
373             row.Set_encoding(j->second.encoding);
374
375             DO_INSERT(row, WidgetLocalizedStartFile)
376         }
377     }
378 }
379
380 void WidgetDAO::registerWidgetFeatures(DbWidgetHandle widgetHandle,
381         const WidgetRegisterInfo &regInfo)
382 {
383     using namespace DPL::DB::ORM;
384     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
385
386
387     FOREACH(pWidgetFeature, widgetConfigurationInfo.featuresList)
388     {
389         wrt::WidgetFeature::Row widgetFeature;
390         widgetFeature.Set_app_id(widgetHandle);
391         widgetFeature.Set_name(pWidgetFeature->name);
392         widgetFeature.Set_required(pWidgetFeature->required);
393
394         wrt::WidgetFeature::widget_feature_id::ColumnType widgetFeatureID;
395         {
396             WRT_DB_INSERT(insert, wrt::WidgetFeature, &WrtDatabase::interface())
397             insert->Values(widgetFeature);
398             widgetFeatureID = insert->Execute();
399         }
400
401         // Insert into table FeatureParam
402         wrt::FeatureParam::Row featureParam;
403         featureParam.Set_widget_feature_id(widgetFeatureID);
404
405         ConfigParserData::ParamsList::const_iterator iter;
406
407         FOREACH(iter, pWidgetFeature->paramsList)
408         {
409             featureParam.Set_name(iter->name);
410             featureParam.Set_value(iter->value);
411
412             DO_INSERT(featureParam, wrt::FeatureParam)
413         }
414     }
415 }
416
417 void WidgetDAO::registerWidgetWindowModes(DbWidgetHandle widgetHandle,
418         const WidgetRegisterInfo &regInfo)
419 {
420     using namespace DPL::DB::ORM;
421     using namespace DPL::DB::ORM::wrt;
422     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
423
424
425     FOREACH(i, widgetConfigurationInfo.windowModes)
426     {
427         wrt::WidgetWindowModes::Row windowMode;
428         windowMode.Set_app_id(widgetHandle);
429         windowMode.Set_window_mode(*i);
430
431         DO_INSERT(windowMode, wrt::WidgetWindowModes)
432     }
433 }
434
435 void WidgetDAO::registerWidgetWarpInfo(DbWidgetHandle widgetHandle,
436         const WidgetRegisterInfo &regInfo)
437 {
438     using namespace DPL::DB::ORM;
439     using namespace DPL::DB::ORM::wrt;
440     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
441
442
443     FOREACH(AccIt, widgetConfigurationInfo.accessInfoSet)
444     {
445         WidgetWARPInfo::Row row;
446         row.Set_app_id(widgetHandle);
447         row.Set_iri(AccIt->m_strIRI);
448         row.Set_subdomain_access(static_cast <int>(
449                                      AccIt->m_bSubDomainAccess));
450
451         DO_INSERT(row, WidgetWARPInfo)
452     }
453 }
454
455 void WidgetDAO::registerWidgetCertificates(DbWidgetHandle widgetHandle,
456         const IWacSecurity &wacSecurity)
457 {
458     using namespace DPL::DB::ORM;
459     using namespace DPL::DB::ORM::wrt;
460
461
462     FOREACH(it, wacSecurity.getCertificateList())
463     {
464         WidgetCertificateFingerprint::Row row;
465         row.Set_app_id(widgetHandle);
466         row.Set_owner(it->owner);
467         row.Set_chainid(it->chainId);
468         row.Set_type(it->type);
469         row.Set_md5_fingerprint(DPL::FromUTF8String(it->strMD5Fingerprint));
470         row.Set_sha1_fingerprint(DPL::FromUTF8String(it->strSHA1Fingerprint));
471         row.Set_common_name(it->strCommonName);
472
473         DO_INSERT(row, WidgetCertificateFingerprint)
474     }
475 }
476
477 void WidgetDAO::registerWidgetPowderData(DbWidgetHandle widgetHandle,
478         const WidgetRegisterInfo &regInfo)
479 {
480     using namespace DPL::DB::ORM;
481     using namespace DPL::DB::ORM::wrt;
482
483     using namespace Powder;
484
485     FOREACH(i, regInfo.powderDescription.categories)
486     {
487         const DPL::String& categoryName(i->first);
488         const Description::CategoryEntry& categoryEntry(i->second);
489         FOREACH(l, categoryEntry.levels)
490         {
491             PowderLevels::id::ColumnType powderID;
492             {
493                 PowderLevels::Row row;
494                 row.Set_app_id(widgetHandle);
495                 row.Set_category(categoryName);
496                 row.Set_level(l->level);
497
498                 WRT_DB_INSERT(insert, PowderLevels, &WrtDatabase::interface())
499                 insert->Values(row);
500                 powderID = insert->Execute();
501             }
502
503             FOREACH(c, l->context)
504             {
505                 PowderLevelContexts::Row row;
506                 row.Set_levelId(powderID);
507                 row.Set_context(*c);
508
509                 DO_INSERT(row, PowderLevelContexts)
510             }
511         }
512     }
513 }
514
515 void WidgetDAO::registerLaunchCertificates(DbWidgetHandle widgetHandle,
516         const CertificateChainList &certificateChainList)
517 {
518     using namespace DPL::DB::ORM;
519     using namespace DPL::DB::ORM::wrt;
520     FOREACH(certChain, certificateChainList)
521     {
522         WidgetCertificate::Row row;
523         row.Set_app_id(widgetHandle);
524         row.Set_encoded_chain(DPL::FromASCIIString(*certChain));
525
526         DO_INSERT(row, WidgetCertificate);
527     }
528 }
529
530 void WidgetDAO::registerWidgetSettings(DbWidgetHandle widgetHandle,
531         const WidgetRegisterInfo &regInfo)
532 {
533     using namespace DPL::DB::ORM;
534     using namespace DPL::DB::ORM::wrt;
535
536     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
537
538     FOREACH(pWidgetSetting, widgetConfigurationInfo.settingsList)
539     {
540         SettginsList::Row row;
541         row.Set_appId(widgetHandle);
542         row.Set_settingName(pWidgetSetting->m_name);
543         row.Set_settingValue(pWidgetSetting->m_value);
544
545         DO_INSERT(row, SettginsList)
546     }
547 }
548
549 void WidgetDAO::registerAppService(DbWidgetHandle widgetHandle,
550                                    const WidgetRegisterInfo &regInfo)
551 {
552     using namespace DPL::DB::ORM;
553     using namespace DPL::DB::ORM::wrt;
554     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
555
556     FOREACH(ASIt, widgetConfigurationInfo.appServiceList)
557     {
558         ApplicationServiceInfo::Row row;
559         row.Set_app_id(widgetHandle);
560         row.Set_src(ASIt->m_src);
561         row.Set_operation(ASIt->m_operation);
562         row.Set_scheme(ASIt->m_scheme);
563         row.Set_mime(ASIt->m_mime);
564
565         DO_INSERT(row, ApplicationServiceInfo)
566     }
567 }
568
569 #undef DO_INSERT
570
571 void WidgetDAO::unregisterWidget(DbWidgetHandle widgetHandle)
572 {
573     LogDebug("Unregistering widget from DB. Handle: " << widgetHandle);
574     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
575     {
576         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
577         using namespace DPL::DB::ORM;
578         using namespace DPL::DB::ORM::wrt;
579
580         CHECK_WIDGET_EXISTENCE(transaction, widgetHandle)
581
582         // Delete from table Widget Info
583         {
584             WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
585             del->Where(Equals<WidgetInfo::app_id>(widgetHandle));
586             del->Execute();
587         }
588
589         // Deleting in other tables is done via "delete cascade" in SQL
590
591         transaction.Commit();
592     }
593     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
594 }
595
596 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
597 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
598 #undef CHECK_WIDGET_EXISTENCE
599
600 } // namespace WrtDB