summaryrefslogtreecommitdiff
path: root/src/FScl_AddressbookUtil.h
blob: 8f1bff383bbf2e927c7a3454e8f43d3a25984de7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
//
// Open Service Platform
// Copyright (c) 2012 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/**
 * @file		FScl_AddressbookUtil.h
 * @brief		This is the header file for the _AddressbookUtil class.
 *
 * This header file contains the declarations of the _AddressbookUtil class.
 */
#ifndef _FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_
#define _FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_

#include <unique_ptr.h>
#include <contacts.h>
#include <FBaseColArrayList.h>
#include <FBaseColAllElementsDeleter.h>
#include "FScl_ContactDbConnector.h"

namespace Tizen { namespace Social
{

class Person;
class Contact;
class UserProfile;

class __ContactsRecordHandle
{
public:
	__ContactsRecordHandle(contacts_record_h handle)
	:__handle(handle)
	{
	}

	~__ContactsRecordHandle(void)
	{
		if (__handle != null)
		{
			contacts_record_destroy(__handle, true);
		}
	}

	void Reset(contacts_record_h handle)
	{
		if (__handle != null)
		{
			
			contacts_record_destroy(__handle, true);
		}

		__handle = handle;
	}

	contacts_record_h Release(void)
	{
		contacts_record_h handle = __handle;
		__handle = null;

		return handle;
	}

private:
	contacts_record_h __handle;
};

template<typename __View>
class __SearchResult
{
public:
	__SearchResult(contacts_list_h list)
		:__list(list), __isFirst(true)
	{

	}

	int GetCount(void) const
	{
		if (__list != null)
		{
			unsigned int count = 0;
			contacts_list_get_count(__list, &count);

			return count;
		}

		return -1;
	}

	contacts_record_h GetCurrentRecord(void) const
	{
		if (__list != null)
		{
			contacts_record_h record = null;
			int ret = contacts_list_get_current_record_p(__list, &record);
			if (ret != CONTACTS_ERROR_NONE)
			{
				return null;
			}

			return record;
		}

		return null;
	}

	result MoveNext(void)
	{
		if (__list != null)
		{
			int ret = CONTACTS_ERROR_NONE;

			if (!__isFirst)
			{
				ret = contacts_list_next(__list);
				if (ret != CONTACTS_ERROR_NONE)
				{
					return E_INVALID_OPERATION;
				}

				return E_SUCCESS;
			}
			else
			{
				if (GetCount() > 0)
				{
					__isFirst = false;

					return E_SUCCESS;
				}

				return E_INVALID_OPERATION;
			}
		}

		return E_INVALID_OPERATION;
	}


	~__SearchResult(void)
	{
		if (__list != null)
		{
			contacts_list_destroy(__list, true);
		}
	}

private:
	contacts_list_h __list;
	bool __isFirst;
};

template<typename __View>
class __Filter
{
public:
	__Filter(void)
	:__filterHandle(null)
	,__destroyFilterHandle(false)
	{
	}

	result Construct(void)
	{
		contacts_filter_h filterHandle = null;
		int ret = contacts_filter_create(__View::GetUri(), &filterHandle);
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		__filterHandle = filterHandle;
		__destroyFilterHandle = true;

		return E_SUCCESS;
	}

	result Construct(contacts_filter_h filterHandle)
	{
		__filterHandle = filterHandle;
		__destroyFilterHandle = false;

		return true;
	}

	contacts_filter_h Get(void) const
	{
		return __filterHandle;
	}

	result AddString(unsigned int propertyId, contacts_match_str_flag_e match, const char* value)
	{
		int ret = contacts_filter_add_str(__filterHandle, propertyId, match, value);
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return E_SUCCESS;
	}

	result AddInt(unsigned int propertyId, contacts_match_int_flag_e match, int value)
	{
		int ret = contacts_filter_add_int(__filterHandle, propertyId, match, value);
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return E_SUCCESS;
	}

	result AddLongLong(unsigned int propertyId, contacts_match_int_flag_e match, long long int value)
	{
		int ret = contacts_filter_add_lli(__filterHandle, propertyId, match, value);
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return E_SUCCESS;
	}

	result AddDouble(unsigned int propertyId, contacts_match_int_flag_e match, double value)
	{
		int ret = contacts_filter_add_double(__filterHandle, propertyId, match, value);
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return E_SUCCESS;
	}

	result AddBool(unsigned int propertyId, bool value)
	{
		int ret = contacts_filter_add_bool(__filterHandle, propertyId, value);
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return E_SUCCESS;
	}

	result AddOperator(contacts_filter_operator_e type)
	{
		int ret = contacts_filter_add_operator(__filterHandle, type);
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return E_SUCCESS;
	}

	result AddFilter(__Filter<__View>& filter)
	{
		contacts_filter_h handle = filter.Get();
		int ret = contacts_filter_add_filter(__filterHandle, handle);
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return E_SUCCESS;
	}

