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 ("%");
55 map<string, string> MediaSearchVisitor::attrFolderEnumMap = {
57 {"folderURI", "FOLDER_PATH"},
58 {"title", "FOLDER_NAME"},
59 {"storageType", "FOLDER_STORAGE_TYPE"},
60 {"modifiedDate", "FOLDER_MODIFIED_TIME"},
63 map<string, string> MediaSearchVisitor::attrMediaEnumMap = {
64 //media item attribues.
66 {"type", "MEDIA_TYPE"},
67 {"mimeType", "MEDIA_MIME_TYPE"},
68 {"title", "MEDIA_TITLE"},
69 {"itemURI", "MEDIA_PATH"},
70 {"thumbnailURIs", "MEDIA_THUMBNAIL_PATH"},
71 {"description", "MEDIA_DESCRIPTION"},
72 {"rating", "MEDIA_RATING"},
73 {"createdDate", "MEDIA_ADDED_TIME"},
74 {"releasedDate", "MEDIA_YEAR"},
75 {"modifiedDate", "MEDIA_MODIFIED_TIME"},
76 //media image,video,audio attribues.
77 {"geolocation.latitude", "MEDIA_LATITUDE"},
78 {"geolocation.longitude", "MEDIA_LONGITUDE"},
79 {"album", "MEDIA_ALBUM"},
80 {"artists", "MEDIA_ARTIST"},
81 {"width", "MEDIA_WIDTH"},
82 {"height", "MEDIA_HEIGHT"},
83 {"playedTime", "MEDIA_LAST_PLAYED_TIME"},
84 {"playCount", "MEDIA_PLAYED_COUNT"},
85 {"genres", "MEDIA_GENRE"},
86 {"size", "MEDIA_SIZE"},
90 MediaSearchVisitor::MediaSearchVisitor()
95 MediaSearchVisitor::~MediaSearchVisitor()
100 void MediaSearchVisitor::visitPreComposite(FilterType& type, int depth)
102 m_query.append(STR_LEFT_BRACKET);
105 void MediaSearchVisitor::visitInComposite(FilterType& type, int depth)
107 if(type == UNION_FILTER)
108 m_query.append(STR_OR);
109 else if(type == INTERSECTION_FILTER)
110 m_query.append(STR_AND);
112 ThrowMsg(PlatformException, "Filter Type is wrong.");
116 void MediaSearchVisitor::visitPostComposite(FilterType& type, int depth)
118 m_query.append(STR_RIGHT_BRACKET);
122 string MediaSearchVisitor::getPlatformAttr(string attrName)
125 map<string, string>::iterator pos;
126 LogDebug("attrName=" << attrName);
127 LogDebug("queryType=" << queryType);
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);
177 // ss << setfill('0') << setiosflags(ios::right) << setw(4) << (date.tm_year + 1900);
178 // ss << setfill('0') << setiosflags(ios::right) << setw(2) << (date.tm_mon + 1);
179 // ss << setfill('0') << setiosflags(ios::right) << setw(2) << date.tm_mday;
184 string MediaSearchVisitor::convertAttribute(string &attrName, AnyPtr& matchValue, MatchFlag& matchFlag)
191 string matchValueStr;
193 if(attrName.compare("MEDIA_TYPE")==0)
195 if(matchValue->toString().compare("IMAGE")==0)
197 else if(matchValue->toString().compare("VIDEO")==0)
199 else if(matchValue->toString().compare("AUDIO")==0)
203 ThrowMsg(PlatformException, "mediaType(" << matchValue->toString() << ") is not supported.");
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 if (attrName.compare("MEDIA_TITLE")==0)
252 string imageCond = STR_LEFT_BRACKET;
253 imageCond += STR_LEFT_BRACKET + " MEDIA_TYPE=0 " + STR_RIGHT_BRACKET;
254 imageCond += STR_AND + " MEDIA_DISPLAY_NAME " + operatorStr + valueStr;
255 imageCond += STR_RIGHT_BRACKET;
257 string videoNAudioCond = STR_LEFT_BRACKET;
258 videoNAudioCond += STR_LEFT_BRACKET + "MEDIA_TYPE=1 " + STR_OR + " MEDIA_TYPE=3" + STR_RIGHT_BRACKET;
259 videoNAudioCond += STR_AND + " MEDIA_TITLE " + operatorStr + valueStr;
260 videoNAudioCond += STR_RIGHT_BRACKET;
262 conditionStr = STR_LEFT_BRACKET + imageCond + STR_OR + videoNAudioCond + STR_RIGHT_BRACKET;
266 conditionStr = STR_LEFT_BRACKET + attrName + operatorStr + valueStr + STR_RIGHT_BRACKET;
273 void MediaSearchVisitor::visitAttributeRange(string& attrName,AnyPtr& initialValue,AnyPtr& endValue,int depth)
275 if(initialValue == NULL || endValue == NULL)
279 string initialValueStr;
282 string attrPlatform = getPlatformAttr(attrName);
284 if(!initialValue->isNullOrUndefined())
286 if(initialValue->getType() == PrimitiveType_Time)
288 tm date = initialValue->getDateTm();
289 initialValueStr = toDateDbStr(date);
294 initialValueStr = initialValue->toString();
298 if (!endValue->isNullOrUndefined())
300 if(endValue->getType() == PrimitiveType_Time)
302 tm date = endValue->getDateTm();
303 endValueStr = toDateDbStr(date);
307 endValueStr = endValue->toString();
311 if (!initialValue->isNullOrUndefined() && endValue->isNullOrUndefined())
313 str = STR_LEFT_BRACKET + attrPlatform + STR_GREATER_THAN + STR_EQUAL +
314 STR_S_QUOTATION + initialValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
316 else if(initialValue->isNullOrUndefined() && !endValue->isNullOrUndefined())
318 str = STR_LEFT_BRACKET + attrPlatform + STR_LESS_THAN + STR_S_QUOTATION +
319 endValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
321 else if (!initialValue->isNullOrUndefined() && !endValue->isNullOrUndefined())
323 str = STR_LEFT_BRACKET + attrPlatform + STR_GREATER_THAN + STR_EQUAL + STR_S_QUOTATION +
324 initialValueStr + STR_S_QUOTATION + STR_AND + attrPlatform + STR_LESS_THAN + STR_S_QUOTATION +
325 endValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
332 string MediaSearchVisitor::getResult() const
337 void MediaSearchVisitor::setQueryType(QueryType value)