tizen 2.4 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 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_decision =
52             static_cast<HandlerState>(row.front().Get_user_allowed());
53     }
54     return handler;
55 }
56 } // namespace
57
58 CustomHandlerDAOReadOnly::CustomHandlerDAOReadOnly(const DPL::String& pkgName)
59     :
60     m_pkgName(pkgName)
61 {}
62
63 CustomHandlerDAOReadOnly::~CustomHandlerDAOReadOnly()
64 {}
65
66 CustomHandlerPtr CustomHandlerDAOReadOnly::getProtocolHandler(
67     const DPL::String& protocol,
68     const DPL::String& url)
69 {
70     WrtLogD("Getting protocol handler");
71     Try {
72         CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
73
74         select->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
75                           And(Equals<ProtocolHandlers::target>(protocol),
76                               Equals<ProtocolHandlers::url>(url))));
77
78         std::list<ProtocolHandlers::Row> list = select->GetRowList();
79         return getSingleHandler(list);
80     } Catch(DPL::DB::SqlConnection::Exception::Base) {
81         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
82                    "Failed to get protocol handler");
83     }
84 }
85
86 CustomHandlerPtr CustomHandlerDAOReadOnly::getContentHandler(
87     const DPL::String& content,
88     const DPL::String& url)
89 {
90     WrtLogD("Getting content handler");
91     Try {
92         CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers);
93
94         select->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
95                           And(Equals<ContentHandlers::target>(content),
96                               Equals<ContentHandlers::url>(url))));
97
98         std::list<ContentHandlers::Row> list = select->GetRowList();
99         return getSingleHandler(list);
100     } Catch(DPL::DB::SqlConnection::Exception::Base) {
101         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
102                    "Failed to get content handler");
103     }
104 }
105
106 CustomHandlerPtr CustomHandlerDAOReadOnly::getActivProtocolHandler(
107     const DPL::String& protocol)
108 {
109     WrtLogD("Getting active protocol handler");
110     Try {
111         CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
112
113         select->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
114                           Equals<ProtocolHandlers::target>(protocol)));
115
116         std::list<ProtocolHandlers::Row> list = select->GetRowList();
117         CustomHandlerPtr handler;
118
119         FOREACH(it, list) {
120             if (it->Get_user_allowed() & Agreed) {
121                 handler.reset(new CustomHandler());
122                 handler->base_url = it->Get_base_url();
123                 handler->target = it->Get_target();
124                 handler->title = it->Get_title();
125                 handler->url = it->Get_url();
126                 handler->user_decision =
127                     static_cast<HandlerState>(it->Get_user_allowed());
128             }
129         }
130         return handler;
131     } Catch(DPL::DB::SqlConnection::Exception::Base) {
132         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
133                    "Failed to get protocol handler");
134     }
135 }
136
137 CustomHandlerPtr CustomHandlerDAOReadOnly::getProtocolHandler(
138     const DPL::String& protocol,
139     const DPL::String& url,
140     const DPL::String& baseURL)
141 {
142     WrtLogD("Check if protocol is registered");
143     Try {
144         CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers);
145
146         select->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
147                           And(Equals<ProtocolHandlers::target>(protocol),
148                               And(Equals<ProtocolHandlers::url>(url),
149                                   Equals<ProtocolHandlers::base_url>(baseURL)
150                                   )
151                               )
152                           )
153                       );
154
155         std::list<ProtocolHandlers::Row> list = select->GetRowList();
156         return getSingleHandler(list);
157     } Catch(DPL::DB::SqlConnection::Exception::Base) {
158         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
159                    "Failed to get content handler");
160     }
161 }
162
163 CustomHandlerPtr CustomHandlerDAOReadOnly::getActivContentHandler(
164     const DPL::String& content)
165 {
166     WrtLogD("Getting active content handler");
167     Try {
168         CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers);
169
170         select->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
171                           Equals<ContentHandlers::target>(content)));
172
173         std::list<ContentHandlers::Row> list = select->GetRowList();
174         CustomHandlerPtr handler;
175
176         FOREACH(it, list) {
177             if (it->Get_user_allowed() & Agreed) {
178                 handler.reset(new CustomHandler());
179                 handler->base_url = it->Get_base_url();
180                 handler->target = it->Get_target();
181                 handler->title = it->Get_title();
182                 handler->url = it->Get_url();
183                 handler->user_decision =
184                     static_cast<HandlerState>(it->Get_user_allowed());
185             }
186         }
187         return handler;
188     } Catch(DPL::DB::SqlConnection::Exception::Base) {
189         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
190                    "Failed to get protocol handler");
191     }
192 }
193
194 CustomHandlerPtr CustomHandlerDAOReadOnly::getContentHandler(
195     const DPL::String& content,
196     const DPL::String& url,
197     const DPL::String& baseURL)
198 {
199     WrtLogD("Check if content is registered");
200     Try {
201         CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers);
202
203         select->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
204                           And(Equals<ContentHandlers::target>(content),
205                               And(Equals<ContentHandlers::url>(url),
206                                   Equals<ContentHandlers::base_url>(baseURL)))));
207
208         std::list<ContentHandlers::Row> list = select->GetRowList();
209         return getSingleHandler(list);
210     } Catch(DPL::DB::SqlConnection::Exception::Base) {
211         ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
212                    "Failed to get content handler");
213     }
214 }
215 } // namespace CustomHandlerDB