merge wrt-plugins-tizen_0.2.0-4
[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                 {"folderURI",           "folder_path"},
69                 {"title",                       "title"},
70                 {"storageType",         "storage_type"},                
71                 {"modifiedDate",        "modified_date"},                               
72 };
73
74 map<string, string> MediaSearchVisitor::attrMediaEnumMap = {
75 //media item attribues.
76                 {"id",                          "media_uuid"},
77                 {"type",                        "content_type"},
78 //              {"mimeType",            "mime_type"},
79                 {"title",                       "display_name"},                
80                 {"itemURI",                     "path"},
81                 {"thumbnailURIs",       "thumbnail_path"},              
82                 {"description",         "description"},         
83                 {"rating",                      "rating"},      
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"}, 
90                 {"album",                       "album"}, 
91                 {"artists",                     "artist"}, 
92                 {"width",                       "width"}, 
93                 {"height",                      "height"},
94                 {"playedTime",          "last_played_time"},            
95                 {"playCount",           "played_count"},                
96                 {"genres",                      "genre"},
97 };
98
99
100 MediaSearchVisitor::MediaSearchVisitor()
101 {
102
103 }
104
105 MediaSearchVisitor::~MediaSearchVisitor()
106 {
107
108 }
109
110 void MediaSearchVisitor::visitPreComposite(FilterType& type, int depth)
111 {
112         m_query.append(STR_LEFT_BRACKET);
113 }
114
115 void MediaSearchVisitor::visitInComposite(FilterType& type, int depth)
116 {
117         if(type == UNION_FILTER)
118                 m_query.append(STR_OR);
119         else if(type == INTERSECTION_FILTER)
120                 m_query.append(STR_AND);
121 }
122
123 void MediaSearchVisitor::visitPostComposite(FilterType& type, int depth)
124 {
125         m_query.append(STR_RIGHT_BRACKET);
126 }
127
128
129 string MediaSearchVisitor::getPlatformAttr(string attrName)
130 {
131         string platformAttr;
132         map<string, string>::iterator pos;
133
134         if( queryType == QUERY_FOLDER)
135         {
136                 pos = attrFolderEnumMap.find(attrName);
137                 
138                 if(attrFolderEnumMap.end() != pos)
139                 {
140                         platformAttr = pos->second;
141                 }
142                 else
143                 {
144                         ThrowMsg(UnsupportedException, "Attribute(" << attrName << ") is not supported.");
145                 }
146         }
147         else if(queryType == QUERY_MEDIA)
148         {
149                 pos = attrMediaEnumMap.find(attrName);
150                 
151                 if(attrMediaEnumMap.end() != pos)
152                 {
153                         platformAttr = pos->second;
154                 }
155                 else
156                 {
157                         ThrowMsg(UnsupportedException, "Attribute(" << attrName << ") is not supported.");
158                 }
159
160         }
161         return platformAttr;
162 }
163
164
165 void MediaSearchVisitor::visitAttribute(string& attrName, MatchFlag& matchFlag, AnyPtr& matchValue, int depth)
166 {
167         string attrPlatform = getPlatformAttr(attrName);
168         if(matchValue == NULL)
169         {
170                 return ;
171         }       
172
173         m_query.append(convertAttribute(attrPlatform, matchValue, matchFlag));
174 }
175
176 string MediaSearchVisitor::toDateDbStr(const tm &date) const
177 {
178         stringstream ss;
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;
182
183         return ss.str();
184 }
185
186 string MediaSearchVisitor::convertAttribute(string &attrName, AnyPtr& matchValue, MatchFlag& matchFlag)
187 {
188         string valueStr;
189         string operatorStr;
190         string conditionStr;
191
192
193         condition_e cond;
194         string matchValueStr;
195
196         if(attrName.compare("content_type")==0)
197         {
198                 if(matchValue->toString().compare("IMAGE")==0)
199                         matchValueStr = "1";
200                 if(matchValue->toString().compare("VIDEO")==0)
201                         matchValueStr = "2";
202                 if(matchValue->toString().compare("AUDIO")==0)
203                         matchValueStr = "3";                    
204                 //Todo. another case throw the exeption.
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         conditionStr = STR_LEFT_BRACKET + attrName + operatorStr + valueStr + STR_RIGHT_BRACKET;
251
252         return conditionStr;
253 }
254
255
256 void MediaSearchVisitor::visitAttributeRange(string& attrName,AnyPtr& initialValue,AnyPtr& endValue,int depth)
257 {
258         if(initialValue == NULL || endValue == NULL)
259                 return;
260
261         string str;
262         string initialValueStr;
263         string endValueStr;
264
265
266         if(!initialValue->isNullOrUndefined()) 
267         {
268                 if(initialValue->getType() == PrimitiveType_Time) 
269                 {
270                         tm date = initialValue->getDateTm();
271                         initialValueStr = toDateDbStr(date);
272
273                 } 
274                 else 
275                 {
276                         initialValueStr = initialValue->toString();
277                 }
278         }
279
280         if (!endValue->isNullOrUndefined()) 
281         {
282                 if(endValue->getType() == PrimitiveType_Time) 
283                 {
284                         tm date = endValue->getDateTm();
285                         endValueStr = toDateDbStr(date);
286                 }
287                 else 
288                 {
289                         endValueStr = endValue->toString();
290                 }
291         }
292
293         if (!initialValue->isNullOrUndefined() && endValue->isNullOrUndefined()) 
294         {
295                 str = STR_LEFT_BRACKET + attrName + STR_GREATER_THAN + STR_EQUAL + STR_S_QUOTATION + initialValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
296         }
297         else if(initialValue->isNullOrUndefined() && !endValue->isNullOrUndefined()) 
298         {
299                 str = STR_LEFT_BRACKET + attrName + STR_LESS_THAN + STR_S_QUOTATION + endValueStr + STR_S_QUOTATION + STR_RIGHT_BRACKET;
300         } 
301         else if (!initialValue->isNullOrUndefined() && !endValue->isNullOrUndefined()) 
302         {
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;
304         }
305
306         m_query.append(str);
307
308 }
309
310 string MediaSearchVisitor::getResult() const
311 {
312         return m_query;
313 }       
314
315 void MediaSearchVisitor::setQueryType(QueryType value)
316 {
317         queryType = value;
318 }
319
320
321
322
323 } // Contact
324 } // Platform
325 } // TizenApis