merge with master
[platform/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.tizen_appid = it->Get_tizen_appid();
61         row.key_name = it->Get_key_name();
62         row.key_value = it->Get_key_value();
63         row.readonly = it->Get_readonly();
64
65         prefRow.push_back(row);
66     }
67 }
68
69 void convertWidgetPropertyKeyList(const ORMWidgetPropertyKeyList& propKeyList,
70                                   WidgetPropertyKeyList& keyList)
71 {
72     FOREACH(it, propKeyList) {
73         keyList.push_back(*it);
74     }
75 }
76 }
77
78 //deprecated
79 DPL::OptionalInt CheckPropertyReadFlag(DbWidgetHandle widgetHandle,
80                                        const WidgetPropertyKey &key)
81 {
82     return CheckPropertyReadFlag(WidgetDAOReadOnly::getTzAppId(widgetHandle),
83                                  key);
84 }
85
86 DPL::OptionalInt CheckPropertyReadFlag(TizenAppId tzAppid,
87                                        const WidgetPropertyKey &key)
88 {
89     LogDebug("Checking Property flag. appid: " << tzAppid <<
90              ", key: " << key);
91
92     Try {
93         using namespace DPL::DB::ORM;
94         using namespace DPL::DB::ORM::wrt;
95         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
96         select->Where(And(Equals<WidgetPreference::tizen_appid>(tzAppid),
97                           Equals<WidgetPreference::key_name>(key)));
98
99         return select->GetSingleValue<WidgetPreference::readonly>();
100     }
101     Catch(DPL::DB::SqlConnection::Exception::Base){
102         ReThrowMsg(Exception::DatabaseError,
103                    "Failure during checking readonly flag for property");
104     }
105 }
106
107 WidgetPropertyKeyList GetPropertyKeyList(TizenAppId tzAppid)
108 {
109     LogDebug("Get PropertyKey list. appid: " << tzAppid);
110     Try {
111         using namespace DPL::DB::ORM;
112         using namespace DPL::DB::ORM::wrt;
113         ORMWidgetPropertyKeyList keyList;
114         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
115         select->Where(Equals<WidgetPreference::tizen_appid>(tzAppid));
116         keyList = select->GetValueList<WidgetPreference::key_name>();
117
118         WidgetPropertyKeyList retKeyList;
119
120         convertWidgetPropertyKeyList(keyList, retKeyList);
121         return retKeyList;
122     }
123     Catch(DPL::DB::SqlConnection::Exception::Base){
124         ReThrowMsg(Exception::DatabaseError,
125                    "Failure during getting propertykey list");
126     }
127 }
128
129 //deprecated
130 WidgetPreferenceList GetPropertyList(DbWidgetHandle widgetHandle)
131 {
132     Try {
133         TizenAppId tzAppid = WidgetDAOReadOnly::getTzAppId(widgetHandle);
134         return GetPropertyList(tzAppid);
135     } Catch(WidgetDAOReadOnly::Exception::WidgetNotExist){
136         WidgetPreferenceList empty;
137         return empty;
138     }
139 }
140
141 WidgetPreferenceList GetPropertyList(TizenAppId tzAppId)
142 {
143     LogDebug("Get Property list. tizenAppId: " << tzAppId);
144     Try {
145         using namespace DPL::DB::ORM;
146         using namespace DPL::DB::ORM::wrt;
147         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
148         select->Where(Equals<WidgetPreference::tizen_appid>(tzAppId));
149
150         ORMWidgetPreferenceList ormPrefList = select->GetRowList();
151         WidgetPreferenceList prefList;
152         convertWidgetPreferenceRow(ormPrefList, prefList);
153
154         return prefList;
155     }
156     Catch(DPL::DB::SqlConnection::Exception::Base){
157         ReThrowMsg(Exception::DatabaseError,
158                    "Failure during getting property list");
159     }
160 }
161
162 WidgetPropertyValue GetPropertyValue(TizenAppId tzAppid,
163                                      const WidgetPropertyKey &key)
164 {
165     LogDebug("Get Property value. appid: " << tzAppid <<
166              ", key: " << key);
167     Try {
168         using namespace DPL::DB::ORM;
169         using namespace DPL::DB::ORM::wrt;
170         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
171         select->Where(And(Equals<WidgetPreference::tizen_appid>(tzAppid),
172                           Equals<WidgetPreference::key_name>(key)));
173
174         ORMWidgetPropertyValue ormPropValue =
175             select->GetSingleValue<WidgetPreference::key_value>();
176
177         WidgetPropertyValue propValue;
178
179         convertPropertyValue(ormPropValue, propValue);
180
181         return propValue;
182     }
183     Catch(DPL::DB::SqlConnection::Exception::Base){
184         ReThrowMsg(Exception::DatabaseError,
185                    "Failure during getting property");
186     }
187 }
188 } // namespace PropertyDAOReadOnly
189 } // namespace WrtDB