c95f07053807295892dcf51f354ee8b41652ccbd
[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
34 namespace {
35
36 typedef DPL::DB::ORM::wrt::WidgetPreference::key_name::ColumnType
37     ORMWidgetPropertyKey;
38 typedef DPL::DB::ORM::wrt::WidgetPreference::key_value::ColumnType
39     ORMWidgetPropertyValue;
40 typedef std::list<DPL::DB::ORM::wrt::WidgetPreference::Row> ORMWidgetPreferenceList;
41 typedef std::list<WidgetPropertyKey> ORMWidgetPropertyKeyList;
42
43
44 void convertPropertyKey(const ORMWidgetPropertyKey& ormKey,
45                         WidgetPropertyKey& key)
46 {
47     key = ormKey;
48 }
49
50 void convertPropertyValue(const ORMWidgetPropertyValue& ormPropertyVal,
51                           WidgetPropertyValue& propertyVal)
52 {
53     propertyVal = ormPropertyVal;
54 }
55
56 void convertWidgetPreferenceRow(const ORMWidgetPreferenceList& ormWidgetPrefRow,
57                                 WidgetPreferenceList& prefRow)
58 {
59     FOREACH(it, ormWidgetPrefRow) {
60         WidgetPreferenceRow row;
61
62         row.tizen_appid = it->Get_tizen_appid();
63         row.key_name = it->Get_key_name();
64         row.key_value = it->Get_key_value();
65         row.readonly = it->Get_readonly();
66
67         prefRow.push_back(row);
68     }
69 }
70
71 void convertWidgetPropertyKeyList(const ORMWidgetPropertyKeyList& propKeyList,
72                                   WidgetPropertyKeyList& keyList)
73 {
74     FOREACH(it, propKeyList) {
75         keyList.push_back(*it);
76     }
77 }
78
79
80 }
81
82 //deprecated
83 DPL::OptionalInt CheckPropertyReadFlag(DbWidgetHandle widgetHandle,
84                                   const WidgetPropertyKey &key)
85 {
86     return CheckPropertyReadFlag(WidgetDAOReadOnly::getTzAppId(widgetHandle),
87                                  key);
88 }
89
90 DPL::OptionalInt CheckPropertyReadFlag(TizenAppId tzAppid,
91                                   const WidgetPropertyKey &key)
92 {
93     LogDebug("Checking Property flag. appid: " << tzAppid <<
94              ", key: " << key);
95
96     Try {
97         using namespace DPL::DB::ORM;
98         using namespace DPL::DB::ORM::wrt;
99         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
100         select->Where(And(Equals<WidgetPreference::tizen_appid>(tzAppid),
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         ORMWidgetPropertyKeyList keyList;
118         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
119         select->Where(Equals<WidgetPreference::tizen_appid>(tzAppid));
120         keyList = select->GetValueList<WidgetPreference::key_name>();
121
122         WidgetPropertyKeyList retKeyList;
123
124         convertWidgetPropertyKeyList(keyList, retKeyList);
125         return retKeyList;
126     }
127     Catch(DPL::DB::SqlConnection::Exception::Base){
128         ReThrowMsg(Exception::DatabaseError,
129                    "Failure during getting propertykey list");
130     }
131 }
132
133 //deprecated
134 WidgetPreferenceList GetPropertyList(DbWidgetHandle widgetHandle)
135 {
136     Try{
137         TizenAppId tzAppid = WidgetDAOReadOnly::getTzAppId(widgetHandle);
138         return GetPropertyList(tzAppid);
139     }Catch(WidgetDAOReadOnly::Exception::WidgetNotExist){
140         WidgetPreferenceList empty;
141         return empty;
142     }
143 }
144
145 WidgetPreferenceList GetPropertyList(TizenAppId tzAppId)
146 {
147     LogDebug("Get Property list. tizenAppId: " << tzAppId);
148     Try {
149         using namespace DPL::DB::ORM;
150         using namespace DPL::DB::ORM::wrt;
151         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
152         select->Where(Equals<WidgetPreference::tizen_appid>(tzAppId));
153
154         ORMWidgetPreferenceList ormPrefList = select->GetRowList();
155         WidgetPreferenceList prefList;
156         convertWidgetPreferenceRow(ormPrefList, prefList);
157
158         return prefList;
159     }
160     Catch(DPL::DB::SqlConnection::Exception::Base){
161         ReThrowMsg(Exception::DatabaseError,
162                    "Failure during getting property list");
163     }
164 }
165
166 WidgetPropertyValue GetPropertyValue(TizenAppId tzAppid,
167                                      const WidgetPropertyKey &key)
168 {
169     LogDebug("Get Property value. appid: " << tzAppid <<
170              ", key: " << key);
171     Try {
172         using namespace DPL::DB::ORM;
173         using namespace DPL::DB::ORM::wrt;
174         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
175         select->Where(And(Equals<WidgetPreference::tizen_appid>(tzAppid),
176                           Equals<WidgetPreference::key_name>(key)));
177
178         ORMWidgetPropertyValue ormPropValue =
179                 select->GetSingleValue<WidgetPreference::key_value>();
180
181         WidgetPropertyValue propValue;
182
183         convertPropertyValue(ormPropValue, propValue);
184
185         return propValue;
186     }
187     Catch(DPL::DB::SqlConnection::Exception::Base){
188         ReThrowMsg(Exception::DatabaseError,
189                    "Failure during getting property");
190     }
191 }
192
193 } // namespace PropertyDAOReadOnly
194 } // namespace WrtDB