Modify the spec file for secure log
[framework/osp/social.git] / src / FScl_AddressbookUtil.h
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                FScl_AddressbookUtil.h
18  * @brief               This is the header file for the _AddressbookUtil class.
19  *
20  * This header file contains the declarations of the _AddressbookUtil class.
21  */
22 #ifndef _FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_
23 #define _FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_
24
25 #include <unique_ptr.h>
26 #include <contacts.h>
27 #include <FBaseColArrayList.h>
28 #include <FBaseColAllElementsDeleter.h>
29 #include "FScl_ContactDbConnector.h"
30
31 namespace Tizen { namespace Social
32 {
33
34 class Person;
35 class Contact;
36 class UserProfile;
37
38 typedef __contacts_record_h ContactRecord;
39
40 struct ContactRecordDeleter
41 {
42         void operator()(ContactRecord* pRecord)
43         {
44                 contacts_record_destroy(pRecord, true);
45         }
46 };
47
48
49 class __ContactsRecordHandle
50 {
51 public:
52         __ContactsRecordHandle(contacts_record_h handle)
53         :__handle(handle)
54         {
55         }
56
57         ~__ContactsRecordHandle(void)
58         {
59                 if (__handle != null)
60                 {
61                         contacts_record_destroy(__handle, true);
62                 }
63         }
64
65         void Reset(contacts_record_h handle)
66         {
67                 if (__handle != null)
68                 {
69                         contacts_record_destroy(__handle, true);
70                 }
71
72                 __handle = handle;
73         }
74
75         contacts_record_h Release(void)
76         {
77                 contacts_record_h handle = __handle;
78                 __handle = null;
79
80                 return handle;
81         }
82
83 private:
84         contacts_record_h __handle;
85 };
86
87 template<typename __View>
88 class __SearchResult
89 {
90 public:
91         __SearchResult(contacts_list_h list)
92                 :__list(list), __isFirst(true)
93         {
94
95         }
96
97         int GetCount(void) const
98         {
99                 if (__list != null)
100                 {
101                         unsigned int count = 0;
102                         contacts_list_get_count(__list, &count);
103
104                         return count;
105                 }
106
107                 return -1;
108         }
109
110         contacts_record_h GetCurrentRecord(void) const
111         {
112                 if (__list != null)
113                 {
114                         contacts_record_h record = null;
115                         int ret = contacts_list_get_current_record_p(__list, &record);
116                         if (ret != CONTACTS_ERROR_NONE)
117                         {
118                                 return null;
119                         }
120
121                         return record;
122                 }
123
124                 return null;
125         }
126
127         result MoveNext(void)
128         {
129                 if (__list != null)
130                 {
131                         int ret = CONTACTS_ERROR_NONE;
132
133                         if (!__isFirst)
134                         {
135                                 ret = contacts_list_next(__list);
136                                 if (ret != CONTACTS_ERROR_NONE)
137                                 {
138                                         return E_INVALID_OPERATION;
139                                 }
140
141                                 return E_SUCCESS;
142                         }
143                         else
144                         {
145                                 if (GetCount() > 0)
146                                 {
147                                         __isFirst = false;
148
149                                         return E_SUCCESS;
150                                 }
151
152                                 return E_INVALID_OPERATION;
153                         }
154                 }
155
156                 return E_INVALID_OPERATION;
157         }
158
159
160         ~__SearchResult(void)
161         {
162                 if (__list != null)
163                 {
164                         contacts_list_destroy(__list, true);
165                 }
166         }
167
168 private:
169         contacts_list_h __list;
170         bool __isFirst;
171 };
172
173 template<typename __View>
174 class __Filter
175 {
176 public:
177         __Filter(void)
178         :__filterHandle(null)
179         ,__destroyFilterHandle(false)
180         {
181         }
182
183         result Construct(void)
184         {
185                 contacts_filter_h filterHandle = null;
186                 int ret = contacts_filter_create(__View::GetUri(), &filterHandle);
187                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
188
189                 __filterHandle = filterHandle;
190                 __destroyFilterHandle = true;
191
192                 return E_SUCCESS;
193         }
194
195         result Construct(contacts_filter_h filterHandle)
196         {
197                 __filterHandle = filterHandle;
198                 __destroyFilterHandle = false;
199         
200                 return E_SUCCESS;
201         }
202
203         contacts_filter_h Get(void) const
204         {
205                 return __filterHandle;
206         }
207
208         result AddString(unsigned int propertyId, contacts_match_str_flag_e match, const char* value)
209         {
210                 int ret = contacts_filter_add_str(__filterHandle, propertyId, match, value);
211                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
212
213                 return E_SUCCESS;
214         }
215
216         result AddInt(unsigned int propertyId, contacts_match_int_flag_e match, int value)
217         {
218                 int ret = contacts_filter_add_int(__filterHandle, propertyId, match, value);
219                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
220
221                 return E_SUCCESS;
222         }
223
224         result AddLongLong(unsigned int propertyId, contacts_match_int_flag_e match, long long int value)
225         {
226                 int ret = contacts_filter_add_lli(__filterHandle, propertyId, match, value);
227                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
228
229                 return E_SUCCESS;
230         }
231
232         result AddDouble(unsigned int propertyId, contacts_match_int_flag_e match, double value)
233         {
234                 int ret = contacts_filter_add_double(__filterHandle, propertyId, match, value);
235                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
236
237                 return E_SUCCESS;
238         }
239
240         result AddBool(unsigned int propertyId, bool value)
241         {
242                 int ret = contacts_filter_add_bool(__filterHandle, propertyId, value);
243                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
244
245                 return E_SUCCESS;
246         }
247
248         result AddOperator(contacts_filter_operator_e type)
249         {
250                 int ret = contacts_filter_add_operator(__filterHandle, type);
251                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
252
253                 return E_SUCCESS;
254         }
255
256         result AddFilter(__Filter<__View>& filter)
257         {
258                 contacts_filter_h handle = filter.Get();
259                 int ret = contacts_filter_add_filter(__filterHandle, handle);
260                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
261
262                 return E_SUCCESS;
263         }
264
265         ~__Filter(void)
266         {
267                 if (__filterHandle != null && __destroyFilterHandle)
268                 {
269                         contacts_filter_destroy(__filterHandle);
270                 }
271         }
272
273 private:
274         contacts_filter_h __filterHandle;
275         bool __destroyFilterHandle;
276 };
277
278 template<typename __View>
279 class __Query
280 {
281 public:
282         __Query(void)
283         :__queryHandle(null)
284         {
285         }
286
287         result Construct(void)
288         {
289                 contacts_query_h queryHandle = null;
290                 int ret = contacts_query_create(__View::GetUri(), &queryHandle);
291                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
292
293                 __queryHandle = queryHandle;
294
295                 return E_SUCCESS;
296         }
297
298         contacts_query_h Get(void) const
299         {
300                 return __queryHandle;
301         }
302
303
304         result SetProjection(unsigned int propertyIds[], int count)
305         {
306                 int ret = contacts_query_set_projection(__queryHandle, propertyIds, count);
307                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
308
309                 return E_SUCCESS;
310         }
311
312         result SetDistinct(bool set)
313         {
314                 int ret = contacts_query_set_distinct(__queryHandle, set);
315                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
316
317                 return E_SUCCESS;
318         }
319
320         result SetFilter(__Filter<__View>& filter)
321         {
322                 contacts_filter_h filterHandle = filter.Get();
323
324                 if (filterHandle != null)
325                 {
326                         int ret = contacts_query_set_filter(__queryHandle, filterHandle);
327                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
328                 }
329
330                 return E_SUCCESS;
331         }
332
333         result SetSort(unsigned int propertyId, bool ascending)
334         {
335                 int ret = contacts_query_set_sort(__queryHandle, propertyId, ascending);
336                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
337
338                 return E_SUCCESS;
339         }
340
341         ~__Query(void)
342         {
343                 if (__queryHandle != null)
344                 {
345                         contacts_query_destroy(__queryHandle);
346                 }
347         }
348
349 private:
350         contacts_query_h __queryHandle;
351
352 };
353
354 class __ContactsAddressbook
355 {
356 public:
357         static const char* GetUri(void)
358         {
359                 return _contacts_address_book._uri;
360         }
361
362         template<typename Type>
363         static Type* ConvertResultTo(__SearchResult<__ContactsAddressbook>& searchResult);
364
365         static int GetAbPropertyId(void)
366         {
367                 return _contacts_address_book.id;
368         }
369
370 };
371
372 class __ContactsPerson
373 {
374 public:
375         static const char* GetUri(void)
376         {
377                 return _contacts_person._uri;
378         }
379
380         template<typename Type>
381         static Type* ConvertResultTo(__SearchResult<__ContactsPerson>& searchResult);
382
383         template<typename Type>
384         static Type* ConvertHandleTo(contacts_record_h recordHandle);
385 };
386
387
388 class __ContactsPersonEmail
389 {
390 public:
391         static const char* GetUri(void)
392         {
393                 return _contacts_person_email._uri;
394         }
395 };
396
397 class __ContactsPersonGroupRel
398 {
399 public:
400         static const char* GetUri(void)
401         {
402                 return _contacts_person_grouprel._uri;
403         }
404
405         template<typename Type>
406         static Type* ConvertHandleTo(contacts_record_h recordHandle);
407
408         template<typename Type>
409         static Type* ConvertResultTo(__SearchResult<__ContactsPersonGroupRel>& searchResult);
410
411         static int GetAbPropertyId(void)
412         {
413                 return _contacts_person_grouprel.address_book_id;
414         }
415 };
416
417 class __ContactsPersonNumber
418 {
419 public:
420         static const char* GetUri(void)
421         {
422                 return _contacts_person_number._uri;
423         }
424 };
425
426
427 class __ContactsGroup
428 {
429 public:
430         static const char* GetUri(void)
431         {
432                 return _contacts_group._uri;
433         }
434
435         template<typename Type>
436         static Type* ConvertResultTo(__SearchResult<__ContactsGroup>& searchResult);
437
438         static int GetAbPropertyId(void)
439         {
440                 return _contacts_group.address_book_id;
441         }
442 };
443
444 class __ContactsGroupRelation
445 {
446 public:
447         static const char* GetUri(void)
448         {
449                 return _contacts_group_relation._uri;
450         }
451
452         template<typename Type>
453         static Type* ConvertResultTo(__SearchResult<__ContactsGroupRelation>& searchResult);
454 };
455
456 class __ContactsContact
457 {
458 public:
459         static const char* GetUri(void)
460         {
461                 return _contacts_contact._uri;
462         }
463
464         template<typename Type>
465         static Type* ConvertResultTo(__SearchResult<__ContactsContact>& searchResult);
466
467         static int GetAbPropertyId(void)
468         {
469                 return _contacts_contact.address_book_id;
470         }
471 };
472
473 class __ContactsContactGroupRel
474 {
475 public:
476         static const char* GetUri(void)
477         {
478                 return _contacts_contact_grouprel._uri;
479         }
480
481         template<typename Type>
482         static Type* ConvertResultTo(__SearchResult<__ContactsContactGroupRel>& searchResult);
483
484         static int GetAbPropertyId(void)
485         {
486                 return _contacts_contact_grouprel.address_book_id;
487         }
488 };
489
490 class __ContactsContactEmail
491 {
492 public:
493         static const char* GetUri(void)
494         {
495                 return _contacts_contact_email._uri;
496         }
497
498         template<typename Type>
499         static Type* ConvertResultTo(__SearchResult<__ContactsContactEmail>& searchResult);
500
501         static int GetAbPropertyId(void)
502         {
503                 return _contacts_contact_email.address_book_id;
504         }
505 };
506
507 class __ContactsContactNumber
508 {
509 public:
510         static const char* GetUri(void)
511         {
512                 return _contacts_contact_number._uri;
513         }
514
515         template<typename Type>
516         static Type* ConvertResultTo(__SearchResult<__ContactsContactNumber>& searchResult);
517
518         static int GetAbPropertyId(void)
519         {
520                 return _contacts_contact_number.address_book_id;
521         }
522 };
523
524 class __ContactsContactUpdatedInfo
525 {
526 public:
527         static const char* GetUri(void)
528         {
529                 return _contacts_contact_updated_info._uri;
530         }
531
532         template<typename Type>
533         static Type* ConvertResultTo(__SearchResult<__ContactsContactUpdatedInfo>& searchResult);
534 };
535
536 class __ContactsMyProfileUpdatedInfo
537 {
538 public:
539         static const char* GetUri(void)
540         {
541                 return _contacts_my_profile_updated_info._uri;
542         }
543 };
544
545 class __ContactsGroupUpdatedInfo
546 {
547 public:
548         static const char* GetUri(void)
549         {
550                 return _contacts_group_updated_info._uri;
551         }
552
553         template<typename Type>
554         static Type* ConvertResultTo(__SearchResult<__ContactsGroupUpdatedInfo>& searchResult);
555 };
556
557 class __ContactsGroupRelUpdatedInfo
558 {
559 public:
560         static const char* GetUri(void)
561         {
562                 return _contacts_grouprel_updated_info._uri;
563         }
564
565         template<typename Type>
566         static Type* ConvertResultTo(__SearchResult<__ContactsGroupRelUpdatedInfo>& searchResult);
567 };
568
569 class __ContactsUserProfile
570 {
571 public:
572         static const char* GetUri(void)
573         {
574                 return _contacts_my_profile._uri;
575         }
576
577         template<typename Type>
578         static Type* ConvertResultTo(__SearchResult<__ContactsUserProfile>& searchResult);
579
580         static int GetAbPropertyId(void)
581         {
582                 return _contacts_my_profile.address_book_id;
583         }
584 };
585
586
587 class _AddressbookUtil
588 {
589 public:
590         template<typename __View>
591         static __SearchResult<__View>* ExecuteQuery(const __Query<__View>& query, int offset = 0, int limit = 0)
592         {
593                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
594                 __SearchResult<__View>* pSearchResult = null;
595
596                 contacts_list_h list = null;
597                 int ret = contacts_db_get_records_with_query(query.Get(), offset, limit, &list);
598                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
599                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] No matched item found. (%d)", GetErrorMessage(E_OBJ_NOT_FOUND), ret);
600                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
601
602                 pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
603                 SysTryReturn(NID_SCL, pSearchResult != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
604
605                 return pSearchResult;
606         }
607
608         template<typename __View>
609         static int GetCountWithQuery(const __Query<__View>& query)
610         {
611                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
612
613                 int count = 0;
614                 int ret = contacts_db_get_count_with_query(query.Get(), &count);
615                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
616                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
617
618                 return count;
619         }
620
621         template<typename __View>
622         static int GetCount(void)
623         {
624                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
625
626                 int count = 0;
627                 int ret = contacts_db_get_count(__View::GetUri(), &count);
628                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
629                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
630
631                 return count;
632         }
633
634         template<typename __View>
635         static __SearchResult<__View>* GetAllRecords(int offset = 0, int limit = 0)
636         {
637                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
638
639                 __SearchResult<__View>* pSearchResult = null;
640
641                 contacts_list_h list = null;
642                 int ret = contacts_db_get_all_records(__View::GetUri(), offset, limit, &list);
643                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
644                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
645
646                 pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
647                 SysTryReturn(NID_SCL, pSearchResult, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
648
649                 return pSearchResult;
650         }
651
652         template<typename __View>
653         static __SearchResult<__View>* GetChangesByVersion(int id, int current, int& latest)
654         {
655                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
656
657                 __SearchResult<__View>* pSearchResult = null;
658
659                 contacts_list_h list = null;
660                 int ret = contacts_db_get_changes_by_version(__View::GetUri(), id, current, &list, &latest);
661                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
662                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
663
664                 pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
665                 SysTryReturn(NID_SCL, pSearchResult, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
666
667                 return pSearchResult;
668         }
669
670         template<typename __View>
671         static int GetChangedItemCountByVersion(int id, int current, int& latest)
672         {
673                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
674
675                 unsigned int count = 0;
676                 contacts_list_h list = null;
677                 int ret = contacts_db_get_changes_by_version(__View::GetUri(), id, current, &list, &latest);
678                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
679                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
680
681                 contacts_list_get_count(list, &count);
682                 contacts_list_destroy(list, true);
683
684                 return count;
685         }
686
687         template<typename __View>
688         static __SearchResult<__View>* Search(const Tizen::Base::String& keyword);
689
690         static Person* CreatePersonN(void);
691
692         template<typename __View, typename ChangeInfo>
693         static Tizen::Base::Collection::IList* SearchWithVersionN(int addressbookId, int currentVersion, int& latestVersion, Tizen::Base::Collection::IListT<AddressbookId>* pRwAbIdList = null)
694         {
695                 SysTryReturn(NID_SCL, currentVersion >= 0, null, E_INVALID_ARG, "[%s] currentVersion %d must be greater that or equal 0.", GetErrorMessage(GetLastResult()), currentVersion);
696
697                 std::unique_ptr<ChangeInfo> pChangeInfo(null);
698
699                 ClearLastResult();
700
701                 std::unique_ptr<Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter> pList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
702                 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
703
704                 result r = pList->Construct();
705                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
706
707                 std::unique_ptr<__SearchResult<__View> > pSearchResult(_AddressbookUtil::template GetChangesByVersion<__View>(addressbookId, currentVersion, latestVersion));
708                 SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
709
710                 while (pSearchResult->MoveNext() == E_SUCCESS)
711                 {
712                         pChangeInfo.reset(__View::template ConvertResultTo<ChangeInfo>(*pSearchResult));
713                         if (pRwAbIdList != null && (pRwAbIdList->Contains(pChangeInfo->GetAddressbookId())))
714                         {
715                                 continue;
716                         }
717
718                         if (pChangeInfo == null)
719                         {
720                                 SysTryReturn(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
721                                 SysTryReturn(NID_SCL, GetLastResult() != E_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
722
723                                 ClearLastResult();
724                                 continue;
725                         }
726
727                         r = pList->Add(*pChangeInfo);
728                         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
729
730                         pChangeInfo.release();
731                 }
732
733                 return pList.release();
734
735         }
736
737         template<typename __View, typename Data>
738         static Tizen::Base::Collection::IList* SearchWithQueryN(__Query<__View>& query, int offset = 0, int limit = 0)
739         {
740                 std::unique_ptr<Data> pData(null);
741
742                 std::unique_ptr<Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter> pDataList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
743                 SysTryReturn(NID_SCL, pDataList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
744
745                 result r = pDataList->Construct();
746                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
747
748                 std::unique_ptr<__SearchResult<__View> > pSearchResult(_AddressbookUtil::ExecuteQuery(query, offset, limit));
749                 if (pSearchResult == null)
750                 {
751                         SysTryReturn(NID_SCL, GetLastResult() == E_OBJ_NOT_FOUND, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
752
753                         ClearLastResult();
754
755                         return pDataList.release();
756                 }
757
758                 while (pSearchResult->MoveNext() == E_SUCCESS)
759                 {
760                         pData.reset(__View::template ConvertResultTo<Data>(*pSearchResult));
761                         if (pData == null)
762                         {
763                                 SysTryReturn(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
764                                 SysTryReturn(NID_SCL, GetLastResult() != E_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
765
766                                 ClearLastResult();
767                                 continue;
768                         }
769
770                         r = pDataList->Add(*pData);
771                         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
772
773                         pData.release();
774                 }
775
776                 return pDataList.release();
777         }
778
779         template<typename __View>
780         static int GetMatchedItemCountWithFilter(contacts_filter_h filterHandle)
781         {
782                 int count = 0;
783
784                 if (filterHandle != null)
785                 {
786                         __Filter<__View> filter;
787                         filter.Construct(filterHandle);
788
789                         __Query<__View> query;
790                         query.Construct();
791                         query.SetFilter(filter);
792
793                         count = _AddressbookUtil::GetCountWithQuery(query);
794                 }
795                 else
796                 {       
797                         __Query<__View> query;
798                         query.Construct();
799
800                         count = _AddressbookUtil::GetCountWithQuery(query);
801                 }
802
803                 return count;
804         }
805
806         static ContactRecord* CreateContactRecordN(const char* pUri)
807         {
808                 ClearLastResult();
809
810                 contacts_record_h recordHandle = null;
811
812                 int ret = contacts_record_create(pUri, &recordHandle);
813                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
814
815                 return recordHandle;
816         }
817
818         static result InsertContactRecordN(contacts_record_h recordHandle, int& recordId)
819         {
820                 int ret = contacts_db_insert_record(recordHandle, &recordId);
821                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
822                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient. (%d)", GetErrorMessage(E_STORAGE_FULL), ret);
823                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
824
825                 return E_SUCCESS;
826         }
827
828         static result DeleteContactRecord(const char* pUri, int recordId)
829         {
830                 int ret = contacts_db_delete_record(pUri, recordId);
831                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
832                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The record is not found. (%d)", GetErrorMessage(E_OBJ_NOT_FOUND), ret);
833                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
834
835                 return E_SUCCESS;
836         }
837
838         static result UpdateContactRecord(contacts_record_h recordHandle)
839         {
840                 int ret = contacts_db_update_record(recordHandle);
841                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
842                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The Record is not found. (%d)", GetErrorMessage(E_OBJ_NOT_FOUND), ret);
843                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient. (%d)", GetErrorMessage(E_STORAGE_FULL), ret);
844                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. Failed to update a contact", GetErrorMessage(E_SYSTEM));
845
846                 return E_SUCCESS;
847         }
848
849         static result UpdateContactRecords(contacts_list_h listHandle)
850         {
851                 int ret = contacts_db_update_records(listHandle);
852                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
853                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
854                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
855                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
856
857                 return E_SUCCESS;
858         }
859
860         static ContactRecord* GetContactRecordN(const char* pUri, RecordId recordId)
861         {
862                 ClearLastResult();
863
864                 contacts_record_h recordHandle = null;
865
866                 int ret = contacts_db_get_record(pUri, recordId, &recordHandle);
867                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
868                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The record is not found. (%d)", GetErrorMessage(E_OBJ_NOT_FOUND), ret);
869                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
870
871                 return recordHandle;
872         }
873
874         static Tizen::Base::Collection::IListT<AddressbookId>* GetRwAbIdListN(void);
875         static Tizen::Base::Collection::IListT<AddressbookId>* GetRoAbIdListN(void);
876
877         static result GetAbListN(Tizen::Base::Collection::IListT<AddressbookId>& rwAbIdList, Tizen::Base::Collection::IListT<AddressbookId>& roAbIdList);
878
879         template<typename __View>
880         static __Filter<__View>* GetRwAbFilterN(void)
881         {
882                 std::unique_ptr< __Filter<__View> > pFilter(new (std::nothrow) __Filter<__View>);
883                 SysTryReturn(NID_SCL, pFilter != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
884
885                 Tizen::Base::Collection::ArrayListT<AddressbookId> rwAbIdList;
886                 rwAbIdList.Construct();
887
888                 Tizen::Base::Collection::ArrayListT<AddressbookId> roAbIdList;
889                 roAbIdList.Construct();
890
891                 GetAbListN(rwAbIdList, roAbIdList);
892
893                 if (roAbIdList.GetCount() == 0)
894                 {
895                         // return null filter
896                         return pFilter.release();
897                 }       
898
899                 pFilter->Construct();
900
901                 bool isFirst = true;
902                 AddressbookId addressbookId = -1;
903
904                 if (rwAbIdList.GetCount() < 5)
905                 {
906                         std::unique_ptr< Tizen::Base::Collection::IEnumeratorT<AddressbookId> > pEnum(rwAbIdList.GetEnumeratorN());
907                         while (pEnum->MoveNext() == E_SUCCESS)
908                         {
909                                 pEnum->GetCurrent(addressbookId);
910                                 if (!isFirst)
911                                 {
912                                         pFilter->AddOperator(CONTACTS_FILTER_OPERATOR_OR);
913                                 }
914                                 else
915                                 {
916                                         isFirst = false;
917                                 }
918
919                                 pFilter->AddInt(__View::GetAbPropertyId(), CONTACTS_MATCH_EQUAL, addressbookId);
920                         }
921                 }
922                 else
923                 {
924                         std::unique_ptr< Tizen::Base::Collection::IEnumeratorT<AddressbookId> > pEnum(roAbIdList.GetEnumeratorN());
925                         while (pEnum->MoveNext() == E_SUCCESS)
926                         {
927                                 pEnum->GetCurrent(addressbookId);
928                                 if (!isFirst)
929                                 {
930                                         pFilter->AddOperator(CONTACTS_FILTER_OPERATOR_AND);
931                                 }
932                                 else
933                                 {
934                                         isFirst = false;
935                                 }
936
937                                 pFilter->AddInt(__View::GetAbPropertyId(), CONTACTS_MATCH_NOT_EQUAL, addressbookId);
938                         }
939                 }
940
941                 return pFilter.release();
942         }
943 };
944
945 }} // Tizen::Social
946
947 #endif //_FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_