2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @author Lukasz Marek (l.marek@samsung.com)
22 #include "WidgetInterfaceDAO.h"
27 #include <dpl/db/sql_connection.h>
28 #include <dpl/log/log.h>
29 #include <dpl/foreach.h>
30 #include <dpl/string.h>
31 #include <Commons/Exception.h>
32 #include <dpl/wrt-dao-ro/property_dao_read_only.h>
33 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
34 #include <dpl/wrt-dao-ro/WrtDatabase.h>
35 #include <dpl/wrt-dao-ro/widget_config.h>
36 #include <dpl/wrt-dao-ro/global_config.h>
37 #include "orm_generator_widget_interface.h"
39 DPL::DB::SqlConnection::Flag::Type DATABASE_FLAGS =
40 DPL::DB::SqlConnection::Flag::UseLucene;
41 DPL::DB::SqlConnection::Flag::Option DATABASE_OPTIONS =
42 DPL::DB::SqlConnection::Flag::RW;
43 const char *KEY_WIDGET_ARG = "widget_arg";
46 namespace WrtDeviceApis {
48 WidgetInterfaceDAO::WidgetInterfaceDAO(int widgetHandle) :
49 m_widgetHandle(widgetHandle),
50 m_databaseInterface(databaseFileName(widgetHandle), DATABASE_FLAGS)
52 if (!checkDatabase()) {
53 LogError("There is a problem with database");
55 m_databaseInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RW);
58 WidgetInterfaceDAO::~WidgetInterfaceDAO()
60 m_databaseInterface.DetachFromThread();
63 bool WidgetInterfaceDAO::checkDatabase()
65 using namespace WrtDB::GlobalConfig;
66 std::string databaseFile = databaseFileName(m_widgetHandle);
68 if (stat(databaseFile.c_str(), &buffer) != 0) {
70 //Create fresh database
72 LogInfo("Creating database " << databaseFile);
75 file.open(GetWrtWidgetInterfaceDatabaseFilePath(), std::ios_base::in);
77 LogError("Cannot create database. SQL file is missing.");
81 std::stringstream buffer;
82 buffer << file.rdbuf();
88 DPL::DB::SqlConnection con(databaseFile,
89 DATABASE_FLAGS, DATABASE_OPTIONS);
90 con.ExecCommand(buffer.str().c_str());
92 Catch(DPL::DB::SqlConnection::Exception::Base)
94 LogError("Cannot create database");
97 copyPropertiesFromWrtDatabase();
102 bool WidgetInterfaceDAO::copyPropertiesFromWrtDatabase()
104 using namespace DPL::DB::ORM;
105 using namespace DPL::DB::ORM::widget_interface;
108 WrtDB::WrtDatabase::attachToThreadRO();
109 m_databaseInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RW);
113 WrtDB::PropertyDAOReadOnly::WidgetPreferenceList existing =
114 WrtDB::PropertyDAOReadOnly::GetPropertyList(m_widgetHandle);
118 //save all properties read from config.xml
119 FOREACH(prop, existing) {
120 std::string key = DPL::ToUTF8String(prop->key_name);
121 if (key != KEY_WIDGET_ARG) {
123 if (!prop->key_value.IsNull()) {
124 value = DPL::ToUTF8String(*(prop->key_value));
127 !prop->readonly.IsNull() && (*prop->readonly);
128 setItem(key, value, readonly, true);
132 Catch(Commons::Exception)
134 LogError("Cannot copy properties read from config.xml");
138 Catch(DPL::DB::SqlConnection::Exception::Base)
140 LogError("Cannot copy properties read from config.xml");
143 WrtDB::WrtDatabase::detachFromThread();
144 m_databaseInterface.DetachFromThread();
148 void WidgetInterfaceDAO::setItem(const std::string& key,
149 const std::string& value,
152 setItem(key, value, readOnly, false);
155 void WidgetInterfaceDAO::setItem(const std::string& key,
156 const std::string& value,
160 using namespace DPL::DB::ORM;
161 using namespace DPL::DB::ORM::widget_interface;
165 ScopedTransaction tran(&m_databaseInterface);
166 //check if key exists
167 Properties::Select select(&m_databaseInterface);
169 Equals<Properties::key>(DPL::FromUTF8String(key)));
170 std::list<Properties::Row> rows = select.GetRowList();
172 if (rows.size() == 0) {
173 Properties::Insert insert(&m_databaseInterface);
175 row.Set_key(DPL::FromUTF8String(key));
176 row.Set_value(DPL::FromUTF8String(value));
177 row.Set_readonly(readOnly ? 1 : 0);
178 row.Set_configxml(fromConfigXml ? 1 : 0);
183 Assert(rows.size() == 1);
184 Properties::Row row = rows.front();
185 if (row.Get_readonly() != 0) {
186 Throw(Commons::LocalStorageValueNoModifableException);
188 row.Set_value(DPL::FromUTF8String(value));
189 row.Set_readonly(readOnly ? 1 : 0);
190 Properties::Update update(&m_databaseInterface);
191 update.Where(Equals<Properties::key>(DPL::FromUTF8String(key)));
197 Catch(DPL::DB::SqlConnection::Exception::Base)
199 LogError("Cannot set item " << key << " with value " << value);
200 ReThrow(Commons::PlatformException);
205 void WidgetInterfaceDAO::removeItem(const std::string& key)
207 using namespace DPL::DB::ORM;
208 using namespace DPL::DB::ORM::widget_interface;
212 Properties::Select select(&m_databaseInterface);
213 select.Where(Equals<Properties::key>(DPL::FromUTF8String(key)));
214 bool readonly = select.GetSingleValue<Properties::readonly>();
216 ThrowMsg(Commons::LocalStorageValueNoModifableException,
217 "Cannot delete item. Item is readonly");
219 Properties::Delete deleteItem(&m_databaseInterface);
220 deleteItem.Where(Equals<Properties::key>(DPL::FromUTF8String(key)));
221 deleteItem.Execute();
223 Catch(DPL::DB::SqlConnection::Exception::Base)
225 LogError("Cannot delete item " << key);
226 ReThrow(Commons::PlatformException);
230 DPL::Optional<std::string> WidgetInterfaceDAO::getValue(
231 const std::string& key) const
233 using namespace DPL::DB::ORM;
234 using namespace DPL::DB::ORM::widget_interface;
238 Properties::Select select(&m_databaseInterface);
239 select.Where(Equals<Properties::key>(DPL::FromUTF8String(key)));
240 std::list<DPL::String> value = select.GetValueList<Properties::value>();
241 if (value.size() == 0) {
242 return DPL::Optional<std::string>();
244 return DPL::Optional<std::string>(DPL::ToUTF8String(value.front()));
246 Catch(DPL::DB::SqlConnection::Exception::Base)
248 LogError("Not found item " << key);
249 ReThrow(Commons::PlatformException);
253 void WidgetInterfaceDAO::clear(bool removeReadOnly)
255 using namespace DPL::DB::ORM;
256 using namespace DPL::DB::ORM::widget_interface;
259 Properties::Delete deleteItem(&m_databaseInterface);
260 if (!removeReadOnly) {
261 deleteItem.Where(Equals<Properties::readonly>(0));
263 deleteItem.Execute();
265 Catch(DPL::DB::SqlConnection::Exception::Base)
267 LogError("Cannot delete all items");
268 ReThrow(Commons::PlatformException);
272 size_t WidgetInterfaceDAO::getStorageSize() const
274 using namespace DPL::DB::ORM;
275 using namespace DPL::DB::ORM::widget_interface;
279 Properties::Select select(&m_databaseInterface);
280 std::list<DPL::String> list =
281 select.GetValueList<Properties::key>();
284 Catch(DPL::DB::SqlConnection::Exception::Base)
286 LogError("Cannot get item count");
287 ReThrow(Commons::PlatformException);
292 std::string WidgetInterfaceDAO::getKeyByIndex(size_t index) const
294 using namespace DPL::DB::ORM;
295 using namespace DPL::DB::ORM::widget_interface;
299 Properties::Select select(&m_databaseInterface);
300 select.OrderBy("key");
301 std::list<DPL::String> list =
302 select.GetValueList<Properties::key>();
303 if (index >= list.size()) {
304 Throw(Commons::InvalidArgumentException);
306 for (size_t i = 0; i < index; ++i) {
309 return DPL::ToUTF8String(list.front());
311 Catch(DPL::DB::SqlConnection::Exception::Base)
313 LogError("Cannot get item count");
314 ReThrow(Commons::PlatformException);
318 std::string WidgetInterfaceDAO::databaseFileName(int widgetHandle)
320 using namespace DPL::DB::ORM;
321 using namespace WrtDB::WidgetConfig;
322 using namespace WrtDB::GlobalConfig;
324 WrtDB::WrtDatabase::attachToThreadRO();
326 std::stringstream filename;
329 WrtDB::WidgetDAOReadOnly widgetDAO(widgetHandle);
330 WrtDB::WidgetPkgName pkgname = widgetDAO.getPkgName();
332 filename << GetWidgetPersistentStoragePath(pkgname)
334 << GetWidgetInterfaceDatabaseFile();
336 Catch(DPL::DB::SqlConnection::Exception::Base)
338 LogError("Cannot get item count");
339 ReThrow(Commons::PlatformException);
341 WrtDB::WrtDatabase::detachFromThread();
342 return filename.str();