tizen 2.4 release
[framework/web/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     WrtLogD("Registering content handler %ls %ls",
55         handler.target.c_str(), handler.base_url.c_str());
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                 WrtLogE("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                 WrtLogD("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         WrtLogD("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             WrtLogD("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             WrtLogD("updated");
120             return;
121         }
122         CUSTOM_HANDLER_DB_INSERT(insert, ContentHandlers);
123         insert->Values(row);
124         insert->Execute();
125         WrtLogD("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     WrtLogD("Registering protocol handler %ls %ls", handler.target.c_str(), handler.base_url.c_str());
136     Try {
137         if (handler.user_decision & Agreed) {
138             //need to disable all previous, agreed entries
139             CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
140             select->Where(And(Equals<ProtocolHandlers::target>(handler.target),
141                               Or(Equals<ProtocolHandlers::user_allowed>(Agreed),
142                                  Equals<ProtocolHandlers::user_allowed>(
143                                      AgreedPermanently))
144                               ));
145             ProtocolHandlers::Select::RowList rows = select->GetRowList();
146             if (rows.size() > 1) {
147                 //more than one activ protocol handler - not good. Remove all.
148                 //this should never happen
149                 WrtLogE("Database data incoherent.");
150                 CUSTOM_HANDLER_DB_DELETE(deleteProtocol, ProtocolHandlers);
151                 deleteProtocol->Where(And(Equals<ProtocolHandlers::target>(
152                                               handler.target),
153                                           Or(Equals<ProtocolHandlers::
154                                                         user_allowed>(Agreed),
155                                              Equals<ProtocolHandlers::
156                                                         user_allowed>(
157                                                  AgreedPermanently))));
158                 deleteProtocol->Execute();
159                 //all protocol handlers removed. New one can be inserted
160             } else if (!rows.empty()) {
161                 //one active handler. Can be updaed
162                 CUSTOM_HANDLER_DB_UPDATE(update, ProtocolHandlers);
163                 update->Where(And(Equals<ProtocolHandlers::target>(handler.
164                                                                        target),
165                                   Or(Equals<ProtocolHandlers::user_allowed>(
166                                          Agreed),
167                                      Equals<ProtocolHandlers::user_allowed>(
168                                          AgreedPermanently))
169                                   ));
170                 ProtocolHandlers::Row rowToUpdate = rows.front();
171
172                 if (handler.user_decision & DecisionSaved) {
173                     //do not ask about previous one
174                     rowToUpdate.Set_user_allowed(DeclinedPermanently);
175                 } else {
176                     //ask for next time
177                     rowToUpdate.Set_user_allowed(Declined);
178                 }
179                 update->Values(rowToUpdate);
180                 update->Execute();
181             }
182         }
183         WrtLogD("Inserting new protocol handler");
184         ProtocolHandlers::Row row;
185         fillRow(row, handler, m_pkgName);
186         if (getProtocolHandler(handler.target, handler.url,
187                                handler.base_url))
188         {
189             WrtLogD("Protocol handler exist. Update its state");
190             CUSTOM_HANDLER_DB_UPDATE(updateRow, ProtocolHandlers);
191             updateRow->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
192                                  And(Equals<ProtocolHandlers::target>(handler.
193                                                                           target),
194                                      And(Equals<ProtocolHandlers::url>(handler.
195                                                                            url),
196                                          Equals<ProtocolHandlers::base_url>(
197                                              handler.base_url)))));
198             updateRow->Values(row);
199             updateRow->Execute();
200             WrtLogD("updated");
201             return;
202         }
203         CUSTOM_HANDLER_DB_INSERT(insert, ProtocolHandlers);
204         insert->Values(row);
205         insert->Execute();
206         WrtLogD("insterted");
207     }
208     Catch(DPL::DB::SqlConnection::Exception::Base){
209         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
210                    "Failed to register custom handler");
211     }
212 }
213
214 void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target,
215                                                 const DPL::String& url)
216 {
217     WrtLogD("Removing content handler %ls %ls", target.c_str(), url.c_str());
218     Try {
219         CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers);
220         deleteFrom->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
221                               And(Equals<ContentHandlers::target>(target),
222                                   Equals<ContentHandlers::url>(url))));
223         deleteFrom->Execute();
224     }
225     Catch(DPL::DB::SqlConnection::Exception::Base) {
226         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
227                    "Failed to remove content handler");
228     }
229 }
230
231 void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target,
232                                                  const DPL::String& url)
233 {
234     WrtLogD("Removing protocol handler %ls %ls", target.c_str(), url.c_str());
235     Try {
236         CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers);
237         deleteFrom->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
238                               And(Equals<ProtocolHandlers::target>(target),
239                                   Equals<ProtocolHandlers::url>(url))));
240         deleteFrom->Execute();
241     }
242     Catch(DPL::DB::SqlConnection::Exception::Base) {
243         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
244                    "Failed to remove content handler");
245     }
246 }
247
248 void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target,
249                                                 const DPL::String& url,
250                                                 const DPL::String& baseURL)
251 {
252     WrtLogD("Removing content handler %ls %ls", target.c_str(), url.c_str());
253     Try {
254         CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers);
255         deleteFrom->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
256                               And(Equals<ContentHandlers::target>(target),
257                                   And(Equals<ContentHandlers::url>(url),
258                                       Equals<ContentHandlers::base_url>(baseURL)))));
259         deleteFrom->Execute();
260     }
261     Catch(DPL::DB::SqlConnection::Exception::Base) {
262         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
263                    "Failed to remove content handler");
264     }
265 }
266
267 void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target,
268                                                  const DPL::String& url,
269                                                  const DPL::String& baseURL)
270 {
271     WrtLogD("Removing protocol handler %ls %ls", target.c_str(), url.c_str());
272     Try {
273         CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers);
274         deleteFrom->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
275                               And(Equals<ProtocolHandlers::target>(target),
276                                   And(Equals<ProtocolHandlers::url>(url),
277                                       Equals<ProtocolHandlers::base_url>(
278                                           baseURL)))));
279         deleteFrom->Execute();
280     }
281     Catch(DPL::DB::SqlConnection::Exception::Base) {
282         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
283                    "Failed to remove content handler");
284     }
285 }
286
287 void CustomHandlerDAO::removeWidgetProtocolHandlers()
288 {
289     WrtLogD("enter");
290     Try {
291         CUSTOM_HANDLER_DB_DELETE(deleteProtocol, ProtocolHandlers);
292         deleteProtocol->Where(Equals<ProtocolHandlers::app_id>(m_pkgName));
293         deleteProtocol->Execute();
294     } Catch(DPL::DB::SqlConnection::Exception::Base) {
295         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
296                    "Failed to remove widget protoc");
297     }
298 }
299
300 void CustomHandlerDAO::removeWidgetContentHandlers()
301 {
302     WrtLogD("enter");
303     Try {
304         CUSTOM_HANDLER_DB_DELETE(deleteContent, ContentHandlers);
305         deleteContent->Where(Equals<ContentHandlers::app_id>(m_pkgName));
306         deleteContent->Execute();
307     } Catch(DPL::DB::SqlConnection::Exception::Base) {
308         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
309                    "Failed to remove widget entries");
310     }
311 }
312 } // namespace CustomHandlerDB