Fix build breaks in contact
[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
55 map<string, string> MediaSearchVisitor::attrFolderEnumMap = {
56                 {"id",                          "id"},
57                 {"folderURI",           "FOLDER_PATH"},
58                 {"title",                       "FOLDER_NAME"},
59                 {"storageType",         "FOLDER_STORAGE_TYPE"},         
60                 {"modifiedDate",        "FOLDER_MODIFIED_TIME"},                                
61 };
62
63 map<string, string> MediaSearchVisitor::attrMediaEnumMap = {
64 //media item attribues.
65                 {"id",                          "MEDIA_ID"},
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"},
87
88 };
89
90 MediaSearchVisitor::MediaSearchVisitor()
91 {
92
93 }
94
95 MediaSearchVisitor::~MediaSearchVisitor()
96 {
97
98 }
99
100 void MediaSearchVisitor::visitPreComposite(FilterType& type, int depth)
101 {
102         m_query.append(STR_LEFT_BRACKET);
103 }
104
105 void MediaSearchVisitor::visitInComposite(FilterType& type, int depth)
106 {
107         if(type == UNION_FILTER)
108                 m_query.append(STR_OR);
109         else if(type == INTERSECTION_FILTER)
110                 m_query.append(STR_AND);
111         else{
112                 ThrowMsg(PlatformException, "Filter Type is wrong.");
113         }
114 }
115
116 void MediaSearchVisitor::visitPostComposite(FilterType& type, int depth)
117 {
118         m_query.append(STR_RIGHT_BRACKET);
119 }
120
121
122 string MediaSearchVisitor::getPlatformAttr(string attrName)
123 {
124         string platformAttr;
125         map<string, string>::iterator pos;
126         LogDebug("attrName=" << attrName);
127         LogDebug("queryType=" << queryType);
128
129         if( queryType == QUERY_FOLDER)
130         {
131                 pos = attrFolderEnumMap.find(attrName);
132                 
133                 if(attrFolderEnumMap.end() != pos)
134                 {
135                         platformAttr = pos->second;
136                 }
137                 else
138                 {
139                         ThrowMsg(PlatformException, "Attribute(" << attrName << ") is not supported.");
140                 }
141         }
142         else if(queryType == QUERY_MEDIA)
143         {
144                 pos = attrMediaEnumMap.find(attrName);
145                 
146                 if(attrMediaEnumMap.end() != pos)
147                 {
148                         platformAttr = pos->second;
149                 }
150                 else
151                 {
152                         ThrowMsg(PlatformException, "Attribute(" << attrName << ") is not supported.");
153                 }
154
155         }
156         return platformAttr;
157 }
158
159
160 void MediaSearchVisitor::visitAttribute(string& attrName, MatchFlag& matchFlag, AnyPtr& matchValue, int depth)
161 {
162         string attrPlatform = getPlatformAttr(attrName);
163         if(matchValue == NULL)
164         {
165                 ThrowMsg(PlatformException, "matchValue is not valid data.");
166         }       
167
168         m_query.append(convertAttribute(attrPlatform, matchValue, matchFlag));
169 }
170
171 string MediaSearchVisitor::toDateDbStr(tm &date) const
172 {
173         time_t time;
174         time = mktime(&date);
175         stringstream ss;
176         ss << time;
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;
180
181         return ss.str();
182 }
183
184 string MediaSearchVisitor::convertAttribute(string &attrName, AnyPtr& matchValue, MatchFlag& matchFlag)
185 {
186         string valueStr;
187         string operatorStr;
188         string conditionStr;
189
190         condition_e cond;
191         string matchValueStr;
192
193         if(attrName.compare("MEDIA_TYPE")==0)
194         {
195                 if(matchValue->toString().compare("IMAGE")==0)
196                         matchValueStr = "0";
197                 else if(matchValue->toString().compare("VIDEO")==0)
198                         matchValueStr = "1";
199                 else if(matchValue->toString().compare("AUDIO")==0)
200                         matchValueStr = "3";                    
201                 else
202                 {
203                         ThrowMsg(PlatformException, "mediaType(" << matchValue->toString() << ") is not supported.");
204                 }
205         }
206         else
207         {
208                 matchValueStr = matchValue->toString();
209         }
210
211         if(matchValue->getType() == PrimitiveType_Time)
212         {
213                 tm date = matchValue->getDateTm();
214                 valueStr = toDateDbStr(date);
215                 cond = EQUAL;
216         }
217         else
218         {
219                 //STARTSWITH('%, ENDSWITH, EXISTS
220                 //valueStr = matchValue->toString();
221         
222                 ostringstream os;
223
224                 if(matchFlag == MATCH_STARTSWITH)
225                 {
226                         cond = LIKE;
227                         os << STR_S_QUOTATION << matchValueStr << STR_PERCENT << STR_S_QUOTATION;
228                 }
229                 else if( matchFlag == MATCH_ENDSWITH )
230                 {
231                         cond = LIKE;
232                         os << STR_S_QUOTATION <<  STR_PERCENT << matchValueStr << STR_S_QUOTATION;
233                 }
234                 else if(  matchFlag ==  MATCH_CONTAINS )
235                 {
236                         cond = LIKE;
237                         os << STR_S_QUOTATION <<  STR_PERCENT << matchValueStr << STR_PERCENT << STR_S_QUOTATION;
238                 }
239                 else
240                 {
241                         cond = EQUAL;
242                         os << STR_S_QUOTATION << matchValueStr << STR_S_QUOTATION;
243                 }
244                 
245                 valueStr = os.str();
246         
247         }
248         operatorStr = operatorKey[cond];
249
250         if (attrName.compare("MEDIA_TITLE")==0)
251         {       
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;
256
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;
261
262                 conditionStr = STR_LEFT_BRACKET + imageCond + STR_OR + videoNAudioCond + STR_RIGHT_BRACKET;     
263         }
264         else
265         {
266         conditionStr = STR_LEFT_BRACKET + attrName + operatorStr + valueStr + STR_RIGHT_BRACKET;
267         }
268
269         return conditionStr;
270 }
271
272
273 void MediaSearchVisitor::visitAttributeRange(string& attrName,AnyPtr& initialValue,AnyPtr& endValue,int depth)
274 {
275         if(initialValue == NULL || endValue == NULL)
276                 return;
277
278         string str;
279         string initialValueStr;
280         string endValueStr;
281
282         string attrPlatform = getPlatformAttr(attrName);
283
284         if(!initialValue->isNullOrUndefined()) 
285         {
286                 if(initialValue->getType() == PrimitiveType_Time) 
287                 {
288                         tm date = initialValue->getDateTm();
289                         initialValueStr = toDateDbStr(date);
290
291                 } 
292                 else 
293                 {
294                         initialValueStr = initialValue->toString();
295                 }
296         }
297
298         if (!endValue->isNullOrUndefined()) 
299         {
300                 if(endValue->getType() == PrimitiveType_Time) 
301                 {
302                         tm date = endValue->getDateTm();
303                         endValueStr = toDateDbStr(date);
304                 }
305                 else 
306                 {
307                         endValueStr = endValue->toString();
308                 }
309         }
310
311         if (!initialValue->isNullOrUndefined() && endValue->isNullOrUndefined()) 
312         {
313                 str = STR_LEFT_BRACKET + attrPlatform + STR_GREATER_THAN + STR_EQUAL + 
314                                 STR_S_QUOTATION + initialValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
315         }
316         else if(initialValue->isNullOrUndefined() && !endValue->isNullOrUndefined()) 
317         {
318                 str = STR_LEFT_BRACKET + attrPlatform + STR_LESS_THAN + STR_S_QUOTATION + 
319                                 endValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
320         } 
321         else if (!initialValue->isNullOrUndefined() && !endValue->isNullOrUndefined()) 
322         {
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;
326         }
327
328         m_query.append(str);
329
330 }
331
332 string MediaSearchVisitor::getResult() const
333 {
334         return m_query;
335 }       
336
337 void MediaSearchVisitor::setQueryType(QueryType value)
338 {
339         queryType = value;
340 }
341
342
343
344
345 } // Contact
346 } // Platform
347 } // TizenApis