	~__Filter(void)
	{
		if (__filterHandle != null && __destroyFilterHandle)
		{
			contacts_filter_destroy(__filterHandle);
		}
	}

private:
	contacts_filter_h __filterHandle;
	bool __destroyFilterHandle;
};

template<typename __View>
class __Query
{
public:
	__Query(void)
	:__queryHandle(null)
	{
	}

	result Construct(void)
	{
		contacts_query_h queryHandle = null;
		int ret = contacts_query_create(__View::GetUri(), &queryHandle);
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		__queryHandle = queryHandle;

		return E_SUCCESS;
	}

	contacts_query_h Get(void) const
	{
		return __queryHandle;
	}


	result SetProjection(unsigned int propertyIds[], int count)
	{
		int ret = contacts_query_set_projection(__queryHandle, propertyIds, count);
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return E_SUCCESS;
	}

	result SetDistinct(bool set)
	{
		int ret = contacts_query_set_distinct(__queryHandle, set);
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return E_SUCCESS;
	}

	result SetFilter(__Filter<__View>& filter)
	{
		contacts_filter_h filterHandle = filter.Get();

		if (filterHandle != null)
		{
			int ret = contacts_query_set_filter(__queryHandle, filterHandle);
			SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
		}

		return E_SUCCESS;
	}

	result SetSort(unsigned int propertyId, bool ascending)
	{
		int ret = contacts_query_set_sort(__queryHandle, propertyId, ascending);
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return E_SUCCESS;
	}

	~__Query(void)
	{
		if (__queryHandle != null)
		{
			contacts_query_destroy(__queryHandle);
		}
	}

private:
	contacts_query_h __queryHandle;

};

class __ContactsAddressbook
{
public:
	static const char* GetUri(void)
	{
		return _contacts_address_book._uri;
	}

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsAddressbook>& searchResult);

};

class __ContactsPerson
{
public:
	static const char* GetUri(void)
	{
		return _contacts_person._uri;
	}

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsPerson>& searchResult);

	template<typename Type>
	static Type* ConvertHandleTo(contacts_record_h recordHandle);
};


class __ContactsPersonEmail
{
public:
	static const char* GetUri(void)
	{
		return _contacts_person_email._uri;
	}
};

class __ContactsPersonGroupRel
{
public:
	static const char* GetUri(void)
	{
		return _contacts_person_grouprel._uri;
	}

	template<typename Type>
	static Type* ConvertHandleTo(contacts_record_h recordHandle);

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsPersonGroupRel>& searchResult);

};

class __ContactsPersonNumber
{
public:
	static const char* GetUri(void)
	{
		return _contacts_person_number._uri;
	}
};


class __ContactsGroup
{
public:
	static const char* GetUri(void)
	{
		return _contacts_group._uri;
	}

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsGroup>& searchResult);
};

class __ContactsGroupRelation
{
public:
	static const char* GetUri(void)
	{
		return _contacts_group_relation._uri;
	}

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsGroupRelation>& searchResult);
};

class __ContactsContact
{
public:
	static const char* GetUri(void)
	{
		return _contacts_contact._uri;
	}

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsContact>& searchResult);
};

class __ContactsContactGroupRel
{
public:
	static const char* GetUri(void)
	{
		return _contacts_contact_grouprel._uri;
	}

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsContactGroupRel>& searchResult);
};

class __ContactsContactEmail
{
public:
	static const char* GetUri(void)
	{
		return _contacts_contact_email._uri;
	}

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsContactEmail>& searchResult);
};

class __ContactsContactNumber
{
public:
	static const char* GetUri(void)
	{
		return _contacts_contact_number._uri;
	}

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsContactNumber>& searchResult);
};

class __ContactsContactUpdatedInfo
{
public:
	static const char* GetUri(void)
	{
		return _contacts_contact_updated_info._uri;
	}

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsContactUpdatedInfo>& searchResult);
};

class __ContactsMyProfileUpdatedInfo
{
public:
	static const char* GetUri(void)
	{
		return _contacts_my_profile_updated_info._uri;
	}
};

class __ContactsGroupUpdatedInfo
{
public:
	static const char* GetUri(void)
	{
		return _contacts_group_updated_info._uri;
	}

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsGroupUpdatedInfo>& searchResult);
};

class __ContactsGroupRelUpdatedInfo
{
public:
	static const char* GetUri(void)
	{
		return _contacts_grouprel_updated_info._uri;
	}

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsGroupRelUpdatedInfo>& searchResult);
};

class __ContactsUserProfile
{
public:
	static const char* GetUri(void)
	{
		return _contacts_my_profile._uri;
	}

	template<typename Type>
	static Type* ConvertResultTo(__SearchResult<__ContactsUserProfile>& searchResult);
};


