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 {"folderURL", "folder_path"},
70 {"storageType", "storage_type"},
71 {"modifiedDate", "modified_date"},
74 map<string, string> MediaSearchVisitor::attrMediaEnumMap = {
75 //media folder attributes.
76 {"folder.id", "folder_uuid"},
77 {"folder.title", "folder_name"},
78 {"folder.folderURL", "folder_path"},
79 {"folder.storageType", "storage_type"},
80 //media item attribues.
82 {"type", "content_type"},
83 // {"mimeType", "mime_type"},
84 {"title", "display_name"},
86 {"thumbnailURL", "thumbnail_path"},
87 {"description", "description"},
89 {"createdDate", "created_date"},
90 {"releasedDate", "released_date"},
91 {"modifiedDate", "modified_date"},
92 //media image,video,audio attribues.
93 {"geolocation.latitude", "latitude"},
94 {"geolocation.longitude", "longitude"},
99 {"playedTime", "last_played_time"},
100 {"playCount", "played_count"},
102 {"artist", "artist"},
106 MediaSearchVisitor::MediaSearchVisitor()
111 MediaSearchVisitor::~MediaSearchVisitor()
116 void MediaSearchVisitor::visitPreComposite(FilterType& type, int depth)
118 m_query = m_query + STR_LEFT_BRACKET;
121 void MediaSearchVisitor::visitInComposite(FilterType& type, int depth)
123 if(type == UNION_FILTER)
124 m_query = m_query + STR_OR;
125 else if(type == INTERSECTION_FILTER)
126 m_query = m_query + STR_AND;
129 void MediaSearchVisitor::visitPostComposite(FilterType& type, int depth)
131 m_query = m_query + STR_RIGHT_BRACKET;
135 string MediaSearchVisitor::getPlatformAttr(string attrName)
138 map<string, string>::iterator pos;
140 if( queryType == QUERY_FOLDER)
142 pos = attrFolderEnumMap.find(attrName);
144 if(attrFolderEnumMap.end() != pos)
146 platformAttr = pos->second;
150 ThrowMsg(UnsupportedException, "Attribute(" << attrName << ") is not supported.");
153 else if(queryType == QUERY_MEDIA)
155 pos = attrMediaEnumMap.find(attrName);
157 if(attrMediaEnumMap.end() != pos)
159 platformAttr = pos->second;
163 ThrowMsg(UnsupportedException, "Attribute(" << attrName << ") is not supported.");
171 void MediaSearchVisitor::visitAttribute(string& attrName, MatchFlag& matchFlag, AnyArrayPtr& matchValues, int depth)
173 string attrPlatform = getPlatformAttr(attrName);
174 if(matchValues == NULL)
179 if(matchValues->size() == 1)
181 AnyPtr matchValue = matchValues->at(0);
183 m_query = convertAttribute(attrPlatform, matchValue, matchFlag);
185 else if(matchValues->size() > 1)
187 AnyArray::iterator iter;
188 m_query.append(STR_LEFT_BRACKET);
189 for(iter = matchValues->begin(); iter != matchValues->end(); iter++)
191 AnyPtr matchValue = *iter;
192 if(iter != matchValues->begin())
194 m_query.append(STR_OR);
196 m_query.append(convertAttribute(attrPlatform, matchValue, matchFlag));
198 m_query.append(STR_RIGHT_BRACKET);
202 string MediaSearchVisitor::toDateDbStr(const tm &date) const
205 ss << setfill('0') << setiosflags(ios::right) << setw(4) << (date.tm_year + 1900);
206 ss << setfill('0') << setiosflags(ios::right) << setw(2) << (date.tm_mon + 1);
207 ss << setfill('0') << setiosflags(ios::right) << setw(2) << date.tm_mday;
212 string MediaSearchVisitor::convertAttribute(string &attrName, AnyPtr& matchValue, MatchFlag& matchFlag)
220 string matchValueStr;
222 if(attrName.compare("content_type")==0)
224 if(matchValue->toString().compare("IMAGE")==0)
226 if(matchValue->toString().compare("VIDEO")==0)
228 if(matchValue->toString().compare("AUDIO")==0)
230 //Todo. another case throw the exeption.
234 matchValueStr = matchValue->toString();
237 if(matchValue->getType() == PrimitiveType_Time)
239 tm date = matchValue->getDateTm();
240 valueStr = toDateDbStr(date);
245 //STARTSWITH('%, ENDSWITH, EXISTS
246 //valueStr = matchValue->toString();
250 if(matchFlag == MATCH_STARTSWITH)
253 os << STR_S_QUOTATION << matchValueStr << STR_PERCENT << STR_S_QUOTATION;
255 else if( matchFlag == MATCH_ENDSWITH )
258 os << STR_S_QUOTATION << STR_PERCENT << matchValueStr << STR_S_QUOTATION;
260 else if( matchFlag == MATCH_CONTAINS )
263 os << STR_S_QUOTATION << STR_PERCENT << matchValueStr << STR_PERCENT << STR_S_QUOTATION;
268 os << STR_S_QUOTATION << matchValueStr << STR_S_QUOTATION;
274 operatorStr = operatorKey[cond];
276 conditionStr = STR_LEFT_BRACKET + attrName + operatorStr + valueStr + STR_RIGHT_BRACKET;
282 void MediaSearchVisitor::visitAttributeRange(string& attrName,AnyPtr& initialValue,AnyPtr& endValue,int depth)
286 string initialValueStr;
290 if(initialValue != NULL)
292 if(initialValue->getType() == PrimitiveType_Time)
294 tm date = initialValue->getDateTm();
295 std::stringstream time;
296 time << mktime(&date);
297 initialValueStr = time.str();
301 initialValueStr = initialValue->toString();
305 if (endValue != NULL)
307 if(endValue->getType() == PrimitiveType_Time)
309 tm date = endValue->getDateTm();
310 std::stringstream time;
311 time << mktime(&date);
312 endValueStr = time.str();
317 endValueStr = endValue->toString();
321 if (initialValue != NULL && endValue == NULL)
323 str = STR_LEFT_BRACKET + attrName + STR_GREATER_THAN + STR_EQUAL + STR_S_QUOTATION + initialValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
325 else if(initialValue == NULL && endValue != NULL)
327 str = STR_LEFT_BRACKET + attrName + STR_LESS_THAN + STR_S_QUOTATION + endValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
329 else if (initialValue != NULL && endValue != NULL)
331 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;
338 string MediaSearchVisitor::getResult() const
343 void MediaSearchVisitor::setQueryType(QueryType value)