Add db related logic 59/202059/4
authorjusung son <jusung07.son@samsung.com>
Fri, 22 Mar 2019 07:09:52 +0000 (16:09 +0900)
committerjusung son <jusung07.son@samsung.com>
Tue, 26 Mar 2019 05:46:21 +0000 (14:46 +0900)
Change-Id: I2c731d3c11fc04e28ef38195654fead4a1d58710
Signed-off-by: jusung son <jusung07.son@samsung.com>
src/notification_ex_service.cc [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index c1ae959..08a9589
  * limitations under the License.
  */
 
+#include <dlog.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <errno.h>
-#include <glib.h>
-#include <math.h>
-#include <gio/gio.h>
-
-#include <aul.h>
-#include <dlog.h>
+#include <iostream>
+#include <sstream>
+#include <algorithm>
+#include <map>
 
 #include <notification-ex/manager.h>
 #include <notification-ex/reporter.h>
 #include <notification-ex/dbus_sender.h>
 #include <notification-ex/dbus_event_listener.h>
 #include <notification-ex/dbus_connection_manager.h>
+#include <notification-ex/db_manager.h>
+#include <notification-ex/common.h>
+#include <notification-ex/exception.h>
+#include <notification-ex/item_info_internal.h>
 #include <notification-ex/button_item.h>
 #include <notification-ex/ex_util.h>
 #include <notification-ex/ievent_info_internal.h>
+#include <notification-ex/iitem_info_internal.h>
 
 #include "debug.h"
 #include "notification_ex_service.h"
 
 using namespace notification;
 using namespace std;
+using namespace notification::item;
 
 class DPMFacade {
   public:
    DPMFacade(unique_ptr<Reporter> reporter, unique_ptr<Manager> manager)
     : reporter_(move(reporter)), manager_(move(manager)) {
+      DBManager::InitializeDB();
+      DBManager::InitializeData();
+      hide_map_ = DBManager::GetHideMap();
    }
 
    void DelegateReporterEvent(const IEventInfo& info, shared_ptr<item::AbstractItem> item) {
@@ -55,30 +62,122 @@ class DPMFacade {
 
    unique_ptr<Reporter> reporter_;
    unique_ptr<Manager> manager_;
+   map<string, string> hide_map_;
 };
 
 static DPMFacade* facade_;
 
 class DPMReporter : public Reporter {
  protected:
+
+  void OnUpdate(const IEventInfo& info,
+      shared_ptr<item::AbstractItem> updatedItem) {
+    DBG("Update !!!");
+    int ret;
+
+    /* check Manager::Hide() */
+    ret = UpdateHideApp(updatedItem);
+    if (ret == NOTIFICATION_ERROR_NONE)
+      return;
+
+    if (ret != NOTIFICATION_ERROR_NOT_EXIST_ID) {
+      SendError(info, static_cast<NotificationError>(ret));
+      return;
+    }
+
+    ret = DBManager::UpdateNotification(updatedItem);
+    if (ret == NOTIFICATION_ERROR_NONE) {
+      /* noti owner */
+      facade_->DelegateManagerEvent(info, updatedItem);
+
+      /* noti to viewers */
+      facade_->DelegateReporterEvent(info, updatedItem);
+    } else {
+      SendError(info, static_cast<NotificationError>(ret));
+    }
+
+  }
+
+  void OnDelete(const IEventInfo& info,
+      shared_ptr<item::AbstractItem> deletedItem) {
+    DBG("Delete !!!");
+    int ret;
+
+    ret = DBManager::DeleteNotification(deletedItem);
+    if (ret == NOTIFICATION_ERROR_NONE) {
+      /* noti owner */
+      facade_->DelegateManagerEvent(info, deletedItem);
+
+      /* noti to viewers */
+      facade_->DelegateReporterEvent(info, deletedItem);
+    } else {
+      SendError(info, static_cast<NotificationError>(ret));
+    }
+
+  }
+
   void OnEvent(const IEventInfo& info,
       list<shared_ptr<item::AbstractItem>> noti_list) override {
     LOGI("Event received (%d) !!", (int)info.GetEventType());
-    for (auto& i : noti_list) {
-      facade_->DelegateManagerEvent(info, i);
+    const IEventInfo::EventType type = info.GetEventType();
 
-      /* noti to viewers */
-      facade_->DelegateReporterEvent(info, i);
+    for (auto& i : noti_list) {
+      switch(type) {
+      case IEventInfo::EventType::Update:
+        OnUpdate(info, i);
+        break;
+      case IEventInfo::EventType::Delete:
+        OnDelete(info, i);
+        break;
+      default :
+        break;
+      }
     }
   }
 
   list<shared_ptr<item::AbstractItem>> OnRequestEvent(const IEventInfo& info) override {
     DBG("Get report !!! %s", info.GetItemId().c_str());
-    DBG("Get !!! uid(%d)", (int)(((const IEventInfoInternal&)info).GetUid()));
-    list<shared_ptr<item::AbstractItem>> ret {
-      make_shared<item::ButtonItem>(info.GetItemId().c_str(), "title")
-    };
-    return ret;
+    return DBManager::GetNotificationList(((const IEventInfoInternal&)info).GetUid());
+  }
+
+  int UpdateHideApp(shared_ptr<item::AbstractItem> updatedItem) {
+    int ret;
+    string hide_list;
+    int uid = static_pointer_cast<IItemInfoInternal>(updatedItem->GetInfo())->GetUid();
+    string map_key = updatedItem->GetId() + updatedItem->GetSenderAppId() + string(to_string(uid));
+    list<string> updated_hide_list = static_pointer_cast<IItemInfoInternal>(updatedItem->GetInfo())->GetHideViewerList();
+
+    /* Check new hide app */
+    auto noti = facade_->hide_map_.find(map_key);
+    if (noti == facade_->hide_map_.end()) {
+      if (updated_hide_list.size() > 0) {
+        for (auto& i : updated_hide_list)
+          hide_list += i + ";";
+
+        ret = DBManager::UpdateHideList(updatedItem, hide_list);
+        if (ret != NOTIFICATION_ERROR_NONE)
+          return ret;
+
+        facade_->hide_map_[map_key] = hide_list;
+        return NOTIFICATION_ERROR_NONE;
+      }
+    }
+
+    /* Check new hide app */
+    hide_list = noti->second;
+    for (auto& i : updated_hide_list) {
+      if(hide_list.find(i + ";") == string::npos) {
+        string new_hide_list = hide_list + i + ";";
+        ret = DBManager::UpdateHideList(updatedItem, new_hide_list);
+        if (ret != NOTIFICATION_ERROR_NONE)
+          return ret;
+
+        facade_->hide_map_[map_key] = new_hide_list;
+        return NOTIFICATION_ERROR_NONE;
+      }
+    }
+
+    return NOTIFICATION_ERROR_NOT_EXIST_ID;
   }
 
  public:
@@ -90,33 +189,87 @@ class DPMReporter : public Reporter {
 
 class DPMManager : public Manager {
  protected:
-  void OnAdd(const IEventInfo& info,
+  void UpdateHideApp(shared_ptr<item::AbstractItem> updatedItem) {
+    string hide_list;
+    int uid = static_pointer_cast<IItemInfoInternal>(updatedItem->GetInfo())->GetUid();
+    string map_key = updatedItem->GetId() + updatedItem->GetSenderAppId() + string(to_string(uid));
+    list<string> updated_hide_list =
+      static_pointer_cast<item::IItemInfoInternal>(updatedItem->GetInfo())->GetHideViewerList();
+
+    auto noti = facade_->hide_map_.find(map_key);
+    if (noti == facade_->hide_map_.end())
+      return;
+
+    /* hide app list synchronization */
+    hide_list = noti->second;
+    istringstream stream(hide_list);
+    string hide_app_id;
+
+    while (getline(stream, hide_app_id, ';')) {
+      list<string>::iterator iter = std::find(updated_hide_list.begin(),
+                                updated_hide_list.end(), hide_app_id);
+      if (iter == updated_hide_list.end())
+        static_pointer_cast<item::IItemInfoInternal>(updatedItem->GetInfo())->AddHideViewer(hide_app_id);
+    }
+  }
+
+  void  OnAdd(const IEventInfo& info,
       list<shared_ptr<item::AbstractItem>> addedItem) override {
     DBG("Add !!!");
-    //TODO: store it to DB
-    for (auto& i : addedItem)
-      facade_->DelegateReporterEvent(info, i);
+    int ret;
+
+    ret = DBManager::InsertNotification(addedItem);
+    if (ret == NOTIFICATION_ERROR_NONE) {
+      for (auto& i : addedItem)
+        facade_->DelegateReporterEvent(info, i);
+    } else {
+        DBG("SendError !!!");
+        SendError(info, static_cast<NotificationError>(ret));
+
+    }
   }
 
   void OnUpdate(const IEventInfo& info,
       shared_ptr<item::AbstractItem> updatedItem) override {
     DBG("Update !!!");
-    facade_->DelegateReporterEvent(info, updatedItem);
+    int ret;
+
+    UpdateHideApp(updatedItem);
+
+    ret = DBManager::UpdateNotification(updatedItem);
+    if (ret == NOTIFICATION_ERROR_NONE) {
+      facade_->DelegateReporterEvent(info, updatedItem);
+    } else {
+      SendError(info, static_cast<NotificationError>(ret));
+    }
   }
 
   void OnDelete(const IEventInfo& info,
       shared_ptr<item::AbstractItem> deletedItem) override {
     DBG("Delete !!!");
-    facade_->DelegateReporterEvent(info, deletedItem);
+    int ret;
+
+    ret = DBManager::DeleteNotification(deletedItem);
+    if (ret == NOTIFICATION_ERROR_NONE) {
+      facade_->DelegateReporterEvent(info, deletedItem);
+    } else {
+      SendError(info, static_cast<NotificationError>(ret));
+    }
+
   }
 
   list<shared_ptr<item::AbstractItem>> OnRequestEvent(const IEventInfo& info) override {
-    DBG("Get !!! %s", info.GetItemId().c_str());
-    //TODO: Get it from DB
-    list<shared_ptr<item::AbstractItem>> ret {
-      make_shared<item::ButtonItem>(info.GetItemId().c_str(), "title")
-    };
-    return ret;
+    DBG("Get !!! %s", info.GetOwner().c_str());
+
+    if (info.GetItemId().empty()) {
+      /* get */
+      return DBManager::GetNotificationList(info.GetOwner(),
+                         ((const IEventInfoInternal&)info).GetUid());
+    } else {
+      /*  FindByRootID */
+      return DBManager::GetNotificationList(info.GetOwner(), info.GetItemId(),
+                         ((const IEventInfoInternal&)info).GetUid());
+    }
   }
 
  public:
@@ -142,14 +295,14 @@ HAPI int notification_ex_service_init() {
     )
   );
 
-  return 0;
+  return NOTIFICATION_ERROR_NONE;
 }
 
 HAPI int notification_ex_service_fini() {
   delete facade_;
-  return 0;
+  return NOTIFICATION_ERROR_NONE;
 }
 
 HAPI GDBusConnection* notification_ex_service_get_gdbus_connection() {
   return DBusConnectionManager::GetInst().GetConnection();
-}
\ No newline at end of file
+}