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