Fix : The original content is restored again if CreateContent API fail
[platform/framework/native/content.git] / src / FCnt_ContentSearchImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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  * @file                FCnt_ContentSearchImpl.cpp
18  * @brief               This is the implementation file for the %_ContentSearchImpl class.
19  *
20  * This file contains implementation of the %_ContentSearchImpl class.
21  */
22
23 #include <new>
24 #include <time.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseInteger.h>
27 #include <FBaseLongLong.h>
28 #include <FBaseFloat.h>
29 #include <FBaseColIList.h>
30 #include <FBaseUtilStringTokenizer.h>
31 #include <FCntContentSearch.h>
32 #include <FCntContentSearchResult.h>
33 #include <FSysEnvironment.h>
34 #include <FApp_AppInfo.h>
35 #include <FBase_StringConverter.h>
36 #include <FBase_LocalizedNumParser.h>
37 #include <FIo_FileImpl.h>
38 #include "FCnt_ContentUtility.h"
39 #include "FCnt_ContentSearchImpl.h"
40 #include "FCnt_ContentInfoImpl.h"
41 #include "FCnt_ImageContentInfoImpl.h"
42 #include "FCnt_AudioContentInfoImpl.h"
43 #include "FCnt_VideoContentInfoImpl.h"
44 #include "FCnt_OtherContentInfoImpl.h"
45 #include "FCnt_ContentInfoHelper.h"
46
47 using namespace Tizen::App;
48 using namespace Tizen::Base;
49 using namespace Tizen::Base::Collection;
50 using namespace Tizen::Base::Utility;
51 using namespace Tizen::Io;
52 using namespace Tizen::System;
53
54 namespace Tizen { namespace Content
55 {
56
57 static const int QUERY_LENGTH = 4096;
58
59 // Declaration for Callback function registered to each media info details
60 bool MediaItemCb(media_info_h media, void* pUserdata);
61 // Declaration for Callback function registered to each column details
62 bool GroupItemCb(const char* pGroupName, void* pUserdata);
63 // Declaration for Callback function registered to each album details
64 bool AlbumItemCb(media_album_h album, void* pUserdata);
65
66 // Default constructor
67 _ContentSearchImpl::_ContentSearchImpl(void)
68         : __contentType(CONTENT_TYPE_UNKNOWN)
69         , __pFinalOutList(NULL)
70         , __inputExpr(L"")
71         , __inputColumnName(L"")
72         , __inputSortOrder(SORT_ORDER_NONE)
73         , __pFilterHandle(NULL)
74 {
75         SysLog(NID_CNT, "Enter\n");
76 }
77
78
79 // Default destructor (disconnects the DB connection)
80 _ContentSearchImpl::~_ContentSearchImpl(void)
81 {
82         SysLog(NID_CNT, "Enter\n");
83
84         int ret = MEDIA_CONTENT_ERROR_NONE;
85         result r = E_SUCCESS;
86
87         ret = media_content_disconnect();
88         r = MapCoreErrorToNativeResult(ret);
89         SysTryLog(NID_CNT, r == E_SUCCESS, "[%s] Propagated in ~_ContentSearchImpl", GetErrorMessage(r));
90 }
91
92  _ContentSearchImpl*
93  _ContentSearchImpl::GetInstance(ContentSearch& contentSearch)
94 {
95         return contentSearch.__pImpl;
96 }
97
98 const _ContentSearchImpl*
99 _ContentSearchImpl::GetInstance(const ContentSearch& contentSearch)
100 {
101         return contentSearch.__pImpl;
102 }
103
104 //make a connection to DB
105 result
106 _ContentSearchImpl::Construct(ContentType type)
107 {
108         SysLog(NID_CNT, "Enter\n");
109
110         result r = E_SUCCESS;
111         int ret = MEDIA_CONTENT_ERROR_NONE;
112
113         ret = media_content_connect();
114         SysTryReturnResult(NID_CNT, r == E_SUCCESS , E_SYSTEM, "Failed to perform media_content_connect operation.");
115
116         __contentType = type;
117
118         return r;
119 }
120
121 // Creates a filter for the input query string along with content type
122 // Initializes filter handle
123 // Image - MEDIA_TYPE=0
124 // Vide0 - MEDIA_TYPE=1
125 // Audio - MEDIA_TYPE=2 or MEDIA_TYPE=3
126 // Others - MEDIA_TYPE=4
127 // All - none
128 // Appends MEDIA_TYPE with "AND" (if the input expression is not empty and content type is not ALL) and  input expression
129 // Sets the condition and order.
130 // If any argument is not proper E_INVALID_ARG is returned
131 result
132 _ContentSearchImpl::CreateQueryFilter(bool isAndAppendReq) const
133 {
134         int ret  = MEDIA_CONTENT_ERROR_NONE;
135         result r = E_SUCCESS;
136
137         std::unique_ptr<char[]> pInputCond;
138         std::unique_ptr<char[]> pSortCol;
139
140         String inputCondition;
141         String slpColumn;
142
143         filter_h tempFilter = NULL;
144
145         ret = media_filter_create(&tempFilter);
146         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "Failed to perform media_filter_create operation.");
147
148         std::unique_ptr<filter_s, SearchFilterHandleDeleter> pFilterHandle(tempFilter);
149         SysTryReturnResult(NID_CNT, pFilterHandle != null, E_OUT_OF_MEMORY, "pFilterHandle is null.");
150
151         switch (__contentType)
152         {
153                 // Image-0,video-1,sound-2,music-3,other-4
154         case CONTENT_TYPE_OTHER:
155                 inputCondition = "MEDIA_TYPE=4 ";
156                 break;
157         case CONTENT_TYPE_IMAGE:
158                 inputCondition = "MEDIA_TYPE=0 ";
159                 break;
160         case CONTENT_TYPE_AUDIO:
161                 inputCondition = "(MEDIA_TYPE=2 or MEDIA_TYPE=3) ";
162                 break;
163         case CONTENT_TYPE_VIDEO:
164                 inputCondition = "MEDIA_TYPE=1 ";
165                 break;
166         case CONTENT_TYPE_ALL:
167                 //If content type is CONTENT_TYPE_ALL, then MEDIA_TYPE is empty
168                 break;
169         default:
170                 break;
171         }
172
173         if (!__inputExpr.IsEmpty())
174         {
175                 if (isAndAppendReq && (!inputCondition.IsEmpty())) //For CONTENT_TYPE_ALL inputCondition is empty
176                 {
177                         r = inputCondition.Append("AND ");
178                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform append operation.");
179                 }
180
181                 r = inputCondition.Append(__inputExpr);
182                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform append operation.");
183         }
184
185         if (!inputCondition.IsEmpty())
186         {
187                 //CopyToCharArrayN: utility function, converts a osp string to char*
188                 pInputCond = std::unique_ptr<char[]>(_StringConverter::CopyToCharArrayN(inputCondition));
189                 SysTryReturnResult(NID_CNT, pInputCond, E_OUT_OF_MEMORY, "pInputCond is NULL.");
190
191                 SysLog(NID_CNT, "pInputCond = %s", pInputCond.get());
192
193                 ret = media_filter_set_condition(pFilterHandle.get(), pInputCond.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
194                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "Failed to perform media_filter_set_condition operation.");
195         }
196
197         if (!__inputColumnName.IsEmpty()) // SortColumn is optional in case of SearchN
198         {
199                 //__inputColumnName (osp column name) is replaced with slpColumn (slp column name).
200                 r = GetSlpColumnName(slpColumn);
201                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform GetSlpColumnName operation.");
202
203                 //CopyToCharArrayN: utility function, converts a osp string to char*
204
205                 pSortCol = std::unique_ptr<char[]>(_StringConverter::CopyToCharArrayN(slpColumn));
206                 SysTryReturnResult(NID_CNT, pSortCol, E_OUT_OF_MEMORY, "pSortCol is NULL.");
207
208                 if (__inputSortOrder == SORT_ORDER_ASCENDING)
209                 {
210                         ret = media_filter_set_order(pFilterHandle.get(), MEDIA_CONTENT_ORDER_ASC, pSortCol.get(), MEDIA_CONTENT_COLLATE_NOCASE);
211                         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "Failed to perform media_filter_set_order operation.");
212                 }
213                 else if (__inputSortOrder == SORT_ORDER_DESCENDING)
214                 {
215                         ret = media_filter_set_order(pFilterHandle.get(), MEDIA_CONTENT_ORDER_DESC, pSortCol.get(), MEDIA_CONTENT_COLLATE_NOCASE);
216                         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "Failed to perform media_filter_set_order operation.");
217                 }
218         }
219
220         __pFilterHandle.reset(pFilterHandle.release());
221
222         return r;
223 }
224
225 // Osp column names are mapped with slp column names
226 // CONTENT_TYPE_OTHER and CONTENT_TYPE_IMAGE (0 - 13 ) are valid columns
227 // CONTENT_TYPE_VIDEO  (0 - 16 ) are valid columns
228 // CONTENT_TYPE_ALL and  CONTENT_TYPE_VIDEO (0 - 18 ) are valid columns
229 // if the given osp column is out of the specified range of the type, E_INVALID_ARG is retuned.
230 result
231 _ContentSearchImpl::GetSlpColumnName(String& inputCol) const
232 {
233         String          ospColumnName(L"");
234         String          slpColumnName(L"");
235         String          columnName(__inputColumnName);
236         result          r = E_SUCCESS;
237         int             maxCols = 0;
238         switch (__contentType)
239         {
240         case CONTENT_TYPE_OTHER:
241                 //fall through
242         case CONTENT_TYPE_IMAGE:
243                 maxCols = MAX_QUERY_COLUMNS_FOR_IMAGE_OTHERS;
244                 break;
245         case CONTENT_TYPE_VIDEO:
246                 maxCols = MAX_QUERY_COLUMNS_FOR_VIDEO;
247                 break;
248         case CONTENT_TYPE_AUDIO:
249                 //fall through
250         case CONTENT_TYPE_ALL:
251                 maxCols = MAX_QUERY_COLUMNS;
252                 break;
253         default:
254                 break;
255         }
256         for (int colIndex = 0; colIndex < maxCols; colIndex++)
257         {
258                 ospColumnName.Clear();
259                 slpColumnName.Clear();
260
261                 ospColumnName = dbfieldinfo[colIndex].dbFieldOspName ;
262                 slpColumnName = dbfieldinfo[colIndex].dbFieldSlpName ;
263
264                 ospColumnName.ToUpper();
265                 columnName.ToUpper();
266                 if (columnName == ospColumnName)
267                 {
268                         inputCol = slpColumnName;
269                         return r;
270                 }
271         }
272         return E_INVALID_ARG;
273 }
274
275 //If the input expression contains any osp column name, it will be replaced with slp column name.
276 // only CONTENT_TYPE_AUDIO and CONTENT_TYPE_ALL allowed to use all given columns
277 // so, check for invalid column for CONTENT_TYPE_OTHER,CONTENT_TYPE_IMAGE and CONTENT_TYPE_VIDEO.
278 // all the osp columns in the expression should be replaced with slp column names.
279 // ' is used as a delimeter in parsing user query. so, \\' is saved as a special@apostrophe string.
280 // \\' is replaced with '', which considers ' as normal character.
281
282 result
283 _ContentSearchImpl::ReplaceOspColumnNameWithSlp(void) const
284 {
285         String          ospColumnName(L"");
286         String          slpColumnName(L"");
287         String          splApostrophe(L"special@apostrophe");
288         result          r = E_SUCCESS;
289         int             maxCols = 0;
290
291         int indexOf = 0;
292
293         String strToBeReplaced(L"\\'");
294         int strLen = strToBeReplaced.GetLength();
295         int startIndex = 0;
296
297         while(E_SUCCESS == __inputExpr.IndexOf(strToBeReplaced,startIndex,indexOf))
298         {
299                 int lenAfterSubStr = indexOf + strLen;
300                 while ((lenAfterSubStr < __inputExpr.GetLength()) && (__inputExpr[lenAfterSubStr] == ' '))
301                 {
302                         lenAfterSubStr++;
303                 }
304                 if ((lenAfterSubStr < __inputExpr.GetLength()) && ((__inputExpr[lenAfterSubStr] == '\'') ||
305                                 ((!__inputExpr.StartsWith(L"AND ", lenAfterSubStr)) && (!__inputExpr.StartsWith(L"OR ", lenAfterSubStr)))))
306                 {
307                         r = __inputExpr.Remove(indexOf,strLen);
308                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "Failed to perform Remove operation.");
309                         r = __inputExpr.Insert(splApostrophe,indexOf);
310                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "Failed to perform Insert operation.");
311                         startIndex = indexOf + splApostrophe.GetLength();
312                 }
313                 else
314                 {
315                         startIndex = lenAfterSubStr;
316                 }
317         }
318
319         SysLog(NID_CNT, "__inputExpr after splApostrophe append = %ls", __inputExpr.GetPointer());
320
321         switch (__contentType)
322         {
323         case CONTENT_TYPE_OTHER:
324                 //fall through
325         case CONTENT_TYPE_IMAGE:
326                 r = CheckInvalidColumnInQuery();
327                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_INVALID_ARG, "Invalid Column In QueryString.");
328                 maxCols = MAX_QUERY_COLUMNS_FOR_IMAGE_OTHERS;
329                 break;
330         case CONTENT_TYPE_VIDEO:
331                 r = CheckInvalidColumnInQuery();
332                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_INVALID_ARG, "Invalid Column In QueryString.");
333                 maxCols = MAX_QUERY_COLUMNS_FOR_VIDEO;
334                 break;
335         case CONTENT_TYPE_AUDIO:
336                 //fall through
337         case CONTENT_TYPE_ALL:
338                 maxCols = MAX_QUERY_COLUMNS;
339                 break;
340         default:
341                 break;
342         }
343         for (int colIndex = 0; colIndex < maxCols; colIndex++)
344         {
345                 ospColumnName.Clear();
346                 slpColumnName.Clear();
347
348                 ospColumnName = dbfieldinfo[colIndex].dbFieldOspName ;
349                 slpColumnName = dbfieldinfo[colIndex].dbFieldSlpName ;
350
351                 ReplaceString(ospColumnName,slpColumnName);
352         }
353
354         // Append ESCAPE '\' for LIKE query
355         r = AppendEscapeKeywordForLikeQuery();
356         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "AppendEscapeKeywordForLikeQuery failed.");
357
358         r = ReplaceDateTimeStringWithInt();
359         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_INVALID_ARG, "ReplaceDateTimeStringWithInt failed.");
360
361         // replace splApostrophe string with actual
362         r = __inputExpr.Replace(splApostrophe, "''");
363         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string Replace failed.");
364
365         return r;
366 }
367
368 //This function is to appened ESCAPE keyword for _ and % special characters in LIKE query.
369
370 result
371 _ContentSearchImpl::AppendEscapeKeywordForLikeQuery(void) const
372 {
373         String  delim = L"'"; //Delimeters  is  ' .
374         result r = E_SUCCESS;
375         bool isCol = true;
376         bool isAppendEscape = false;
377         String token;
378         String tokenUpper;
379         String inputExpr = __inputExpr;
380
381         // Create a StringTokenizer instance
382         StringTokenizer  strTok(inputExpr, delim);
383
384         inputExpr.Clear();
385
386         while (strTok.HasMoreTokens())
387         {
388                 r = strTok.GetNextToken(token);
389                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "GetNextToken failed.");
390                 if (isCol) //column name
391                 {
392                         isCol = false;
393                         tokenUpper = token;
394                         tokenUpper.ToUpper();
395                         if (tokenUpper.Contains(" LIKE"))
396                         {
397                                 isAppendEscape = true;
398                         }
399                         else
400                         {
401                                 isAppendEscape = false;
402                         }
403                         r = inputExpr.Append(token);
404                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
405                 }
406                 else // value of the column
407                 {
408                         isCol = true;
409                         r = inputExpr.Append("'");
410                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
411                         r = inputExpr.Append(token);
412                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
413                         r = inputExpr.Append("'");
414                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
415
416                         if (isAppendEscape)
417                         {
418                                 if (token.Contains("\\_") || token.Contains("\\%"))
419                                 {
420                                         r = inputExpr.Append("ESCAPE'\\'  ");
421                                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
422                                 }
423                         }
424                 }
425         }
426
427         __inputExpr.Clear();
428         r = __inputExpr.Insert(inputExpr, 0);
429         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string insert failed.");
430
431         return r;
432 }
433
434 //This function is to replace DateTime value(osp datetime type is string) which is string with int.(SLP dateTime column type is int)
435
436 result
437 _ContentSearchImpl::ReplaceDateTimeStringWithInt(void) const
438 {
439         String  delim = L"'"; //Delimeters  is  ' .
440         result r = E_SUCCESS;
441         bool isCol = true;
442         bool isConvertReq = false;
443         bool isBetweenExistsInDateTimeQuery = false;
444         String token;
445         String tokenUpper;
446         String inputExpr = __inputExpr;
447
448         if(!inputExpr.Contains("MEDIA_ADDED_TIME"))
449         {
450                 return r;
451         }
452
453         // Create a StringTokenizer instance
454         StringTokenizer  strTok(inputExpr, delim);
455
456         inputExpr.Clear();
457
458         while (strTok.HasMoreTokens())
459         {
460                 r = strTok.GetNextToken(token);
461                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "GetNextToken failed.");
462                 if (isCol) //column name
463                 {
464                         isCol = false;
465                         tokenUpper = token;
466                         tokenUpper.ToUpper();
467                         if(isBetweenExistsInDateTimeQuery)
468                         {
469                                 isBetweenExistsInDateTimeQuery = false;
470                                 isConvertReq = true;
471                         }
472                         else
473                         {
474                                 if (tokenUpper.Contains("MEDIA_ADDED_TIME"))
475                                 {
476                                         if (tokenUpper.Contains("BETWEEN"))
477                                         {
478                                                 isBetweenExistsInDateTimeQuery = true;
479                                         }
480                                         isConvertReq = true;
481                                 }
482                                 else
483                                 {
484                                         isConvertReq = false;
485                                 }
486                         }
487
488                         r = inputExpr.Append(token);
489                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "string append failed.");
490                 }
491                 else // value of the column
492                 {
493                         isCol = true;
494                         r = inputExpr.Append("'");
495                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "string append failed.");
496
497                         if (isConvertReq)
498                         {
499                                 Tizen::Base::DateTime dt;
500                                 r = Tizen::Base::DateTime::Parse(token, dt);
501                                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to parse DateTime.");
502
503                                 int year = dt.GetYear();
504                                 int month = dt.GetMonth();
505                                 int day = dt.GetDay();
506                                 int hour = dt.GetHour();
507                                 int minute = dt.GetMinute();
508                                 int second = dt.GetSecond();
509
510                                 time_t rawTime;
511                                 struct tm* timeInfo;
512
513                                 time(&rawTime);
514                                 timeInfo = localtime(&rawTime);
515                                 timeInfo->tm_year = year - 1900;
516                                 timeInfo->tm_mon = month - 1;
517                                 timeInfo->tm_mday = day;
518                                 timeInfo->tm_hour = hour;
519                                 timeInfo->tm_min = minute;
520                                 timeInfo->tm_sec = second;
521
522                                 time_t seconds = mktime(timeInfo);
523                                 SysTryReturnResult(NID_CNT, seconds != -1, E_INVALID_ARG, "Failed to convert DateTime to broken-down time.");
524
525                                 long long ticksInSeconds = (long long)seconds;
526
527                                 r = inputExpr.Append(ticksInSeconds);
528                                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "string append failed.");
529                         }
530                         else
531                         {
532                                 r = inputExpr.Append(token);
533                                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "string append failed.");
534                         }
535
536                         r = inputExpr.Append("'");
537                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "string append failed.");
538                 }
539         }
540
541         __inputExpr.Clear();
542         r = __inputExpr.Insert(inputExpr, 0);
543         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "string insert failed.");
544
545         return r;
546 }
547
548 // CONTENT_TYPE_OTHER and CONTENT_TYPE_IMAGE (0 - 13 ) are valid columns
549 // CONTENT_TYPE_VIDEO  (0 - 16 ) are valid columns
550 // CONTENT_TYPE_ALL and  CONTENT_TYPE_VIDEO (0 - 18 ) are valid columns
551 // This functions checks for invalid column in the given input string (only allowed columns should be used by content type other wise
552 // E_INVALID_ARG is returned)
553 // StringTokenizer is used to parse input expression, by using delimater "'", to differentiate column and value.
554 // As values may also contain column names
555 // Ex: 1. where author = syam_author_01 2. where album = jalsa_album_name_01.
556
557 result
558 _ContentSearchImpl::CheckInvalidColumnInQuery(void) const
559 {
560         result r = E_SUCCESS;
561         int tokenNo = 0;
562         String  delim = L"'"; //Delimeters  is  ' .
563          bool isColReplaceReq = true;
564         String token;
565         String inputExpr = __inputExpr;
566
567         // Create a StringTokenizer instance
568         StringTokenizer  strTok(inputExpr, delim);
569
570         inputExpr.Clear();
571
572         while (strTok.HasMoreTokens())
573         {
574                 r = strTok.GetNextToken(token);
575                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "strTok GetNextToken failed.");
576
577                 SysLog(NID_CNT, "In CheckInvalidColumnInQuery token[%d] = %ls", ++tokenNo, token.GetPointer());
578
579                 if (isColReplaceReq)
580                 {
581                         isColReplaceReq = false;
582                         r = inputExpr.Append(token);
583                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
584                         token.ToUpper();
585                         switch (__contentType)
586                         {
587                         case CONTENT_TYPE_OTHER:
588                                 //fall through
589                         case CONTENT_TYPE_IMAGE:
590                                 if ((token.Contains("TITLE")) || (token.Contains("ARTIST")) || (token.Contains("GENRE")))
591                                 {
592                                         SysLog(NID_CNT, "Title or Artist or Genre are not valid cloumns for this content type");
593                                         return E_INVALID_ARG;
594                                 }
595                                 //fall through
596                         case CONTENT_TYPE_VIDEO:
597                                 if ((token.Contains("COMPOSER")) || (token.Contains("ALBUM")))
598                                 {
599                                         SysLog(NID_CNT, "Composer or Album  are not valid cloumns for this content type");
600                                         return E_INVALID_ARG;
601                                 }
602                                 break;
603                         default:
604                                 break;
605                         }
606                 }
607                 else
608                 {
609                         isColReplaceReq = true;
610                         r = inputExpr.Append("'");
611                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
612                         r = inputExpr.Append(token);
613                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
614                         r = inputExpr.Append("'");
615                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
616                 }
617         }
618         return r;
619 }
620
621 // It replaces native column name with core column name.
622 // StringTokenizer is used to parse input expression, by using delimiter "'", to differentiate column and value.
623 // As values may also contain column names
624 // Only column names are replaced but not values(values may contain column names)
625 // Ex: 1. where author = syam_author_01 2. where album = jalsa_album_name_01.
626
627 result
628 _ContentSearchImpl::ReplaceString(String ospColumnName, String slpColumnName) const
629 {
630         String  delim = L"'"; //Delimiters  is  ' .
631         result r = E_SUCCESS;
632          bool isColReplaceReq = true;
633         String token;
634         String inputExpr = __inputExpr;
635
636         // Create a StringTokenizer instance
637         StringTokenizer  strTok(inputExpr, delim);
638
639         inputExpr.Clear();
640
641         while (strTok.HasMoreTokens())
642         {
643                 r = strTok.GetNextToken(token);         // Osp, StringTokenizer, Sample, code
644                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string GetNextToken() failed.");
645                 if (isColReplaceReq) //column name
646                 {
647                         isColReplaceReq = false;
648                         r = token.Replace(ospColumnName, slpColumnName);
649                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string replace() failed.");
650                         r = inputExpr.Append(token);
651                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
652                 }
653                 else // value of the column
654                 {
655                         isColReplaceReq = true;
656                         r = inputExpr.Append("'");
657                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
658                         r = inputExpr.Append(token);
659                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
660                         r = inputExpr.Append("'");
661                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
662                 }
663         }
664         __inputExpr.Clear();
665         r = __inputExpr.Insert(inputExpr, 0);
666         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string insert() failed.");
667
668         return r;
669 }
670
671 // Returns list of search result for the given expression and content type.
672 Collection::IList*
673 _ContentSearchImpl::SearchN(int pageNo, int countPerPage, int& totalPageCount, int& totalCount, const String& whereExpr, const String& sortColumn, SortOrder sortOrder) const
674 {
675         SysLog(NID_CNT, "pageNo = %d, countPerPage = %d, whereExpr = %ls", pageNo, countPerPage, whereExpr.GetPointer());
676
677         ClearLastResult();
678
679         result r        = E_SUCCESS;
680         int ret         = MEDIA_CONTENT_ERROR_NONE;
681         int offset      = 0;
682
683         __inputColumnName.Clear();
684         __inputExpr.Clear();
685         __inputColumnName = sortColumn;
686         __inputSortOrder = sortOrder;
687         totalPageCount = 0;
688         totalCount = 0;
689
690         if (!whereExpr.IsEmpty()) // copy whereExpr if not empty to class variable __inputExpr
691         {
692                 r = __inputExpr.Append(whereExpr);
693                 SysTryReturn(NID_CNT, r == E_SUCCESS, NULL, r, "[%s] __inputExpr.Append Failed as whereExpr is not empty.", GetErrorMessage(r));
694
695                 r = ReplaceOspColumnNameWithSlp();
696                 SysTryReturn(NID_CNT, r == E_SUCCESS, NULL, r, "[%s] ReplaceOspColumnNameWithSlp for InputExpr Failed.", GetErrorMessage(r));
697
698                 SysLog(NID_CNT, "After replace __inputExpr = %ls", __inputExpr.GetPointer());
699         }
700         if ((__inputColumnName.IsEmpty()) && ((__inputSortOrder == SORT_ORDER_ASCENDING) || (__inputSortOrder == SORT_ORDER_DESCENDING)))
701         {
702                 SysLog(NID_CNT, "sort column name is empty and sort oder is not none. so,E_INVALID_ARG occured.");
703                 SetLastResult(E_INVALID_ARG);
704                 return null;
705         }
706         else
707         {
708                 std::unique_ptr<ArrayList, AllElementsDeleter> pFinalOutList(new (std::nothrow) ArrayList());
709                 SysTryReturn(NID_CNT, pFinalOutList.get() != null, NULL, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to construct ArrayList.");
710
711                 r = pFinalOutList->Construct();
712                 SysTryReturn(NID_CNT, !IsFailed(r), NULL, r, "[%s] Failed to construct ArrayList.", GetErrorMessage(r));
713
714                 __pFinalOutList = std::move(pFinalOutList);
715
716                 r = CreateQueryFilter(true);
717                 SysTryReturn(NID_CNT, !IsFailed(r), NULL, r, "[%s] Failed to perform CreateQueryFilter operation.", GetErrorMessage(r));
718
719                 ret = media_info_get_media_count_from_db(__pFilterHandle.get(), &totalCount);
720                 r = MapCoreErrorToNativeResult(ret);
721                 SysTryReturn(NID_CNT, r == E_SUCCESS , NULL, r, "[%s] Failed to perform media_info_get_media_count_from_db operation.", GetErrorMessage(r));
722
723                 SysLog(NID_CNT, "totalCount is [%d] for media_info_get_media_count_from_db", totalCount);
724
725                 if (totalCount > 0)
726                 {
727                         if ((totalCount % countPerPage) == 0)
728                         {
729                                 totalPageCount = totalCount / countPerPage;
730                         }
731                         else
732                         {
733                                 totalPageCount = (totalCount / countPerPage) + 1;
734                         }
735
736                         SysTryReturn(NID_CNT, ((pageNo >= 1) && (pageNo <= totalPageCount)) , NULL, E_INVALID_ARG,
737                                         "[%s] Failed to perform SearchN operation. pageNo : %d, totalPageCount : %d.", GetErrorMessage(r), pageNo, totalPageCount);
738
739                         offset = (pageNo * countPerPage) - countPerPage;
740
741                         SysLog(NID_CNT, " SearchN   totalCount [%d] totalPageCount[%d] __countPerPage[%d] __pageNo[%d] offset[%d]",
742                                         totalCount, totalPageCount, countPerPage, pageNo, offset);
743
744                         ret = media_filter_set_offset(__pFilterHandle.get(), offset, countPerPage);
745                         r = MapCoreErrorToNativeResult(ret);
746                         SysTryReturn(NID_CNT, r == E_SUCCESS, NULL, r,
747                         "[%s] Failed to perform media_filter_set_offset operation.", GetErrorMessage(r));
748
749                         r = ExecuteAndFillFinalOutList();
750                         SysTryReturn(NID_CNT, r == E_SUCCESS, NULL, E_SYSTEM, "[E_SYSTEM] ExecuteAndFillFinalOutList Failed.");
751                 }
752                 else if (pageNo > 1)
753                 {
754                         SysLogException(NID_CNT, E_INVALID_ARG, "[%s] (pageNo > 1) and  (totalcount = 0).", GetErrorMessage(E_INVALID_ARG));
755
756                         return null;
757                 }
758         }
759         SetLastResult(r);
760         return __pFinalOutList.release();
761 }
762
763 // Fills final result list
764 result
765 _ContentSearchImpl::ExecuteAndFillFinalOutList(void) const
766 {
767         SysLog(NID_CNT, "Enter\n");
768
769         int ret                                         = MEDIA_CONTENT_ERROR_NONE;
770         result r                                        = E_SUCCESS;
771
772         std::unique_ptr<GList, SearchGListDeleter> pItemList;
773         GList* pTemp = NULL;
774         std::unique_ptr<media_info_s, SearchMediaHandleDeleter> pMediaHandle;
775
776         std::unique_ptr<ImageContentInfo> pImageContentInfo;
777         std::unique_ptr<AudioContentInfo> pAudioContentInfo;
778         std::unique_ptr<VideoContentInfo> pVideoContentInfo;
779         std::unique_ptr<OtherContentInfo> pOtherContentInfo;
780
781         _ImageContentInfoImpl* pImageContentInfoImpl = null;
782         _AudioContentInfoImpl* pAudioContentInfoImpl = null;
783         _VideoContentInfoImpl* pVideoContentInfoImpl = null;
784         _OtherContentInfoImpl* pOtherContentInfoImpl = null;
785
786         std::unique_ptr<ContentSearchResult> pContentSearchResult;
787
788         pTemp = pItemList.get();
789
790         ret = media_info_foreach_media_from_db(__pFilterHandle.get(), MediaItemCb, &pTemp);
791         r = MapCoreErrorToNativeResult(ret);
792         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform media_info_foreach_media_from_db operation.");
793
794         SysTryReturnResult(NID_CNT, pTemp != NULL, r, "media_info_foreach_media_from_db pTemp is null.");
795
796         media_content_type_e mediaType = MEDIA_CONTENT_TYPE_OTHERS;
797
798         for (int idx = 0; idx < (int)g_list_length(pTemp); idx++)
799         {
800                 pMediaHandle.reset(static_cast<media_info_h>(g_list_nth_data(pTemp, idx)));
801
802                 ret = media_info_get_media_type(pMediaHandle.get(), &mediaType);
803                 r = MapCoreErrorToNativeResult(ret);
804                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_info_get_media_type.");
805
806                 std::unique_ptr<char, UtilCharDeleter> pMediaPath;
807                 char* pTempPath = null;
808
809                 ret = media_info_get_file_path(pMediaHandle.get(), &pTempPath);
810                 r = MapCoreErrorToNativeResult(ret);
811                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_info_get_file_path operation.");
812
813                 pMediaPath.reset(pTempPath);
814                 String contentPath(pMediaPath.get());
815                 String changedPath(contentPath);
816
817                 // If the api version is 2.0, the content path has to be changed.
818                 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
819                 {
820                         r = _FileImpl::ConvertPhysicalToVirtualPath(contentPath, changedPath);
821                         SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG,
822                                         "[%s] Failed to convert physical path to virtual path.", GetErrorMessage(E_INVALID_ARG));
823                 }
824
825                 switch (mediaType)
826                 {
827                 case MEDIA_CONTENT_TYPE_OTHERS:
828                         pOtherContentInfo = std::unique_ptr<OtherContentInfo>(new (std::nothrow) OtherContentInfo);
829                         SysTryReturnResult(NID_CNT, pOtherContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pOtherContentInfo.");
830
831                         r = pOtherContentInfo->Construct(&changedPath);
832                         r = ConvertErrorToResult(r);
833                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Construct operation to OtherContentInfo.");
834
835                         pOtherContentInfoImpl = null;
836                         pOtherContentInfoImpl = _OtherContentInfoImpl::GetInstance(*(pOtherContentInfo.get()));
837                         SysTryReturnResult(NID_CNT, pOtherContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to assign operation to pOtherContentInfoImpl.");
838
839                         r = _ContentUtility::FillContentData(pMediaHandle.get(), pOtherContentInfoImpl);
840                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
841
842                         _ContentInfoHelper::SetContentInfoImpl(pOtherContentInfo.get(), pOtherContentInfoImpl);
843
844                         pContentSearchResult = std::unique_ptr<ContentSearchResult>(new (std::nothrow) ContentSearchResult);
845                         SysTryReturnResult(NID_CNT, pContentSearchResult.get() != null, E_OUT_OF_MEMORY, "Failed to create pContentSearchResult.");
846
847                         pContentSearchResult->SetContentType(CONTENT_TYPE_OTHER);
848                         pContentSearchResult->SetContentInfo(pOtherContentInfo.release());
849
850                         // Shallow copy, adds just the pointer: not the element
851                         r = __pFinalOutList->Add(*(pContentSearchResult.release()));
852                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation for ArrayList.");
853
854                         break;
855                 case MEDIA_CONTENT_TYPE_IMAGE:
856                         pImageContentInfo = std::unique_ptr<ImageContentInfo>(new (std::nothrow) ImageContentInfo);
857                         SysTryReturnResult(NID_CNT, pImageContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pImageContentInfo.");
858
859                         r = pImageContentInfo->Construct(&changedPath);
860                         r = ConvertErrorToResult(r);
861                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Construct operation to ImageContentInfo.");
862
863                         pImageContentInfoImpl = null;
864                         pImageContentInfoImpl = _ImageContentInfoImpl::GetInstance(*(pImageContentInfo.get()));
865                         SysTryReturnResult(NID_CNT, pImageContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to assign operation to pImageContentInfoImpl.");
866
867                         r = _ContentUtility::FillContentData(pMediaHandle.get(), pImageContentInfoImpl);
868                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
869
870                         r = _ContentUtility::FillImageContentData(pMediaHandle.get(), pImageContentInfoImpl);
871                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform GetDataFromImageTable operation.");
872
873                         _ContentInfoHelper::SetContentInfoImpl(pImageContentInfo.get(), pImageContentInfoImpl);
874
875                         pContentSearchResult = std::unique_ptr<ContentSearchResult>(new (std::nothrow) ContentSearchResult);
876                         SysTryReturnResult(NID_CNT, pContentSearchResult.get() != null, E_OUT_OF_MEMORY, "Failed to create pContentSearchResult.");
877
878                         pContentSearchResult->SetContentType(CONTENT_TYPE_IMAGE);
879                         pContentSearchResult->SetContentInfo(pImageContentInfo.release());
880
881                         // Shallow copy, adds just the pointer: not the element
882                         r = __pFinalOutList->Add(*(pContentSearchResult.release()));
883                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation for ArrayList.");
884
885                         break;
886                 case MEDIA_CONTENT_TYPE_MUSIC:
887                         //fall through
888                 case MEDIA_CONTENT_TYPE_SOUND:
889                         pAudioContentInfo = std::unique_ptr<AudioContentInfo>(new (std::nothrow) AudioContentInfo);
890                         SysTryReturnResult(NID_CNT, pAudioContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pAudioContentInfo.");
891
892                         r = pAudioContentInfo->Construct(&changedPath);
893                         r = ConvertErrorToResult(r);
894                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Construct operation to AudioContentInfo.");
895
896                         pAudioContentInfoImpl = null;
897                         pAudioContentInfoImpl = _AudioContentInfoImpl::GetInstance(*(pAudioContentInfo.get()));
898                         SysTryReturnResult(NID_CNT, pAudioContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to assign operation to pAudioContentInfoImpl.");
899
900                         r = _ContentUtility::FillContentData(pMediaHandle.get(), pAudioContentInfoImpl);
901                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
902
903                         r = _ContentUtility::FillAudioContentData(pMediaHandle.get(), pAudioContentInfoImpl);
904                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillAudioContentData operation.");
905
906                         _ContentInfoHelper::SetContentInfoImpl(pAudioContentInfo.get(), pAudioContentInfoImpl);
907
908                         pContentSearchResult = std::unique_ptr<ContentSearchResult>(new (std::nothrow) ContentSearchResult);
909                         SysTryReturnResult(NID_CNT, pContentSearchResult.get() != null, E_OUT_OF_MEMORY, "Failed to create pContentSearchResult.");
910
911                         pContentSearchResult->SetContentType(CONTENT_TYPE_AUDIO);
912                         pContentSearchResult->SetContentInfo(pAudioContentInfo.release());
913
914                         // Shallow copy, adds just the pointer: not the element
915                         r = __pFinalOutList->Add(*(pContentSearchResult.release()));
916                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation for ArrayList.");
917
918                         break;
919                 case MEDIA_CONTENT_TYPE_VIDEO:
920                         pVideoContentInfo = std::unique_ptr<VideoContentInfo>(new (std::nothrow) VideoContentInfo);
921                         SysTryReturnResult(NID_CNT, pVideoContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pVideoContentInfo.");
922
923                         r = pVideoContentInfo->Construct(&changedPath);
924                         r = ConvertErrorToResult(r);
925                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Construct operation to VideoContentInfo.");
926
927                         pVideoContentInfoImpl = null;
928                         pVideoContentInfoImpl = _VideoContentInfoImpl::GetInstance(*(pVideoContentInfo.get()));
929                         SysTryReturnResult(NID_CNT, pVideoContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to assign operation to pVideoContentInfoImpl.");
930
931                         r = _ContentUtility::FillContentData(pMediaHandle.get(), pVideoContentInfoImpl);
932                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
933
934                         r = _ContentUtility::FillVideoContentData(pMediaHandle.get(), pVideoContentInfoImpl);
935                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillVideoContentData operation.");
936
937                         _ContentInfoHelper::SetContentInfoImpl(pVideoContentInfo.get(), pVideoContentInfoImpl);
938
939                         pContentSearchResult = std::unique_ptr<ContentSearchResult>(new (std::nothrow) ContentSearchResult);
940                         SysTryReturnResult(NID_CNT, pContentSearchResult.get() != null, E_OUT_OF_MEMORY, "Failed to create pContentSearchResult.");
941
942                         pContentSearchResult->SetContentType(CONTENT_TYPE_VIDEO);
943                         pContentSearchResult->SetContentInfo(pVideoContentInfo.release());
944
945                         // Shallow copy, adds just the pointer: not the element
946                         r = __pFinalOutList->Add(*(pContentSearchResult.release()));
947                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation for ArrayList.");
948
949                         break;
950                 default:
951                         break;
952                 }
953         }
954         return r;
955 }
956
957 // returns  given column value list in the requested order
958 Collection::IList*
959 _ContentSearchImpl::GetValueListN(const String& sortColumn, SortOrder sortOrder)
960 {
961         SysLog(NID_CNT, "inputColumn = %ls", sortColumn.GetPointer());
962         ClearLastResult();
963
964         result r        = E_SUCCESS;
965         int colIndex    = 0;
966         int maxCols     = 0;
967
968         __inputColumnName.Clear();
969         __inputExpr.Clear();
970
971         __inputColumnName = sortColumn;
972         __inputSortOrder  = sortOrder;
973
974         String          ospColumnName(L"");
975         String          slpColumnName(L"");
976
977         String          columnName(__inputColumnName);
978
979         if ((__inputColumnName.IsEmpty()) && ((__inputSortOrder == SORT_ORDER_ASCENDING) || (__inputSortOrder == SORT_ORDER_DESCENDING)))
980         {
981                 SysLog(NID_CNT, "sort column name is empty and sort oder is not none. so,E_INVALID_ARG occured.");
982                 SetLastResult(E_INVALID_ARG);
983                 return null;
984         }
985         else
986         {
987                 std::unique_ptr<ArrayList, AllElementsDeleter> pFinalOutList(new (std::nothrow) ArrayList());
988                 SysTryReturn(NID_CNT, pFinalOutList.get() != null, NULL, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to construct ArrayList.");
989
990                 r = pFinalOutList->Construct();
991                 SysTryReturn(NID_CNT, !IsFailed(r), NULL, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to construct ArrayList.");
992
993                 __pFinalOutList = std::move(pFinalOutList);
994
995                 switch (__contentType)
996                 {
997                 case CONTENT_TYPE_OTHER:
998                         //fall through
999                 case CONTENT_TYPE_IMAGE:
1000                         maxCols = MAX_QUERY_COLUMNS_FOR_IMAGE_OTHERS;
1001                         break;
1002                 case CONTENT_TYPE_VIDEO:
1003                         maxCols = MAX_QUERY_COLUMNS_FOR_VIDEO;
1004                         break;
1005                 case CONTENT_TYPE_AUDIO:
1006                         //fall through
1007                 case CONTENT_TYPE_ALL:
1008                         maxCols = MAX_QUERY_COLUMNS;
1009                         break;
1010                 default:
1011                         break;
1012                 }
1013
1014                 for (colIndex = 0; colIndex < maxCols; colIndex++)
1015                 {
1016                         ospColumnName.Clear();
1017                         slpColumnName.Clear();
1018
1019                         ospColumnName = dbfieldinfo[colIndex].dbFieldOspName ;
1020                         slpColumnName = dbfieldinfo[colIndex].dbFieldSlpName ;
1021
1022                         ospColumnName.ToUpper();
1023                         columnName.ToUpper();
1024                         if (columnName == ospColumnName)
1025                         {
1026                                 r = FillColumnsList(colIndex);
1027                                 SysTryReturn(NID_CNT, r == E_SUCCESS, NULL, r, "[%s] FillColumnsList Failed.", GetErrorMessage(r));
1028                                 break;
1029                         }
1030                 }
1031                 if (colIndex == maxCols)
1032                 {
1033                         SysLogException(NID_CNT, E_INVALID_ARG, "[%s] The column is invalid.", GetErrorMessage(E_INVALID_ARG));
1034
1035                         return null;
1036                 }
1037         }
1038         SetLastResult(r);
1039         return __pFinalOutList.release();
1040 }
1041
1042 // returns  given column value list in the requested order
1043 Collection::IList*
1044 _ContentSearchImpl::GetValueListN(int pageNo, int countPerPage, int& totalPageCount, int& totalCount, const String& sortColumn, SortOrder sortOrder) const
1045 {
1046         SysLog(NID_CNT, "pageNo = %d, countPerPage = %d, inputColumn = %ls", pageNo, countPerPage, sortColumn.GetPointer());
1047
1048         ClearLastResult();
1049
1050         result r        = E_SUCCESS;
1051         int colIndex    = 0;
1052         int maxCols     = 0;
1053
1054         __inputColumnName.Clear();
1055         __inputExpr.Clear();
1056
1057         __inputColumnName = sortColumn;
1058         __inputSortOrder  = sortOrder;
1059
1060         String          ospColumnName(L"");
1061         String          slpColumnName(L"");
1062
1063         String          columnName(__inputColumnName);
1064
1065         if ((__inputColumnName.IsEmpty()) && ((__inputSortOrder == SORT_ORDER_ASCENDING) || (__inputSortOrder == SORT_ORDER_DESCENDING)))
1066         {
1067                 SysLog(NID_CNT, "sort column name is empty and sort oder is not none. so,E_INVALID_ARG occured.");
1068                 SetLastResult(E_INVALID_ARG);
1069                 return null;
1070         }
1071         else
1072         {
1073                 std::unique_ptr<ArrayList, AllElementsDeleter> pFinalOutList(new (std::nothrow) ArrayList());
1074                 SysTryReturn(NID_CNT, pFinalOutList.get() != null, NULL, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to construct ArrayList.");
1075
1076                 r = pFinalOutList->Construct();
1077                 SysTryReturn(NID_CNT, !IsFailed(r), NULL, r, "[%s] Failed to construct ArrayList.", GetErrorMessage(r));
1078
1079                 __pFinalOutList = std::move(pFinalOutList);
1080
1081                 switch (__contentType)
1082                 {
1083                 case CONTENT_TYPE_OTHER:
1084                         //fall through
1085                 case CONTENT_TYPE_IMAGE:
1086                         maxCols = MAX_QUERY_COLUMNS_FOR_IMAGE_OTHERS;
1087                         break;
1088                 case CONTENT_TYPE_VIDEO:
1089                         maxCols = MAX_QUERY_COLUMNS_FOR_VIDEO;
1090                         break;
1091                 case CONTENT_TYPE_AUDIO:
1092                         //fall through
1093                 case CONTENT_TYPE_ALL:
1094                         maxCols = MAX_QUERY_COLUMNS;
1095                         break;
1096                 default:
1097                         break;
1098                 }
1099
1100                 for (colIndex = 0; colIndex < maxCols; colIndex++)
1101                 {
1102                         ospColumnName.Clear();
1103                         slpColumnName.Clear();
1104
1105                         ospColumnName = dbfieldinfo[colIndex].dbFieldOspName ;
1106                         slpColumnName = dbfieldinfo[colIndex].dbFieldSlpName ;
1107
1108                         ospColumnName.ToUpper();
1109                         columnName.ToUpper();
1110                         if (columnName == ospColumnName)
1111                         {
1112                                 r = FillColumnsList(pageNo, countPerPage, totalPageCount, totalCount, colIndex);
1113                                 SysTryReturn(NID_CNT, r == E_SUCCESS, NULL, r, "[%s] FillColumnsList Failed.", GetErrorMessage(r));
1114                                 break;
1115                         }
1116                 }
1117                 if (colIndex == maxCols)
1118                 {
1119                         SysLogException(NID_CNT, E_INVALID_ARG, "[%s] The column is invalid.", GetErrorMessage(E_INVALID_ARG));
1120
1121                         return null;
1122                 }
1123         }
1124         SetLastResult(r);
1125         return __pFinalOutList.release();
1126 }
1127
1128 // prepares query expression to be used in group api. colIndex is mapped to actual group value.
1129 media_group_e
1130 _ContentSearchImpl::GetIndexAndCreateQueryExp(int colIndex) const
1131 {
1132         media_group_e groupIndex = MEDIA_CONTENT_GROUP_DISPLAY_NAME;;
1133         switch (colIndex)
1134         {
1135         case 0://"ContentType", "MEDIA_TYPE"
1136                 groupIndex = MEDIA_CONTENT_GROUP_TYPE;
1137                 __inputExpr = "MEDIA_TYPE IS NOT NULL";
1138                 break;
1139         case 1://"ContentFileName", "MEDIA_DISPLAY_NAME"
1140                 groupIndex = MEDIA_CONTENT_GROUP_DISPLAY_NAME;
1141                 __inputExpr = "MEDIA_DISPLAY_NAME IS NOT NULL";
1142                 break;
1143         case 2://"ContentName", "MEDIA_CONTENT_NAME"
1144                 groupIndex = MEDIA_CONTENT_GROUP_CONTENT_NAME;
1145                 __inputExpr = "MEDIA_CONTENT_NAME IS NOT NULL";
1146                 break;
1147         case 3://"Category", "MEDIA_CATEGORY"
1148                 groupIndex = MEDIA_CONTENT_GROUP_CATEGORY;
1149                 __inputExpr = "MEDIA_CATEGORY IS NOT NULL";
1150                 break;
1151         case 4://"Author", "MEDIA_AUTHOR"
1152                 groupIndex = MEDIA_CONTENT_GROUP_AUTHOR;
1153                 __inputExpr = "MEDIA_AUTHOR IS NOT NULL";
1154                 break;
1155         case 5://"keyword", "MEDIA_KEYWORD"
1156                 groupIndex = MEDIA_CONTENT_GROUP_KEYWORD;
1157                 __inputExpr = "MEDIA_KEYWORD IS NOT NULL";
1158                 break;
1159         case 6://"Provider", "MEDIA_PROVIDER"
1160                 groupIndex = MEDIA_CONTENT_GROUP_PROVIDER;
1161                 __inputExpr = "MEDIA_PROVIDER IS NOT NULL";
1162                 break;
1163         case 7://"Rating", "MEDIA_AGE_RATING"
1164                 groupIndex = MEDIA_CONTENT_GROUP_AGE_RATING;
1165                 __inputExpr = "MEDIA_AGE_RATING IS NOT NULL";
1166                 break;
1167         case 8://"LocationTag", "MEDIA_LOCATION_TAG"
1168                 groupIndex = MEDIA_CONTENT_GROUP_LOCATION_TAG;
1169                 __inputExpr = "MEDIA_LOCATION_TAG IS NOT NULL";
1170                 break;
1171         case 9://"ContentSize", "MEDIA_SIZE"
1172                 groupIndex = MEDIA_CONTENT_GROUP_SIZE;
1173                 __inputExpr = "MEDIA_SIZE IS NOT NULL";
1174                 break;
1175         case 10://"DateTime", "MEDIA_ADDED_TIME"
1176                 groupIndex = MEDIA_CONTENT_GROUP_ADDED_TIME;
1177                 __inputExpr = "MEDIA_ADDED_TIME IS NOT NULL";
1178                 break;
1179         case 11://"Latitude", "MEDIA_LATITUDE"
1180                 groupIndex = MEDIA_CONTENT_GROUP_LATITUDE;
1181                 __inputExpr = "MEDIA_LATITUDE IS NOT NULL";
1182                 break;
1183         case 12://"Longitude", "MEDIA_LONGITUDE"
1184                 groupIndex = MEDIA_CONTENT_GROUP_LONGITUDE;
1185                 __inputExpr = "MEDIA_LONGITUDE IS NOT NULL";
1186                 break;
1187         case 13://"Altitude", "MEDIA_ALTITUDE"
1188                 groupIndex = MEDIA_CONTENT_GROUP_ALTITUDE;
1189                 __inputExpr = "MEDIA_ALTITUDE IS NOT NULL";
1190                 break;
1191         case 14://"Title", "MEDIA_TITLE"
1192                 groupIndex = MEDIA_CONTENT_GROUP_TITLE;
1193                 __inputExpr = "MEDIA_TITLE IS NOT NULL";
1194                 break;
1195         case 15://"Artist", "MEDIA_ARTIST"
1196                 groupIndex = MEDIA_CONTENT_GROUP_ARTIST;
1197                 __inputExpr = "MEDIA_ARTIST IS NOT NULL";
1198                 break;
1199         case 16://"Genre", "MEDIA_GENRE"
1200                 groupIndex = MEDIA_CONTENT_GROUP_GENRE;
1201                 __inputExpr = "MEDIA_GENRE IS NOT NULL";
1202                 break;
1203         case 17://"Year", "MEDIA_YEAR"
1204                 groupIndex = MEDIA_CONTENT_GROUP_YEAR;
1205                 __inputExpr = "MEDIA_YEAR IS NOT NULL";
1206                 break;
1207         case 18://"Composer", "MEDIA_COMPOSER"
1208                 groupIndex = MEDIA_CONTENT_GROUP_COMPOSER;
1209                 __inputExpr = "MEDIA_COMPOSER IS NOT NULL";
1210                 break;
1211         case 19://"Album", "MEDIA_ALBUM"
1212                 __inputExpr = "MEDIA_ALBUM IS NOT NULL";
1213                 break;
1214         default:
1215                 break;
1216         }
1217         return groupIndex;
1218 }
1219
1220 // Fills  given column value list and destroys filter handle
1221 result
1222 _ContentSearchImpl::FillColumnsList(int colIndex) const
1223 {
1224         result r        = E_SUCCESS;
1225         int ret         = MEDIA_CONTENT_ERROR_NONE;
1226         int totalCount  = 0;
1227         media_group_e groupIndex = GetIndexAndCreateQueryExp(colIndex);
1228
1229         r = CreateQueryFilter(true);
1230         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "CreateQueryFilter Failed.");
1231
1232         if (colIndex == ALBUM_COLUMN_NUM)
1233         {
1234                 ret = media_album_get_album_count_from_db(__pFilterHandle.get(), &totalCount);
1235         }
1236         else
1237         {
1238                 ret = media_group_get_group_count_from_db(__pFilterHandle.get(), groupIndex, &totalCount);
1239         }
1240         r = MapCoreErrorToNativeResult(ret);
1241         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_album/group count_from_db operation.");
1242
1243         SysLog(NID_CNT, "totalCount = %d for media_album/group count_from_db", totalCount);
1244
1245         if (totalCount > 0)
1246         {
1247                 if (colIndex == ALBUM_COLUMN_NUM)
1248                 {
1249                         r = ExecuteAndFillAlbumValues();
1250                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "ExecuteAndFillAlbumValues Failed.");
1251                 }
1252                 else
1253                 {
1254                         r = ExecuteAndFillGetValueListN(groupIndex, colIndex);
1255                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "ExecuteAndFillGetValueListN Failed.");
1256                 }
1257         }
1258         return r;
1259 }
1260
1261 // prepares input expression to be sent for create filter and fills  given column value list
1262 result
1263 _ContentSearchImpl::FillColumnsList(int pageNo, int countPerPage, int& totalPageCount, int& totalCount, int colIndex) const
1264 {
1265         result r        = E_SUCCESS;
1266         int ret         = MEDIA_CONTENT_ERROR_NONE;
1267         int offset      = 0;
1268         totalPageCount = 0;
1269         totalCount = 0;
1270         media_group_e groupIndex = GetIndexAndCreateQueryExp(colIndex);
1271
1272         r = CreateQueryFilter(true);
1273         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "CreateQueryFilter Failed.");
1274
1275         if (colIndex == ALBUM_COLUMN_NUM)
1276         {
1277                 ret = media_album_get_album_count_from_db(__pFilterHandle.get(), &totalCount);
1278         }
1279         else
1280         {
1281                 ret = media_group_get_group_count_from_db(__pFilterHandle.get(), groupIndex, &totalCount);
1282         }
1283         r = MapCoreErrorToNativeResult(ret);
1284         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_album/group count_from_db operation.");
1285
1286         SysLog(NID_CNT, "totalCount = %d for media_album/group count_from_db", totalCount);
1287
1288         if (totalCount > 0)
1289         {
1290                 if ((totalCount % countPerPage) == 0)
1291                 {
1292                         totalPageCount = totalCount / countPerPage;
1293                 }
1294                 else
1295                 {
1296                         totalPageCount = (totalCount / countPerPage) + 1;
1297                 }
1298
1299                 SysTryReturnResult(NID_CNT, ((pageNo >= 1) && (pageNo <= totalPageCount)), E_INVALID_ARG,
1300                                 "Failed to perform FillColumnsList operation. pageNo : %d, totalPageCount : %d.", pageNo, totalPageCount);
1301
1302                 offset = (pageNo * countPerPage) - countPerPage;
1303
1304                 SysLog(NID_CNT, "GetValueListN totalCount [%d] totalPageCount[%d] __countPerPage[%d] __pageNo[%d] offset[%d]",
1305                                 totalCount, totalPageCount, countPerPage, pageNo, offset);
1306
1307                 ret = media_filter_set_offset(__pFilterHandle.get(),offset,countPerPage);
1308                 r = MapCoreErrorToNativeResult(ret);
1309                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "failed to perform media_filter_set_offset operation.");
1310
1311                 if (colIndex == ALBUM_COLUMN_NUM)
1312                 {
1313                         r = ExecuteAndFillAlbumValues();
1314                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "ExecuteAndFillAlbumValues Failed.");
1315                 }
1316                 else
1317                 {
1318                         r = ExecuteAndFillGetValueListN(groupIndex, colIndex);
1319                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "ExecuteAndFillGetValueListN Failed.");
1320                 }
1321
1322         }
1323         else if (pageNo > 1)
1324         {
1325                 SysLogException(NID_CNT, r = E_INVALID_ARG, "[%s] (pageNo > 1) and  (totalcount = 0).", GetErrorMessage(E_INVALID_ARG));
1326         }
1327
1328         return r;
1329 }
1330
1331 //  fills  given column value list for GetValuelistN api
1332 result
1333 _ContentSearchImpl::ExecuteAndFillGetValueListN(media_group_e groupIndex, int colIndex) const
1334 {
1335         SysLog(NID_CNT, "Enter\n");
1336
1337         int ret                     = MEDIA_CONTENT_ERROR_NONE;
1338         result r                    = E_SUCCESS;
1339         result res                  = E_SUCCESS;
1340         std::unique_ptr<GList, SearchGListDeleter> pItemList;
1341         GList* pTemp = NULL;
1342
1343         std::unique_ptr<Object> pValue;
1344
1345         DateTime dateTime;
1346
1347         long long contentSize = 0;
1348         long long addedTime     = 0;
1349
1350         double dVal     = 0;
1351         char *pColumnVal = null;
1352         int contentType = 0;
1353
1354         pTemp = pItemList.get();
1355
1356         ret = media_group_foreach_group_from_db(__pFilterHandle.get(), groupIndex, GroupItemCb, &pTemp);
1357         r = MapCoreErrorToNativeResult(ret);
1358         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform media_group_foreach_group_from_db operation.");
1359
1360         SysTryReturnResult(NID_CNT, pTemp != NULL, r, "media_info_foreach_media_from_db pTemp is null.");
1361
1362         for (int idx = 0; idx < (int)g_list_length(pTemp); idx++)
1363         {
1364                 SysLog(NID_CNT, "idx = %d and (int)g_list_length(pItemList) = %d", idx, (int)g_list_length(pTemp));
1365
1366                 pColumnVal = (char *)g_list_nth_data(pTemp, idx);
1367
1368                 String strColVal(pColumnVal);
1369
1370                 _ContentUtility::DoSafeFree(pColumnVal);
1371
1372                 SysLog(NID_CNT, "pColumnVal = %ls", strColVal.GetPointer());
1373
1374                 switch (colIndex)
1375                 {
1376                 case 0://"ContentType", "MEDIA_TYPE"
1377                         if ((strColVal.CompareTo(L"Unknown") != 0) && (!strColVal.IsEmpty()))
1378                         {
1379                                 res = Integer::Parse(strColVal, contentType);
1380                                 SysTryLog(NID_CNT, res == E_SUCCESS, "[%s] Integer parse failed.", GetErrorMessage(res));
1381                         }
1382                         switch ((media_content_type_e)contentType)
1383                         {
1384                         case MEDIA_CONTENT_TYPE_OTHERS:
1385                                 pValue.reset(new (std::nothrow) String(CONTENT_TYPE_OTHER));
1386                                 SysLog(NID_CNT, "mediaType = CONTENT_TYPE_OTHER");
1387                                 break;
1388                         case MEDIA_CONTENT_TYPE_IMAGE:
1389                                 pValue.reset(new (std::nothrow) String(CONTENT_TYPE_IMAGE));
1390                                 SysLog(NID_CNT, "mediaType = CONTENT_TYPE_IMAGE");
1391                                 break;
1392                         case MEDIA_CONTENT_TYPE_SOUND:
1393                                 //fall through
1394                         case MEDIA_CONTENT_TYPE_MUSIC:
1395                                 pValue.reset(new (std::nothrow) String(CONTENT_TYPE_AUDIO));
1396                                 SysLog(NID_CNT, "mediaType = CONTENT_TYPE_AUDIO");
1397                                 break;
1398                         case MEDIA_CONTENT_TYPE_VIDEO:
1399                                 pValue.reset(new (std::nothrow) String(CONTENT_TYPE_VIDEO));
1400                                 SysLog(NID_CNT, "mediaType = CONTENT_TYPE_VIDEO");
1401                                 break;
1402                         default:
1403                                 break;
1404                         }
1405                         break;
1406                 case 1://"ContentFileName", "MEDIA_DISPLAY_NAME"
1407                 //fall through
1408                 case 2://"ContentName", "MEDIA_CONTENT_NAME"
1409                 //fall through
1410                 case 3://"Category", "MEDIA_CATEGORY"
1411                 //fall through
1412                 case 4://"Author", "MEDIA_AUTHOR"
1413                 //fall through
1414                 case 5://"keyword", "MEDIA_KEYWORD"
1415                 //fall through
1416                 case 6://"Provider", "MEDIA_PROVIDER"
1417                 //fall through
1418                 case 7://"Rating", "MEDIA_AGE_RATING"
1419                 //fall through
1420                 case 8://"LocationTag", "MEDIA_LOCATION_TAG"
1421                 //fall through
1422                 case 14://"Title", "MEDIA_TITLE"
1423                 //fall through
1424                 case 15://"Artist", "MEDIA_ARTIST"
1425                 //fall through
1426                 case 16://"Genre", "MEDIA_GENRE"
1427                 //fall through
1428                 case 17://"Year", "MEDIA_YEAR"
1429                 //fall through
1430                 case 18://"Composer", "MEDIA_COMPOSER"
1431
1432                         pValue.reset(new (std::nothrow) String(strColVal));
1433                         break;
1434
1435                 case 9://"ContentSize", "MEDIA_SIZE" 
1436                         if ((strColVal.CompareTo(L"Unknown") != 0) && (!strColVal.IsEmpty()))
1437                         {
1438                                 res = LongLong::Parse(strColVal, contentSize);
1439                                 SysTryLog(NID_CNT, res == E_SUCCESS, "[%s] LongLong parse failed.", GetErrorMessage(res));
1440                         }
1441
1442                         pValue.reset(new (std::nothrow) LongLong(contentSize));
1443                         break;
1444                 case 10://"DateTime", "MEDIA_ADDED_TIME" 
1445                         if ((strColVal.CompareTo(L"Unknown") != 0) && (!strColVal.IsEmpty()))
1446                         {
1447                                 res = LongLong::Parse(strColVal, addedTime);
1448                                 SysTryLog(NID_CNT, res == E_SUCCESS, "[%s] LongLong parse failed.", GetErrorMessage(res));
1449                         }
1450
1451                         res = dateTime.SetValue(1970, 1, 1);
1452                         SysTryLog(NID_CNT, res == E_SUCCESS, "[%s] dateTime.SetValue failed.", GetErrorMessage(res));
1453
1454                         res = dateTime.AddSeconds(addedTime);
1455                         SysTryLog(NID_CNT, res == E_SUCCESS, "[%s] dateTime.AddSeconds failed.", GetErrorMessage(res));
1456
1457                         SysLog(NID_CNT, "DateTime : %ls", dateTime.ToString().GetPointer());
1458
1459                         pValue.reset(new (std::nothrow) DateTime(dateTime));
1460
1461                         break;
1462                 case 11://"Latitude", "MEDIA_LATITUDE"
1463                         //fall through
1464                 case 12://"Longitude", "MEDIA_LONGITUDE"
1465                         //fall through
1466                 case 13://"Altitude", "MEDIA_ALTITUDE"
1467                         if ((strColVal.CompareTo(L"Unknown") != 0) && (!strColVal.IsEmpty()))
1468                         {
1469                                 dVal = _LocalizedNumParser::ToDouble(strColVal, "C");
1470                                 r = GetLastResult();
1471                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform ToDouble operation.");
1472                         }
1473                         pValue.reset(new (std::nothrow) Double(dVal));
1474                         break;
1475
1476                 case 19://"Album", "MEDIA_ALBUM"
1477                         break;
1478                 default:
1479                         break;
1480                 }
1481                 if (pValue.get() != NULL)
1482                 {
1483                         r = __pFinalOutList->Add(*(pValue.release()));
1484                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform arraylist Add operation.");
1485                 }
1486         }
1487
1488         return r;
1489 }
1490
1491 result
1492 _ContentSearchImpl::ExecuteAndFillAlbumValues(void) const
1493 {
1494         int ret = MEDIA_CONTENT_ERROR_NONE;
1495         result r = E_SUCCESS;
1496         std::unique_ptr<GList, SearchGListDeleter> pItemList;
1497         GList* pTemp = NULL;
1498         std::unique_ptr<media_album_s, AlbumHandleDeleter> pAlbumHandle;
1499         std::unique_ptr<media_album_s, AlbumHandleDeleter> pTempAlbumHandle;
1500
1501         char* pName = NULL;
1502         std::unique_ptr<char, CharDeleter> pAlbumName;
1503         std::unique_ptr< String > pValue;
1504         int lastIndex = 0;
1505
1506         pTemp = pItemList.get();
1507
1508         ret = media_album_foreach_album_from_db(__pFilterHandle.get(), AlbumItemCb, &pTemp);
1509         r = MapCoreErrorToNativeResult(ret);
1510         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform media_album_foreach_album_from_db operation.");
1511         SysTryReturnResult(NID_CNT, pTemp != NULL, r, "media_info_foreach_media_from_db pTemp is null.");
1512
1513         for (int idx = 0; idx < (int)g_list_length(pTemp); idx++)
1514         {
1515                 pAlbumHandle.reset(static_cast<media_album_h>(g_list_nth_data(pTemp, idx)));
1516                 ret = media_album_get_name(pAlbumHandle.get(), &pName);
1517                 r = MapCoreErrorToNativeResult(ret);
1518                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_album_get_name.");
1519                 
1520                 if (pName != NULL)
1521                 {
1522                         SysLog(NID_CNT, "pColumnVal = %s", pName);
1523
1524                         pAlbumName.reset(pName);
1525                         pValue.reset(new (std::nothrow) String(pAlbumName.get()));
1526                         SysTryReturnResult(NID_CNT, pValue != NULL, E_OUT_OF_MEMORY, "media_info_foreach_media_from_db pTemp is null.");
1527
1528                         if (idx == 0)
1529                         {
1530                                 r = __pFinalOutList->Add(pValue.get());
1531                                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform arraylist Add operation.");
1532                                 pValue.release();
1533                         }
1534                         else
1535                         {
1536                                 String* pTempNameList(static_cast< String* >(__pFinalOutList->GetAt(lastIndex)));
1537
1538                                 if (pValue->CompareTo(*pTempNameList) != 0)
1539                                 {
1540                                         r = __pFinalOutList->Add(pValue.get());
1541                                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform arraylist Add operation.");
1542
1543                                         pValue.release();
1544                                         lastIndex++;
1545                                 }
1546                         }
1547                 }
1548         }
1549
1550         return r;
1551 }
1552
1553 result
1554 _ContentSearchImpl::MapCoreErrorToNativeResult(int reason) const
1555 {
1556         result r = E_SUCCESS;
1557
1558         switch (reason)
1559         {
1560         case MEDIA_CONTENT_ERROR_NONE:
1561                 r = E_SUCCESS;
1562                 break;
1563
1564         case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
1565                 r = E_INVALID_ARG;
1566                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
1567                 break;
1568
1569         case MEDIA_CONTENT_ERROR_DB_FAILED:
1570                 r = E_SYSTEM;
1571                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
1572                 break;
1573
1574         case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
1575                 r = E_OUT_OF_MEMORY;
1576                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
1577                 break;
1578
1579         default:
1580                 SysLog(NID_CNT, "default");
1581                 r = E_SYSTEM;
1582                 break;
1583         }
1584         return r;
1585 }
1586
1587 result
1588 _ContentSearchImpl::ConvertErrorToResult(result res) const
1589 {
1590         result r = E_SUCCESS;
1591
1592         switch (res)
1593         {
1594         case E_FILE_NOT_FOUND:
1595                 // It is processed by normal case when the content exist in DB but the actual file doesn't exist.
1596                 r = E_SUCCESS;
1597                 break;
1598
1599         case E_IO:
1600                 r = E_SYSTEM;
1601                 break;
1602
1603         default:
1604                 r = res;
1605                 break;
1606         }
1607         return r;
1608 }
1609
1610 // Callback function registered to each media info details
1611 // all items are appended to the list
1612 bool
1613 MediaItemCb(media_info_h media, void* pUserdata)
1614 {
1615         int ret  = MEDIA_CONTENT_ERROR_NONE;
1616         media_info_h new_media = NULL;
1617         ret = media_info_clone(&new_media, media);
1618         SysTryLog(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, "[E_SYSTEM] Failed to perform media_info_clone");
1619
1620         GList** pList = (GList**)pUserdata;
1621         *pList = g_list_append(*pList, new_media);
1622
1623         return true;
1624 }
1625
1626 // Callback function registered for column values
1627 // all items are appended to the list
1628 bool
1629 GroupItemCb(const char* pGroupName, void* pUserdata)
1630 {
1631         char* pGrpName = strdup(pGroupName);
1632         GList** pList = (GList**)pUserdata;
1633         *pList = g_list_append(*pList, pGrpName);
1634
1635         return true;
1636 }
1637
1638 // Callback function registered to each media info details
1639 // all items are appended to the list
1640 bool
1641 AlbumItemCb(media_album_h album, void* pUserdata)
1642 {
1643         int ret  = MEDIA_CONTENT_ERROR_NONE;
1644         media_album_h new_album = NULL;
1645         ret = media_album_clone(&new_album, album);
1646         SysTryLog(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, "[E_SYSTEM] Failed to perform media_album_clone");
1647
1648         GList** pList = (GList**)pUserdata;
1649         *pList = g_list_append(*pList, new_album);
1650
1651         return true;
1652 }
1653
1654 }}