Update User Agent String
[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
23 #include <dpl/wrt-dao-ro/property_dao_read_only.h>
24 #include <dpl/log/log.h>
25 #include <dpl/foreach.h>
26 #include <dpl/wrt-dao-ro/webruntime_database.h>
27 #include <dpl/wrt-dao-ro/WrtDatabase.h>
28 #include <orm_generator_wrt.h>
29
30 namespace WrtDB {
31 namespace PropertyDAOReadOnly {
32
33 namespace {
34
35 typedef DPL::DB::ORM::wrt::WidgetPreference::key_name::ColumnType
36     ORMWidgetPropertyKey;
37 typedef DPL::DB::ORM::wrt::WidgetPreference::key_value::ColumnType
38     ORMWidgetPropertyValue;
39 typedef std::list<DPL::DB::ORM::wrt::WidgetPreference::Row> ORMWidgetPreferenceList;
40 typedef std::list<WidgetPropertyKey> ORMWidgetPropertyKeyList;
41
42
43 void convertPropertyKey(const ORMWidgetPropertyKey& ormKey,
44                         WidgetPropertyKey& key)
45 {
46     key = ormKey;
47 }
48
49 void convertPropertyValue(const ORMWidgetPropertyValue& ormPropertyVal,
50                           WidgetPropertyValue& propertyVal)
51 {
52     propertyVal = ormPropertyVal;
53 }
54
55 void convertWidgetPreferenceRow(const ORMWidgetPreferenceList& ormWidgetPrefRow,
56                                 WidgetPreferenceList& prefRow)
57 {
58     FOREACH(it, ormWidgetPrefRow) {
59         WidgetPreferenceRow row;
60
61         row.app_id = it->Get_app_id();
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 }
80
81 WidgetPropertyKeyList GetPropertyKeyList(DbWidgetHandle widgetHandle)
82 {
83     LogDebug("Get PropertyKey list. Handle: " << widgetHandle);
84     Try {
85         using namespace DPL::DB::ORM;
86         using namespace DPL::DB::ORM::wrt;
87         ORMWidgetPropertyKeyList keyList;
88         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
89         select->Where(Equals<WidgetPreference::app_id>(widgetHandle));
90         keyList = select->GetValueList<WidgetPreference::key_name>();
91
92         WidgetPropertyKeyList retKeyList;
93
94         convertWidgetPropertyKeyList(keyList, retKeyList);
95         return retKeyList;
96     }
97     Catch(DPL::DB::SqlConnection::Exception::Base){
98         ReThrowMsg(Exception::DatabaseError,
99                    "Failure during getting propertykey list");
100     }
101 }
102
103 WidgetPreferenceList GetPropertyList(DbWidgetHandle widgetHandle)
104 {
105     LogDebug("Get Property list. Handle: " << widgetHandle);
106     Try {
107         using namespace DPL::DB::ORM;
108         using namespace DPL::DB::ORM::wrt;
109         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
110         select->Where(Equals<WidgetPreference::app_id>(widgetHandle));
111
112         ORMWidgetPreferenceList ormPrefList = select->GetRowList();
113         WidgetPreferenceList prefList;
114         convertWidgetPreferenceRow(ormPrefList, prefList);
115
116         return prefList;
117     }
118     Catch(DPL::DB::SqlConnection::Exception::Base){
119         ReThrowMsg(Exception::DatabaseError,
120                    "Failure during getting property list");
121     }
122 }
123
124 WidgetPropertyValue GetPropertyValue(DbWidgetHandle widgetHandle,
125                                      const WidgetPropertyKey &key)
126 {
127     LogDebug("Get Property value. Handle: " << widgetHandle <<
128              ", key: " << key);
129     Try {
130         using namespace DPL::DB::ORM;
131         using namespace DPL::DB::ORM::wrt;
132         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
133         select->Where(And(Equals<WidgetPreference::app_id>(widgetHandle),
134                           Equals<WidgetPreference::key_name>(key)));
135
136         ORMWidgetPropertyValue ormPropValue =
137                 select->GetSingleValue<WidgetPreference::key_value>();
138
139         WidgetPropertyValue propValue;
140
141         convertPropertyValue(ormPropValue, propValue);
142
143         return propValue;
144     }
145     Catch(DPL::DB::SqlConnection::Exception::Base){
146         ReThrowMsg(Exception::DatabaseError,
147                    "Failure during getting property");
148     }
149 }
150
151 DPL::OptionalInt CheckPropertyReadFlag(DbWidgetHandle widgetHandle,
152                                   const WidgetPropertyKey &key)
153 {
154     LogDebug("Checking Property flag. Handle: " << widgetHandle <<
155              ", key: " << key);
156     Try {
157         using namespace DPL::DB::ORM;
158         using namespace DPL::DB::ORM::wrt;
159         WRT_DB_SELECT(select, WidgetPreference, &WrtDatabase::interface())
160         select->Where(And(Equals<WidgetPreference::app_id>(widgetHandle),
161                           Equals<WidgetPreference::key_name>(key)));
162
163         return select->GetSingleValue<WidgetPreference::readonly>();
164     }
165     Catch(DPL::DB::SqlConnection::Exception::Base){
166         ReThrowMsg(Exception::DatabaseError,
167                    "Failure during checking readonly flag for property");
168     }
169 }
170
171 } // namespace PropertyDAOReadOnly
172 } // namespace WrtDB