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