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