2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
20 #include <Commons/Exception.h>
21 #include "ContentSearchVisitor.h"
22 #include "ContentUtility.h"
28 const string STR_LEFT_BRACKET(" (");
29 const string STR_RIGHT_BRACKET(") ");
30 const string STR_AND(" AND ");
31 const string STR_OR(" OR ");
32 const string STR_GREATER_THAN (">");
33 const string STR_LESS_THAN ("<");
34 const string STR_EQUAL ("=");
35 const string STR_S_QUOTATION ("'");
36 const string STR_PERCENT ("%");
37 const string STR_MIN_LATITUDE("-90");
38 const string STR_MAX_LATITUDE("90");
39 const string STR_MIN_LONGITUDE("-180");
40 const string STR_MAX_LONGITUDE("180");
58 map<string, string> MediaSearchVisitor::attrFolderEnumMap = {
60 {"directoryURI", "FOLDER_PATH"},
61 {"title", "FOLDER_NAME"},
62 {"storageType", "FOLDER_STORAGE_TYPE"},
63 {"modifiedDate", "FOLDER_MODIFIED_TIME"},
66 map<string, string> MediaSearchVisitor::attrMediaEnumMap = {
67 //media item attribues.
69 {"type", "MEDIA_TYPE"},
70 {"mimeType", "MEDIA_MIME_TYPE"},
71 {"name", "MEDIA_DISPLAY_NAME"},
72 {"title", "MEDIA_TITLE"},
73 {"contentURI", "MEDIA_PATH"},
74 {"thumbnailURIs", "MEDIA_THUMBNAIL_PATH"},
75 {"description", "MEDIA_DESCRIPTION"},
76 {"rating", "MEDIA_RATING"},
77 {"createdDate", "MEDIA_ADDED_TIME"},
78 {"releaseDate", "MEDIA_DATETAKEN"},
79 {"modifiedDate", "MEDIA_MODIFIED_TIME"},
80 //media image,video,audio attribues.
81 {"geolocation.latitude", "MEDIA_LATITUDE"},
82 {"geolocation.longitude", "MEDIA_LONGITUDE"},
83 {"album", "MEDIA_ALBUM"},
84 {"artists", "MEDIA_ARTIST"},
85 {"width", "MEDIA_WIDTH"},
86 {"height", "MEDIA_HEIGHT"},
87 {"genres", "MEDIA_GENRE"},
88 {"size", "MEDIA_SIZE"},
92 MediaSearchVisitor::MediaSearchVisitor()
97 MediaSearchVisitor::~MediaSearchVisitor()
102 void MediaSearchVisitor::visitPreComposite(FilterType& type, int depth)
104 m_query.append(STR_LEFT_BRACKET);
107 void MediaSearchVisitor::visitInComposite(FilterType& type, int depth)
109 if(type == UNION_FILTER)
110 m_query.append(STR_OR);
111 else if(type == INTERSECTION_FILTER)
112 m_query.append(STR_AND);
114 ThrowMsg(PlatformException, "Filter Type is wrong.");
118 void MediaSearchVisitor::visitPostComposite(FilterType& type, int depth)
120 m_query.append(STR_RIGHT_BRACKET);
124 string MediaSearchVisitor::getPlatformAttr(string attrName)
127 map<string, string>::iterator pos;
129 if( queryType == QUERY_FOLDER)
131 pos = attrFolderEnumMap.find(attrName);
133 if(attrFolderEnumMap.end() != pos)
135 platformAttr = pos->second;
139 ThrowMsg(PlatformException, "Attribute(" << attrName << ") is not supported.");
142 else if(queryType == QUERY_MEDIA)
144 pos = attrMediaEnumMap.find(attrName);
146 if(attrMediaEnumMap.end() != pos)
148 platformAttr = pos->second;
152 ThrowMsg(PlatformException, "Attribute(" << attrName << ") is not supported.");
160 void MediaSearchVisitor::visitAttribute(string& attrName, MatchFlag& matchFlag, AnyPtr& matchValue, int depth)
162 string attrPlatform = getPlatformAttr(attrName);
163 if(matchValue == NULL)
165 ThrowMsg(PlatformException, "matchValue is not valid data.");
168 m_query.append(convertAttribute(attrPlatform, matchValue, matchFlag));
171 string MediaSearchVisitor::toDateDbStr(tm &date) const
174 time = mktime(&date);
180 string MediaSearchVisitor::convertAttribute(string &attrName, AnyPtr& matchValue, MatchFlag& matchFlag)
186 string matchValueStr;
188 if(attrName.compare("MEDIA_TYPE")==0)
190 if(matchValue->getString().compare("IMAGE")==0)
192 else if(matchValue->getString().compare("VIDEO")==0)
194 else if(matchValue->getString().compare("AUDIO")==0)
196 else if(matchValue->getString().compare("OTHER")==0)
200 ThrowMsg(PlatformException, "mediaType(" << matchValue->getString() << ") is not supported.");
203 else if(attrName.compare("MEDIA_PATH")==0 || attrName.compare("MEDIA_THUMBNAIL_PATH")==0)
205 matchValueStr = ContentUtility::convertUriToPath(matchValue->getString());
207 else if(attrName.compare("MEDIA_LATITUDE")==0){
208 if(matchValue->getDouble() < MIN_LATITUDE ||
209 matchValue->getDouble() > MAX_LATITUDE)
211 ThrowMsg(PlatformException, "latitude range is -90 ~ 90");
215 matchValueStr = matchValue->toString();
218 else if(attrName.compare("MEDIA_LONGITUDE")==0){
219 if(matchValue->getDouble() < MIN_LONGITUDE||
220 matchValue->getDouble() > MAX_LONGITUDE)
222 ThrowMsg(PlatformException, "longitude range is -180 ~ 180");
226 matchValueStr = matchValue->toString();
231 matchValueStr = matchValue->toString();
234 if(matchValue->isType(PrimitiveType_Time))
236 tm date = *matchValue->getDateTm();
237 if(attrName.compare("MEDIA_DATETAKEN")==0)
240 strftime(buf, sizeof(buf), "'%Y:%m:%d %H:%M:%S'", &date);
245 valueStr = toDateDbStr(date);
247 m_operationKey = EQUAL;
256 m_operationKey = LIKE;
257 os << STR_S_QUOTATION << STR_PERCENT << matchValueStr << STR_PERCENT << STR_S_QUOTATION;
259 case MATCH_STARTSWITH:
260 m_operationKey = LIKE;
261 os << STR_S_QUOTATION << matchValueStr << STR_PERCENT << STR_S_QUOTATION;
264 m_operationKey = LIKE;
265 os << STR_S_QUOTATION << STR_PERCENT << matchValueStr << STR_S_QUOTATION;
267 case MATCH_FULLSTRING:
268 m_operationKey = FULLSTRING;
269 os << STR_S_QUOTATION << matchValueStr << STR_S_QUOTATION << " COLLATE NOCASE ";
272 m_operationKey = EXISTS;
276 m_operationKey = EQUAL;
277 os << STR_S_QUOTATION << matchValueStr << STR_S_QUOTATION;
283 operatorStr = operatorKey[m_operationKey];
285 if( m_operationKey != EXISTS)
287 conditionStr = STR_LEFT_BRACKET + attrName + operatorStr + valueStr + STR_RIGHT_BRACKET;
291 conditionStr = STR_LEFT_BRACKET + attrName + operatorStr + STR_RIGHT_BRACKET;
297 condition_e MediaSearchVisitor::getOperationKey()
299 return m_operationKey;
303 void MediaSearchVisitor::visitAttributeRange(string& attrName,AnyPtr& initialValue,AnyPtr& endValue,int depth)
305 if(initialValue == NULL || endValue == NULL)
309 string initialValueStr;
312 string attrPlatform = getPlatformAttr(attrName);
314 if(!initialValue->isNullOrUndefined())
316 if(initialValue->isType(PrimitiveType_Time))
318 tm date = *initialValue->getDateTm();
319 if(attrPlatform.compare("MEDIA_DATETAKEN")==0)
322 strftime(buf, sizeof(buf), "'%Y:%m:%d %H:%M:%S'", &date);
323 initialValueStr = buf;
327 initialValueStr = toDateDbStr(date);
332 initialValueStr = initialValue->toString();
336 if (!endValue->isNullOrUndefined())
338 if(endValue->isType(PrimitiveType_Time))
340 tm date = *endValue->getDateTm();
341 if(attrPlatform.compare("MEDIA_DATETAKEN")==0)
344 strftime(buf, sizeof(buf), "'%Y:%m:%d %H:%M:%S'", &date);
349 endValueStr = toDateDbStr(date);
354 endValueStr = endValue->toString();
358 if (!initialValue->isNullOrUndefined() && endValue->isNullOrUndefined())
360 initialValueStr = initialValue->toString();
361 if(attrPlatform.compare("MEDIA_LATITUDE")==0)
363 if(initialValue->getDouble() < MIN_LATITUDE)
365 initialValueStr = STR_MIN_LATITUDE;
368 else if(attrPlatform.compare("MEDIA_LONGITUDE")==0)
370 if(initialValue->getDouble() < MIN_LONGITUDE)
372 initialValueStr = STR_MIN_LONGITUDE;
375 str = STR_LEFT_BRACKET + attrPlatform + STR_GREATER_THAN + STR_EQUAL +
376 STR_S_QUOTATION + initialValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
378 else if(initialValue->isNullOrUndefined() && !endValue->isNullOrUndefined())
380 if(attrPlatform.compare("MEDIA_LATITUDE")==0)
382 initialValueStr = STR_MIN_LATITUDE;
383 str = STR_LEFT_BRACKET + attrPlatform + STR_GREATER_THAN + STR_EQUAL + STR_S_QUOTATION +
384 initialValueStr + STR_S_QUOTATION + STR_AND + attrPlatform + STR_LESS_THAN + STR_S_QUOTATION +
385 endValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
387 else if(attrPlatform.compare("MEDIA_LONGITUDE")==0)
389 initialValueStr = STR_MIN_LONGITUDE;
390 str = STR_LEFT_BRACKET + attrPlatform + STR_GREATER_THAN + STR_EQUAL + STR_S_QUOTATION +
391 initialValueStr + STR_S_QUOTATION + STR_AND + attrPlatform + STR_LESS_THAN + STR_S_QUOTATION +
392 endValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
396 str = STR_LEFT_BRACKET + attrPlatform + STR_LESS_THAN + STR_S_QUOTATION +
397 endValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
400 else if (!initialValue->isNullOrUndefined() && !endValue->isNullOrUndefined())
402 if(attrPlatform.compare("MEDIA_LATITUDE")==0)
404 if(initialValue->getDouble() < MIN_LATITUDE)
406 initialValueStr = STR_MIN_LATITUDE;
409 else if(attrPlatform.compare("MEDIA_LONGITUDE")==0)
411 if(initialValue->getDouble() < MIN_LONGITUDE)
413 initialValueStr = STR_MIN_LONGITUDE;
416 str = STR_LEFT_BRACKET + attrPlatform + STR_GREATER_THAN + STR_EQUAL + STR_S_QUOTATION +
417 initialValueStr + STR_S_QUOTATION + STR_AND + attrPlatform + STR_LESS_THAN + STR_S_QUOTATION +
418 endValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
425 string MediaSearchVisitor::getResult() const
430 void MediaSearchVisitor::setQueryType(QueryType value)