Tizen 2.0 Release
[framework/web/wrt-commons.git] / modules / custom_handler_dao / dao / custom_handler_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  * This file contains the declaration of custom handler dao class.
18  *
19  * @file    custom_handler_dao_read_only.cpp
20  * @author  Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
21  * @version 1.0
22  * @brief   This file contains the declaration of custom handler dao
23  */
24
25 #include <wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h>
26 #include <wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h>
27
28 #include <orm_generator_custom_handler.h>
29
30 using namespace DPL::DB::ORM;
31 using namespace DPL::DB::ORM::custom_handler;
32
33 namespace CustomHandlerDB {
34
35 namespace {
36
37 template <typename T>
38 CustomHandlerPtr getSingleHandler(std::list<T> row)
39 {
40     CustomHandlerPtr handler;
41     if (!row.empty()) {
42         // There should be only one
43         if (row.size() > 1) {
44             ThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
45                      "More than one handler registered");
46         }
47
48         handler.reset(new CustomHandler());
49         handler->target = row.front().Get_target();
50         handler->base_url = row.front().Get_base_url();
51         handler->url = row.front().Get_url();
52         handler->title = row.front().Get_title();
53         handler->user_allowed = static_cast<bool>(row.front().Get_user_allowed());
54         handler->user_decision =
55             static_cast<HandlerState>(row.front().Get_user_allowed());
56     }
57     return handler;
58 }
59
60 } // namespace
61
62 CustomHandlerDAOReadOnly::CustomHandlerDAOReadOnly(const DPL::String& pkgName) :
63         m_pkgName(pkgName)
64 {
65 }
66
67 CustomHandlerDAOReadOnly::~CustomHandlerDAOReadOnly()
68 {
69 }
70
71 CustomHandlerPtr CustomHandlerDAOReadOnly::getProtocolHandler(
72         const DPL::String& protocol,
73         const DPL::String& url)
74 {
75     LogDebug("Getting protocol handler");
76     Try{
77         CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
78
79         select->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
80                       And(Equals<ProtocolHandlers::target>(protocol),
81                           Equals<ProtocolHandlers::url>(url))));
82
83         std::list<ProtocolHandlers::Row> list = select->GetRowList();
84         return getSingleHandler(list);
85     } Catch(DPL::DB::SqlConnection::Exception::Base) {
86         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
87                    "Failed to get protocol handler");
88     }
89 }
90
91 CustomHandlerPtr CustomHandlerDAOReadOnly::getContentHandler(
92         const DPL::String& content,
93         const DPL::String& url)
94 {
95     LogDebug("Getting content handler");
96     Try{
97         CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers);
98
99         select->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
100                       And(Equals<ContentHandlers::target>(content),
101                           Equals<ContentHandlers::url>(url))));
102
103         std::list<ContentHandlers::Row> list = select->GetRowList();
104         return getSingleHandler(list);
105     } Catch(DPL::DB::SqlConnection::Exception::Base) {
106         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
107                    "Failed to get content handler");
108     }
109 }
110
111 CustomHandlerPtr CustomHandlerDAOReadOnly::getActivProtocolHandler(
112         const DPL::String& protocol)
113 {
114     LogDebug("Getting active protocol handler");
115     Try{
116         CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
117
118         select->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
119                           Equals<ProtocolHandlers::target>(protocol)));
120
121         std::list<ProtocolHandlers::Row> list = select->GetRowList();
122         CustomHandlerPtr handler;
123
124         FOREACH(it, list) {
125             if (it->Get_user_allowed() & Agreed) {
126                 handler.reset(new CustomHandler());
127                 handler->base_url = it->Get_base_url();
128                 handler->target = it->Get_target();
129                 handler->title = it->Get_title();
130                 handler->url = it->Get_url();
131                 handler->user_allowed =
132                     static_cast<bool>(it->Get_user_allowed());
133                 handler->user_decision =
134                     static_cast<HandlerState>(it->Get_user_allowed());
135             }
136         }
137         return handler;
138     } Catch(DPL::DB::SqlConnection::Exception::Base) {
139         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
140                    "Failed to get protocol handler");
141     }
142 }
143
144 CustomHandlerPtr CustomHandlerDAOReadOnly::getProtocolHandler(
145         const DPL::String& protocol,
146         const DPL::String& url,
147         const DPL::String& baseURL)
148 {
149     LogDebug("Check if protocol is registered");
150     Try{
151         CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
152
153         select->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
154                           And(Equals<ProtocolHandlers::target>(protocol),
155                               And(Equals<ProtocolHandlers::url>(url),
156                                   Equals<ProtocolHandlers::base_url>(baseURL)
157                               )
158                            )
159                        )
160                  );
161
162         std::list<ProtocolHandlers::Row> list = select->GetRowList();
163         return getSingleHandler(list);
164     } Catch(DPL::DB::SqlConnection::Exception::Base) {
165         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
166                    "Failed to get content handler");
167     }
168 }
169
170
171 CustomHandlerPtr CustomHandlerDAOReadOnly::getActivContentHandler(
172         const DPL::String& content)
173 {
174     LogDebug("Getting active content handler");
175     Try{
176         CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers);
177
178         select->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
179                           Equals<ContentHandlers::target>(content)));
180
181         std::list<ContentHandlers::Row> list = select->GetRowList();
182         CustomHandlerPtr handler;
183
184         FOREACH(it, list) {
185             if (it->Get_user_allowed() & Agreed) {
186                 handler.reset(new CustomHandler());
187                 handler->base_url = it->Get_base_url();
188                 handler->target = it->Get_target();
189                 handler->title = it->Get_title();
190                 handler->url = it->Get_url();
191                 handler->user_allowed =
192                     static_cast<bool>(it->Get_user_allowed());
193                 handler->user_decision =
194                     static_cast<HandlerState>(it->Get_user_allowed());
195             }
196         }
197         return handler;
198     } Catch(DPL::DB::SqlConnection::Exception::Base) {
199         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
200                    "Failed to get protocol handler");
201     }
202 }
203
204 CustomHandlerPtr CustomHandlerDAOReadOnly::getContentHandler(
205         const DPL::String& content,
206         const DPL::String& url,
207         const DPL::String& baseURL)
208 {
209     LogDebug("Check if content is registered");
210     Try{
211         CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers);
212
213         select->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
214                       And(Equals<ContentHandlers::target>(content),
215                       And(Equals<ContentHandlers::url>(url),
216                           Equals<ContentHandlers::base_url>(baseURL)))));
217
218         std::list<ContentHandlers::Row> list = select->GetRowList();
219         return getSingleHandler(list);
220     } Catch(DPL::DB::SqlConnection::Exception::Base) {
221         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
222                    "Failed to get content handler");
223     }
224 }
225
226 CustomHandlerPtr CustomHandlerDAOReadOnly::getAllowedProtocolHandler(
227         const DPL::String& protocol)
228 {
229     LogDebug("Getting allowed protocol handler");
230     Try{
231         CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
232
233         select->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
234                       And(Equals<ProtocolHandlers::target>(protocol),
235                           Equals<ProtocolHandlers::user_allowed>(true))));
236
237         std::list<ProtocolHandlers::Row> list = select->GetRowList();
238         return getSingleHandler(list);
239     } Catch(DPL::DB::SqlConnection::Exception::Base) {
240         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
241                    "Failed to get content handler");
242     }
243 }
244
245 CustomHandlerPtr CustomHandlerDAOReadOnly::getAllowedContentHandler(
246         const DPL::String& protocol)
247 {
248     LogDebug("Getting allowed content handler");
249     Try{
250         CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers)
251
252         select->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
253                       And(Equals<ContentHandlers::target>(protocol),
254                           Equals<ContentHandlers::user_allowed>(true))));
255
256         std::list<ContentHandlers::Row> list = select->GetRowList();
257         return getSingleHandler(list);
258     } Catch(DPL::DB::SqlConnection::Exception::Base) {
259         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
260                    "Failed to get content handler");
261     }
262 }
263
264 } // namespace CustomHandlerDB