[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 c308c9623855f6856cd5256627d0211df092cf92..217a96ed69a9df15412ad343d09fdbc075c3ea85 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 44630dfc543a6d6bc3d03e1849c6f18c5ed1fca2..7cb713163bb9c575150b775d28460a87fe16c9d1 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 5a70f031949510e98469da7190a25cd2b37172ad..4efa98aa42e6c84385b50817496e3e880ecf2137 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 4c063462a83980a9e165b751bb4a201b8c19d709..7e5a6624844bbc4bcc531f81cb429c2b74fb9b59 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 267faa2a855b75c22f0529e934602be4d80ef696..a1ae828ebc9f291f0b0f838c06bfa93a7eabb8ba 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 275c61c5922f9f8b8fa6c0a1388bdb80a4adf13a..2fff4e1df15ae914c6300ee9bf78d877859d7845 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 696e75dffc21e3e87a246c280f747cb1c4df7c55..a9aa8b74a4cb2d9ae7a5356da28700ae723b5d6a 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 98418e1c46e33ce5fc1dfc71a062f490a20405b4..a8a530fd023361c1ef816947a8ecdba3b1300d15 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 7f8e14068e69c13665f15d9b36a0696c8f688c40..a9d2431935eee48edc4526271835cf8666a74e71 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 72dca01a0223bea4af371bef0cd64a4e7d3590fc..bad9ce0d9597ffc4c5be6851252489e5dd792f1c 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 5bce20280646c0df5d02eef965aa4528c38724a1..c84c28f4fef8167e464b096720378df36c190b6a 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));
     }
   }