Additional Include: stddef.h
[platform/framework/web/wrt-commons.git] / modules / widget_dao / dao / property_dao.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  * @file   property_dao.h
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @version 1.0
20  * @brief   This file contains the definition of property dao class.
21  */
22 #include <stddef.h>
23 #include <dpl/wrt-dao-rw/property_dao.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 PropertyDAO {
33
34 void RemoveProperty(DbWidgetHandle widgetHandle,
35                     const PropertyDAOReadOnly::WidgetPropertyKey &key)
36 {
37     //TODO below there are two queries.
38     // First query asks if given property can be removed,
39     // Second removes it. Maybe it should be combined two one.
40
41     LogDebug("Removing Property. Handle: " << widgetHandle << ", key: " << key);
42     Try {
43         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
44
45         DPL::OptionalInt readonly = PropertyDAOReadOnly::CheckPropertyReadFlag(
46                 widgetHandle,
47                 key);
48         if (!readonly.IsNull() && *readonly == 1) {
49             LogError("'" << key <<
50                      "' key is readonly. Cannot remove property !");
51             ThrowMsg(PropertyDAOReadOnly::Exception::ReadOnlyProperty,
52                      "Property is readonly");
53         }
54
55         // Key is not readonly, or has no flag defined
56
57         using namespace DPL::DB::ORM;
58         using namespace DPL::DB::ORM::wrt;
59         WRT_DB_DELETE(del, WidgetPreference, &WrtDatabase::interface())
60         del->Where(And(
61                        Equals<WidgetPreference::app_id>(widgetHandle),
62                        Equals<WidgetPreference::key_name>(key)));
63         del->Execute();
64
65         transaction.Commit();
66     }
67     Catch(DPL::DB::SqlConnection::Exception::Base){
68         ReThrowMsg(PropertyDAOReadOnly::Exception::DatabaseError,
69                    "Failure during removing property");
70     }
71 }
72
73 void SetProperty(DbWidgetHandle widgetHandle,
74                  const PropertyDAOReadOnly::WidgetPropertyKey &key,
75                  const PropertyDAOReadOnly::WidgetPropertyValue &value,
76                  bool readOnly)
77 {
78     LogDebug("Setting/updating Property. Handle: " << widgetHandle <<
79              ", key: " << key);
80     Try {
81         using namespace DPL::DB::ORM;
82         using namespace DPL::DB::ORM::wrt;
83         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
84
85         DPL::OptionalInt readonly = PropertyDAOReadOnly::CheckPropertyReadFlag(
86                 widgetHandle,
87                 key);
88         if (!readonly.IsNull() && *readonly == 1) {
89             LogError("'" << key <<
90                      "' key is readonly. Cannot remove property !");
91             ThrowMsg(PropertyDAOReadOnly::Exception::ReadOnlyProperty,
92                      "Property is readonly");
93         }
94
95         if (readonly.IsNull()) {
96             WidgetPreference::Row row;
97             row.Set_app_id(widgetHandle);
98             row.Set_key_name(key);
99             row.Set_key_value(value);
100             row.Set_readonly(readOnly ? 1 : 0);
101
102             WRT_DB_INSERT(insert, WidgetPreference, &WrtDatabase::interface())
103             insert->Values(row);
104             insert->Execute();
105         } else {
106             WidgetPreference::Row row;
107             row.Set_key_value(value);
108
109             WRT_DB_UPDATE(update, WidgetPreference, &WrtDatabase::interface())
110             update->Where(And(
111                               Equals<WidgetPreference::app_id>(widgetHandle),
112                               Equals<WidgetPreference::key_name>(key)));
113
114             update->Values(row);
115             update->Execute();
116         }
117         transaction.Commit();
118     }
119     Catch(DPL::DB::SqlConnection::Exception::Base){
120         ReThrowMsg(PropertyDAOReadOnly::Exception::DatabaseError,
121                    "Failure during setting/updating property");
122     }
123 }
124
125 void RegisterProperties(DbWidgetHandle widgetHandle,
126                         const WidgetRegisterInfo &regInfo)
127 {
128     LogDebug("Registering proferences for widget. Handle: " << widgetHandle);
129
130     // Try-Catch in WidgetDAO
131
132     using namespace DPL::DB::ORM;
133     using namespace DPL::DB::ORM::wrt;
134     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
135
136     FOREACH(it, widgetConfigurationInfo.preferencesList)
137     {
138         { // Insert into table Widget Preferences
139             WidgetPreference::Row row;
140             row.Set_app_id(widgetHandle);
141             row.Set_key_name(it->name);
142             row.Set_key_value(it->value);
143             int readonly = true == it->readonly ? 1 : 0;
144             row.Set_readonly(readonly);
145
146             WRT_DB_INSERT(insert, WidgetPreference, &WrtDatabase::interface())
147             insert->Values(row);
148             insert->Execute();
149         }
150     }
151 }
152
153 } // namespace PropertyDAO
154 } // namespace WrtDB