[messaging] Implement isMatching(json) methods. 20/219520/2
authorMichal Michalski <m.michalski2@partner.samsung.com>
Thu, 5 Dec 2019 11:16:41 +0000 (12:16 +0100)
committerMichal Michalski <m.michalski2@partner.samsung.com>
Fri, 6 Dec 2019 08:39:00 +0000 (09:39 +0100)
+ Implement AttributeFilter::isMatching(const picojson::value&)
+ Implement AttributeRangeFilter::isMatching(const picojson::value&)
+ Implement CompositeFilter::isMatching(const picojson::value&)
+ Switch to new isMatching() method for MessageFolder objects.

[Verification] tct-messaging-*-tizen-tests 100% pass.

Signed-off-by: Michal Michalski <m.michalski2@partner.samsung.com>
Change-Id: I10799622eb280b252fdd8535d5cd6eaff4c3b447

src/messaging/MsgCommon/AbstractFilter.cpp
src/messaging/MsgCommon/AttributeFilter.cpp
src/messaging/MsgCommon/AttributeFilter.h
src/messaging/MsgCommon/AttributeRangeFilter.cpp
src/messaging/MsgCommon/AttributeRangeFilter.h
src/messaging/MsgCommon/CompositeFilter.cpp
src/messaging/MsgCommon/CompositeFilter.h
src/messaging/message_folder.cc
src/messaging/message_folder.h
src/messaging/message_storage_email.cc
src/messaging/message_storage_short_msg.cc

index c308c96..217a96e 100644 (file)
  */
 
 #include "AbstractFilter.h"
+#include <algorithm>
+#include <limits>
 #include "common/logger.h"
 #include "common/platform_exception.h"
 #include "common/tools.h"
