2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <Commons/Exception.h>
21 #include "MediaSearchVisitor.h"
26 namespace Mediacontent {
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 ("%");
54 map<string, int> MediaSearchVisitor::attrEnumMap = {
55 {"id", media_attribute_e::CONTACT_ID},
56 {"title", media_attribute_e::MEDIA_TITLE},
57 {"filePath", media_attribute_e::MEDIA_FILEPATH},
58 {"thumbnailPath", media_attribute_e::MEDIA_THUMBNAILPATH},
59 {"createdDate", media_attribute_e::MEDIA_CREATEDDATE},
60 {"modifiedDate", media_attribute_e::MEDIA_MODIFIEDDATE},
61 {"description", media_attribute_e::MEDIA_DESCRIPTION},
62 {"rating", media_attribute_e::MEDIA_RATING},
66 map<string, string> MediaSearchVisitor::attrFolderEnumMap = {
68 {"folderURI", "folder_path"},
70 {"storageType", "storage_type"},
71 {"modifiedDate", "modified_date"},
74 map<string, string> MediaSearchVisitor::attrMediaEnumMap = {
75 //media item attribues.
77 {"type", "content_type"},
78 // {"mimeType", "mime_type"},
79 {"title", "display_name"},
81 {"thumbnailURIs", "thumbnail_path"},
82 {"description", "description"},
84 {"createdDate", "created_date"},
85 {"releasedDate", "released_date"},
86 {"modifiedDate", "modified_date"},
87 //media image,video,audio attribues.
88 {"geolocation.latitude", "latitude"},
89 {"geolocation.longitude", "longitude"},
91 {"artists", "artist"},
94 {"playedTime", "last_played_time"},
95 {"playCount", "played_count"},
100 MediaSearchVisitor::MediaSearchVisitor()
105 MediaSearchVisitor::~MediaSearchVisitor()
110 void MediaSearchVisitor::visitPreComposite(FilterType& type, int depth)
112 m_query.append(STR_LEFT_BRACKET);
115 void MediaSearchVisitor::visitInComposite(FilterType& type, int depth)
117 if(type == UNION_FILTER)
118 m_query.append(STR_OR);
119 else if(type == INTERSECTION_FILTER)
120 m_query.append(STR_AND);
123 void MediaSearchVisitor::visitPostComposite(FilterType& type, int depth)
125 m_query.append(STR_RIGHT_BRACKET);
129 string MediaSearchVisitor::getPlatformAttr(string attrName)
132 map<string, string>::iterator pos;
134 if( queryType == QUERY_FOLDER)
136 pos = attrFolderEnumMap.find(attrName);
138 if(attrFolderEnumMap.end() != pos)
140 platformAttr = pos->second;
144 ThrowMsg(UnsupportedException, "Attribute(" << attrName << ") is not supported.");
147 else if(queryType == QUERY_MEDIA)
149 pos = attrMediaEnumMap.find(attrName);
151 if(attrMediaEnumMap.end() != pos)
153 platformAttr = pos->second;
157 ThrowMsg(UnsupportedException, "Attribute(" << attrName << ") is not supported.");
165 void MediaSearchVisitor::visitAttribute(string& attrName, MatchFlag& matchFlag, AnyPtr& matchValue, int depth)
167 string attrPlatform = getPlatformAttr(attrName);
168 if(matchValue == NULL)
173 m_query.append(convertAttribute(attrPlatform, matchValue, matchFlag));
176 string MediaSearchVisitor::toDateDbStr(const tm &date) const
179 ss << setfill('0') << setiosflags(ios::right) << setw(4) << (date.tm_year + 1900);
180 ss << setfill('0') << setiosflags(ios::right) << setw(2) << (date.tm_mon + 1);
181 ss << setfill('0') << setiosflags(ios::right) << setw(2) << date.tm_mday;
186 string MediaSearchVisitor::convertAttribute(string &attrName, AnyPtr& matchValue, MatchFlag& matchFlag)
194 string matchValueStr;
196 if(attrName.compare("content_type")==0)
198 if(matchValue->toString().compare("IMAGE")==0)
200 if(matchValue->toString().compare("VIDEO")==0)
202 if(matchValue->toString().compare("AUDIO")==0)
204 //Todo. another case throw the exeption.
208 matchValueStr = matchValue->toString();
211 if(matchValue->getType() == PrimitiveType_Time)
213 tm date = matchValue->getDateTm();
214 valueStr = toDateDbStr(date);
219 //STARTSWITH('%, ENDSWITH, EXISTS
220 //valueStr = matchValue->toString();
224 if(matchFlag == MATCH_STARTSWITH)
227 os << STR_S_QUOTATION << matchValueStr << STR_PERCENT << STR_S_QUOTATION;
229 else if( matchFlag == MATCH_ENDSWITH )
232 os << STR_S_QUOTATION << STR_PERCENT << matchValueStr << STR_S_QUOTATION;
234 else if( matchFlag == MATCH_CONTAINS )
237 os << STR_S_QUOTATION << STR_PERCENT << matchValueStr << STR_PERCENT << STR_S_QUOTATION;
242 os << STR_S_QUOTATION << matchValueStr << STR_S_QUOTATION;
248 operatorStr = operatorKey[cond];
250 conditionStr = STR_LEFT_BRACKET + attrName + operatorStr + valueStr + STR_RIGHT_BRACKET;
256 void MediaSearchVisitor::visitAttributeRange(string& attrName,AnyPtr& initialValue,AnyPtr& endValue,int depth)
258 if(initialValue == NULL || endValue == NULL)
262 string initialValueStr;
266 if(!initialValue->isNullOrUndefined())
268 if(initialValue->getType() == PrimitiveType_Time)
270 tm date = initialValue->getDateTm();
271 initialValueStr = toDateDbStr(date);
276 initialValueStr = initialValue->toString();
280 if (!endValue->isNullOrUndefined())
282 if(endValue->getType() == PrimitiveType_Time)
284 tm date = endValue->getDateTm();
285 endValueStr = toDateDbStr(date);
289 endValueStr = endValue->toString();
293 if (!initialValue->isNullOrUndefined() && endValue->isNullOrUndefined())
295 str = STR_LEFT_BRACKET + attrName + STR_GREATER_THAN + STR_EQUAL + STR_S_QUOTATION + initialValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
297 else if(initialValue->isNullOrUndefined() && !endValue->isNullOrUndefined())
299 str = STR_LEFT_BRACKET + attrName + STR_LESS_THAN + STR_S_QUOTATION + endValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
301 else if (!initialValue->isNullOrUndefined() && !endValue->isNullOrUndefined())
303 str = STR_LEFT_BRACKET + attrName + STR_GREATER_THAN + STR_EQUAL + STR_S_QUOTATION + initialValueStr + STR_S_QUOTATION + STR_AND + attrName + STR_LESS_THAN + STR_S_QUOTATION + endValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
310 string MediaSearchVisitor::getResult() const
315 void MediaSearchVisitor::setQueryType(QueryType value)