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);
251 return std::string();
254 void WidgetInterfaceDAO::clear(bool removeReadOnly)
256 using namespace DPL::DB::ORM;
257 using namespace DPL::DB::ORM::widget_interface;
260 Properties::Delete deleteItem(&m_databaseInterface);
261 if (!removeReadOnly) {
262 deleteItem.Where(Equals<Properties::readonly>(0));
264 deleteItem.Execute();
266 Catch(DPL::DB::SqlConnection::Exception::Base)
268 LogError("Cannot delete all items");
269 ReThrow(Commons::PlatformException);
273 size_t WidgetInterfaceDAO::getStorageSize() const
275 using namespace DPL::DB::ORM;
276 using namespace DPL::DB::ORM::widget_interface;
280 Properties::Select select(&m_databaseInterface);
281 std::list<DPL::String> list =
282 select.GetValueList<Properties::key>();
285 Catch(DPL::DB::SqlConnection::Exception::Base)
287 LogError("Cannot get item count");
288 ReThrow(Commons::PlatformException);
293 std::string WidgetInterfaceDAO::getValueByIndex(size_t index) const
295 using namespace DPL::DB::ORM;
296 using namespace DPL::DB::ORM::widget_interface;
300 Properties::Select select(&m_databaseInterface);
301 select.OrderBy("key");
302 std::list<DPL::String> list =
303 select.GetValueList<Properties::value>();
304 if (index >= list.size()) {
305 Throw(Commons::InvalidArgumentException);
307 for (size_t i = 0; i < index; ++i) {
310 return DPL::ToUTF8String(list.front());
312 Catch(DPL::DB::SqlConnection::Exception::Base)
314 LogError("Cannot get item count");
315 ReThrow(Commons::PlatformException);
317 return std::string();
320 std::string WidgetInterfaceDAO::databaseFileName(int widgetHandle)
322 using namespace DPL::DB::ORM;
323 using namespace WrtDB::WidgetConfig;
324 using namespace WrtDB::GlobalConfig;
326 WrtDB::WrtDatabase::attachToThreadRO();
328 std::stringstream filename;
331 WrtDB::WidgetDAOReadOnly widgetDAO(widgetHandle);
332 DPL::Optional<DPL::String> pkgname = widgetDAO.getPkgname();
334 filename << GetWidgetPersistentStoragePath(*pkgname)
336 << GetWidgetInterfaceDatabaseFile();
338 Catch(DPL::DB::SqlConnection::Exception::Base)
340 LogError("Cannot get item count");
341 ReThrow(Commons::PlatformException);
343 WrtDB::WrtDatabase::detachFromThread();
344 return filename.str();