From: Michal Michalski Date: Thu, 5 Dec 2019 11:16:41 +0000 (+0100) Subject: [messaging] Implement isMatching(json) methods. X-Git-Tag: accepted/tizen/unified/20191212.123659~6^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F20%2F219520%2F2;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [messaging] Implement isMatching(json) methods. + 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 Change-Id: I10799622eb280b252fdd8535d5cd6eaff4c3b447 --- diff --git a/src/messaging/MsgCommon/AbstractFilter.cpp b/src/messaging/MsgCommon/AbstractFilter.cpp index c308c96..217a96e 100644 --- a/src/messaging/MsgCommon/AbstractFilter.cpp +++ b/src/messaging/MsgCommon/AbstractFilter.cpp @@ -15,11 +15,11 @@ */ #include "AbstractFilter.h" +#include +#include #include "common/logger.h" #include "common/platform_exception.h" #include "common/tools.h" -#include -#include namespace extension { namespace tizen { diff --git a/src/messaging/MsgCommon/AttributeFilter.cpp b/src/messaging/MsgCommon/AttributeFilter.cpp index 44630df..7cb7131 100644 --- a/src/messaging/MsgCommon/AttributeFilter.cpp +++ b/src/messaging/MsgCommon/AttributeFilter.cpp @@ -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()) { + 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()); diff --git a/src/messaging/MsgCommon/AttributeFilter.h b/src/messaging/MsgCommon/AttributeFilter.h index 5a70f03..4efa98a 100644 --- a/src/messaging/MsgCommon/AttributeFilter.h +++ b/src/messaging/MsgCommon/AttributeFilter.h @@ -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; diff --git a/src/messaging/MsgCommon/AttributeRangeFilter.cpp b/src/messaging/MsgCommon/AttributeRangeFilter.cpp index 4c06346..7e5a662 100644 --- a/src/messaging/MsgCommon/AttributeRangeFilter.cpp +++ b/src/messaging/MsgCommon/AttributeRangeFilter.cpp @@ -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", diff --git a/src/messaging/MsgCommon/AttributeRangeFilter.h b/src/messaging/MsgCommon/AttributeRangeFilter.h index 267faa2..a1ae828 100644 --- a/src/messaging/MsgCommon/AttributeRangeFilter.h +++ b/src/messaging/MsgCommon/AttributeRangeFilter.h @@ -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; diff --git a/src/messaging/MsgCommon/CompositeFilter.cpp b/src/messaging/MsgCommon/CompositeFilter.cpp index 275c61c..2fff4e1 100644 --- a/src/messaging/MsgCommon/CompositeFilter.cpp +++ b/src/messaging/MsgCommon/CompositeFilter.cpp @@ -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()); diff --git a/src/messaging/MsgCommon/CompositeFilter.h b/src/messaging/MsgCommon/CompositeFilter.h index 696e75d..a9aa8b7 100644 --- a/src/messaging/MsgCommon/CompositeFilter.h +++ b/src/messaging/MsgCommon/CompositeFilter.h @@ -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; diff --git a/src/messaging/message_folder.cc b/src/messaging/message_folder.cc index 98418e1..a8a530f 100644 --- a/src/messaging/message_folder.cc +++ b/src/messaging/message_folder.cc @@ -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().c_str(), fp->attributes.at(MESSAGE_FOLDER_ATTRIBUTE_NAME).get().c_str()); diff --git a/src/messaging/message_folder.h b/src/messaging/message_folder.h index 7f8e140..a9d2431 100644 --- a/src/messaging/message_folder.h +++ b/src/messaging/message_folder.h @@ -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; diff --git a/src/messaging/message_storage_email.cc b/src/messaging/message_storage_email.cc index 72dca01..bad9ce0 100644 --- a/src/messaging/message_storage_email.cc +++ b/src/messaging/message_storage_email.cc @@ -230,7 +230,7 @@ static gboolean FindFoldersGlibTask(void* data) { auto json = picojson::value(picojson::array()); auto& folders_list = json.get(); 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)); } } diff --git a/src/messaging/message_storage_short_msg.cc b/src/messaging/message_storage_short_msg.cc index 5bce202..c84c28f 100644 --- a/src/messaging/message_storage_short_msg.cc +++ b/src/messaging/message_storage_short_msg.cc @@ -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)); } }