class _AddressbookUtil
{
public:
	template<typename __View>
	static __SearchResult<__View>* ExecuteQuery(const __Query<__View>& query, int offset = 0, int limit = 0)
	{
		SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
		__SearchResult<__View>* pSearchResult = null;

		contacts_list_h list = null;
		int ret = contacts_db_get_records_with_query(query.Get(), offset, limit, &list);
		SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
		SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] No matched item found.", GetErrorMessage(E_OBJ_NOT_FOUND));
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));

		pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
		SysTryReturn(NID_SCL, pSearchResult != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return pSearchResult;
	}

	template<typename __View>
	static int GetCountWithQuery(const __Query<__View>& query)
	{
		SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));

		int count = 0;
		int ret = contacts_db_get_count_with_query(query.Get(), &count);
		SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));

		return count;
	}

	template<typename __View>
	static int GetCount(void)
	{
		SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));

		int count = 0;
		int ret = contacts_db_get_count(__View::GetUri(), &count);
		SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));

		return count;
	}

	template<typename __View>
	static __SearchResult<__View>* GetAllRecords(int offset = 0, int limit = 0)
	{
		SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));

		__SearchResult<__View>* pSearchResult = null;

		contacts_list_h list = null;
		int ret = contacts_db_get_all_records(__View::GetUri(), offset, limit, &list);
		SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));

		pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
		SysTryReturn(NID_SCL, pSearchResult, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return pSearchResult;
	}

	template<typename __View>
	static __SearchResult<__View>* GetChangesByVersion(int id, int current, int& latest)
	{
		SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));

		__SearchResult<__View>* pSearchResult = null;

		contacts_list_h list = null;
		int ret = contacts_db_get_changes_by_version(__View::GetUri(), id, current, &list, &latest);
		SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));

		pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
		SysTryReturn(NID_SCL, pSearchResult, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		return pSearchResult;
	}

	template<typename __View>
	static int GetChangedItemCountByVersion(int id, int current, int& latest)
	{
		SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));

		unsigned int count = 0;
		contacts_list_h list = null;
		int ret = contacts_db_get_changes_by_version(__View::GetUri(), id, current, &list, &latest);
		SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
		SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));

		contacts_list_get_count(list, &count);
		contacts_list_destroy(list, true);

		return count;
	}

	template<typename __View>
	static __SearchResult<__View>* Search(const Tizen::Base::String& keyword);

	static Person* CreatePersonN(void);

	static result ConvertUserProfileToContact(const UserProfile& userProfile, Contact& contact);

	template<typename __View, typename ChangeInfo>
	static Tizen::Base::Collection::IList* SearchWithVersionN(int addressbookId, int currentVersion, int& latestVersion)
	{
		SysTryReturn(NID_SCL, currentVersion >= 0, null, E_INVALID_ARG, "[%s] currentVersion %d must be greater that or equal 0.", GetErrorMessage(GetLastResult()), currentVersion);

		std::unique_ptr<ChangeInfo> pChangeInfo(null);

		ClearLastResult();

		std::unique_ptr<Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter> pList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
		SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		result r = pList->Construct();
		SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		std::unique_ptr<__SearchResult<__View> > pSearchResult(_AddressbookUtil::template GetChangesByVersion<__View>(addressbookId, currentVersion, latestVersion));
		SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));

		while (pSearchResult->MoveNext() == E_SUCCESS)
		{
			pChangeInfo.reset(__View::template ConvertResultTo<ChangeInfo>(*pSearchResult));
			if (pChangeInfo == null)
			{
				SysTryReturn(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
				SysTryReturn(NID_SCL, GetLastResult() != E_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));

				ClearLastResult();
				continue;
			}

			r = pList->Add(*pChangeInfo);
			SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));

			pChangeInfo.release();
		}

		return pList.release();

	}

	template<typename __View, typename Data>
	static Tizen::Base::Collection::IList* SearchWithQueryN(__Query<__View>& query, int offset = 0, int limit = 0)
	{
		std::unique_ptr<Data> pData(null);

		std::unique_ptr<Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter> pDataList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
		SysTryReturn(NID_SCL, pDataList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		result r = pDataList->Construct();
		SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

		std::unique_ptr<__SearchResult<__View> > pSearchResult(_AddressbookUtil::ExecuteQuery(query, offset, limit));
		if (pSearchResult == null)
		{
			SysTryReturn(NID_SCL, GetLastResult() == E_OBJ_NOT_FOUND, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));

			ClearLastResult();

			return pDataList.release();
		}

		while (pSearchResult->MoveNext() == E_SUCCESS)
		{
			pData.reset(__View::template ConvertResultTo<Data>(*pSearchResult));
			if (pData == null)
			{
				SysTryReturn(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
				SysTryReturn(NID_SCL, GetLastResult() != E_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));

				ClearLastResult();
				continue;
			}

			r = pDataList->Add(*pData);
			SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));

			pData.release();
		}

		return pDataList.release();
	}

	template<typename __View>
	static int GetMatchedItemCountWithFilter(contacts_filter_h filterHandle)
	{
		__Filter<__View> filter;
		filter.Construct(filterHandle);

		__Query<__View> query;
		query.Construct();
		query.SetFilter(filter);

		int count = _AddressbookUtil::GetCountWithQuery(query);

		return count;
	}
};

}} // Tizen::Social

#endif //_FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_