*/
#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 {
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());
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;
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",
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;
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());
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;
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());
};
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);
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;
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));
}
}
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));
}
}