CustomHandlerDAO implementation
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Thu, 6 Dec 2012 11:11:10 +0000 (12:11 +0100)
committerGerrit Code Review <gerrit2@kim11>
Wed, 12 Dec 2012 10:34:30 +0000 (19:34 +0900)
[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

modules/custom_handler_dao/dao/CustomHandlerDatabase.cpp
modules/custom_handler_dao/dao/custom_handler_dao.cpp
modules/custom_handler_dao/dao/custom_handler_dao_read_only.cpp
modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h
modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/common_dao_types.h
modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h
modules/custom_handler_dao/include/wrt-commons/custom-handler-dao-rw/custom_handler_dao.h

index c11f2a0..d0b26c5 100644 (file)
@@ -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
index ee3d52f..cbab366 100644 (file)
 
 #include <wrt-commons/custom-handler-dao-rw/custom_handler_dao.h>
 #include <wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h>
+#include <orm_generator_custom_handler.h>
+
+using namespace DPL::DB::ORM;
+using namespace DPL::DB::ORM::custom_handler;
 
 namespace CustomHandlerDB {
 
+namespace {
+
+template <typename T>
+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<ContentHandlers::app_id>(m_pkgName),
+                         And(Equals<ContentHandlers::target>(target),
+                             Equals<ContentHandlers::url>(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<ProtocolHandlers::app_id>(m_pkgName),
+                         And(Equals<ProtocolHandlers::target>(target),
+                             Equals<ProtocolHandlers::url>(url))));
+       deleteFrom->Execute();
+    }
+    Catch(DPL::DB::SqlConnection::Exception::Base) {
+       ReThrowMsg(CustomHandlerDAO::Exception::DatabaseError,
+                  "Failed to remove content handler");
+    }
+
+}
+
 } // namespace CustomHandlerDB
index 2cd5e19..ef8e3ef 100644 (file)
 #include <wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h>
 #include <wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h>
 
+#include <orm_generator_custom_handler.h>
+
+using namespace DPL::DB::ORM;
+using namespace DPL::DB::ORM::custom_handler;
+
 namespace CustomHandlerDB {
 
-CustomHandlerDAOReadOnly::CustomHandlerDAOReadOnly(const DPL::String& pkgName)
+namespace {
+
+template <typename T>
+CustomHandlerPtr getSingleHandler(std::list<T> 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<ProtocolHandlers::app_id>(m_pkgName),
+                      And(Equals<ProtocolHandlers::target>(protocol),
+                          Equals<ProtocolHandlers::url>(url))));
+
+        std::list<ProtocolHandlers::Row> 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<ContentHandlers::app_id>(m_pkgName),
+                      And(Equals<ContentHandlers::target>(content),
+                          Equals<ContentHandlers::url>(url))));
+
+        std::list<ContentHandlers::Row> 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<ProtocolHandlers::app_id>(m_pkgName),
+                      And(Equals<ProtocolHandlers::target>(protocol),
+                          Equals<ProtocolHandlers::user_allowed>(true))));
+
+        std::list<ProtocolHandlers::Row> 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<ContentHandlers::app_id>(m_pkgName),
+                      And(Equals<ContentHandlers::target>(protocol),
+                          Equals<ContentHandlers::user_allowed>(true))));
+
+        std::list<ContentHandlers::Row> list = select->GetRowList();
+        return getSingleHandler(list);
+    } Catch(DPL::DB::SqlConnection::Exception::Base) {
+        ReThrowMsg(CustomHandlerDAOReadOnly::Exception::DatabaseError,
+                   "Failed to get content handler");
+    }
+}
+
 } // namespace CustomHandlerDB
index 9b6bd71..fea6309 100644 (file)
 #include <dpl/mutex.h>
 #include <dpl/db/thread_database_support.h>
 
-extern DPL::Mutex g_CustomHandlerDbQueriesMutex;
-
-#define CUSTOM_HANDLER_DB_INTERNAL(tlsCommand, InternalType, interface)      \
-    static DPL::ThreadLocalVariable<InternalType> *tlsCommand ## Ptr = NULL; \
-    {                                                                        \
-        DPL::Mutex::ScopedLock lock(&customHandlerDbQueriesMutex);           \
-        if (!tlsCommand ## Ptr) {                                            \
-            static DPL::ThreadLocalVariable<InternalType> tmp;               \
-            tlsCommand ## Ptr = &tmp;                                        \
-        }                                                                    \
-    }                                                                        \
-    DPL::ThreadLocalVariable<InternalType> &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<InternalType> *tlsCommand ## Ptr = NULL;   \
+    {                                                                          \
+        DPL::Mutex::ScopedLock lock(                                           \
+                &CustomHandlerDB::Interface::g_dbQueriesMutex);                \
+        if (!tlsCommand ## Ptr) {                                              \
+            static DPL::ThreadLocalVariable<InternalType> tmp;                 \
+            tlsCommand ## Ptr = &tmp;                                          \
+        }                                                                      \
+    }                                                                          \
+    DPL::ThreadLocalVariable<InternalType> &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_ */
 
index ad55dc1..6be001c 100644 (file)
 #ifndef SHARE_COMMON_DAO_TYPES_H_
 #define SHARE_COMMON_DAO_TYPES_H_
 
+#include <list>
+#include <memory>
+#include <dpl/string.h>
+
 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<CustomHandler> CustomHandlerPtr;
+
 } // namespace CustomHandlerDB
 
 #endif /* SHARE_COMMON_DAO_TYPES_H_ */
index 941057b..021d6a8 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <dpl/string.h>
 #include <dpl/exception.h>
+#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
index e192ed7..d1307fe 100644 (file)
@@ -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