cbab36690ecca2a2651cd880c3cdde0c4155554c
[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
27 using namespace DPL::DB::ORM;
28 using namespace DPL::DB::ORM::custom_handler;
29
30 namespace CustomHandlerDB {
31
32 namespace {
33
34 template <typename T>
35 void fillRow(T& row, const CustomHandler& handler, const DPL::String& pkgName)
36 {
37     row.Set_app_id(pkgName);
38     row.Set_target(handler.target);
39     row.Set_base_url(handler.base_url);
40     row.Set_url(handler.url);
41     row.Set_title(handler.title);
42     row.Set_user_allowed(handler.user_allowed);
43 }
44
45 } // namespace
46
47 CustomHandlerDAO::CustomHandlerDAO(const DPL::String& pkgName) :
48     CustomHandlerDAOReadOnly(pkgName)
49 {
50 }
51
52 CustomHandlerDAO::~CustomHandlerDAO()
53 {
54 }
55
56 void CustomHandlerDAO::registerContentHandler(const CustomHandler& handler)
57 {
58     LogDebug("Registering content handler " << handler.target << " " <<
59                 handler.base_url);
60     Try {
61         ContentHandlers::Row row;
62         fillRow(row, handler, m_pkgName);
63
64         // TODO remove previous if necessary
65         CUSTOM_HANDLER_DB_INSERT(insert, ContentHandlers);
66         insert->Values(row);
67         insert->Execute();
68     }
69     Catch(DPL::DB::SqlConnection::Exception::Base){
70         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
71                    "Failed to register custom handler");
72     }
73 }
74
75 void CustomHandlerDAO::registerProtocolHandler(const CustomHandler& handler)
76 {
77     LogDebug("Registering protocol handler " << handler.target << " " <<
78             handler.base_url);
79     Try {
80         ProtocolHandlers::Row row;
81         fillRow(row, handler, m_pkgName);
82
83         // TODO remove previous if necessary
84         CUSTOM_HANDLER_DB_INSERT(insert, ProtocolHandlers);
85         insert->Values(row);
86         insert->Execute();
87     }
88     Catch(DPL::DB::SqlConnection::Exception::Base){
89         ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
90                    "Failed to register custom handler");
91     }
92 }
93
94 void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target,
95                                                 const DPL::String& url)
96 {
97     LogDebug("Removing content handler " << target << " " << url);
98     Try {
99        CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers);
100        deleteFrom->Where(And(Equals<ContentHandlers::app_id>(m_pkgName),
101                          And(Equals<ContentHandlers::target>(target),
102                              Equals<ContentHandlers::url>(url))));
103        deleteFrom->Execute();
104     }
105     Catch(DPL::DB::SqlConnection::Exception::Base) {
106        ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
107                   "Failed to remove content handler");
108     }
109 }
110
111 void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target,
112                                                  const DPL::String& url)
113 {
114     LogDebug("Removing protocol handler " << target << " " << url);
115     Try {
116        CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers);
117        deleteFrom->Where(And(Equals<ProtocolHandlers::app_id>(m_pkgName),
118                          And(Equals<ProtocolHandlers::target>(target),
119                              Equals<ProtocolHandlers::url>(url))));
120        deleteFrom->Execute();
121     }
122     Catch(DPL::DB::SqlConnection::Exception::Base) {
123        ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
124                   "Failed to remove content handler");
125     }
126
127 }
128
129 } // namespace CustomHandlerDB