From c6de30f74f6dced4e4da5abeb44f07649123eae0 Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Thu, 6 Dec 2012 12:11:10 +0100 Subject: [PATCH] CustomHandlerDAO implementation [Issue#] N/A [Feature] N/A [Problem] N/A [Cause] CustomHandlers database needs DAO [Solution] Implementation of CustomHandlerDAO added [Verification] Succesfull compilation. Verify with: wrt-tests-dao --output=text --regexp=custom Change-Id: I4e190777e7b4b53b526bdd6635e2ba1d9a387200 --- .../dao/CustomHandlerDatabase.cpp | 13 ++- .../custom_handler_dao/dao/custom_handler_dao.cpp | 92 +++++++++++++++++ .../dao/custom_handler_dao_read_only.cpp | 111 ++++++++++++++++++++- .../custom-handler-dao-ro/CustomHandlerDatabase.h | 57 ++++++----- .../custom-handler-dao-ro/common_dao_types.h | 20 ++++ .../custom_handler_dao_read_only.h | 26 ++++- .../custom-handler-dao-rw/custom_handler_dao.h | 21 +++- 7 files changed, 303 insertions(+), 37 deletions(-) diff --git a/modules/custom_handler_dao/dao/CustomHandlerDatabase.cpp b/modules/custom_handler_dao/dao/CustomHandlerDatabase.cpp index c11f2a0..d0b26c5 100644 --- a/modules/custom_handler_dao/dao/CustomHandlerDatabase.cpp +++ b/modules/custom_handler_dao/dao/CustomHandlerDatabase.cpp @@ -24,24 +24,23 @@ DPL::DB::SqlConnection::Flag::Type CustomHandler_DB_FLAGS = DPL::DB::SqlConnection::Flag::UseLucene; } -DPL::Mutex g_CustomHandlerDbQueriesMutex; -DPL::DB::ThreadDatabaseSupport m_customHandlerdbInterface( - CustomHandler_DB_DATABASE, CustomHandler_DB_FLAGS); - +DPL::Mutex g_dbQueriesMutex; +DPL::DB::ThreadDatabaseSupport g_dbInterface(CustomHandler_DB_DATABASE, + CustomHandler_DB_FLAGS); void attachDatabaseRO() { - m_customHandlerdbInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RO); + g_dbInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RO); } void attachDatabaseRW() { - m_customHandlerdbInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RW); + g_dbInterface.AttachToThread(DPL::DB::SqlConnection::Flag::RW); } void detachDatabase() { - m_customHandlerdbInterface.DetachFromThread(); + g_dbInterface.DetachFromThread(); } } //namespace Interface diff --git a/modules/custom_handler_dao/dao/custom_handler_dao.cpp b/modules/custom_handler_dao/dao/custom_handler_dao.cpp index ee3d52f..cbab366 100644 --- a/modules/custom_handler_dao/dao/custom_handler_dao.cpp +++ b/modules/custom_handler_dao/dao/custom_handler_dao.cpp @@ -22,9 +22,28 @@ #include #include +#include + +using namespace DPL::DB::ORM; +using namespace DPL::DB::ORM::custom_handler; namespace CustomHandlerDB { +namespace { + +template +void fillRow(T& row, const CustomHandler& handler, const DPL::String& pkgName) +{ + row.Set_app_id(pkgName); + row.Set_target(handler.target); + row.Set_base_url(handler.base_url); + row.Set_url(handler.url); + row.Set_title(handler.title); + row.Set_user_allowed(handler.user_allowed); +} + +} // namespace + CustomHandlerDAO::CustomHandlerDAO(const DPL::String& pkgName) : CustomHandlerDAOReadOnly(pkgName) { @@ -34,4 +53,77 @@ CustomHandlerDAO::~CustomHandlerDAO() { } +void CustomHandlerDAO::registerContentHandler(const CustomHandler& handler) +{ + LogDebug("Registering content handler " << handler.target << " " << + handler.base_url); + Try { + ContentHandlers::Row row; + fillRow(row, handler, m_pkgName); + + // TODO remove previous if necessary + CUSTOM_HANDLER_DB_INSERT(insert, ContentHandlers); + insert->Values(row); + insert->Execute(); + } + Catch(DPL::DB::SqlConnection::Exception::Base){ + ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError, + "Failed to register custom handler"); + } +} + +void CustomHandlerDAO::registerProtocolHandler(const CustomHandler& handler) +{ + LogDebug("Registering protocol handler " << handler.target << " " << + handler.base_url); + Try { + ProtocolHandlers::Row row; + fillRow(row, handler, m_pkgName); + + // TODO remove previous if necessary + CUSTOM_HANDLER_DB_INSERT(insert, ProtocolHandlers); + insert->Values(row); + insert->Execute(); + } + Catch(DPL::DB::SqlConnection::Exception::Base){ + ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError, + "Failed to register custom handler"); + } +} + +void CustomHandlerDAO::unregisterContentHandler(const DPL::String& target, + const DPL::String& url) +{ + LogDebug("Removing content handler " << target << " " << url); + Try { + CUSTOM_HANDLER_DB_DELETE(deleteFrom, ContentHandlers); + deleteFrom->Where(And(Equals(m_pkgName), + And(Equals(target), + Equals(url)))); + deleteFrom->Execute(); + } + Catch(DPL::DB::SqlConnection::Exception::Base) { + ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError, + "Failed to remove content handler"); + } +} + +void CustomHandlerDAO::unregisterProtocolHandler(const DPL::String& target, + const DPL::String& url) +{ + LogDebug("Removing protocol handler " << target << " " << url); + Try { + CUSTOM_HANDLER_DB_DELETE(deleteFrom, ProtocolHandlers); + deleteFrom->Where(And(Equals(m_pkgName), + And(Equals(target), + Equals(url)))); + deleteFrom->Execute(); + } + Catch(DPL::DB::SqlConnection::Exception::Base) { + ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError, + "Failed to remove content handler"); + } + +} + } // namespace CustomHandlerDB diff --git a/modules/custom_handler_dao/dao/custom_handler_dao_read_only.cpp b/modules/custom_handler_dao/dao/custom_handler_dao_read_only.cpp index 2cd5e19..ef8e3ef 100644 --- a/modules/custom_handler_dao/dao/custom_handler_dao_read_only.cpp +++ b/modules/custom_handler_dao/dao/custom_handler_dao_read_only.cpp @@ -25,9 +25,40 @@ #include #include +#include + +using namespace DPL::DB::ORM; +using namespace DPL::DB::ORM::custom_handler; + namespace CustomHandlerDB { -CustomHandlerDAOReadOnly::CustomHandlerDAOReadOnly(const DPL::String& pkgName) +namespace { + +template +CustomHandlerPtr getSingleHandler(std::list row) +{ + CustomHandlerPtr handler; + if (!row.empty()) { + // There should be only one + if (row.size() > 1) { + ThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError, + "More than one handler registered"); + } + + handler.reset(new CustomHandler()); + handler->target = row.front().Get_target(); + handler->base_url = row.front().Get_base_url(); + handler->url = row.front().Get_url(); + handler->title = row.front().Get_title(); + handler->user_allowed = row.front().Get_user_allowed(); + } + return handler; +} + +} // namespace + +CustomHandlerDAOReadOnly::CustomHandlerDAOReadOnly(const DPL::String& pkgName) : + m_pkgName(pkgName) { } @@ -35,4 +66,82 @@ CustomHandlerDAOReadOnly::~CustomHandlerDAOReadOnly() { } +CustomHandlerPtr CustomHandlerDAOReadOnly::getProtocolHandler( + const DPL::String& protocol, + const DPL::String& url) +{ + LogDebug("Getting protocol handler"); + Try{ + CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers); + + select->Where(And(Equals(m_pkgName), + And(Equals(protocol), + Equals(url)))); + + std::list list = select->GetRowList(); + return getSingleHandler(list); + } Catch(DPL::DB::SqlConnection::Exception::Base) { + ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError, + "Failed to get protocol handler"); + } +} + +CustomHandlerPtr CustomHandlerDAOReadOnly::getContentHandler( + const DPL::String& content, + const DPL::String& url) +{ + LogDebug("Getting content handler"); + Try{ + CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers); + + select->Where(And(Equals(m_pkgName), + And(Equals(content), + Equals(url)))); + + std::list list = select->GetRowList(); + return getSingleHandler(list); + } Catch(DPL::DB::SqlConnection::Exception::Base) { + ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError, + "Failed to get content handler"); + } +} + +CustomHandlerPtr CustomHandlerDAOReadOnly::getAllowedProtocolHandler( + const DPL::String& protocol) +{ + LogDebug("Getting allowed protocol handler"); + Try{ + CUSTOM_HANDLER_DB_SELECT(select, ProtocolHandlers); + + select->Where(And(Equals(m_pkgName), + And(Equals(protocol), + Equals(true)))); + + std::list list = select->GetRowList(); + return getSingleHandler(list); + } Catch(DPL::DB::SqlConnection::Exception::Base) { + ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError, + "Failed to get content handler"); + } +} + +CustomHandlerPtr CustomHandlerDAOReadOnly::getAllowedContentHandler( + const DPL::String& protocol) +{ + LogDebug("Getting allowed content handler"); + Try{ + CUSTOM_HANDLER_DB_SELECT(select, ContentHandlers) + + select->Where(And(Equals(m_pkgName), + And(Equals(protocol), + Equals(true)))); + + std::list list = select->GetRowList(); + return getSingleHandler(list); + } Catch(DPL::DB::SqlConnection::Exception::Base) { + ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError, + "Failed to get content handler"); + } +} + } // namespace CustomHandlerDB diff --git a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h index 9b6bd71..fea6309 100644 --- a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h +++ b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h @@ -21,32 +21,6 @@ #include #include -extern DPL::Mutex g_CustomHandlerDbQueriesMutex; - -#define CUSTOM_HANDLER_DB_INTERNAL(tlsCommand, InternalType, interface) \ - static DPL::ThreadLocalVariable *tlsCommand ## Ptr = NULL; \ - { \ - DPL::Mutex::ScopedLock lock(&customHandlerDbQueriesMutex); \ - if (!tlsCommand ## Ptr) { \ - static DPL::ThreadLocalVariable tmp; \ - tlsCommand ## Ptr = &tmp; \ - } \ - } \ - DPL::ThreadLocalVariable &tlsCommand = *tlsCommand ## Ptr; \ - if (tlsCommand.IsNull()) { tlsCommand = InternalType(interface); } - -#define CUSTOM_HANDLER_DB_SELECT(name, type, interface) \ - CUSTOM_HANDLER_DB_INTERNAL(name, type::Select, interface) - -#define CUSTOM_HANDLER_DB_INSERT(name, type, interface) \ - CUSTOM_HANDLER_DB_INTERNAL(name, type::Insert, interface) - -#define CUSTOM_HANDLER_DB_UPDATE(name, type, interface) \ - CUSTOM_HANDLER_DB_INTERNAL(name, type::Update, interface) - -#define CUSTOM_HANDLER_DB_DELETE(name, type, interface) \ - CUSTOM_HANDLER_DB_INTERNAL(name, type::Delete, interface) - namespace CustomHandlerDB { namespace Interface { @@ -54,10 +28,39 @@ void attachDatabaseRO(); void attachDatabaseRW(); void detachDatabase(); -extern DPL::DB::ThreadDatabaseSupport m_customHandlerdbInterface; +extern DPL::Mutex g_dbQueriesMutex; +extern DPL::DB::ThreadDatabaseSupport g_dbInterface; } // namespace Interface } // namespace CustomHandlerDB +#define CUSTOM_HANDLER_DB_INTERNAL(tlsCommand, InternalType) \ + static DPL::ThreadLocalVariable *tlsCommand ## Ptr = NULL; \ + { \ + DPL::Mutex::ScopedLock lock( \ + &CustomHandlerDB::Interface::g_dbQueriesMutex); \ + if (!tlsCommand ## Ptr) { \ + static DPL::ThreadLocalVariable tmp; \ + tlsCommand ## Ptr = &tmp; \ + } \ + } \ + DPL::ThreadLocalVariable &tlsCommand = *tlsCommand ## Ptr; \ + if (tlsCommand.IsNull()) \ + { \ + tlsCommand = InternalType(&CustomHandlerDB::Interface::g_dbInterface); \ + } + +#define CUSTOM_HANDLER_DB_SELECT(name, type) \ + CUSTOM_HANDLER_DB_INTERNAL(name, type::Select) + +#define CUSTOM_HANDLER_DB_INSERT(name, type) \ + CUSTOM_HANDLER_DB_INTERNAL(name, type::Insert) + +#define CUSTOM_HANDLER_DB_UPDATE(name, type) \ + CUSTOM_HANDLER_DB_INTERNAL(name, type::Update) + +#define CUSTOM_HANDLER_DB_DELETE(name, type) \ + CUSTOM_HANDLER_DB_INTERNAL(name, type::Delete) + #endif /* _CUSTOM_HANDLER_DATABASE_H_ */ diff --git a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/common_dao_types.h b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/common_dao_types.h index ad55dc1..6be001c 100644 --- a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/common_dao_types.h +++ b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/common_dao_types.h @@ -24,8 +24,28 @@ #ifndef SHARE_COMMON_DAO_TYPES_H_ #define SHARE_COMMON_DAO_TYPES_H_ +#include +#include +#include + namespace CustomHandlerDB { +/** + * @brief Custom Handler struct + * + * Describes custom handler for protocol and content. + */ +struct CustomHandler +{ + DPL::String target; // protocol/content ("mailto"/"application/x-soup") + DPL::String base_url; // base url of registered page + DPL::String url; // url used for protocol/content handling + DPL::String title; // user friendly handler name + bool user_allowed; // true if user has allowed the handler +}; + +typedef std::shared_ptr CustomHandlerPtr; + } // namespace CustomHandlerDB #endif /* SHARE_COMMON_DAO_TYPES_H_ */ diff --git a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h index 941057b..021d6a8 100644 --- a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h +++ b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h @@ -27,6 +27,7 @@ #include #include +#include "common_dao_types.h" namespace CustomHandlerDB { @@ -47,7 +48,30 @@ class CustomHandlerDAOReadOnly explicit CustomHandlerDAOReadOnly(const DPL::String& pkgName); virtual ~CustomHandlerDAOReadOnly(); - // TODO + /** + * Returns protocol handler + */ + CustomHandlerPtr getProtocolHandler(const DPL::String& protocol, + const DPL::String& url); + + /** + * Returns content handler + */ + CustomHandlerPtr getContentHandler(const DPL::String& content, + const DPL::String& url); + + /** + * Returns allowed handler for given protocol + */ + CustomHandlerPtr getAllowedProtocolHandler(const DPL::String& protocol); + + /** + * Returns allowed handler for given content + */ + CustomHandlerPtr getAllowedContentHandler(const DPL::String& protocol); + + protected: + DPL::String m_pkgName; }; } // namespace CustomHandlerDB diff --git a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-rw/custom_handler_dao.h b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-rw/custom_handler_dao.h index e192ed7..d1307fe 100644 --- a/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-rw/custom_handler_dao.h +++ b/modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-rw/custom_handler_dao.h @@ -34,7 +34,26 @@ class CustomHandlerDAO : public CustomHandlerDAOReadOnly explicit CustomHandlerDAO(const DPL::String& pkgName); virtual ~CustomHandlerDAO(); - // TODO + /** + * Registers custom content handler + */ + void registerContentHandler(const CustomHandler& handler); + + /** + * Registers custom protocol handler + */ + void registerProtocolHandler(const CustomHandler& handler); + + /** + * Unregisters custom content handler + */ + void unregisterContentHandler(const DPL::String& target, + const DPL::String& burl); + /** + * Unregisters custom protocol handler + */ + void unregisterProtocolHandler(const DPL::String& target, + const DPL::String& url); }; } // namespace CustomHandlerDB -- 2.7.4