Fix code for TDIS-5396
[framework/osp/social.git] / src / FScl_AddressbookUtil.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FScl_AddressbookUtil.h
19  * @brief               This is the header file for the _AddressbookUtil class.
20  *
21  * This header file contains the declarations of the _AddressbookUtil class.
22  */
23 #ifndef _FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_
24 #define _FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_
25
26 #include <unique_ptr.h>
27 #include <contacts.h>
28 #include <FBaseColArrayList.h>
29 #include <FBaseColAllElementsDeleter.h>
30 #include "FScl_ContactDbConnector.h"
31
32 namespace Tizen { namespace Social
33 {
34
35 class Person;
36 class Contact;
37 class UserProfile;
38
39 class __ContactsRecordHandle
40 {
41 public:
42         __ContactsRecordHandle(contacts_record_h handle)
43         :__handle(handle)
44         {
45         }
46
47         ~__ContactsRecordHandle(void)
48         {
49                 if (__handle != null)
50                 {
51                         contacts_record_destroy(__handle, true);
52                 }
53         }
54
55         void Reset(contacts_record_h handle)
56         {
57                 if (__handle != null)
58                 {
59                         
60                         contacts_record_destroy(__handle, true);
61                 }
62
63                 __handle = handle;
64         }
65
66         contacts_record_h Release(void)
67         {
68                 contacts_record_h handle = __handle;
69                 __handle = null;
70
71                 return handle;
72         }
73
74 private:
75         contacts_record_h __handle;
76 };
77
78 template<typename __View>
79 class __SearchResult
80 {
81 public:
82         __SearchResult(contacts_list_h list)
83                 :__list(list), __isFirst(true)
84         {
85
86         }
87
88         int GetCount(void) const
89         {
90                 if (__list != null)
91                 {
92                         unsigned int count = 0;
93                         contacts_list_get_count(__list, &count);
94
95                         return count;
96                 }
97
98                 return -1;
99         }
100
101         contacts_record_h GetCurrentRecord(void) const
102         {
103                 if (__list != null)
104                 {
105                         contacts_record_h record = null;
106                         int ret = contacts_list_get_current_record_p(__list, &record);
107                         if (ret != CONTACTS_ERROR_NONE)
108                         {
109                                 return null;
110                         }
111
112                         return record;
113                 }
114
115                 return null;
116         }
117
118         result MoveNext(void)
119         {
120                 if (__list != null)
121                 {
122                         int ret = CONTACTS_ERROR_NONE;
123
124                         if (!__isFirst)
125                         {
126                                 ret = contacts_list_next(__list);
127                                 if (ret != CONTACTS_ERROR_NONE)
128                                 {
129                                         return E_INVALID_OPERATION;
130                                 }
131
132                                 return E_SUCCESS;
133                         }
134                         else
135                         {
136                                 if (GetCount() > 0)
137                                 {
138                                         __isFirst = false;
139
140                                         return E_SUCCESS;
141                                 }
142
143                                 return E_INVALID_OPERATION;
144                         }
145                 }
146
147                 return E_INVALID_OPERATION;
148         }
149
150
151         ~__SearchResult(void)
152         {
153                 if (__list != null)
154                 {
155                         contacts_list_destroy(__list, true);
156                 }
157         }
158
159 private:
160         contacts_list_h __list;
161         bool __isFirst;
162 };
163
164 template<typename __View>
165 class __Filter
166 {
167 public:
168         __Filter(void)
169         :__filterHandle(null)
170         ,__destroyFilterHandle(false)
171         {
172         }
173
174         result Construct(void)
175         {
176                 contacts_filter_h filterHandle = null;
177                 int ret = contacts_filter_create(__View::GetUri(), &filterHandle);
178                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
179
180                 __filterHandle = filterHandle;
181                 __destroyFilterHandle = true;
182
183                 return E_SUCCESS;
184         }
185
186         result Construct(contacts_filter_h filterHandle)
187         {
188                 __filterHandle = filterHandle;
189                 __destroyFilterHandle = false;
190
191                 return true;
192         }
193
194         contacts_filter_h Get(void) const
195         {
196                 return __filterHandle;
197         }
198
199         result AddString(unsigned int propertyId, contacts_match_str_flag_e match, const char* value)
200         {
201                 int ret = contacts_filter_add_str(__filterHandle, propertyId, match, value);
202                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
203
204                 return E_SUCCESS;
205         }
206
207         result AddInt(unsigned int propertyId, contacts_match_int_flag_e match, int value)
208         {
209                 int ret = contacts_filter_add_int(__filterHandle, propertyId, match, value);
210                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
211
212                 return E_SUCCESS;
213         }
214
215         result AddLongLong(unsigned int propertyId, contacts_match_int_flag_e match, long long int value)
216         {
217                 int ret = contacts_filter_add_lli(__filterHandle, propertyId, match, value);
218                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
219
220                 return E_SUCCESS;
221         }
222
223         result AddDouble(unsigned int propertyId, contacts_match_int_flag_e match, double value)
224         {
225                 int ret = contacts_filter_add_double(__filterHandle, propertyId, match, value);
226                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
227
228                 return E_SUCCESS;
229         }
230
231         result AddBool(unsigned int propertyId, bool value)
232         {
233                 int ret = contacts_filter_add_bool(__filterHandle, propertyId, value);
234                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
235
236                 return E_SUCCESS;
237         }
238
239         result AddOperator(contacts_filter_operator_e type)
240         {
241                 int ret = contacts_filter_add_operator(__filterHandle, type);
242                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
243
244                 return E_SUCCESS;
245         }
246
247         result AddFilter(__Filter<__View>& filter)
248         {
249                 contacts_filter_h handle = filter.Get();
250                 int ret = contacts_filter_add_filter(__filterHandle, handle);
251                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
252
253                 return E_SUCCESS;
254         }
255
256         ~__Filter(void)
257         {
258                 if (__filterHandle != null && __destroyFilterHandle)
259                 {
260                         contacts_filter_destroy(__filterHandle);
261                 }
262         }
263
264 private:
265         contacts_filter_h __filterHandle;
266         bool __destroyFilterHandle;
267 };
268
269 template<typename __View>
270 class __Query
271 {
272 public:
273         __Query(void)
274         :__queryHandle(null)
275         {
276         }
277
278         result Construct(void)
279         {
280                 contacts_query_h queryHandle = null;
281                 int ret = contacts_query_create(__View::GetUri(), &queryHandle);
282                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
283
284                 __queryHandle = queryHandle;
285
286                 return E_SUCCESS;
287         }
288
289         contacts_query_h Get(void) const
290         {
291                 return __queryHandle;
292         }
293
294
295         result SetProjection(unsigned int propertyIds[], int count)
296         {
297                 int ret = contacts_query_set_projection(__queryHandle, propertyIds, count);
298                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
299
300                 return E_SUCCESS;
301         }
302
303         result SetDistinct(bool set)
304         {
305                 int ret = contacts_query_set_distinct(__queryHandle, set);
306                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
307
308                 return E_SUCCESS;
309         }
310
311         result SetFilter(__Filter<__View>& filter)
312         {
313                 contacts_filter_h filterHandle = filter.Get();
314
315                 if (filterHandle != null)
316                 {
317                         int ret = contacts_query_set_filter(__queryHandle, filterHandle);
318                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
319                 }
320
321                 return E_SUCCESS;
322         }
323
324         result SetSort(unsigned int propertyId, bool ascending)
325         {
326                 int ret = contacts_query_set_sort(__queryHandle, propertyId, ascending);
327                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
328
329                 return E_SUCCESS;
330         }
331
332         ~__Query(void)
333         {
334                 if (__queryHandle != null)
335                 {
336                         contacts_query_destroy(__queryHandle);
337                 }
338         }
339
340 private:
341         contacts_query_h __queryHandle;
342
343 };
344
345 class __ContactsAddressbook
346 {
347 public:
348         static const char* GetUri(void)
349         {
350                 return _contacts_address_book._uri;
351         }
352
353         template<typename Type>
354         static Type* ConvertResultTo(__SearchResult<__ContactsAddressbook>& searchResult);
355
356 };
357
358 class __ContactsPerson
359 {
360 public:
361         static const char* GetUri(void)
362         {
363                 return _contacts_person._uri;
364         }
365
366         template<typename Type>
367         static Type* ConvertResultTo(__SearchResult<__ContactsPerson>& searchResult);
368
369         template<typename Type>
370         static Type* ConvertHandleTo(contacts_record_h recordHandle);
371 };
372
373
374 class __ContactsPersonEmail
375 {
376 public:
377         static const char* GetUri(void)
378         {
379                 return _contacts_person_email._uri;
380         }
381 };
382
383 class __ContactsPersonGroupRel
384 {
385 public:
386         static const char* GetUri(void)
387         {
388                 return _contacts_person_grouprel._uri;
389         }
390
391         template<typename Type>
392         static Type* ConvertHandleTo(contacts_record_h recordHandle);
393
394         template<typename Type>
395         static Type* ConvertResultTo(__SearchResult<__ContactsPersonGroupRel>& searchResult);
396
397 };
398
399 class __ContactsPersonNumber
400 {
401 public:
402         static const char* GetUri(void)
403         {
404                 return _contacts_person_number._uri;
405         }
406 };
407
408
409 class __ContactsGroup
410 {
411 public:
412         static const char* GetUri(void)
413         {
414                 return _contacts_group._uri;
415         }
416
417         template<typename Type>
418         static Type* ConvertResultTo(__SearchResult<__ContactsGroup>& searchResult);
419 };
420
421 class __ContactsGroupRelation
422 {
423 public:
424         static const char* GetUri(void)
425         {
426                 return _contacts_group_relation._uri;
427         }
428
429         template<typename Type>
430         static Type* ConvertResultTo(__SearchResult<__ContactsGroupRelation>& searchResult);
431 };
432
433 class __ContactsContact
434 {
435 public:
436         static const char* GetUri(void)
437         {
438                 return _contacts_contact._uri;
439         }
440
441         template<typename Type>
442         static Type* ConvertResultTo(__SearchResult<__ContactsContact>& searchResult);
443 };
444
445 class __ContactsContactGroupRel
446 {
447 public:
448         static const char* GetUri(void)
449         {
450                 return _contacts_contact_grouprel._uri;
451         }
452
453         template<typename Type>
454         static Type* ConvertResultTo(__SearchResult<__ContactsContactGroupRel>& searchResult);
455 };
456
457 class __ContactsContactEmail
458 {
459 public:
460         static const char* GetUri(void)
461         {
462                 return _contacts_contact_email._uri;
463         }
464
465         template<typename Type>
466         static Type* ConvertResultTo(__SearchResult<__ContactsContactEmail>& searchResult);
467 };
468
469 class __ContactsContactNumber
470 {
471 public:
472         static const char* GetUri(void)
473         {
474                 return _contacts_contact_number._uri;
475         }
476
477         template<typename Type>
478         static Type* ConvertResultTo(__SearchResult<__ContactsContactNumber>& searchResult);
479 };
480
481 class __ContactsContactUpdatedInfo
482 {
483 public:
484         static const char* GetUri(void)
485         {
486                 return _contacts_contact_updated_info._uri;
487         }
488
489         template<typename Type>
490         static Type* ConvertResultTo(__SearchResult<__ContactsContactUpdatedInfo>& searchResult);
491 };
492
493 class __ContactsMyProfileUpdatedInfo
494 {
495 public:
496         static const char* GetUri(void)
497         {
498                 return _contacts_my_profile_updated_info._uri;
499         }
500 };
501
502 class __ContactsGroupUpdatedInfo
503 {
504 public:
505         static const char* GetUri(void)
506         {
507                 return _contacts_group_updated_info._uri;
508         }
509
510         template<typename Type>
511         static Type* ConvertResultTo(__SearchResult<__ContactsGroupUpdatedInfo>& searchResult);
512 };
513
514 class __ContactsGroupRelUpdatedInfo
515 {
516 public:
517         static const char* GetUri(void)
518         {
519                 return _contacts_grouprel_updated_info._uri;
520         }
521
522         template<typename Type>
523         static Type* ConvertResultTo(__SearchResult<__ContactsGroupRelUpdatedInfo>& searchResult);
524 };
525
526 class __ContactsUserProfile
527 {
528 public:
529         static const char* GetUri(void)
530         {
531                 return _contacts_my_profile._uri;
532         }
533
534         template<typename Type>
535         static Type* ConvertResultTo(__SearchResult<__ContactsUserProfile>& searchResult);
536 };
537
538
539 class _AddressbookUtil
540 {
541 public:
542         template<typename __View>
543         static __SearchResult<__View>* ExecuteQuery(const __Query<__View>& query, int offset = 0, int limit = 0)
544         {
545                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
546                 __SearchResult<__View>* pSearchResult = null;
547
548                 contacts_list_h list = null;
549                 int ret = contacts_db_get_records_with_query(query.Get(), offset, limit, &list);
550                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
551                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] No matched item found.", GetErrorMessage(E_OBJ_NOT_FOUND));
552                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
553
554                 pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
555                 SysTryReturn(NID_SCL, pSearchResult != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
556
557                 return pSearchResult;
558         }
559
560         template<typename __View>
561         static int GetCountWithQuery(const __Query<__View>& query)
562         {
563                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
564
565                 int count = 0;
566                 int ret = contacts_db_get_count_with_query(query.Get(), &count);
567                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
568                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
569
570                 return count;
571         }
572
573         template<typename __View>
574         static int GetCount(void)
575         {
576                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
577
578                 int count = 0;
579                 int ret = contacts_db_get_count(__View::GetUri(), &count);
580                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
581                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
582
583                 return count;
584         }
585
586         template<typename __View>
587         static __SearchResult<__View>* GetAllRecords(int offset = 0, int limit = 0)
588         {
589                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
590
591                 __SearchResult<__View>* pSearchResult = null;
592
593                 contacts_list_h list = null;
594                 int ret = contacts_db_get_all_records(__View::GetUri(), offset, limit, &list);
595                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
596                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
597
598                 pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
599                 SysTryReturn(NID_SCL, pSearchResult, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
600
601                 return pSearchResult;
602         }
603
604         template<typename __View>
605         static __SearchResult<__View>* GetChangesByVersion(int id, int current, int& latest)
606         {
607                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
608
609                 __SearchResult<__View>* pSearchResult = null;
610
611                 contacts_list_h list = null;
612                 int ret = contacts_db_get_changes_by_version(__View::GetUri(), id, current, &list, &latest);
613                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
614                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
615
616                 pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
617                 SysTryReturn(NID_SCL, pSearchResult, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
618
619                 return pSearchResult;
620         }
621
622         template<typename __View>
623         static int GetChangedItemCountByVersion(int id, int current, int& latest)
624         {
625                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
626
627                 unsigned int count = 0;
628                 contacts_list_h list = null;
629                 int ret = contacts_db_get_changes_by_version(__View::GetUri(), id, current, &list, &latest);
630                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
631                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
632
633                 contacts_list_get_count(list, &count);
634                 contacts_list_destroy(list, true);
635
636                 return count;
637         }
638
639         template<typename __View>
640         static __SearchResult<__View>* Search(const Tizen::Base::String& keyword);
641
642         static Person* CreatePersonN(void);
643
644         template<typename __View, typename ChangeInfo>
645         static Tizen::Base::Collection::IList* SearchWithVersionN(int addressbookId, int currentVersion, int& latestVersion)
646         {
647                 SysTryReturn(NID_SCL, currentVersion >= 0, null, E_INVALID_ARG, "[%s] currentVersion %d must be greater that or equal 0.", GetErrorMessage(GetLastResult()), currentVersion);
648
649                 std::unique_ptr<ChangeInfo> pChangeInfo(null);
650
651                 ClearLastResult();
652
653                 std::unique_ptr<Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter> pList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
654                 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
655
656                 result r = pList->Construct();
657                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
658
659                 std::unique_ptr<__SearchResult<__View> > pSearchResult(_AddressbookUtil::template GetChangesByVersion<__View>(addressbookId, currentVersion, latestVersion));
660                 SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
661
662                 while (pSearchResult->MoveNext() == E_SUCCESS)
663                 {
664                         pChangeInfo.reset(__View::template ConvertResultTo<ChangeInfo>(*pSearchResult));
665                         if (pChangeInfo == null)
666                         {
667                                 SysTryReturn(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
668                                 SysTryReturn(NID_SCL, GetLastResult() != E_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
669
670                                 ClearLastResult();
671                                 continue;
672                         }
673
674                         r = pList->Add(*pChangeInfo);
675                         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
676
677                         pChangeInfo.release();
678                 }
679
680                 return pList.release();
681
682         }
683
684         template<typename __View, typename Data>
685         static Tizen::Base::Collection::IList* SearchWithQueryN(__Query<__View>& query, int offset = 0, int limit = 0)
686         {
687                 std::unique_ptr<Data> pData(null);
688
689                 std::unique_ptr<Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter> pDataList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
690                 SysTryReturn(NID_SCL, pDataList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
691
692                 result r = pDataList->Construct();
693                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
694
695                 std::unique_ptr<__SearchResult<__View> > pSearchResult(_AddressbookUtil::ExecuteQuery(query, offset, limit));
696                 if (pSearchResult == null)
697                 {
698                         SysTryReturn(NID_SCL, GetLastResult() == E_OBJ_NOT_FOUND, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
699
700                         ClearLastResult();
701
702                         return pDataList.release();
703                 }
704
705                 while (pSearchResult->MoveNext() == E_SUCCESS)
706                 {
707                         pData.reset(__View::template ConvertResultTo<Data>(*pSearchResult));
708                         if (pData == null)
709                         {
710                                 SysTryReturn(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
711                                 SysTryReturn(NID_SCL, GetLastResult() != E_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
712
713                                 ClearLastResult();
714                                 continue;
715                         }
716
717                         r = pDataList->Add(*pData);
718                         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
719
720                         pData.release();
721                 }
722
723                 return pDataList.release();
724         }
725
726         template<typename __View>
727         static int GetMatchedItemCountWithFilter(contacts_filter_h filterHandle)
728         {
729                 __Filter<__View> filter;
730                 filter.Construct(filterHandle);
731
732                 __Query<__View> query;
733                 query.Construct();
734                 query.SetFilter(filter);
735
736                 int count = _AddressbookUtil::GetCountWithQuery(query);
737
738                 return count;
739         }
740 };
741
742 }} // Tizen::Social
743
744 #endif //_FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_