Fixed web app can't update.
[framework/web/wrt-commons.git] / modules / widget_dao / dao / property_dao_read_only.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  * property_dao_read_only.cpp
18  *
19  *  Created on: Nov 16, 2011
20  *      Author: Krzysztof Jackiewicz(k.jackiewicz@samsung.com)
21  */
22 #include <stddef.h>
23 #include <dpl/wrt-dao-ro/property_dao_read_only.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 PropertyDAOReadOnly {
33 namespace {
34 typedef DPL::DB::ORM::wrt::WidgetPreference::key_name::ColumnType
35 ORMWidgetPropertyKey;
36 typedef DPL::DB::ORM::wrt::WidgetPreference::key_value::ColumnType
37 ORMWidgetPropertyValue;
38 typedef std::list<DPL::DB::ORM::wrt::WidgetPreference::Row>
39 ORMWidgetPreferenceList;
40 typedef std::list<WidgetPropertyKey> ORMWidgetPropertyKeyList;
41
42 void convertPropertyKey(const ORMWidgetPropertyKey& ormKey,
43                         WidgetPropertyKey& key)
44 {
45     key = ormKey;
46 }
47
48 void convertPropertyValue(const ORMWidgetPropertyValue& ormPropertyVal,
49                           WidgetPropertyValue& propertyVal)
50 {
51     propertyVal = ormPropertyVal;
52 }
53
54 void convertWidgetPreferenceRow(const ORMWidgetPreferenceList& ormWidgetPrefRow,
55                                 WidgetPreferenceList& prefRow)
56 {
57     FOREACH(it, ormWidgetPrefRow) {
58         WidgetPreferenceRow row;
59
60         row.appId = it->Get_app_id();
61         row.tizen_appid = it->Get_tizen_appid();
62         row.key_name = it->Get_key_name();
63         row.key_value = it->Get_key_value();
64         row.readonly = it->Get_readonly();
65
66         prefRow.push_back(row);
67     }
68 }
69
70 void convertWidgetPropertyKeyList(const ORMWidgetPropertyKeyList& propKeyList,
71                                   WidgetPropertyKeyList& keyList)
72 {
73     FOREACH(it, propKeyList) {
74         keyList.push_back(*it);
75     }
76 }
77 }
78
79 //deprecated
80 DPL::OptionalInt CheckPropertyReadFlag(DbWidgetHandle widgetHandle,
81                                        const WidgetPropertyKey &key)
82 {
83     return CheckPropertyReadFlag(WidgetDAOReadOnly::getTzAppId(widgetHandle),
84                                  key);
85 }
86
87 DPL::OptionalInt CheckPropertyReadFlag(TizenAppId tzAppid,
88                                        const WidgetPropertyKey &key)
89 {
90     LogDebug("Checking Property flag. appid: " << tzAppid <<
91              ", key: " << key);
92
93     Try {
94         using namespace DPL::DB::ORM;
95         using namespace DPL::DB::ORM::wrt;
96
97         DbWidgetHandle widgetHandle(WidgetDAOReadOnly::getHandle(tzAppid));
98
99         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
100         select->Where(And(Equals<WidgetPreference::app_id>(widgetHandle),
101                           Equals<WidgetPreference::key_name>(key)));
102
103         return select->GetSingleValue<WidgetPreference::readonly>();
104     }
105     Catch(DPL::DB::SqlConnection::Exception::Base){
106         ReThrowMsg(Exception::DatabaseError,
107                    "Failure during checking readonly flag for property");
108     }
109 }
110
111 WidgetPropertyKeyList GetPropertyKeyList(TizenAppId tzAppid)
112 {
113     LogDebug("Get PropertyKey list. appid: " << tzAppid);
114     Try {
115         using namespace DPL::DB::ORM;
116         using namespace DPL::DB::ORM::wrt;
117
118         DbWidgetHandle widgetHandle(WidgetDAOReadOnly::getHandle(tzAppid));
119
120         ORMWidgetPropertyKeyList keyList;
121         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
122         select->Where(Equals<WidgetPreference::app_id>(widgetHandle));
123         keyList = select->GetValueList<WidgetPreference::key_name>();
124
125         WidgetPropertyKeyList retKeyList;
126
127         convertWidgetPropertyKeyList(keyList, retKeyList);
128         return retKeyList;
129     }
130     Catch(DPL::DB::SqlConnection::Exception::Base){
131         ReThrowMsg(Exception::DatabaseError,
132                    "Failure during getting propertykey list");
133     }
134 }
135
136 //deprecated
137 WidgetPreferenceList GetPropertyList(DbWidgetHandle widgetHandle)
138 {
139     Try {
140         TizenAppId tzAppid = WidgetDAOReadOnly::getTzAppId(widgetHandle);
141         return GetPropertyList(tzAppid);
142     } Catch(WidgetDAOReadOnly::Exception::WidgetNotExist){
143         WidgetPreferenceList empty;
144         return empty;
145     }
146 }
147
148 WidgetPreferenceList GetPropertyList(TizenAppId tzAppId)
149 {
150     LogDebug("Get Property list. tizenAppId: " << tzAppId);
151     Try {
152         using namespace DPL::DB::ORM;
153         using namespace DPL::DB::ORM::wrt;
154
155         DbWidgetHandle widgetHandle(WidgetDAOReadOnly::getHandle(tzAppId));
156
157         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
158         select->Where(Equals<WidgetPreference::app_id>(widgetHandle));
159
160         ORMWidgetPreferenceList ormPrefList = select->GetRowList();
161         WidgetPreferenceList prefList;
162         convertWidgetPreferenceRow(ormPrefList, prefList);
163
164         return prefList;
165     }
166     Catch(DPL::DB::SqlConnection::Exception::Base){
167         ReThrowMsg(Exception::DatabaseError,
168                    "Failure during getting property list");
169     }
170 }
171
172 WidgetPropertyValue GetPropertyValue(TizenAppId tzAppid,
173                                      const WidgetPropertyKey &key)
174 {
175     LogDebug("Get Property value. appid: " << tzAppid <<
176              ", key: " << key);
177     Try {
178         using namespace DPL::DB::ORM;
179         using namespace DPL::DB::ORM::wrt;
180
181         DbWidgetHandle widgetHandle(WidgetDAOReadOnly::getHandle(tzAppid));
182
183         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
184         select->Where(And(Equals<WidgetPreference::app_id>(widgetHandle),
185                           Equals<WidgetPreference::key_name>(key)));
186
187         ORMWidgetPropertyValue ormPropValue =
188             select->GetSingleValue<WidgetPreference::key_value>();
189
190         WidgetPropertyValue propValue;
191
192         convertPropertyValue(ormPropValue, propValue);
193
194         return propValue;
195     }
196     Catch(DPL::DB::SqlConnection::Exception::Base){
197         ReThrowMsg(Exception::DatabaseError,
198                    "Failure during getting property");
199     }
200 }
201 } // namespace PropertyDAOReadOnly
202 } // namespace WrtDB