2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file property_dao.h
18 * @author Pawel Sikorski (p.sikorski@samsung.com)
20 * @brief This file contains the definition of property dao class.
23 #include <dpl/wrt-dao-rw/property_dao.h>
24 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
25 #include <dpl/log/log.h>
26 #include <dpl/foreach.h>
27 #include <dpl/wrt-dao-ro/webruntime_database.h>
28 #include <dpl/wrt-dao-ro/WrtDatabase.h>
29 #include <orm_generator_wrt.h>
32 namespace PropertyDAO {
34 void RemoveProperty(DbWidgetHandle widgetHandle,
35 const PropertyDAOReadOnly::WidgetPropertyKey &key)
37 RemoveProperty(WidgetDAOReadOnly::getPkgName(widgetHandle),key);
40 void RemoveProperty(WidgetPkgName pkgName,
41 const PropertyDAOReadOnly::WidgetPropertyKey &key)
43 //TODO below there are two queries.
44 // First query asks if given property can be removed,
45 // Second removes it. Maybe it should be combined two one.
47 LogDebug("Removing Property. pkgName: " << pkgName << ", key: " << key);
49 DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
51 DPL::OptionalInt readonly = PropertyDAOReadOnly::CheckPropertyReadFlag(
54 if (!readonly.IsNull() && *readonly == 1) {
55 LogError("'" << key <<
56 "' key is readonly. Cannot remove property !");
57 ThrowMsg(PropertyDAOReadOnly::Exception::ReadOnlyProperty,
58 "Property is readonly");
61 // Key is not readonly, or has no flag defined
63 using namespace DPL::DB::ORM;
64 using namespace DPL::DB::ORM::wrt;
65 WRT_DB_DELETE(del, WidgetPreference, &WrtDatabase::interface())
67 Equals<WidgetPreference::pkgname>(pkgName),
68 Equals<WidgetPreference::key_name>(key)));
73 Catch(DPL::DB::SqlConnection::Exception::Base){
74 ReThrowMsg(PropertyDAOReadOnly::Exception::DatabaseError,
75 "Failure during removing property");
80 void SetProperty(DbWidgetHandle widgetHandle,
81 const PropertyDAOReadOnly::WidgetPropertyKey &key,
82 const PropertyDAOReadOnly::WidgetPropertyValue &value,
85 SetProperty(WidgetDAOReadOnly::getPkgName(widgetHandle),key,value,readOnly);
88 void SetProperty(WidgetPkgName pkgName,
89 const PropertyDAOReadOnly::WidgetPropertyKey &key,
90 const PropertyDAOReadOnly::WidgetPropertyValue &value,
93 LogDebug("Setting/updating Property. pkgName: " << pkgName <<
96 using namespace DPL::DB::ORM;
97 using namespace DPL::DB::ORM::wrt;
98 DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
100 DPL::OptionalInt readonly = PropertyDAOReadOnly::CheckPropertyReadFlag(
103 if (!readonly.IsNull() && *readonly == 1) {
104 LogError("'" << key <<
105 "' key is readonly. Cannot remove property !");
106 ThrowMsg(PropertyDAOReadOnly::Exception::ReadOnlyProperty,
107 "Property is readonly");
110 if (readonly.IsNull()) {
111 WidgetPreference::Row row;
112 row.Set_pkgname(pkgName);
113 row.Set_key_name(key);
114 row.Set_key_value(value);
115 row.Set_readonly(readOnly ? 1 : 0);
117 WRT_DB_INSERT(insert, WidgetPreference, &WrtDatabase::interface())
121 WidgetPreference::Row row;
122 row.Set_key_value(value);
124 WRT_DB_UPDATE(update, WidgetPreference, &WrtDatabase::interface())
126 Equals<WidgetPreference::pkgname>(pkgName),
127 Equals<WidgetPreference::key_name>(key)));
132 transaction.Commit();
134 Catch(DPL::DB::SqlConnection::Exception::Base){
135 ReThrowMsg(PropertyDAOReadOnly::Exception::DatabaseError,
136 "Failure during setting/updating property");
141 void RegisterProperties(DbWidgetHandle widgetHandle,
142 const WidgetRegisterInfo ®Info)
144 RegisterProperties(WidgetDAOReadOnly::getPkgName(widgetHandle),regInfo);
147 void RegisterProperties(WidgetPkgName pkgName,
148 const WidgetRegisterInfo ®Info)
150 LogDebug("Registering proferences for widget. pkgName: " << pkgName);
152 // Try-Catch in WidgetDAO
154 using namespace DPL::DB::ORM;
155 using namespace DPL::DB::ORM::wrt;
156 const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
158 FOREACH(it, widgetConfigurationInfo.preferencesList)
160 { // Insert into table Widget Preferences
161 WidgetPreference::Row row;
162 row.Set_pkgname(pkgName);
163 row.Set_key_name(it->name);
164 row.Set_key_value(it->value);
165 int readonly = true == it->readonly ? 1 : 0;
166 row.Set_readonly(readonly);
168 WRT_DB_INSERT(insert, WidgetPreference, &WrtDatabase::interface())
175 } // namespace PropertyDAO