Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Mediacontent / MediaSearchVisitor.cpp
1 /*
2 * Copyright (c) 2011 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
18 #include "time.h"
19 #include <iomanip>
20 #include <Commons/Exception.h>
21 #include "MediaSearchVisitor.h"
22
23
24 namespace TizenApis {
25 namespace Platform {
26 namespace Mediacontent {
27
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
38
39
40
41 typedef enum
42 {
43         MEDIA_ID = 0,
44         MEDIA_TITLE,
45         MEDIA_FILEPATH,
46         MEDIA_THUMBNAILPATH,
47         MEDIA_CREATEDDATE,
48         MEDIA_MODIFIEDDATE,
49         MEDIA_DESCRIPTION,
50         MEDIA_RATING,
51
52 }media_attribute_e;
53 /*
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},       
63
64 };
65 */
66 map<string, string> MediaSearchVisitor::attrFolderEnumMap = {
67                 {"id",                          "id"},
68                 {"folderURL",           "folder_path"},
69                 {"title",                       "title"},
70                 {"storageType",         "storage_type"},                
71                 {"modifiedDate",        "modified_date"},                               
72 };
73
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.
81                 {"id",                          "media_uuid"},
82                 {"type",                        "content_type"},
83 //              {"mimeType",            "mime_type"},
84                 {"title",                       "display_name"},                
85                 {"fileURL",                     "path"},
86                 {"thumbnailURL",        "thumbnail_path"},              
87                 {"description",         "description"},         
88                 {"rating",                      "rating"},      
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"}, 
95                 {"album",                       "album"}, 
96                 {"artist",                      "artist"}, 
97                 {"width",                       "width"}, 
98                 {"height",                      "height"},
99                 {"playedTime",          "last_played_time"},            
100                 {"playCount",           "played_count"},                
101                 {"genre",                       "genre"},
102                 {"artist",                      "artist"},
103 };
104
105
106 MediaSearchVisitor::MediaSearchVisitor()
107 {
108
109 }
110
111 MediaSearchVisitor::~MediaSearchVisitor()
112 {
113
114 }
115
116 void MediaSearchVisitor::visitPreComposite(FilterType& type, int depth)
117 {
118         m_query = m_query + STR_LEFT_BRACKET;
119 }
120
121 void MediaSearchVisitor::visitInComposite(FilterType& type, int depth)
122 {
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;
127 }
128
129 void MediaSearchVisitor::visitPostComposite(FilterType& type, int depth)
130 {
131         m_query = m_query + STR_RIGHT_BRACKET;
132 }
133
134
135 string MediaSearchVisitor::getPlatformAttr(string attrName)
136 {
137         string platformAttr;
138         map<string, string>::iterator pos;
139
140         if( queryType == QUERY_FOLDER)
141         {
142                 pos = attrFolderEnumMap.find(attrName);
143                 
144                 if(attrFolderEnumMap.end() != pos)
145                 {
146                         platformAttr = pos->second;
147                 }
148                 else
149                 {
150                         ThrowMsg(UnsupportedException, "Attribute(" << attrName << ") is not supported.");
151                 }
152         }
153         else if(queryType == QUERY_MEDIA)
154         {
155                 pos = attrMediaEnumMap.find(attrName);
156                 
157                 if(attrMediaEnumMap.end() != pos)
158                 {
159                         platformAttr = pos->second;
160                 }
161                 else
162                 {
163                         ThrowMsg(UnsupportedException, "Attribute(" << attrName << ") is not supported.");
164                 }
165
166         }
167         return platformAttr;
168 }
169
170
171 void MediaSearchVisitor::visitAttribute(string& attrName, MatchFlag& matchFlag, AnyArrayPtr& matchValues, int depth)
172 {
173         string attrPlatform = getPlatformAttr(attrName);
174         if(matchValues == NULL)
175         {
176                 return ;
177         }       
178
179         if(matchValues->size() == 1)
180         {
181                 AnyPtr matchValue = matchValues->at(0);
182
183                 m_query =  convertAttribute(attrPlatform, matchValue, matchFlag);
184         }
185         else if(matchValues->size() > 1)
186         {
187                 AnyArray::iterator iter;
188                 m_query.append(STR_LEFT_BRACKET);
189                 for(iter = matchValues->begin(); iter != matchValues->end(); iter++)
190                 {
191                         AnyPtr matchValue = *iter;
192                         if(iter != matchValues->begin())
193                         {
194                                 m_query.append(STR_OR);
195                         }
196                         m_query.append(convertAttribute(attrPlatform, matchValue, matchFlag));
197                 }
198                 m_query.append(STR_RIGHT_BRACKET);
199         }
200 }
201
202 string MediaSearchVisitor::toDateDbStr(const tm &date) const
203 {
204         stringstream ss;
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;
208
209         return ss.str();
210 }
211
212 string MediaSearchVisitor::convertAttribute(string &attrName, AnyPtr& matchValue, MatchFlag& matchFlag)
213 {
214         string valueStr;
215         string operatorStr;
216         string conditionStr;
217
218
219         condition_e cond;
220         string matchValueStr;
221
222         if(attrName.compare("content_type")==0)
223         {
224                 if(matchValue->toString().compare("IMAGE")==0)
225                         matchValueStr = "1";
226                 if(matchValue->toString().compare("VIDEO")==0)
227                         matchValueStr = "2";
228                 if(matchValue->toString().compare("AUDIO")==0)
229                         matchValueStr = "3";                    
230                 //Todo. another case throw the exeption.
231         }
232         else
233         {
234                 matchValueStr = matchValue->toString();
235         }\r
236
237         if(matchValue->getType() == PrimitiveType_Time)
238         {
239                 tm date = matchValue->getDateTm();
240                 valueStr = toDateDbStr(date);
241                 cond = EQUAL;
242         }
243         else
244         {
245                 //STARTSWITH('%, ENDSWITH, EXISTS
246                 //valueStr = matchValue->toString();
247         
248                 ostringstream os;
249
250                 if(matchFlag == MATCH_STARTSWITH)
251                 {
252                         cond = LIKE;
253                         os << STR_S_QUOTATION << matchValueStr << STR_PERCENT << STR_S_QUOTATION;
254                 }
255                 else if( matchFlag == MATCH_ENDSWITH )
256                 {
257                         cond = LIKE;
258                         os << STR_S_QUOTATION <<  STR_PERCENT << matchValueStr << STR_S_QUOTATION;
259                 }
260                 else if(  matchFlag ==  MATCH_CONTAINS )
261                 {
262                         cond = LIKE;
263                         os << STR_S_QUOTATION <<  STR_PERCENT << matchValueStr << STR_PERCENT << STR_S_QUOTATION;
264                 }
265                 else
266                 {
267                         cond = EQUAL;
268                         os << STR_S_QUOTATION << matchValueStr << STR_S_QUOTATION;
269                 }
270                 
271                 valueStr = os.str();
272         
273         }
274         operatorStr = operatorKey[cond];
275
276         conditionStr = STR_LEFT_BRACKET + attrName + operatorStr + valueStr + STR_RIGHT_BRACKET;
277
278         return conditionStr;
279 }
280
281
282 void MediaSearchVisitor::visitAttributeRange(string& attrName,AnyPtr& initialValue,AnyPtr& endValue,int depth)
283 {
284
285         string str;
286         string initialValueStr;
287         string endValueStr;
288
289
290         if(initialValue != NULL) 
291         {
292                 if(initialValue->getType() == PrimitiveType_Time) 
293                 {
294                         tm date = initialValue->getDateTm();
295                         std::stringstream time;
296                         time << mktime(&date);
297                         initialValueStr = time.str();
298                 } 
299                 else 
300                 {
301                         initialValueStr = initialValue->toString();
302                 }
303         }
304
305         if (endValue != NULL) 
306         {
307                 if(endValue->getType() == PrimitiveType_Time) 
308                 {
309                         tm date = endValue->getDateTm();
310                         std::stringstream time;
311                         time << mktime(&date);
312                         endValueStr = time.str();
313
314                 }
315                 else 
316                 {
317                         endValueStr = endValue->toString();
318                 }
319         }
320
321         if (initialValue != NULL && endValue == NULL) 
322         {
323                 str = STR_LEFT_BRACKET + attrName + STR_GREATER_THAN + STR_EQUAL + STR_S_QUOTATION + initialValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
324         }
325         else if(initialValue == NULL && endValue != NULL) 
326         {
327                 str = STR_LEFT_BRACKET + attrName + STR_LESS_THAN + STR_S_QUOTATION + endValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
328         } 
329         else if (initialValue != NULL && endValue != NULL) 
330         {
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;
332         }
333
334         m_query = str;
335
336 }
337
338 string MediaSearchVisitor::getResult() const
339 {
340         return m_query;
341 }       
342
343 void MediaSearchVisitor::setQueryType(QueryType value)
344 {
345         queryType = value;
346 }
347
348
349
350
351 } // Contact
352 } // Platform
353 } // TizenApis