Initialize Tizen 2.3
[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/wrt_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 void RemoveProperty(TizenAppId tzAppid,
34                     const PropertyDAOReadOnly::WidgetPropertyKey &key)
35 {
36     //TODO below there are two queries.
37     // First query asks if given property can be removed,
38     // Second removes it. Maybe it should be combined two one.
39
40     WrtLogD("Removing Property. appid: %ls, key: %ls",
41         tzAppid.c_str(), key.c_str());
42     Try {
43         DPL::DB::ORM::wrt::ScopedTransaction transaction(
44             &WrtDatabase::interface());
45
46         DPL::OptionalInt readonly = PropertyDAOReadOnly::CheckPropertyReadFlag(
47                 tzAppid,
48                 key);
49         if (!!readonly && *readonly == 1) {
50             WrtLogE("'%ls' key is readonly. Cannot remove property !",
51                 key.c_str());
52             ThrowMsg(PropertyDAOReadOnly::Exception::ReadOnlyProperty,
53                      "Property is readonly");
54         }
55
56         // Key is not readonly, or has no flag defined
57
58         using namespace DPL::DB::ORM;
59         using namespace DPL::DB::ORM::wrt;
60         WRT_DB_DELETE(del, WidgetPreference, &WrtDatabase::interface())
61         del->Where(And(
62                        Equals<WidgetPreference::tizen_appid>(tzAppid),
63                        Equals<WidgetPreference::key_name>(key)));
64         del->Execute();
65
66         transaction.Commit();
67     }
68     Catch(DPL::DB::SqlConnection::Exception::Base){
69         ReThrowMsg(PropertyDAOReadOnly::Exception::DatabaseError,
70                    "Failure during removing property");
71     }
72 }
73
74 //deprecated
75 void SetProperty(DbWidgetHandle widgetHandle,
76                  const PropertyDAOReadOnly::WidgetPropertyKey &key,
77                  const PropertyDAOReadOnly::WidgetPropertyValue &value,
78                  bool readOnly)
79 {
80     SetProperty(WidgetDAOReadOnly::getTizenAppId(
81                     widgetHandle), key, value, readOnly);
82 }
83
84 void SetProperty(TizenAppId tzAppid,
85                  const PropertyDAOReadOnly::WidgetPropertyKey &key,
86                  const PropertyDAOReadOnly::WidgetPropertyValue &value,
87                  bool readOnly)
88 {
89     WrtLogD("Setting/updating Property. appid: %ls, key: %ls",
90         tzAppid.c_str(), key.c_str());
91     Try {
92         using namespace DPL::DB::ORM;
93         using namespace DPL::DB::ORM::wrt;
94         DPL::DB::ORM::wrt::ScopedTransaction transaction(
95             &WrtDatabase::interface());
96
97         DPL::OptionalInt readonly = PropertyDAOReadOnly::CheckPropertyReadFlag(
98                 tzAppid,
99                 key);
100         if (!!readonly && *readonly == 1) {
101             WrtLogE("'%ls' key is readonly. Cannot remove property !",
102                 key.c_str());
103             ThrowMsg(PropertyDAOReadOnly::Exception::ReadOnlyProperty,
104                      "Property is readonly");
105         }
106         DbWidgetHandle widgetHandle(WidgetDAOReadOnly::getHandle(tzAppid));
107
108         if (!readonly) {
109             WidgetPreference::Row row;
110             row.Set_app_id(widgetHandle);
111             row.Set_tizen_appid(tzAppid);
112             row.Set_key_name(key);
113             row.Set_key_value(value);
114             row.Set_readonly(readOnly ? 1 : 0);
115
116             WRT_DB_INSERT(insert, WidgetPreference, &WrtDatabase::interface())
117             insert->Values(row);
118             insert->Execute();
119         } else {
120             WidgetPreference::Row row;
121             row.Set_key_value(value);
122
123             WRT_DB_UPDATE(update, WidgetPreference, &WrtDatabase::interface())
124             update->Where(And(
125                               Equals<WidgetPreference::tizen_appid>(tzAppid),
126                               Equals<WidgetPreference::key_name>(key)));
127
128             update->Values(row);
129             update->Execute();
130         }
131         transaction.Commit();
132     }
133     Catch(DPL::DB::SqlConnection::Exception::Base){
134         ReThrowMsg(PropertyDAOReadOnly::Exception::DatabaseError,
135                    "Failure during setting/updating property");
136     }
137 }
138
139 void RegisterProperties(DbWidgetHandle widgetHandle, TizenAppId tzAppid,
140                         const WidgetRegisterInfo &regInfo)
141 {
142     WrtLogD("Registering proferences for widget. appid: %ls", tzAppid.c_str());
143
144     // Try-Catch in WidgetDAO
145
146     using namespace DPL::DB::ORM;
147     using namespace DPL::DB::ORM::wrt;
148     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
149
150     FOREACH(it, widgetConfigurationInfo.preferencesList)
151     {
152         { // Insert into table Widget Preferences
153             WidgetPreference::Row row;
154             row.Set_app_id(widgetHandle);
155             row.Set_tizen_appid(tzAppid);
156             row.Set_key_name(it->name);
157             row.Set_key_value(it->value);
158             int readonly = true == it->readonly ? 1 : 0;
159             row.Set_readonly(readonly);
160
161             WRT_DB_INSERT(insert, WidgetPreference, &WrtDatabase::interface())
162             insert->Values(row);
163             insert->Execute();
164         }
165     }
166 }
167 } // namespace PropertyDAO
168 } // namespace WrtDB