tizen 2.3.1 release
[framework/web/wearable/wrt-commons.git] / modules / custom_handler_dao / dao / custom_handler_dao.cpp
1 /*
2  * Copyright (c) 2012 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    custom_handler_dao.cpp
18  * @author  Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version 1.0
20  * @brief    This file contains the definition of custom handler dao class.
21  */
22
23 #include <wrt-commons/custom-handler-dao-rw/custom_handler_dao.h>
24 #include <wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h>
25 #include <orm_generator_custom_handler.h>
26 #include <wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h>
27
28 using namespace DPL::DB::ORM;
29 using namespace DPL::DB::ORM::custom_handler;
30
31 namespace CustomHandlerDB {
32 namespace {
33 template <typename T>
34 void fillRow(T& row, const CustomHandler& handler, const DPL::String& pkgName)
35 {
36     row.Set_app_id(pkgName);
37     row.Set_target(handler.target);
38     row.Set_base_url(handler.base_url);
39     row.Set_url(handler.url);
40     row.Set_title(handler.title);
41     row.Set_user_allowed(handler.user_decision);
42 }
43 } // namespace
44
45 CustomHandlerDAO::CustomHandlerDAO(const DPL::String& pkgName) :
46     CustomHandlerDAOReadOnly(pkgName)
47 {}
48
49 CustomHandlerDAO::~CustomHandlerDAO()
50 {}
51
52 void CustomHandlerDAO::registerContentHandler(const CustomHandler& handler)
53 {
54     LogDebug("Registering content handler " << handler.target << " " <<
55              handler.base_url);
56     Try {
57         if (handler.user_decision & Agreed) {
58             //need to disable all previous, agreed entries
59             CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers);
60             select->Where(And(Equals<ContentHandlers::target>(handler.target),
61                               Or(Equals<ContentHandlers::user_allowed>(Agreed),
62                                  Equals<ContentHandlers::user_allowed>(
63                                      AgreedPermanently))
64                               ));
65             ContentHandlers::Select::RowList rows = select->GetRowList();
66             if (rows.size() > 1) {
67                 //more than one activ content handler - not good. Remove all.
68                 //this should never happen
69                 LogError("Database data incoherent.");
70                 CUSTOM_HANDLER_DB_DELETE(deleteContent, ContentHandlers);
71                 deleteContent->Where(And(Equals<ContentHandlers::target>(
72                                              handler.target),
73                                          Or(Equals<ContentHandlers::
74                                                        user_allowed>(Agreed),
75                                             Equals<ContentHandlers::
76                                                        user_allowed>(
77                                                 AgreedPermanently))));
78                 deleteContent->Execute();
79                 //all content handlers removed. New one can be inserted
80             } else if (!rows.empty()) {
81                 //one active handler. Can be updaed
82                 LogDebug("Activ content handler exist. Update");
83                 CUSTOM_HANDLER_DB_UPDATE(update, ContentHandlers);
84                 update->Where(And(Equals<ContentHandlers::target>(handler.
85                                                                       target),
86                                   Or(Equals<ContentHandlers::user_allowed>(
87                                          Agreed),
88                                      Equals<ContentHandlers::user_allowed>(
89                                          AgreedPermanently))
90                                   ));
91                 ContentHandlers::Row rowToUpdate = rows.front();
92
93                 if (handler.user_decision & DecisionSaved) {
94                     //do not ask about previous one
95                     rowToUpdate.Set_user_allowed(DeclinedPermanently);
96                 } else {
97                     //ask for next time
98                     rowToUpdate.Set_user_allowed(Declined);
99                 }
100                 update->Values(rowToUpdate);
101                 update->Execute();
102             }
103         }
104         LogDebug("Inserting new content handler");
105         ContentHandlers::Row row;
106         fillRow(row, handler, m_pkgName);
107         if (getContentHandler(handler.target, handler.url, handler.base_url)) {
108             LogDebug("Content handler exist. Update its state");
109             CUSTOM_HANDLER_DB_UPDATE(updateRow, ContentHandlers);
110             updateRow->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
111                                  And(Equals<ContentHandlers::target>(handler.
112                                                                          target),
113                                      And(Equals<ContentHandlers::url>(handler.
114                                                                           url),
115                                          Equals<ContentHandlers::base_url>(
116                                              handler.base_url)))));
117             updateRow->Values(row);
118             updateRow->Execute();
119             LogDebug("updated");
120             return;
121         }
122         CUSTOM_HANDLER_DB_INSERT(insert, ContentHandlers);
123         insert->Values(row);
124         insert->Execute();
125         LogDebug("insterted");
126     }
127     Catch(DPL::DB::SqlConnection::Exception::Base){
128         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
129                    "Failed to register custom handler");
130     }
131 }
132
133 void CustomHandlerDAO::registerProtocolHandler(const CustomHandler& handler)
134 {
135     LogDebug("Registering protocol handler " << handler.target << " " <<
136              handler.base_url);
137     Try {
138         if (handler.user_decision & Agreed) {
139             //need to disable all previous, agreed entries
140             CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
141             select->Where(And(Equals<ProtocolHandlers::target>(handler.target),
142                               Or(Equals<ProtocolHandlers::user_allowed>(Agreed),
143                                  Equals<ProtocolHandlers::user_allowed>(
144                                      AgreedPermanently))
145                               ));
146             ProtocolHandlers::Select::RowList rows = select->GetRowList();
147             if (rows.size() > 1) {
148                 //more than one activ protocol handler - not good. Remove all.
149                 //this should never happen
150                 LogError("Database data incoherent.");
151                 CUSTOM_HANDLER_DB_DELETE(deleteProtocol, ProtocolHandlers);
152                 deleteProtocol->Where(And(Equals<ProtocolHandlers::target>(
153                                               handler.target),
154                                           Or(Equals<ProtocolHandlers::
155                                                         user_allowed>(Agreed),
156                                              Equals<ProtocolHandlers::
157                                                         user_allowed>(
158                                                  AgreedPermanently))));
159                 deleteProtocol->Execute();
160                 //all protocol handlers removed. New one can be inserted
161             } else if (!rows.empty()) {
162                 //one active handler. Can be updaed
163                 CUSTOM_HANDLER_DB_UPDATE(update, ProtocolHandlers);
164                 update->Where(And(Equals<ProtocolHandlers::target>(handler.
165                                                                        target),
166                                   Or(Equals<ProtocolHandlers::user_allowed>(
167                                          Agreed),
168                                      Equals<ProtocolHandlers::user_allowed>(
169                                          AgreedPermanently))
170                                   ));
171                 ProtocolHandlers::Row rowToUpdate = rows.front();
172
173                 if (handler.user_decision & DecisionSaved) {
174                     //do not ask about previous one
175                     rowToUpdate.Set_user_allowed(DeclinedPermanently);
176                 } else {
177                     //ask for next time
178                     rowToUpdate.Set_user_allowed(Declined);
179                 }
180                 update->Values(rowToUpdate);
181                 update->Execute();
182             }
183         }
184         LogDebug("Inserting new protocol handler");
185         ProtocolHandlers::Row row;
186         fillRow(row, handler, m_pkgName);
187         if (getProtocolHandler(handler.target, handler.url,
188                                handler.base_url))
189         {
190             LogDebug("Protocol handler exist. Update its state");
191             CUSTOM_HANDLER_DB_UPDATE(updateRow, ProtocolHandlers);
192             updateRow->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
193                                  And(Equals<ProtocolHandlers::target>(handler.
194                                                                           target),
195                                      And(Equals<ProtocolHandlers::url>(handler.
196                                                                            url),
197                                          Equals<ProtocolHandlers::base_url>(
198                                              handler.base_url)))));
199             updateRow->Values(row);
200             updateRow->Execute();
201             LogDebug("updated");
202             return;
203         }
204         CUSTOM_HANDLER_DB_INSERT(insert, ProtocolHandlers);
205         insert->Values(row);
206         insert->Execute();
207         LogDebug("insterted");
208     }
209     Catch(DPL::DB::SqlConnection::Exception::Base){
210         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
211                    "Failed to register custom handler");
212     }
213 }
214
215 void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target,
216                                                 const DPL::String& url)
217 {
218     LogDebug("Removing content handler " << target << " " << url);
219     Try {
220         CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers);
221         deleteFrom->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
222                               And(Equals<ContentHandlers::target>(target),
223                                   Equals<ContentHandlers::url>(url))));
224         deleteFrom->Execute();
225     }
226     Catch(DPL::DB::SqlConnection::Exception::Base) {
227         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
228                    "Failed to remove content handler");
229     }
230 }
231
232 void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target,
233                                                  const DPL::String& url)
234 {
235     LogDebug("Removing protocol handler " << target << " " << url);
236     Try {
237         CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers);
238         deleteFrom->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
239                               And(Equals<ProtocolHandlers::target>(target),
240                                   Equals<ProtocolHandlers::url>(url))));
241         deleteFrom->Execute();
242     }
243     Catch(DPL::DB::SqlConnection::Exception::Base) {
244         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
245                    "Failed to remove content handler");
246     }
247 }
248
249 void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target,
250                                                 const DPL::String& url,
251                                                 const DPL::String& baseURL)
252 {
253     LogDebug("Removing content handler " << target << " " << url);
254     Try {
255         CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers);
256         deleteFrom->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
257                               And(Equals<ContentHandlers::target>(target),
258                                   And(Equals<ContentHandlers::url>(url),
259                                       Equals<ContentHandlers::base_url>(baseURL)))));
260         deleteFrom->Execute();
261     }
262     Catch(DPL::DB::SqlConnection::Exception::Base) {
263         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
264                    "Failed to remove content handler");
265     }
266 }
267
268 void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target,
269                                                  const DPL::String& url,
270                                                  const DPL::String& baseURL)
271 {
272     LogDebug("Removing protocol handler " << target << " " << url);
273     Try {
274         CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers);
275         deleteFrom->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
276                               And(Equals<ProtocolHandlers::target>(target),
277                                   And(Equals<ProtocolHandlers::url>(url),
278                                       Equals<ProtocolHandlers::base_url>(
279                                           baseURL)))));
280         deleteFrom->Execute();
281     }
282     Catch(DPL::DB::SqlConnection::Exception::Base) {
283         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
284                    "Failed to remove content handler");
285     }
286 }
287
288 void CustomHandlerDAO::removeWidgetProtocolHandlers()
289 {
290     LogDebug("ente");
291     Try {
292         CUSTOM_HANDLER_DB_DELETE(deleteProtocol, ProtocolHandlers);
293         deleteProtocol->Where(Equals<ProtocolHandlers::app_id>(m_pkgName));
294         deleteProtocol->Execute();
295     } Catch(DPL::DB::SqlConnection::Exception::Base) {
296         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
297                    "Failed to remove widget protoc");
298     }
299 }
300
301 void CustomHandlerDAO::removeWidgetContentHandlers()
302 {
303     LogDebug("ente");
304     Try {
305         CUSTOM_HANDLER_DB_DELETE(deleteContent, ContentHandlers);
306         deleteContent->Where(Equals<ContentHandlers::app_id>(m_pkgName));
307         deleteContent->Execute();
308     } Catch(DPL::DB::SqlConnection::Exception::Base) {
309         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
310                    "Failed to remove widget entries");
311     }
312 }
313 } // namespace CustomHandlerDB