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 {
47 WidgetInterfaceDAO::WidgetInterfaceDAO(int widgetHandle) :
48 m_widgetHandle(widgetHandle),
49 m_databaseInterface(databaseFileName(widgetHandle), DATABASE_FLAGS)
51 if (!checkDatabase()) {
52 LogError("There is a problem with database");
54 m_databaseInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RW);
57 WidgetInterfaceDAO::~WidgetInterfaceDAO()
59 m_databaseInterface.DetachFromThread();
62 bool WidgetInterfaceDAO::checkDatabase()
64 using namespace WrtDB::GlobalConfig;
65 std::string databaseFile = databaseFileName(m_widgetHandle);
67 if (stat(databaseFile.c_str(), &buffer) != 0) {
68 //Create fresh database
70 LogInfo("Creating database " << databaseFile);
73 file.open(GetWrtWidgetInterfaceDatabaseFilePath(), std::ios_base::in);
75 LogError("Cannot create database. SQL file is missing.");
79 std::stringstream buffer;
80 buffer << file.rdbuf();
86 DPL::DB::SqlConnection con(databaseFile,
87 DATABASE_FLAGS, DATABASE_OPTIONS);
88 con.ExecCommand(buffer.str().c_str());
90 Catch(DPL::DB::SqlConnection::Exception::Base)
92 LogError("Cannot create database");
95 copyPropertiesFromWrtDatabase();
100 bool WidgetInterfaceDAO::copyPropertiesFromWrtDatabase()
102 using namespace DPL::DB::ORM;
103 using namespace DPL::DB::ORM::widget_interface;
106 WrtDB::WrtDatabase::attachToThreadRO();
107 m_databaseInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RW);
111 WrtDB::PropertyDAOReadOnly::WidgetPreferenceList existing =
112 WrtDB::PropertyDAOReadOnly::GetPropertyList(m_widgetHandle);
116 //save all properties read from config.xml
117 FOREACH(prop, existing) {
118 std::string key = DPL::ToUTF8String(prop->key_name);
119 if (key != KEY_WIDGET_ARG) {
121 if (!prop->key_value.IsNull()) {
122 value = DPL::ToUTF8String(*(prop->key_value));
125 !prop->readonly.IsNull() && (*prop->readonly);
126 setItem(key, value, readonly, true);
130 Catch(Commons::Exception)
132 LogError("Cannot copy properties read from config.xml");
136 Catch(DPL::DB::SqlConnection::Exception::Base)
138 LogError("Cannot copy properties read from config.xml");
141 WrtDB::WrtDatabase::detachFromThread();
142 m_databaseInterface.DetachFromThread();
146 void WidgetInterfaceDAO::setItem(const std::string& key,
147 const std::string& value,
150 setItem(key, value, readOnly, false);
153 void WidgetInterfaceDAO::setItem(const std::string& key,
154 const std::string& value,
158 using namespace DPL::DB::ORM;
159 using namespace DPL::DB::ORM::widget_interface;
163 ScopedTransaction tran(&m_databaseInterface);
164 //check if key exists
165 Properties::Select select(&m_databaseInterface);
167 Equals<Properties::key>(DPL::FromUTF8String(key)));
168 std::list<Properties::Row> rows = select.GetRowList();
171 Properties::Insert insert(&m_databaseInterface);
173 row.Set_key(DPL::FromUTF8String(key));
174 row.Set_value(DPL::FromUTF8String(value));
175 row.Set_readonly(readOnly ? 1 : 0);
176 row.Set_configxml(fromConfigXml ? 1 : 0);
180 Assert(rows.size() == 1);
181 Properties::Row row = rows.front();
182 if (row.Get_readonly() != 0) {
183 Throw(Commons::LocalStorageValueNoModifableException);
185 row.Set_value(DPL::FromUTF8String(value));
186 row.Set_readonly(readOnly ? 1 : 0);
187 Properties::Update update(&m_databaseInterface);
188 update.Where(Equals<Properties::key>(DPL::FromUTF8String(key)));
194 Catch(DPL::DB::SqlConnection::Exception::Base)
196 LogError("Cannot set item " << key << " with value " << value);
197 ReThrow(Commons::PlatformException);
201 void WidgetInterfaceDAO::removeItem(const std::string& key)
203 using namespace DPL::DB::ORM;
204 using namespace DPL::DB::ORM::widget_interface;
208 Properties::Select select(&m_databaseInterface);
209 select.Where(Equals<Properties::key>(DPL::FromUTF8String(key)));
210 bool readonly = select.GetSingleValue<Properties::readonly>();
212 ThrowMsg(Commons::LocalStorageValueNoModifableException,
213 "Cannot delete item. Item is readonly");
215 Properties::Delete deleteItem(&m_databaseInterface);
216 deleteItem.Where(Equals<Properties::key>(DPL::FromUTF8String(key)));
217 deleteItem.Execute();
219 Catch(DPL::DB::SqlConnection::Exception::Base)
221 LogError("Cannot delete item " << key);
222 ReThrow(Commons::PlatformException);
226 DPL::Optional<std::string> WidgetInterfaceDAO::getValue(
227 const std::string& key) const
229 using namespace DPL::DB::ORM;
230 using namespace DPL::DB::ORM::widget_interface;
234 Properties::Select select(&m_databaseInterface);
235 select.Where(Equals<Properties::key>(DPL::FromUTF8String(key)));
236 std::list<DPL::String> value = select.GetValueList<Properties::value>();
238 return DPL::Optional<std::string>();
240 return DPL::Optional<std::string>(DPL::ToUTF8String(value.front()));
242 Catch(DPL::DB::SqlConnection::Exception::Base)
244 LogError("Not found item " << key);
245 ReThrow(Commons::PlatformException);
249 void WidgetInterfaceDAO::clear(bool removeReadOnly)
251 using namespace DPL::DB::ORM;
252 using namespace DPL::DB::ORM::widget_interface;
255 Properties::Delete deleteItem(&m_databaseInterface);
256 if (!removeReadOnly) {
257 deleteItem.Where(Equals<Properties::readonly>(0));
259 deleteItem.Execute();
261 Catch(DPL::DB::SqlConnection::Exception::Base)
263 LogError("Cannot delete all items");
264 ReThrow(Commons::PlatformException);
268 size_t WidgetInterfaceDAO::getStorageSize() const
270 using namespace DPL::DB::ORM;
271 using namespace DPL::DB::ORM::widget_interface;
275 Properties::Select select(&m_databaseInterface);
276 std::list<DPL::String> list =
277 select.GetValueList<Properties::key>();
280 Catch(DPL::DB::SqlConnection::Exception::Base)
282 LogError("Cannot get item count");
283 ReThrow(Commons::PlatformException);
288 std::string WidgetInterfaceDAO::getKeyByIndex(size_t index) const
290 using namespace DPL::DB::ORM;
291 using namespace DPL::DB::ORM::widget_interface;
295 Properties::Select select(&m_databaseInterface);
296 select.OrderBy("key");
297 std::list<DPL::String> list =
298 select.GetValueList<Properties::key>();
299 if (index >= list.size()) {
300 Throw(Commons::InvalidArgumentException);
302 for (size_t i = 0; i < index; ++i) {
305 return DPL::ToUTF8String(list.front());
307 Catch(DPL::DB::SqlConnection::Exception::Base)
309 LogError("Cannot get item count");
310 ReThrow(Commons::PlatformException);
314 std::string WidgetInterfaceDAO::databaseFileName(int widgetHandle)
316 using namespace DPL::DB::ORM;
317 using namespace WrtDB::WidgetConfig;
318 using namespace WrtDB::GlobalConfig;
320 WrtDB::WrtDatabase::attachToThreadRO();
322 std::stringstream filename;
325 WrtDB::WidgetDAOReadOnly widgetDAO(widgetHandle);
326 WrtDB::TizenPkgId pkgid = widgetDAO.getTizenPkgId();
328 filename << GetWidgetPersistentStoragePath(pkgid)
330 << GetWidgetInterfaceDatabaseFile();
332 Catch(DPL::DB::SqlConnection::Exception::Base)
334 LogError("Cannot get item count");
335 ReThrow(Commons::PlatformException);
337 WrtDB::WrtDatabase::detachFromThread();
338 return filename.str();