Tizen 2.0 Release
[framework/web/wrt-commons.git] / modules / widget_dao / dao / property_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  * @file   property_dao.h
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @version 1.0
20  * @brief   This file contains the definition of property dao class.
21  */
22 #include <stddef.h>
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>
30
31 namespace WrtDB {
32 namespace PropertyDAO {
33 //deprecated
34 void RemoveProperty(DbWidgetHandle widgetHandle,
35                     const PropertyDAOReadOnly::WidgetPropertyKey &key)
36 {
37     RemoveProperty(WidgetDAOReadOnly::getPkgName(widgetHandle),key);
38 }
39
40 void RemoveProperty(WidgetPkgName pkgName,
41                     const PropertyDAOReadOnly::WidgetPropertyKey &key)
42 {
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.
46
47     LogDebug("Removing Property. pkgName: " << pkgName << ", key: " << key);
48     Try {
49         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
50
51         DPL::OptionalInt readonly = PropertyDAOReadOnly::CheckPropertyReadFlag(
52                 pkgName,
53                 key);
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");
59         }
60
61         // Key is not readonly, or has no flag defined
62
63         using namespace DPL::DB::ORM;
64         using namespace DPL::DB::ORM::wrt;
65         WRT_DB_DELETE(del, WidgetPreference, &WrtDatabase::interface())
66         del->Where(And(
67                        Equals<WidgetPreference::pkgname>(pkgName),
68                        Equals<WidgetPreference::key_name>(key)));
69         del->Execute();
70
71         transaction.Commit();
72     }
73     Catch(DPL::DB::SqlConnection::Exception::Base){
74         ReThrowMsg(PropertyDAOReadOnly::Exception::DatabaseError,
75                    "Failure during removing property");
76     }
77 }
78
79 //deprecated
80 void SetProperty(DbWidgetHandle widgetHandle,
81                  const PropertyDAOReadOnly::WidgetPropertyKey &key,
82                  const PropertyDAOReadOnly::WidgetPropertyValue &value,
83                  bool readOnly)
84 {
85     SetProperty(WidgetDAOReadOnly::getPkgName(widgetHandle),key,value,readOnly);
86 }
87
88 void SetProperty(WidgetPkgName pkgName,
89                  const PropertyDAOReadOnly::WidgetPropertyKey &key,
90                  const PropertyDAOReadOnly::WidgetPropertyValue &value,
91                  bool readOnly)
92 {
93     LogDebug("Setting/updating Property. pkgName: " << pkgName <<
94              ", key: " << key);
95     Try {
96         using namespace DPL::DB::ORM;
97         using namespace DPL::DB::ORM::wrt;
98         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
99
100         DPL::OptionalInt readonly = PropertyDAOReadOnly::CheckPropertyReadFlag(
101                 pkgName,
102                 key);
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");
108         }
109
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);
116
117             WRT_DB_INSERT(insert, WidgetPreference, &WrtDatabase::interface())
118             insert->Values(row);
119             insert->Execute();
120         } else {
121             WidgetPreference::Row row;
122             row.Set_key_value(value);
123
124             WRT_DB_UPDATE(update, WidgetPreference, &WrtDatabase::interface())
125             update->Where(And(
126                               Equals<WidgetPreference::pkgname>(pkgName),
127                               Equals<WidgetPreference::key_name>(key)));
128
129             update->Values(row);
130             update->Execute();
131         }
132         transaction.Commit();
133     }
134     Catch(DPL::DB::SqlConnection::Exception::Base){
135         ReThrowMsg(PropertyDAOReadOnly::Exception::DatabaseError,
136                    "Failure during setting/updating property");
137     }
138 }
139
140 //deprecated
141 void RegisterProperties(DbWidgetHandle widgetHandle,
142                         const WidgetRegisterInfo &regInfo)
143 {
144     RegisterProperties(WidgetDAOReadOnly::getPkgName(widgetHandle),regInfo);
145 }
146
147 void RegisterProperties(WidgetPkgName pkgName,
148                         const WidgetRegisterInfo &regInfo)
149 {
150     LogDebug("Registering proferences for widget. pkgName: " << pkgName);
151
152     // Try-Catch in WidgetDAO
153
154     using namespace DPL::DB::ORM;
155     using namespace DPL::DB::ORM::wrt;
156     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
157
158     FOREACH(it, widgetConfigurationInfo.preferencesList)
159     {
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);
167
168             WRT_DB_INSERT(insert, WidgetPreference, &WrtDatabase::interface())
169             insert->Values(row);
170             insert->Execute();
171         }
172     }
173 }
174
175 } // namespace PropertyDAO
176 } // namespace WrtDB