Update wrt-commons_0.2.53
[framework/web/wrt-commons.git] / modules / widget_dao / dao / plugin_dao.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  * @file   plugin_dao.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @author  Grzegorz Krawczyk (g.krawczyk@samsung.com)
20  * @version 1.0
21  * @brief   This file contains the definition of plugin dao class.
22  */
23
24 #include <dpl/wrt-dao-rw/plugin_dao.h>
25 #include <dpl/log/log.h>
26 #include <dpl/foreach.h>
27 #include <orm_generator_wrt.h>
28 #include <dpl/wrt-dao-ro/webruntime_database.h>
29 #include <dpl/wrt-dao-ro/WrtDatabase.h>
30
31 namespace WrtDB {
32
33 PluginDAO::PluginDAO(DbPluginHandle pluginHandle) :
34     PluginDAOReadOnly(pluginHandle)
35 {
36 }
37
38 PluginDAO::PluginDAO(const std::string &libraryName) :
39     PluginDAOReadOnly(libraryName)
40 {
41 }
42
43 DbPluginHandle PluginDAO::registerPlugin(const PluginMetafileData& metafile,
44                                        const std::string& pluginPath)
45 {
46     LogDebug("Registering plugin. Path: " << pluginPath);
47
48     Try {
49         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
50         DbPluginHandle handle;
51
52         if (isPluginInstalled(metafile.m_libraryName)) {
53             handle = PluginDAO(metafile.m_libraryName).getPluginHandle();
54             LogInfo(" >> Library " << metafile.m_libraryName <<
55                     " is already registered. Handle: " << handle);
56         } else {
57             LogDebug("Register Plugin: " << metafile.m_libraryName);
58
59             using namespace DPL::DB::ORM;
60             using namespace DPL::DB::ORM::wrt;
61
62             typedef PluginProperties::Row PluginPropertiesRow;
63
64             PluginPropertiesRow row;
65             row.Set_PluginLibraryName(
66                 DPL::FromUTF8String(metafile.m_libraryName));
67             row.Set_InstallationState(INSTALLATION_IN_PROGRESS);
68             row.Set_PluginLibraryPath(
69                 DPL::FromUTF8String(pluginPath));
70
71             WRT_DB_INSERT(insert, PluginProperties, &WrtDatabase::interface())
72             insert->Values(row);
73             handle = insert->Execute();
74             LogDebug(" >> Plugin Registered. Handle: " << handle);
75         }
76         transaction.Commit();
77         return handle;
78     }
79     Catch(DPL::DB::SqlConnection::Exception::Base)
80     {
81         ReThrowMsg(PluginDAO::Exception::DatabaseError,
82                    "Failed in RegisterPlugin");
83     }
84 }
85
86 void PluginDAO::registerPluginImplementedObject(const std::string& objectName,
87         DbPluginHandle pluginHandle)
88 {
89     LogDebug("Registering plugin object: " << objectName);
90
91     Try {
92         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
93
94         LogDebug("Register Object: " << objectName);
95
96         using namespace DPL::DB::ORM;
97         using namespace DPL::DB::ORM::wrt;
98
99         typedef PluginImplementedObjects::Row PluginObjectsRow;
100
101         PluginObjectsRow row;
102         row.Set_PluginObject(DPL::FromUTF8String(objectName));
103         row.Set_PluginPropertiesId(pluginHandle);
104
105         WRT_DB_INSERT(insert, PluginImplementedObjects, &WrtDatabase::interface())
106         insert->Values(row);
107         insert->Execute();
108         transaction.Commit();
109     }
110     Catch(DPL::DB::SqlConnection::Exception::Base)
111     {
112         ReThrowMsg(PluginDAO::Exception::DatabaseError,
113                    "Failed in RegisterPluginObject");
114     }
115 }
116
117 void PluginDAO::registerPluginRequiredObject(const std::string& objectName,
118         DbPluginHandle pluginHandle)
119 {
120     LogDebug("Registering plugin object: " << objectName);
121
122     Try {
123         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
124
125         LogDebug("Register Object: " << objectName);
126
127         using namespace DPL::DB::ORM;
128         using namespace DPL::DB::ORM::wrt;
129
130         typedef PluginRequiredObjects::Row PluginObjectsRow;
131
132         PluginObjectsRow row;
133         row.Set_PluginPropertiesId(pluginHandle);
134         row.Set_PluginObject(DPL::FromUTF8String(objectName));
135
136         WRT_DB_INSERT(insert, PluginRequiredObjects, &WrtDatabase::interface())
137         insert->Values(row);
138         insert->Execute();
139         transaction.Commit();
140     }
141     Catch(DPL::DB::SqlConnection::Exception::Base)
142     {
143         ReThrowMsg(PluginDAO::Exception::DatabaseError,
144                    "Failed in RegisterPluginObject");
145     }
146 }
147
148 void PluginDAO::registerPluginLibrariesDependencies(
149         DbPluginHandle pluginHandle,
150         const PluginHandleSetPtr& dependencies)
151 {
152     LogDebug("Registering plugin library dependencies: " << pluginHandle);
153
154     Try {
155         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
156
157         using namespace DPL::DB::ORM;
158         using namespace DPL::DB::ORM::wrt;
159
160         typedef PluginDependencies::Row PluginDependeciesRow;
161         PluginDependeciesRow row;
162
163         FOREACH(it, *dependencies)
164         {
165             row.Set_PluginPropertiesId(pluginHandle);
166             row.Set_RequiredPluginPropertiesId(*it);
167
168             WRT_DB_INSERT(insert, PluginDependencies, &WrtDatabase::interface())
169             insert->Values(row);
170             insert->Execute();
171             transaction.Commit();
172         }
173     }
174     Catch(DPL::DB::SqlConnection::Exception::Base)
175     {
176         ReThrowMsg(PluginDAO::Exception::DatabaseError,
177                    "Failed in RegisterPluginObject");
178     }
179 }
180
181 void PluginDAO::setPluginInstallationStatus(DbPluginHandle pluginHandle,
182                                             PluginInstallationState state)
183 {
184     Try {
185         LogDebug(
186             "Set installation state: " << state << " handle " << pluginHandle);
187
188         using namespace DPL::DB::ORM;
189         using namespace DPL::DB::ORM::wrt;
190         ScopedTransaction transaction(&WrtDatabase::interface());
191
192         typedef wrt::PluginProperties::Row PluginPropertiesRow;
193
194         PluginPropertiesRow row;
195         row.Set_InstallationState(state);
196
197         WRT_DB_UPDATE(update, PluginProperties, &WrtDatabase::interface())
198         update->Where(
199             Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
200
201         update->Values(row);
202         update->Execute();
203         transaction.Commit();
204     }
205     Catch(DPL::DB::SqlConnection::Exception::Base)
206     {
207         ReThrowMsg(PluginDAO::Exception::DatabaseError,
208                    "Failed in RegisterLibraryDependencies");
209     }
210 }
211
212 } // namespace WrtDB