Initialize Tizen 2.3
[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/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 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 WidgetPreferenceList GetPropertyListRows(DbWidgetHandle widgetHandle)
79 {
80     Try {
81         using namespace DPL::DB::ORM;
82         using namespace DPL::DB::ORM::wrt;
83         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
84             select->Where(Equals<WidgetPreference::app_id>(widgetHandle));
85         ORMWidgetPreferenceList ormPrefList = select->GetRowList();
86         WidgetPreferenceList prefList;
87         convertWidgetPreferenceRow(ormPrefList, prefList);
88         return prefList;
89     }Catch(DPL::DB::SqlConnection::Exception::Base){
90         ReThrowMsg(Exception::DatabaseError,
91                 "Failure during getting property list");
92     }
93 }
94 }
95
96 //deprecated
97 DPL::OptionalInt CheckPropertyReadFlag(DbWidgetHandle widgetHandle,
98                                        const WidgetPropertyKey &key)
99 {
100     return CheckPropertyReadFlag(WidgetDAOReadOnly::getTizenAppId(widgetHandle),
101                                  key);
102 }
103
104 DPL::OptionalInt CheckPropertyReadFlag(TizenAppId tzAppid,
105                                        const WidgetPropertyKey &key)
106 {
107     WrtLogD("Checking Property flag. appid: %ls, key: %ls",
108         tzAppid.c_str(), key.c_str());
109
110     Try {
111         using namespace DPL::DB::ORM;
112         using namespace DPL::DB::ORM::wrt;
113
114         DbWidgetHandle widgetHandle(WidgetDAOReadOnly::getHandle(tzAppid));
115
116         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
117         select->Where(And(Equals<WidgetPreference::app_id>(widgetHandle),
118                           Equals<WidgetPreference::key_name>(key)));
119
120         return select->GetSingleValue<WidgetPreference::readonly>();
121     }
122     Catch(DPL::DB::SqlConnection::Exception::Base){
123         ReThrowMsg(Exception::DatabaseError,
124                    "Failure during checking readonly flag for property");
125     }
126 }
127
128 WidgetPropertyKeyList GetPropertyKeyList(TizenAppId tzAppid)
129 {
130     WrtLogD("Get PropertyKey list. appid: %ls", tzAppid.c_str());
131     Try {
132         using namespace DPL::DB::ORM;
133         using namespace DPL::DB::ORM::wrt;
134
135         DbWidgetHandle widgetHandle(WidgetDAOReadOnly::getHandle(tzAppid));
136
137         ORMWidgetPropertyKeyList keyList;
138         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
139         select->Where(Equals<WidgetPreference::app_id>(widgetHandle));
140         keyList = select->GetValueList<WidgetPreference::key_name>();
141
142         WidgetPropertyKeyList retKeyList;
143
144         convertWidgetPropertyKeyList(keyList, retKeyList);
145         return retKeyList;
146     }
147     Catch(DPL::DB::SqlConnection::Exception::Base){
148         ReThrowMsg(Exception::DatabaseError,
149                    "Failure during getting propertykey list");
150     }
151 }
152
153 WidgetPreferenceList GetPropertyList(DbWidgetHandle widgetHandle)
154 {
155     if(!(WidgetDAOReadOnly::isWidgetInstalled(widgetHandle)))
156         ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
157                 "Failed to get widget");
158     return GetPropertyListRows(widgetHandle);
159 }
160
161 WidgetPreferenceList GetPropertyList(TizenAppId tzAppId)
162 {
163     WrtLogD("Get Property list. tizenAppId: %ls", tzAppId.c_str());
164     DbWidgetHandle widgetHandle(WidgetDAOReadOnly::getHandle(tzAppId));
165     return GetPropertyListRows(widgetHandle);
166 }
167
168
169 WidgetPropertyValue GetPropertyValue(TizenAppId tzAppid,
170                                      const WidgetPropertyKey &key)
171 {
172     WrtLogD("Get Property value. appid: %ls, key: %ls",
173         tzAppid.c_str(), key.c_str());
174     Try {
175         using namespace DPL::DB::ORM;
176         using namespace DPL::DB::ORM::wrt;
177
178         DbWidgetHandle widgetHandle(WidgetDAOReadOnly::getHandle(tzAppid));
179
180         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
181         select->Where(And(Equals<WidgetPreference::app_id>(widgetHandle),
182                           Equals<WidgetPreference::key_name>(key)));
183
184         ORMWidgetPropertyValue ormPropValue =
185             select->GetSingleValue<WidgetPreference::key_value>();
186
187         WidgetPropertyValue propValue;
188
189         convertPropertyValue(ormPropValue, propValue);
190
191         return propValue;
192     }
193     Catch(DPL::DB::SqlConnection::Exception::Base){
194         ReThrowMsg(Exception::DatabaseError,
195                    "Failure during getting property");
196     }
197 }
198 } // namespace PropertyDAOReadOnly
199 } // namespace WrtDB