Merge "[Account] update logs" into tizen_3.0
[platform/core/api/webapi-plugins.git] / src / content / content_filter.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "content/content_filter.h"
18
19 #include <vector>
20
21 #include "common/converter.h"
22 #include "common/logger.h"
23
24 using common::AttributeMatchFlag;
25 using common::CompositeFilterType;
26 using common::ErrorCode;
27 using common::JsonCast;
28 using common::PlatformResult;
29
30 namespace extension {
31 namespace content {
32
33 namespace {
34
35 std::map<std::string, std::string> const attributeNameMap = {
36     {"id", "MEDIA_ID"},
37     {"type", "MEDIA_TYPE"},
38     {"mimeType", "MEDIA_MIME_TYPE"},
39     {"name", "MEDIA_DISPLAY_NAME"},
40     {"title", "MEDIA_TITLE"},
41     {"contentURI", "MEDIA_PATH"},
42     {"thumbnailURIs", "MEDIA_THUMBNAIL_PATH"},
43     {"description", "MEDIA_DESCRIPTION"},
44     {"rating", "MEDIA_RATING"},
45     {"createdDate", "MEDIA_ADDED_TIME"},
46     {"releaseDate", "MEDIA_DATETAKEN"},
47     {"modifiedDate", "MEDIA_MODIFIED_TIME"},
48     {"geolocation.latitude", "MEDIA_LATITUDE"},
49     {"geolocation.longitude", "MEDIA_LONGITUDE"},
50     {"duration", "MEDIA_DURATION"},
51     {"album", "MEDIA_ALBUM"},
52     {"artists", "MEDIA_ARTIST"},
53     {"width", "MEDIA_WIDTH"},
54     {"height", "MEDIA_HEIGHT"},
55     {"genres", "MEDIA_GENRE"},
56     {"size", "MEDIA_SIZE"},
57 };
58
59 std::string escapeValueString(const std::string& data) {
60   LoggerD("Enter");
61   std::string out;
62   // If string won't be resized, then it will be faster
63   out.reserve(data.size());
64   for (auto c : data) {
65     if (c == '\\')
66       out += "\\\\";
67     else if (c == '\"')
68       out += "\\\"";
69     else if (c == '\'')
70       out += "\\\'";
71     else if (c == '\n')
72       out += "\\\n";
73     else if (c == '\r')
74       out += "\\\r";
75     else
76       out += c;
77   }
78   return out;
79 }
80
81 }  // namespace
82
83 PlatformResult ContentFilter::MapField(const std::string& name,
84                                        std::string* result) {
85   LoggerD("Enter");
86   auto it = attributeNameMap.find(name);
87   if (it != attributeNameMap.end())
88     *result = it->second;
89   else
90   {
91     return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR);
92   }
93   return PlatformResult(ErrorCode::NO_ERROR);
94 }
95
96 PlatformResult ContentFilter::BuildQuery(const picojson::object& jsFilter,
97                                          std::string* queryToCall) {
98   LoggerD("Enter");
99   std::vector<std::vector<std::string> > partialqueries;
100   partialqueries.push_back(std::vector<std::string>());
101
102   visitor.SetOnAttributeFilter([&](const std::string& name,
103                                    AttributeMatchFlag match_flag,
104                                    const picojson::value& match_value) {
105     LoggerD("entered OnAttributeFilter");
106
107
108     std::string query;
109     std::string matchValue;
110
111     PlatformResult result = MapField(name, &query);
112     if (!result)
113       return result;
114
115     if (AttributeMatchFlag::kExactly == match_flag ||
116         AttributeMatchFlag::kFullString == match_flag) {
117       query += " = ";
118     } else if (AttributeMatchFlag::kContains == match_flag ||
119                AttributeMatchFlag::kStartsWith == match_flag ||
120                AttributeMatchFlag::kEndsWith == match_flag) {
121       query += " LIKE ";
122     } else if (AttributeMatchFlag::kExists == match_flag) {
123       query += " IS NOT NULL ";
124     } else {
125       return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR);
126     }
127     if (AttributeMatchFlag::kExists != match_flag) {
128       query.append("\"");
129       matchValue = escapeValueString(JsonCast<std::string>(match_value));
130       if (name == "type") {
131         if (matchValue == "IMAGE") {
132           matchValue = "0";
133         } else if (matchValue == "VIDEO") {
134           matchValue = "1";
135         } else if (matchValue == "AUDIO") {
136           matchValue = "3";
137         } else {  // OTHER
138           matchValue = "4";
139         }
140       } else if (name == "contentURI") {
141         const char* uri_prefix = "file://";
142         size_t found = matchValue.find(uri_prefix);
143         if (found != std::string::npos) {
144           //simple convertion of URI to globalpath
145           matchValue = matchValue.substr(found + strlen(uri_prefix));
146         }
147       }
148       switch (match_flag) {
149         case AttributeMatchFlag::kStartsWith :
150           query += matchValue + "%";
151           break;
152         case AttributeMatchFlag::kEndsWith :
153           query += "%" + matchValue;
154           break;
155         case AttributeMatchFlag::kContains :
156           query += "%" + matchValue + "%";
157           break;
158         default :
159           query += matchValue;
160       }
161       query.append("\"");
162     }
163
164     partialqueries.back().push_back(query);
165
166     return result;
167   });
168
169   visitor.SetOnCompositeFilterBegin([&](CompositeFilterType type) {
170     LoggerD("entered OnCompositeFilterBegin");
171     partialqueries.push_back(std::vector<std::string>());
172     return PlatformResult(ErrorCode::NO_ERROR);
173   });
174
175   visitor.SetOnCompositeFilterEnd([&](CompositeFilterType calType) {
176     LoggerD("entered OnCompositeFilterEnd");
177     std::string finalQuery;
178     std::string separator;
179
180     if (CompositeFilterType::kUnion == calType)
181       separator = " OR ";
182     else
183       separator = " AND ";
184
185     if (partialqueries.back().empty()) {
186       partialqueries.pop_back();
187       return PlatformResult(ErrorCode::NO_ERROR);
188     }
189     if (partialqueries.back().size() != 1)
190       finalQuery.append("(");
191
192     for (unsigned long i = 0; i < partialqueries.back().size(); i++) {
193       finalQuery += partialqueries.back().at(i);
194       if (i != partialqueries.back().size() - 1) {
195         finalQuery += separator;
196       }
197     }
198
199     if (partialqueries.back().size() != 1)
200       finalQuery.append(")");
201     partialqueries.pop_back();
202     partialqueries.back().push_back(finalQuery);
203
204     return PlatformResult(ErrorCode::NO_ERROR);
205   });
206
207   visitor.SetOnAttributeRangeFilter([&](const std::string& name,
208                                         const picojson::value& initial_value,
209                                         const picojson::value& end_value) {
210     LoggerD("entered OnAttributeFilter");
211
212     std::string query = "";
213     std::string paramName;
214     PlatformResult result = MapField(name, &paramName);
215     if (!result)
216       return result;
217
218     std::string initialValue = escapeValueString(JsonCast<std::string>(initial_value));
219     std::string endValue = escapeValueString(JsonCast<std::string>(end_value));
220     query += paramName;
221     query += " >= \"";
222     query += initialValue;
223     query += "\" AND ";
224     query += paramName;
225     query += " <= \"";
226     query += endValue;
227     query += "\"";
228     partialqueries.back().push_back(query);
229
230     return result;
231   });
232
233   if (!visitor.Visit(jsFilter)) {
234     return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR);
235   }
236
237   if (partialqueries.empty()) {
238     return LogAndCreateResult(ErrorCode::SYNTAX_ERR);
239   }
240   if (partialqueries.back().empty()) {
241     LoggerD("Resolved to empty string!");
242     *queryToCall = "";
243     return PlatformResult(ErrorCode::NO_ERROR);
244   }
245
246   *queryToCall = partialqueries.back().front();
247   return PlatformResult(ErrorCode::NO_ERROR);
248 }
249
250 }  // namespace content
251 }  // namespace extension