-/*
- * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-/*
- * @author Lukasz Marek (l.marek@samsung.com)
- * @version 0.1
- * @brief
- */
-
-#include "WidgetInterfaceDAO.h"
-#include <string>
-#include <sstream>
-#include <fstream>
-#include <sys/stat.h>
-#include <dpl/db/sql_connection.h>
-#include <dpl/log/log.h>
-#include <dpl/foreach.h>
-#include <dpl/string.h>
-#include <Commons/Exception.h>
-#include <dpl/wrt-dao-ro/property_dao_read_only.h>
-#include <dpl/wrt-dao-ro/widget_dao_read_only.h>
-#include <dpl/wrt-dao-ro/WrtDatabase.h>
-#include <dpl/wrt-dao-ro/widget_config.h>
-#include <dpl/wrt-dao-ro/global_config.h>
-#include "orm_generator_widget_interface.h"
-namespace {
-DPL::DB::SqlConnection::Flag::Type DATABASE_FLAGS =
- DPL::DB::SqlConnection::Flag::UseLucene;
-DPL::DB::SqlConnection::Flag::Option DATABASE_OPTIONS =
- DPL::DB::SqlConnection::Flag::RW;
-const char *KEY_WIDGET_ARG = "widget_arg";
-}
-
-namespace WrtDeviceApis {
-WidgetInterfaceDAO::WidgetInterfaceDAO(int widgetHandle) :
- m_widgetHandle(widgetHandle),
- m_databaseInterface(databaseFileName(widgetHandle), DATABASE_FLAGS)
-{
- if (!checkDatabase()) {
- LogError("There is a problem with database");
- }
- m_databaseInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RW);
-}
-
-WidgetInterfaceDAO::~WidgetInterfaceDAO()
-{
- m_databaseInterface.DetachFromThread();
-}
-
-bool WidgetInterfaceDAO::checkDatabase()
-{
- using namespace WrtDB::GlobalConfig;
- std::string databaseFile = databaseFileName(m_widgetHandle);
- struct stat buffer;
- if (stat(databaseFile.c_str(), &buffer) != 0) {
- //Create fresh database
-
- LogDebug("Creating database " << databaseFile);
-
- std::fstream file;
- file.open(GetWrtWidgetInterfaceDatabaseFilePath(), std::ios_base::in);
- if (!file) {
- LogError("Cannot create database. SQL file is missing.");
- return false;
- }
-
- std::stringstream buffer;
- buffer << file.rdbuf();
-
- file.close();
-
- Try
- {
- DPL::DB::SqlConnection con(databaseFile,
- DATABASE_FLAGS, DATABASE_OPTIONS);
- con.ExecCommand(buffer.str().c_str());
- }
- Catch(DPL::DB::SqlConnection::Exception::Base)
- {
- LogError("Cannot create database");
- return false;
- }
- copyPropertiesFromWrtDatabase();
- }
- return true;
-}
-
-bool WidgetInterfaceDAO::copyPropertiesFromWrtDatabase()
-{
- using namespace DPL::DB::ORM;
- using namespace DPL::DB::ORM::widget_interface;
-
- bool result = true;
- WrtDB::WrtDatabase::attachToThreadRO();
- m_databaseInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RW);
-
- Try
- {
- WrtDB::PropertyDAOReadOnly::WidgetPreferenceList existing =
- WrtDB::PropertyDAOReadOnly::GetPropertyList(m_widgetHandle);
-
- Try
- {
- //save all properties read from config.xml
- FOREACH(prop, existing) {
- std::string key = DPL::ToUTF8String(prop->key_name);
- if (key != KEY_WIDGET_ARG) {
- std::string value;
- if (!prop->key_value.IsNull()) {
- value = DPL::ToUTF8String(*(prop->key_value));
- }
- bool readonly =
- !prop->readonly.IsNull() && (*prop->readonly);
- setItem(key, value, readonly, true);
- }
- }
- }
- Catch(Commons::Exception)
- {
- LogError("Cannot copy properties read from config.xml");
- result = false;
- }
- }
- Catch(DPL::DB::SqlConnection::Exception::Base)
- {
- LogError("Cannot copy properties read from config.xml");
- result = false;
- }
- WrtDB::WrtDatabase::detachFromThread();
- m_databaseInterface.DetachFromThread();
- return result;
-}
-
-void WidgetInterfaceDAO::setItem(const std::string& key,
- const std::string& value,
- bool readOnly)
-{
- setItem(key, value, readOnly, false);
-}
-
-void WidgetInterfaceDAO::setItem(const std::string& key,
- const std::string& value,
- bool readOnly,
- bool fromConfigXml)
-{
- using namespace DPL::DB::ORM;
- using namespace DPL::DB::ORM::widget_interface;
-
- Try
- {
- ScopedTransaction tran(&m_databaseInterface);
- //check if key exists
- Properties::Select select(&m_databaseInterface);
- select.Where(
- Equals<Properties::key>(DPL::FromUTF8String(key)));
- std::list<Properties::Row> rows = select.GetRowList();
-
- if (rows.empty()) {
- Properties::Insert insert(&m_databaseInterface);
- Properties::Row row;
- row.Set_key(DPL::FromUTF8String(key));
- row.Set_value(DPL::FromUTF8String(value));
- row.Set_readonly(readOnly ? 1 : 0);
- row.Set_configxml(fromConfigXml ? 1 : 0);
- insert.Values(row);
- insert.Execute();
- } else {
- Assert(rows.size() == 1);
- Properties::Row row = rows.front();
- if (row.Get_readonly() != 0) {
- Throw(Commons::LocalStorageValueNoModifableException);
- }
- row.Set_value(DPL::FromUTF8String(value));
- row.Set_readonly(readOnly ? 1 : 0);
- Properties::Update update(&m_databaseInterface);
- update.Where(Equals<Properties::key>(DPL::FromUTF8String(key)));
- update.Values(row);
- update.Execute();
- }
- tran.Commit();
- }
- Catch(DPL::DB::SqlConnection::Exception::Base)
- {
- LogError("Cannot set item " << key << " with value " << value);
- ReThrow(Commons::PlatformException);
- }
-}
-
-void WidgetInterfaceDAO::removeItem(const std::string& key)
-{
- using namespace DPL::DB::ORM;
- using namespace DPL::DB::ORM::widget_interface;
-
- Try
- {
- Properties::Select select(&m_databaseInterface);
- select.Where(Equals<Properties::key>(DPL::FromUTF8String(key)));
- bool readonly = select.GetSingleValue<Properties::readonly>();
- if (readonly) {
- ThrowMsg(Commons::LocalStorageValueNoModifableException,
- "Cannot delete item. Item is readonly");
- }
- Properties::Delete deleteItem(&m_databaseInterface);
- deleteItem.Where(Equals<Properties::key>(DPL::FromUTF8String(key)));
- deleteItem.Execute();
- }
- Catch(DPL::DB::SqlConnection::Exception::Base)
- {
- LogError("Cannot delete item " << key);
- ReThrow(Commons::PlatformException);
- }
-}
-
-DPL::Optional<std::string> WidgetInterfaceDAO::getValue(
- const std::string& key) const
-{
- using namespace DPL::DB::ORM;
- using namespace DPL::DB::ORM::widget_interface;
-
- Try
- {
- Properties::Select select(&m_databaseInterface);
- select.Where(Equals<Properties::key>(DPL::FromUTF8String(key)));
- std::list<DPL::String> value = select.GetValueList<Properties::value>();
- if (value.empty()) {
- return DPL::Optional<std::string>();
- }
- return DPL::Optional<std::string>(DPL::ToUTF8String(value.front()));
- }
- Catch(DPL::DB::SqlConnection::Exception::Base)
- {
- LogError("Not found item " << key);
- ReThrow(Commons::PlatformException);
- }
-}
-
-void WidgetInterfaceDAO::clear(bool removeReadOnly)
-{
- using namespace DPL::DB::ORM;
- using namespace DPL::DB::ORM::widget_interface;
- Try
- {
- Properties::Delete deleteItem(&m_databaseInterface);
- if (!removeReadOnly) {
- deleteItem.Where(Equals<Properties::readonly>(0));
- }
- deleteItem.Execute();
- }
- Catch(DPL::DB::SqlConnection::Exception::Base)
- {
- LogError("Cannot delete all items");
- ReThrow(Commons::PlatformException);
- }
-}
-
-size_t WidgetInterfaceDAO::getStorageSize() const
-{
- using namespace DPL::DB::ORM;
- using namespace DPL::DB::ORM::widget_interface;
-
- Try
- {
- Properties::Select select(&m_databaseInterface);
- std::list<DPL::String> list =
- select.GetValueList<Properties::key>();
- return list.size();
- }
- Catch(DPL::DB::SqlConnection::Exception::Base)
- {
- LogError("Cannot get item count");
- ReThrow(Commons::PlatformException);
- }
- return 0;
-}
-
-std::string WidgetInterfaceDAO::getKeyByIndex(size_t index) const
-{
- using namespace DPL::DB::ORM;
- using namespace DPL::DB::ORM::widget_interface;
-
- Try
- {
- Properties::Select select(&m_databaseInterface);
- select.OrderBy("key");
- std::list<DPL::String> list =
- select.GetValueList<Properties::key>();
- if (index >= list.size()) {
- Throw(Commons::InvalidArgumentException);
- }
- for (size_t i = 0; i < index; ++i) {
- list.pop_front();
- }
- return DPL::ToUTF8String(list.front());
- }
- Catch(DPL::DB::SqlConnection::Exception::Base)
- {
- LogError("Cannot get item count");
- ReThrow(Commons::PlatformException);
- }
-}
-
-std::string WidgetInterfaceDAO::databaseFileName(int widgetHandle)
-{
- using namespace DPL::DB::ORM;
- using namespace WrtDB::WidgetConfig;
- using namespace WrtDB::GlobalConfig;
-
- WrtDB::WrtDatabase::attachToThreadRO();
-
- std::stringstream filename;
- Try
- {
- WrtDB::WidgetDAOReadOnly widgetDAO(widgetHandle);
- WrtDB::TizenPkgId pkgid = widgetDAO.getTizenPkgId();
-
- filename << GetWidgetPersistentStoragePath(pkgid)
- << "/"
- << GetWidgetInterfaceDatabaseFile();
- }
- Catch(DPL::DB::SqlConnection::Exception::Base)
- {
- LogError("Cannot get item count");
- ReThrow(Commons::PlatformException);
- }
- WrtDB::WrtDatabase::detachFromThread();
- return filename.str();
-}
-}