-// Copyright 2014 Samsung Electronics Co, Ltd. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
#include "content/content_filter.h"
#include "common/converter.h"
#include "common/logger.h"
+#include "common/tools.h"
using common::AttributeMatchFlag;
using common::CompositeFilterType;
{"createdDate", "MEDIA_ADDED_TIME"},
{"releaseDate", "MEDIA_DATETAKEN"},
{"modifiedDate", "MEDIA_MODIFIED_TIME"},
- {"geolocation.latitude", "MEDIA_LATITUDE"},
- {"geolocation.longitude", "MEDIA_LONGITUDE"},
- {"duration", "MEDIA_DURATION"},
+ {"geolocation.latitude",
+ "MEDIA_LATITUDE"}, // Deprecated since 9.0 //TODO remove after 2 versions
+ {"geolocation.longitude",
+ "MEDIA_LONGITUDE"}, // Deprecated since 9.0 //TODO remove after 2 versions
+ {"duration",
+ "MEDIA_DURATION"}, // Deprecated since 9.0 //TODO remove after 2 versions
{"album", "MEDIA_ALBUM"},
{"artists", "MEDIA_ARTIST"},
{"width", "MEDIA_WIDTH"},
};
std::string escapeValueString(const std::string& data) {
+ ScopeLogger();
std::string out;
// If string won't be resized, then it will be faster
out.reserve(data.size());
} // namespace
-PlatformResult ContentFilter::buildQuery(const picojson::object& jsFilter,
+PlatformResult ContentFilter::MapField(const std::string& name, std::string* result) {
+ ScopeLogger();
+ auto it = attributeNameMap.find(name);
+ if (it != attributeNameMap.end()) {
+ if (name == "rating" || name == "description" ||
+ name == "geolocation.latitude" || name == "geolocation.longitude" ||
+ name == "duration") {
+ std::string warning = "Filtering by attribute '" + name + "'";
+ common::tools::PrintDeprecationWarningFor(warning.c_str());
+ }
+ *result = it->second;
+ } else {
+ return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR);
+ }
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+PlatformResult ContentFilter::BuildQuery(const picojson::object& jsFilter,
std::string* queryToCall) {
- std::vector<std::vector<std::string> > partialqueries;
+ ScopeLogger();
+ std::vector<std::vector<std::string>> partialqueries;
partialqueries.push_back(std::vector<std::string>());
- visitor.SetOnAttributeFilter([&](const std::string& name,
- AttributeMatchFlag match_flag,
+ visitor.SetOnAttributeFilter([&](const std::string& name, AttributeMatchFlag match_flag,
const picojson::value& match_value) {
+ ScopeLogger("Entered into asynchronous function, visitor.SetOnAttributeFilter's argument");
+
std::string query;
- LoggerD("entered OnAttributeFilter");
std::string matchValue;
- auto it = attributeNameMap.find(name);
- if (it != attributeNameMap.end())
- query += it->second;
- else
- return PlatformResult(ErrorCode::INVALID_VALUES_ERR);
+
+ PlatformResult result = MapField(name, &query);
+ if (!result) return result;
if (AttributeMatchFlag::kExactly == match_flag ||
AttributeMatchFlag::kFullString == match_flag) {
} else if (AttributeMatchFlag::kExists == match_flag) {
query += " IS NOT NULL ";
} else {
- return PlatformResult(ErrorCode::INVALID_VALUES_ERR);
+ return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR);
}
- query.append("\"");
-
if (AttributeMatchFlag::kExists != match_flag) {
+ query.append("\"");
matchValue = escapeValueString(JsonCast<std::string>(match_value));
if (name == "type") {
if (matchValue == "IMAGE") {
} else { // OTHER
matchValue = "4";
}
+ } else if (name == "contentURI") {
+ const char* uri_prefix = "file://";
+ size_t found = matchValue.find(uri_prefix);
+ if (found != std::string::npos) {
+ // simple convertion of URI to globalpath
+ matchValue = matchValue.substr(found + strlen(uri_prefix));
+ }
+ }
+ switch (match_flag) {
+ case AttributeMatchFlag::kStartsWith:
+ query += matchValue + "%";
+ break;
+ case AttributeMatchFlag::kEndsWith:
+ query += "%" + matchValue;
+ break;
+ case AttributeMatchFlag::kContains:
+ query += "%" + matchValue + "%";
+ break;
+ default:
+ query += matchValue;
}
- query += matchValue;
+ query.append("\"");
}
- query.append("\"");
+
partialqueries.back().push_back(query);
- LoggerD("about to call with condition %s", query.c_str());
- return PlatformResult(ErrorCode::NO_ERROR);
+ return result;
});
visitor.SetOnCompositeFilterBegin([&](CompositeFilterType type) {
- LoggerD("entered OnCompositeFilterBegin");
+ ScopeLogger("Entered into asynchronous function, visitor.SetOnCompositeFilterBegin's argument");
partialqueries.push_back(std::vector<std::string>());
return PlatformResult(ErrorCode::NO_ERROR);
});
visitor.SetOnCompositeFilterEnd([&](CompositeFilterType calType) {
- LoggerD("entered OnCompositeFilterEnd");
+ ScopeLogger("Entered into asynchronous function, visitor.SetOnCompositeFilterEnd's argument");
std::string finalQuery;
std::string separator;
else
separator = " AND ";
- LoggerD("Composite filter: %i", partialqueries.back().size());
if (partialqueries.back().empty()) {
partialqueries.pop_back();
return PlatformResult(ErrorCode::NO_ERROR);
}
- if (partialqueries.back().size() != 1)
- finalQuery.append("(");
+ if (partialqueries.back().size() != 1) finalQuery.append("(");
for (unsigned long i = 0; i < partialqueries.back().size(); i++) {
finalQuery += partialqueries.back().at(i);
}
}
- if (partialqueries.back().size() != 1)
- finalQuery.append(")");
+ if (partialqueries.back().size() != 1) finalQuery.append(")");
partialqueries.pop_back();
partialqueries.back().push_back(finalQuery);
+
return PlatformResult(ErrorCode::NO_ERROR);
});
visitor.SetOnAttributeRangeFilter([&](const std::string& name,
const picojson::value& initial_value,
const picojson::value& end_value) {
- LoggerD("entered OnAttributeFilter");
+ ScopeLogger("Entered into asynchronous function, visitor.SetOnAttributeRangeFilter's argument");
+
std::string query = "";
std::string paramName;
- auto it = attributeNameMap.find(name);
- if (it != attributeNameMap.end())
- paramName = it->second;
- else
- return PlatformResult(ErrorCode::INVALID_VALUES_ERR);
+ PlatformResult result = MapField(name, ¶mName);
+ if (!result) return result;
+
std::string initialValue = escapeValueString(JsonCast<std::string>(initial_value));
std::string endValue = escapeValueString(JsonCast<std::string>(end_value));
query += paramName;
query += "\"";
partialqueries.back().push_back(query);
- LoggerD("about to call with condition %s", query.c_str());
- return PlatformResult(ErrorCode::NO_ERROR);
+ return result;
});
if (!visitor.Visit(jsFilter)) {
- return PlatformResult(ErrorCode::INVALID_VALUES_ERR);
+ return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR);
}
if (partialqueries.empty()) {
- LoggerE("Filter parsing error!");
- return PlatformResult(ErrorCode::SYNTAX_ERR);
+ return LogAndCreateResult(ErrorCode::SYNTAX_ERR);
}
if (partialqueries.back().empty()) {
LoggerD("Resolved to empty string!");