-#include <algorithm>
-#include <limits>
 
 namespace extension {
 namespace tizen {
index 44630df..7cb7131 100644 (file)
@@ -66,6 +66,23 @@ bool AttributeFilter::isMatching(const FilterableObject *const filtered_object)
   return filtered_object->isMatchingAttribute(m_attribute_name, m_match_flag, m_match_value);
 }
 
+bool AttributeFilter::isMatching(const picojson::value &json) const {
+  ScopeLogger();
+  if (!json.is<picojson::object>()) {
+    LoggerE("JSON is not an object. Cannot access attribute %s", m_attribute_name.c_str());
+    return false;
+  }
+  if (!json.contains(m_attribute_name)) {
+    LoggerW("Attribute %s not found.", m_attribute_name.c_str());
+    return false;
+  }
+  if (common::AttributeMatchFlag::kExists == m_match_flag) {
+    LoggerD("Attribute %s found.", m_attribute_name.c_str());
+    return true;
+  }
+  return FilterUtils::isMatching(*m_match_value, json.get(m_attribute_name), m_match_flag);
+}
+
 AttributeFilterPtr castToAttributeFilter(AbstractFilterPtr from) {
   if (ATTRIBUTE_FILTER != from->getFilterType()) {
     LoggerE("Trying to get AttributeFilterPtr but filter's type is: %d", from->getFilterType());
index 5a70f03..4efa98a 100644 (file)
@@ -40,7 +40,8 @@ class AttributeFilter : public AbstractFilter {
   AnyPtr getMatchValue() const;
   void setMatchValue(AnyPtr match_value);
 
-  virtual bool isMatching(const FilterableObject *const filtered_object) const;
+  virtual bool isMatching(const FilterableObject *const filtered_object) const override;
+  virtual bool isMatching(const picojson::value &json) const override;
 
  private:
   std::string m_attribute_name;
index 4c06346..7e5a662 100644 (file)
@@ -64,6 +64,11 @@ bool AttributeRangeFilter::isMatching(const FilterableObject *const filtered_obj
   return filtered_object->isMatchingAttributeRange(m_attribute_name, m_initial_value, m_end_value);
 }
 
+bool AttributeRangeFilter::isMatching(const picojson::value &json) const {
+  LoggerW("AttributeRange filter is not supported for JSON objects.");
+  return false;
+}
+
 AttributeRangeFilterPtr castToAttributeRangeFilter(AbstractFilterPtr from) {
   if (ATTRIBUTE_RANGE_FILTER != from->getFilterType()) {
     LoggerE("Trying to get AttributeRangeFilterPtr but filter's type is: %d",
index 267faa2..a1ae828 100644 (file)
@@ -41,7 +41,8 @@ class AttributeRangeFilter : public AbstractFilter {
   AnyPtr getEndValue() const;
   void setEndValue(AnyPtr end_value);
 
-  virtual bool isMatching(const FilterableObject *const filtered_object) const;
+  virtual bool isMatching(const FilterableObject *const filtered_object) const override;
+  virtual bool isMatching(const picojson::value &json) const override;
 
  private:
   std::string m_attribute_name;
index 275c61c..2fff4e1 100644 (file)
@@ -87,6 +87,30 @@ bool CompositeFilter::isMatching(const FilterableObject* const filtered_object)
   return composite_result;
 }
 
+bool CompositeFilter::isMatching(const picojson::value& json) const {
+  auto src_filters = getFilters();
+  if (src_filters.empty()) {
+    return true;
+  }
+
+  for (const auto& filter : src_filters) {
+    bool match = filter->isMatching(json);
+    if (!match && m_type == INTERSECTION) {
+      // intersection means that all filters need to match to accept object.
+      return false;
+    }
+    if (match && m_type == UNION) {
+      // union means that any matching filter is enough to accept object.
+      return true;
+    }
+  }
+
+  // If its an intersection of filters, and we haven't found a discriminating filter than its a
+  // match.
+  // If its a union of filters, and we haven't found matching filter, than its a mismatch.
+  return m_type == INTERSECTION;
+}
+
 CompositeFilterPtr castToCompositeFilter(AbstractFilterPtr from) {
   if (COMPOSITE_FILTER != from->getFilterType()) {
     LoggerE("Trying to get CompositeFilterPtr but filter's type is: %d", from->getFilterType());
index 696e75d..a9aa8b7 100644 (file)
@@ -37,7 +37,8 @@ class CompositeFilter : public AbstractFilter {
   const AbstractFilterPtrVector& getFilters() const;
   void addFilter(const AbstractFilterPtr& filter);
 
-  virtual bool isMatching(const FilterableObject* const filtered_object) const;
+  virtual bool isMatching(const FilterableObject* const filtered_object) const override;
+  virtual bool isMatching(const picojson::value& json) const override;
 
  private:
   CompositeFilterType m_type;
index 98418e1..a8a530f 100644 (file)
@@ -97,7 +97,7 @@ FolderPtrVector filterFolders(tizen::AbstractFilterPtr filter,
 
   FolderPtrVector filtered_folders;
   for (const auto& fp : source_folders) {
-    if (filter->isMatching(fp.get())) {
+    if (filter->isMatching(picojson::value(fp->attributes))) {
       LoggerD("folder id: %s, folder name: %s, match: yes",
               fp->attributes.at("id").get<std::string>().c_str(),
               fp->attributes.at(MESSAGE_FOLDER_ATTRIBUTE_NAME).get<std::string>().c_str());
index 7f8e140..a9d2431 100644 (file)
@@ -38,8 +38,7 @@ enum MessageFolderType {
 };
 extern const char* MessageFolderTypeStr[MESSAGE_FOLDER_TYPE_NOTSTANDARD];
 
-class MessageFolder : public tizen::FilterableObject {
- public:
+struct MessageFolder : public tizen::FilterableObject {
   MessageFolder(const std::string& id, const std::string& parent_id, const std::string& service_id,
                 const std::string& content_type, const std::string& name, const std::string& path,
                 const std::string& type, bool synchronizable);
@@ -49,6 +48,7 @@ class MessageFolder : public tizen::FilterableObject {
   virtual bool isMatchingAttribute(const std::string& attribute_name,
                                    const common::AttributeMatchFlag match_flag,
                                    tizen::AnyPtr match_value) const override;
+
   virtual bool isMatchingAttributeRange(const std::string& attribute_name,
                                         tizen::AnyPtr initial_value,
                                         tizen::AnyPtr end_value) const override;
index 72dca01..bad9ce0 100644 (file)
@@ -230,7 +230,7 @@ static gboolean FindFoldersGlibTask(void* data) {
   auto json = picojson::value(picojson::array());
   auto& folders_list = json.get<picojson::array>();
   for (const auto& folder : folders) {
-    if (params->filter->isMatching(&folder)) {
+    if (params->filter->isMatching(picojson::value(folder.attributes))) {
       folders_list.push_back(picojson::value(folder.attributes));
     }
   }
index 5bce202..c84c28f 100644 (file)
@@ -216,7 +216,7 @@ static gboolean findFoldersCB(void* data) {
     auto id = std::to_string(array.size() + 1);
     auto service_id = std::to_string(params->account_id);
     MessageFolder folder(id, "", service_id, content_type, type, "", type, false);
-    if (params->filter->isMatching(&folder)) {
+    if (params->filter->isMatching(picojson::value(folder.attributes))) {
       array.push_back(picojson::value(folder.attributes));
     }
   }