tizen beta release
[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             row.Set_InstallURI(
71                 DPL::FromUTF8String(metafile.m_featuresInstallURI));
72             row.Set_KeyCN(
73                 DPL::FromUTF8String(metafile.m_featuresKeyCN));
74             row.Set_RootKeyCN(
75                 DPL::FromUTF8String(metafile.m_featuresRootCN));
76             row.Set_RootKeyFingerprint(
77                 DPL::FromUTF8String(metafile.m_featuresRootFingerprint));
78
79             WRT_DB_INSERT(insert, PluginProperties, &WrtDatabase::interface())
80             insert->Values(row);
81             handle = insert->Execute();
82             LogDebug(" >> Plugin Registered. Handle: " << handle);
83         }
84         transaction.Commit();
85         return handle;
86     }
87     Catch(DPL::DB::SqlConnection::Exception::Base)
88     {
89         ReThrowMsg(PluginDAO::Exception::DatabaseError,
90                    "Failed in RegisterPlugin");
91     }
92 }
93
94 void PluginDAO::registerPluginImplementedObject(const std::string& objectName,
95         DbPluginHandle pluginHandle)
96 {
97     LogDebug("Registering plugin object: " << objectName);
98
99     Try {
100         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
101
102         LogDebug("Register Object: " << objectName);
103
104         using namespace DPL::DB::ORM;
105         using namespace DPL::DB::ORM::wrt;
106
107         typedef PluginImplementedObjects::Row PluginObjectsRow;
108
109         PluginObjectsRow row;
110         row.Set_PluginObject(DPL::FromUTF8String(objectName));
111         row.Set_PluginPropertiesId(pluginHandle);
112
113         WRT_DB_INSERT(insert, PluginImplementedObjects, &WrtDatabase::interface())
114         insert->Values(row);
115         insert->Execute();
116         transaction.Commit();
117     }
118     Catch(DPL::DB::SqlConnection::Exception::Base)
119     {
120         ReThrowMsg(PluginDAO::Exception::DatabaseError,
121                    "Failed in RegisterPluginObject");
122     }
123 }
124
125 void PluginDAO::registerPluginRequiredObject(const std::string& objectName,
126         DbPluginHandle pluginHandle)
127 {
128     LogDebug("Registering plugin object: " << objectName);
129
130     Try {
131         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
132
133         LogDebug("Register Object: " << objectName);
134
135         using namespace DPL::DB::ORM;
136         using namespace DPL::DB::ORM::wrt;
137
138         typedef PluginRequiredObjects::Row PluginObjectsRow;
139
140         PluginObjectsRow row;
141         row.Set_PluginPropertiesId(pluginHandle);
142         row.Set_PluginObject(DPL::FromUTF8String(objectName));
143
144         WRT_DB_INSERT(insert, PluginRequiredObjects, &WrtDatabase::interface())
145         insert->Values(row);
146         insert->Execute();
147         transaction.Commit();
148     }
149     Catch(DPL::DB::SqlConnection::Exception::Base)
150     {
151         ReThrowMsg(PluginDAO::Exception::DatabaseError,
152                    "Failed in RegisterPluginObject");
153     }
154 }
155
156 void PluginDAO::registerPluginLibrariesDependencies(
157         DbPluginHandle pluginHandle,
158         const PluginHandleSetPtr& dependencies)
159 {
160     LogDebug("Registering plugin library dependencies: " << pluginHandle);
161
162     Try {
163         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
164
165         using namespace DPL::DB::ORM;
166         using namespace DPL::DB::ORM::wrt;
167
168         typedef PluginDependencies::Row PluginDependeciesRow;
169         PluginDependeciesRow row;
170
171         FOREACH(it, *dependencies)
172         {
173             row.Set_PluginPropertiesId(pluginHandle);
174             row.Set_RequiredPluginPropertiesId(*it);
175
176             WRT_DB_INSERT(insert, PluginDependencies, &WrtDatabase::interface())
177             insert->Values(row);
178             insert->Execute();
179             transaction.Commit();
180         }
181     }
182     Catch(DPL::DB::SqlConnection::Exception::Base)
183     {
184         ReThrowMsg(PluginDAO::Exception::DatabaseError,
185                    "Failed in RegisterPluginObject");
186     }
187 }
188
189 void PluginDAO::setPluginInstallationStatus(DbPluginHandle pluginHandle,
190                                             PluginInstallationState state)
191 {
192     Try {
193         LogDebug(
194             "Set installation state: " << state << " handle " << pluginHandle);
195
196         using namespace DPL::DB::ORM;
197         using namespace DPL::DB::ORM::wrt;
198         ScopedTransaction transaction(&WrtDatabase::interface());
199
200         typedef wrt::PluginProperties::Row PluginPropertiesRow;
201
202         PluginPropertiesRow row;
203         row.Set_InstallationState(state);
204
205         WRT_DB_UPDATE(update, PluginProperties, &WrtDatabase::interface())
206         update->Where(
207             Equals<PluginProperties::PluginPropertiesId>(pluginHandle));
208
209         update->Values(row);
210         update->Execute();
211         transaction.Commit();
212     }
213     Catch(DPL::DB::SqlConnection::Exception::Base)
214     {
215         ReThrowMsg(PluginDAO::Exception::DatabaseError,
216                    "Failed in RegisterLibraryDependencies");
217     }
218 }
219
220 } // namespace WrtDB