2409363403a32e07bd50186656c3d098993f7d4e
[framework/osp/social.git] / src / FScl_CalendarbookFilterImpl.cpp
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_CalendarbookFilterImpl.cpp
19 * @brief        This is the implementation for _CalendarbookFilterImpl class.
20 *
21 * This file contains definitions of @e _CalendarbookFilterImpl class.
22 */
23
24 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseDateTime.h>
27 #include <FBase_StringConverter.h>
28 #include <FSclCalendarbookFilter.h>
29 #include "FScl_CalendarbookFilterImpl.h"
30 #include "FScl_CalendarbookUtil.h"
31
32 using namespace Tizen::Base;
33
34 namespace Tizen { namespace Social
35 {
36
37 static const long long _CALENDAR_TODO_NO_START_DATE = -0x7fffffffffffffffLL;
38 static const long long _CALENDAR_TODO_NO_DUE_DATE = 0x7fffffffffffffffLL;
39
40 // CB_FI_TYPE_CALENDAR
41 // The entries must be ordered same as CalendarFilterProperty
42 const _CalendarbookFilterImpl::__FilterPropertyInfoEntry _CalendarbookFilterImpl::__calendarFilterPropertyInfos[] =
43 {
44         {CALENDAR_FI_PR_CALENDAR_ID, _calendar_book.id, DATA_TYPE_INT},
45         {CALENDAR_FI_PR_ACCOUNT_ID, _calendar_book.account_id, DATA_TYPE_INT},
46         {CALENDAR_FI_PR_NAME, _calendar_book.name, DATA_TYPE_STRING},
47         {CALENDAR_FI_PR_ITEM_TYPE, _calendar_book.store_type, DATA_TYPE_INT},
48 };
49
50 // CB_FI_TYPE_EVENT
51 // The entries must be ordered same as EventFilterProperty
52 const _CalendarbookFilterImpl::__FilterPropertyInfoEntry _CalendarbookFilterImpl::__eventFilterPropertyInfos[] =
53 {
54         {EVENT_FI_PR_EVENT_ID, _calendar_event.id, DATA_TYPE_INT},
55         {EVENT_FI_PR_CALENDAR_ID, _calendar_event.calendar_book_id, DATA_TYPE_INT},
56         {EVENT_FI_PR_SUBJECT, _calendar_event.summary, DATA_TYPE_STRING},
57         {EVENT_FI_PR_DESCRIPTION, _calendar_event.description, DATA_TYPE_STRING},
58         {EVENT_FI_PR_LOCATION, _calendar_event.location, DATA_TYPE_STRING},
59         {EVENT_FI_PR_BUSY_STATUS, _calendar_event.busy_status, DATA_TYPE_INT},
60         {EVENT_FI_PR_PRIORITY, _calendar_event.priority, DATA_TYPE_INT},
61         {EVENT_FI_PR_SENSITIVITY, _calendar_event.sensitivity, DATA_TYPE_INT},
62         {EVENT_FI_PR_STATUS, _calendar_event.event_status, DATA_TYPE_INT},
63         {EVENT_FI_PR_UID, _calendar_event.uid, DATA_TYPE_STRING},
64         {EVENT_FI_PR_LATITUDE, _calendar_event.latitude, DATA_TYPE_DOUBLE},
65         {EVENT_FI_PR_LONGITUDE, _calendar_event.longitude, DATA_TYPE_DOUBLE},
66         {EVENT_FI_PR_LAST_REVISED_TIME, _calendar_event.last_modified_time, DATA_TYPE_DATE_TIME},
67         {EVENT_FI_PR_HAS_REMINDER, _calendar_event.has_alarm, DATA_TYPE_BOOL},
68         {EVENT_FI_PR_HAS_ATTENDEE, _calendar_event.has_attendee, DATA_TYPE_BOOL},
69 };
70
71 // CB_FI_TYPE_TODO
72 // The entries must be ordered same as TodoFilterProperty
73 const _CalendarbookFilterImpl::__FilterPropertyInfoEntry _CalendarbookFilterImpl::__todoFilterPropertyInfos[] =
74 {
75                 {TODO_FI_PR_TODO_ID, _calendar_todo.id, DATA_TYPE_INT},
76                 {TODO_FI_PR_CALENDAR_ID, _calendar_todo.calendar_book_id, DATA_TYPE_INT},
77                 {TODO_FI_PR_START_DATE, _calendar_todo.start_time, DATA_TYPE_DATE_TIME},
78                 {TODO_FI_PR_DUE_DATE, _calendar_todo.due_time, DATA_TYPE_DATE_TIME},
79                 {TODO_FI_PR_SUBJECT, _calendar_todo.summary, DATA_TYPE_STRING},
80                 {TODO_FI_PR_DESCRIPTION, _calendar_todo.description, DATA_TYPE_STRING},
81                 {TODO_FI_PR_LOCATION, _calendar_todo.location, DATA_TYPE_STRING},
82                 {TODO_FI_PR_PRIORITY, _calendar_todo.priority, DATA_TYPE_INT},
83                 {TODO_FI_PR_SENSITIVITY, _calendar_todo.sensitivity, DATA_TYPE_INT},
84                 {TODO_FI_PR_STATUS, _calendar_todo.todo_status, DATA_TYPE_INT},
85                 {TODO_FI_PR_LATITUDE, _calendar_todo.latitude, DATA_TYPE_DOUBLE},
86                 {TODO_FI_PR_LONGITUDE, _calendar_todo.longitude, DATA_TYPE_DOUBLE},
87                 {TODO_FI_PR_LAST_REVISED_TIME, _calendar_todo.last_modified_time, DATA_TYPE_DATE_TIME},
88                 {TODO_FI_PR_HAS_REMINDER, _calendar_todo.has_alarm, DATA_TYPE_BOOL},
89 };
90
91 // CB_FI_TYPE_ALL_DAY_EVENT_INSTANCE
92 // The entries must be ordered same as EventInstanceFilterProperty
93 const _CalendarbookFilterImpl::__FilterPropertyInfoEntry _CalendarbookFilterImpl::__allDayEventInstanceFilterPropertyInfos[] =
94 {
95                 {EVENT_INST_FI_PR_ORIGINAL_EVENT_ID, _calendar_instance_allday_calendar_book.event_id, DATA_TYPE_INT},
96                 {EVENT_INST_FI_PR_CALENDAR_ID, _calendar_instance_allday_calendar_book.calendar_book_id, DATA_TYPE_INT},
97                 {EVENT_INST_FI_PR_START_TIME, _calendar_instance_allday_calendar_book.start_time, DATA_TYPE_DATE_TIME},
98                 {EVENT_INST_FI_PR_END_TIME, _calendar_instance_allday_calendar_book.end_time, DATA_TYPE_DATE_TIME},
99                 {EVENT_INST_FI_PR_SUBJECT, _calendar_instance_allday_calendar_book.summary, DATA_TYPE_STRING},
100                 {EVENT_INST_FI_PR_DESCRIPTION, _calendar_instance_allday_calendar_book.description, DATA_TYPE_STRING},
101                 {EVENT_INST_FI_PR_LOCATION, _calendar_instance_allday_calendar_book.location, DATA_TYPE_STRING},
102                 {EVENT_INST_FI_PR_BUSY_STATUS, _calendar_instance_allday_calendar_book.busy_status, DATA_TYPE_INT},
103                 {EVENT_INST_FI_PR_PRIORITY, _calendar_instance_allday_calendar_book.priority, DATA_TYPE_INT},
104                 {EVENT_INST_FI_PR_SENSITIVITY, _calendar_instance_allday_calendar_book.sensitivity, DATA_TYPE_INT},
105                 {EVENT_INST_FI_PR_STATUS, _calendar_instance_allday_calendar_book.event_status, DATA_TYPE_INT},
106                 {EVENT_INST_FI_PR_LATITUDE, _calendar_instance_allday_calendar_book.latitude, DATA_TYPE_DOUBLE},
107                 {EVENT_INST_FI_PR_LONGITUDE, _calendar_instance_allday_calendar_book.longitude, DATA_TYPE_DOUBLE},
108                 {EVENT_INST_FI_PR_IS_RECURRING, _calendar_instance_allday_calendar_book.has_rrule, DATA_TYPE_BOOL},
109                 {EVENT_INST_FI_PR_HAS_REMINDER, _calendar_instance_allday_calendar_book.has_alarm, DATA_TYPE_BOOL},
110 };
111
112 // CB_FI_TYPE_NON_ALL_DAY_EVENT_INSTANCE
113 // The entries must be ordered same as EventInstanceFilterProperty
114 const _CalendarbookFilterImpl::__FilterPropertyInfoEntry _CalendarbookFilterImpl::__nonAllDayEventInstanceFilterPropertyInfos[] =
115 {
116                 {EVENT_INST_FI_PR_ORIGINAL_EVENT_ID, _calendar_instance_normal_calendar_book.event_id, DATA_TYPE_INT},
117                 {EVENT_INST_FI_PR_CALENDAR_ID, _calendar_instance_normal_calendar_book.calendar_book_id, DATA_TYPE_INT},
118                 {EVENT_INST_FI_PR_START_TIME, _calendar_instance_normal_calendar_book.start_time, DATA_TYPE_DATE_TIME},
119                 {EVENT_INST_FI_PR_END_TIME, _calendar_instance_normal_calendar_book.end_time, DATA_TYPE_DATE_TIME},
120                 {EVENT_INST_FI_PR_SUBJECT, _calendar_instance_normal_calendar_book.summary, DATA_TYPE_STRING},
121                 {EVENT_INST_FI_PR_DESCRIPTION, _calendar_instance_normal_calendar_book.description, DATA_TYPE_STRING},
122                 {EVENT_INST_FI_PR_LOCATION, _calendar_instance_normal_calendar_book.location, DATA_TYPE_STRING},
123                 {EVENT_INST_FI_PR_BUSY_STATUS, _calendar_instance_normal_calendar_book.busy_status, DATA_TYPE_INT},
124                 {EVENT_INST_FI_PR_PRIORITY, _calendar_instance_normal_calendar_book.priority, DATA_TYPE_INT},
125                 {EVENT_INST_FI_PR_SENSITIVITY, _calendar_instance_normal_calendar_book.sensitivity, DATA_TYPE_INT},
126                 {EVENT_INST_FI_PR_STATUS, _calendar_instance_normal_calendar_book.event_status, DATA_TYPE_INT},
127                 {EVENT_INST_FI_PR_LATITUDE, _calendar_instance_normal_calendar_book.latitude, DATA_TYPE_DOUBLE},
128                 {EVENT_INST_FI_PR_LONGITUDE, _calendar_instance_normal_calendar_book.longitude, DATA_TYPE_DOUBLE},
129                 {EVENT_INST_FI_PR_IS_RECURRING, _calendar_instance_normal_calendar_book.has_rrule, DATA_TYPE_BOOL},
130                 {EVENT_INST_FI_PR_HAS_REMINDER, _calendar_instance_normal_calendar_book.has_alarm, DATA_TYPE_BOOL},
131 };
132
133 const _CalendarbookFilterImpl::__FilterInfoEntry _CalendarbookFilterImpl::__filterInfos[] =
134 {
135         {CB_FI_TYPE_EVENT, _calendar_event._uri, EVENT_FI_PR_EVENT_ID, sizeof(__eventFilterPropertyInfos)/sizeof(_CalendarbookFilterImpl::__FilterPropertyInfoEntry), __eventFilterPropertyInfos},
136         {CB_FI_TYPE_TODO, _calendar_todo._uri, TODO_FI_PR_TODO_ID, sizeof(__todoFilterPropertyInfos)/sizeof(_CalendarbookFilterImpl::__FilterPropertyInfoEntry), __todoFilterPropertyInfos},
137         {CB_FI_TYPE_CALENDAR, _calendar_book._uri, CALENDAR_FI_PR_CALENDAR_ID, sizeof(__calendarFilterPropertyInfos)/sizeof(_CalendarbookFilterImpl::__FilterPropertyInfoEntry), __calendarFilterPropertyInfos},
138         {CB_FI_TYPE_ALL_DAY_EVENT_INSTANCE, _calendar_instance_allday_calendar_book._uri, EVENT_INST_FI_PR_ORIGINAL_EVENT_ID, sizeof(__allDayEventInstanceFilterPropertyInfos)/sizeof(_CalendarbookFilterImpl::__FilterPropertyInfoEntry), __allDayEventInstanceFilterPropertyInfos},
139         {CB_FI_TYPE_NON_ALL_DAY_EVENT_INSTANCE, _calendar_instance_normal_calendar_book._uri, EVENT_INST_FI_PR_ORIGINAL_EVENT_ID, sizeof(__nonAllDayEventInstanceFilterPropertyInfos)/sizeof(_CalendarbookFilterImpl::__FilterPropertyInfoEntry), __nonAllDayEventInstanceFilterPropertyInfos},
140 };
141
142 bool
143 _CalendarbookFilterImpl::IsValidProperty(CalendarbookFilterType filterType, unsigned long property)
144 {
145         int filterInfoCount = sizeof(__filterInfos)/sizeof(__FilterInfoEntry);
146
147         if (filterType < 0 || filterType > (filterInfoCount - 1))
148         {
149                 return false;
150         }
151
152         unsigned long baseIndex = __filterInfos[filterType].baseIndex;
153         int propertyInfoCount = __filterInfos[filterType].propertyInfoCount;
154
155         if (property < baseIndex || property >= (baseIndex + propertyInfoCount))
156         {
157                 return false;
158         }
159
160         return true;
161 }
162
163 // IsValidProperty must be called before calling this. If IsValidProperty returns false
164 // this should not be called.
165 const char*
166 _CalendarbookFilterImpl::GetUriFromType(CalendarbookFilterType filterType)
167 {
168         return __filterInfos[filterType].pViewUri;
169 }
170
171 // IsValidProperty must be called before calling this. If IsValidProperty returns false
172 // this should not be called.
173 unsigned int
174 _CalendarbookFilterImpl::GetViewPropertyId(CalendarbookFilterType filterType, unsigned long property)
175 {
176         const __FilterPropertyInfoEntry* propertyInfos = __filterInfos[filterType].propertyInfos;
177         unsigned long baseIndex = __filterInfos[filterType].baseIndex;
178
179         return propertyInfos[property - baseIndex].viewPropertyId;
180 }
181
182 // IsValidProperty must be called before calling this. If IsValidProperty returns__nonAllDayEventInstanceFilterPropertyInfos false
183 // this should not be called.
184 __DataType
185 _CalendarbookFilterImpl::GetPropertyType(CalendarbookFilterType filterType, unsigned long property)
186 {
187         const __FilterPropertyInfoEntry* propertyInfos = __filterInfos[filterType].propertyInfos;
188         unsigned long baseIndex = __filterInfos[filterType].baseIndex;
189
190         return propertyInfos[property - baseIndex].dataType;
191 }
192
193 _CalendarbookFilterImpl::_CalendarbookFilterImpl(CalendarbookFilterType type)
194         : __filterHandle(null)
195         , __filterType(type)
196         , __isEmpty(true)
197 {
198         calendar_filter_h filterHandle = null;
199         const char* pUri = GetUriFromType(type);
200
201         int ret = calendar_filter_create(pUri, &filterHandle);
202         SysTryReturnVoidResult(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(GetLastResult()));
203
204         __filterHandle = filterHandle;
205 }
206
207 _CalendarbookFilterImpl::~_CalendarbookFilterImpl(void)
208 {
209         if (__filterHandle != null)
210         {
211                 calendar_filter_destroy(__filterHandle);
212         }
213 }
214
215 bool
216 _CalendarbookFilterImpl::Equals(const Object& rhs) const
217 {
218         const _CalendarbookFilterImpl* pCalendarbookFilter = dynamic_cast<const _CalendarbookFilterImpl*>(&rhs);
219         if (this == pCalendarbookFilter)
220         {
221                 return true;
222         }
223
224         return false;
225 }
226
227 int
228 _CalendarbookFilterImpl::GetHashCode(void) const
229 {
230         return (int)__filterHandle;
231 }
232
233 calendar_match_int_flag_e
234 _CalendarbookFilterImpl::GetMatchIntFlag(FilterComparisonOperator comparisonOperator)
235 {
236         calendar_match_int_flag_e match = CALENDAR_MATCH_NONE;
237         switch(comparisonOperator)
238         {
239         case FI_CMP_OP_EQUAL:
240                 match = CALENDAR_MATCH_EQUAL;
241                 break;
242         case FI_CMP_OP_LESS_THAN:
243                 match = CALENDAR_MATCH_LESS_THAN;
244                 break;
245         case FI_CMP_OP_LESS_THAN_OR_EQUAL:
246                 match = CALENDAR_MATCH_LESS_THAN_OR_EQUAL;
247                 break;
248         case FI_CMP_OP_GREATER_THAN:
249                 match = CALENDAR_MATCH_GREATER_THAN;
250                 break;
251         case FI_CMP_OP_GREATER_THAN_OR_EQUAL:
252                 match = CALENDAR_MATCH_GREATER_THAN_OR_EQUAL;
253                 break;
254         case FI_CMP_OP_IS_NULL:
255                 match = CALENDAR_MATCH_NONE;
256                 break;
257         default:
258                 match = CALENDAR_MATCH_NONE;
259                 break;
260         };
261
262         return match;
263 }
264
265 calendar_match_str_flag_e
266 _CalendarbookFilterImpl::GetMatchStrFlag(FilterStringOperator stringOperstor)
267 {
268         calendar_match_str_flag_e match = CALENDAR_MATCH_EXISTS;
269         switch(stringOperstor)
270         {
271         case FI_STR_OP_EQUAL:
272                 match = CALENDAR_MATCH_EXACTLY;
273                 break;
274         case FI_STR_OP_FULL_STRING:
275                 match = CALENDAR_MATCH_FULLSTRING;
276                 break;
277         case FI_STR_OP_START_WITH:
278                 match = CALENDAR_MATCH_STARTSWITH;
279                 break;
280         case FI_STR_OP_END_WITH:
281                 match = CALENDAR_MATCH_ENDSWITH;
282                 break;
283         case FI_STR_OP_CONTAIN:
284                 match = CALENDAR_MATCH_CONTAINS;
285                 break;
286         case FI_STR_OP_IS_NOT_NULL:
287                 match = CALENDAR_MATCH_EXISTS;
288                 break;
289         default:
290                 match = CALENDAR_MATCH_EXISTS;
291                 break;
292         };
293
294         return match;
295 }
296
297 result
298 _CalendarbookFilterImpl::AppendInt(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterComparisonOperator comparisonOperator, int value)
299 {
300         SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. filterProperty = %ld", GetErrorMessage(E_INVALID_ARG), filterProperty);
301         SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_INT, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be int.", GetErrorMessage(E_INVALID_ARG), filterProperty);
302         SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
303                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
304                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
305         SysTryReturn(NID_SCL, !(!__isEmpty && (conjunctiveOperator == FI_CONJ_OP_NONE)),\
306                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
307                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
308
309         unsigned int id = _CalendarbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
310         calendar_match_int_flag_e match = GetMatchIntFlag(comparisonOperator);
311
312         int convertedValue = 0;
313         switch (filterProperty)
314         {
315         case EVENT_FI_PR_BUSY_STATUS:
316                 convertedValue = _CalendarbookUtil::ConvertBusyStatusToCSEventBusyStatus(value);
317                 break;
318         case EVENT_FI_PR_PRIORITY:
319                 convertedValue = _CalendarbookUtil::ConvertEventPriorityToCSEventPriority(value);
320                 break;
321         case EVENT_FI_PR_SENSITIVITY:
322                 convertedValue = _CalendarbookUtil::ConvertSensitivityToCSSensitivity(value);
323                 break;
324         case EVENT_FI_PR_STATUS:
325                 convertedValue = _CalendarbookUtil::ConvertEventStatusToCSEventStatus(value);
326                 break;
327         case TODO_FI_PR_PRIORITY:
328                 convertedValue = _CalendarbookUtil::ConvertTodoPriorityToCSTodoPriority(value);
329                 break;
330         case TODO_FI_PR_SENSITIVITY:
331                 convertedValue = _CalendarbookUtil::ConvertSensitivityToCSSensitivity(value);
332                 break;
333         case TODO_FI_PR_STATUS:
334                 convertedValue = _CalendarbookUtil::ConvertTodoStatusToCSTodoStatus(value);
335                 break;
336         case CALENDAR_FI_PR_ITEM_TYPE:
337                 convertedValue = _CalendarbookUtil::ConvertCalendarItemTypeToCSCalendarbookType(value);
338                 break;
339         case EVENT_INST_FI_PR_BUSY_STATUS:
340                 convertedValue = _CalendarbookUtil::ConvertBusyStatusToCSEventBusyStatus(value);
341                 break;
342         case EVENT_INST_FI_PR_PRIORITY:
343                 convertedValue = _CalendarbookUtil::ConvertEventPriorityToCSEventPriority(value);
344                 break;
345         case EVENT_INST_FI_PR_SENSITIVITY:
346                 convertedValue = _CalendarbookUtil::ConvertSensitivityToCSSensitivity(value);
347                 break;
348         case EVENT_INST_FI_PR_STATUS:
349                 convertedValue = _CalendarbookUtil::ConvertEventStatusToCSEventStatus(value);
350                 break;
351         default:
352                 convertedValue = value;
353                 break;
354         }
355
356         if (!__isEmpty)
357         {
358                 calendar_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CALENDAR_FILTER_OPERATOR_AND : CALENDAR_FILTER_OPERATOR_OR;
359
360                 calendar_filter_add_operator(__filterHandle, filterOperator);
361         }
362
363         int ret = calendar_filter_add_int(__filterHandle, id, match, convertedValue);
364         SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
365
366         __isEmpty = false;
367
368         return E_SUCCESS;
369 }
370
371 result
372 _CalendarbookFilterImpl::AppendDouble(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterComparisonOperator comparisonOperator, double value)
373 {
374         SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. filterProperty = %ld", GetErrorMessage(E_INVALID_ARG), filterProperty);
375         SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_DOUBLE, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be double.", GetErrorMessage(E_INVALID_ARG), filterProperty);
376         SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
377                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
378                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
379         SysTryReturn(NID_SCL, !(!__isEmpty && (conjunctiveOperator == FI_CONJ_OP_NONE)),\
380                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
381                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
382
383         unsigned int id = _CalendarbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
384         calendar_match_int_flag_e match = GetMatchIntFlag(comparisonOperator);
385
386         if (!__isEmpty)
387         {
388                 calendar_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CALENDAR_FILTER_OPERATOR_AND : CALENDAR_FILTER_OPERATOR_OR;
389
390                 calendar_filter_add_operator(__filterHandle, filterOperator);
391         }
392
393         int ret = calendar_filter_add_double(__filterHandle, id, match, value);
394         SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
395
396         __isEmpty = false;
397
398         return E_SUCCESS;
399 }
400
401 result
402 _CalendarbookFilterImpl::AppendBool(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterComparisonOperator comparisonOperator, bool value)
403 {
404         SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. filterProperty = %ld", GetErrorMessage(E_INVALID_ARG), filterProperty);
405         SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_BOOL, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be bool.", GetErrorMessage(E_INVALID_ARG), filterProperty);
406         SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
407                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
408                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
409         SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
410                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
411                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
412         SysTryReturn(NID_SCL, comparisonOperator == FI_CMP_OP_EQUAL, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. comparisonOperator %d must be FI_CMP_OP_EQUAL.", GetErrorMessage(E_INVALID_ARG), comparisonOperator);
413
414         unsigned int viewPropertyId = _CalendarbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
415
416         if (!__isEmpty)
417         {
418                 calendar_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CALENDAR_FILTER_OPERATOR_AND : CALENDAR_FILTER_OPERATOR_OR;
419
420                 calendar_filter_add_operator(__filterHandle, filterOperator);
421         }
422
423         int ret = calendar_filter_add_int(__filterHandle, viewPropertyId, CALENDAR_MATCH_EQUAL, value);
424         SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
425
426         __isEmpty = false;
427
428         return E_SUCCESS;
429 }
430
431 result
432 _CalendarbookFilterImpl::AppendDateTime(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterComparisonOperator comparisonOperator, const DateTime& value)
433 {
434         SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. filterProperty = %ld", GetErrorMessage(E_INVALID_ARG), filterProperty);
435         SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_DATE_TIME, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be DateTime.", GetErrorMessage(E_INVALID_ARG), filterProperty);
436         SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
437                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
438                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
439         SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
440                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
441                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
442         SysTryReturn(NID_SCL, _CalendarbookUtil::CheckValidDateTime(value), null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. The value is not in a valid range.");
443
444         unsigned int id = _CalendarbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
445         calendar_match_int_flag_e match = GetMatchIntFlag(comparisonOperator);
446
447         if (!__isEmpty)
448         {
449                 calendar_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CALENDAR_FILTER_OPERATOR_AND : CALENDAR_FILTER_OPERATOR_OR;
450
451                 calendar_filter_add_operator(__filterHandle, filterOperator);
452         }
453
454
455         if (filterProperty == EVENT_FI_PR_LAST_REVISED_TIME || filterProperty == TODO_FI_PR_LAST_REVISED_TIME)
456         {
457                 int ret = calendar_filter_add_lli(__filterHandle, id, match, _CalendarbookUtil::ConvertDateTimeToEpochTime(value));
458                 SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
459         }
460         else if (filterProperty == TODO_FI_PR_START_DATE)
461         {
462                 calendar_time_s convertedTime;
463
464                 if (comparisonOperator == FI_CMP_OP_IS_NULL)
465                 {
466                         convertedTime.type = CALENDAR_TIME_UTIME;
467                         convertedTime.time.utime = _CALENDAR_TODO_NO_START_DATE;
468                         match = CALENDAR_MATCH_EQUAL;
469                 }
470                 else
471                 {
472                         _CalendarbookUtil::ConvertDateTimeToCalTime(value, convertedTime);
473                 }
474
475                 int ret = calendar_filter_add_caltime(__filterHandle, id, match, convertedTime);
476                 SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
477         }
478         else if (filterProperty == TODO_FI_PR_DUE_DATE)
479         {
480                 calendar_time_s convertedTime;
481
482                 if (comparisonOperator == FI_CMP_OP_IS_NULL)
483                 {
484                         convertedTime.type = CALENDAR_TIME_UTIME;
485                         convertedTime.time.utime = _CALENDAR_TODO_NO_DUE_DATE;
486                         match = CALENDAR_MATCH_EQUAL;
487                 }
488                 else
489                 {
490                         _CalendarbookUtil::ConvertDateTimeToCalTime(value, convertedTime);
491                 }
492
493                 int ret = calendar_filter_add_caltime(__filterHandle, id, match, convertedTime);
494                 SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
495         }
496         else if ((filterProperty == EVENT_INST_FI_PR_START_TIME || filterProperty == EVENT_INST_FI_PR_END_TIME) && __filterType == CB_FI_TYPE_NON_ALL_DAY_EVENT_INSTANCE)
497         {
498                 calendar_time_s convertedTime;
499                 _CalendarbookUtil::ConvertDateTimeToCalTime(value, convertedTime);
500
501                 int ret = calendar_filter_add_caltime(__filterHandle, id, match, convertedTime);
502                 SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
503         }
504         else if ((filterProperty == EVENT_INST_FI_PR_START_TIME || filterProperty == EVENT_INST_FI_PR_END_TIME) && (__filterType == CB_FI_TYPE_ALL_DAY_EVENT_INSTANCE))
505         {
506                 calendar_time_s convertedTime;
507                 _CalendarbookUtil::ConvertDateToCalTime(value, convertedTime);
508
509                 int ret = calendar_filter_add_caltime(__filterHandle, id, match, convertedTime);
510                 SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
511         }
512         else
513         {
514                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be DateTime.", GetErrorMessage(E_INVALID_ARG), filterProperty);
515                 return E_INVALID_ARG;
516         }
517
518         __isEmpty = false;
519
520         return E_SUCCESS;
521 }
522
523 result
524 _CalendarbookFilterImpl::AppendString(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterStringOperator comparisonOperator, const String& value)
525 {
526         SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. filterProperty = %ld", GetErrorMessage(E_INVALID_ARG), filterProperty);
527         SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_STRING, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be String.", GetErrorMessage(E_INVALID_ARG), filterProperty);
528         SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND  || conjunctiveOperator == FI_CONJ_OP_OR)),\
529                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
530                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
531         SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
532                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
533                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
534
535         unsigned int viewPropertyId = _CalendarbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
536
537         calendar_match_str_flag_e match = GetMatchStrFlag(comparisonOperator);
538         std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(value));
539         SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
540
541         if (!__isEmpty)
542         {
543                 calendar_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CALENDAR_FILTER_OPERATOR_AND : CALENDAR_FILTER_OPERATOR_OR;
544
545                 calendar_filter_add_operator(__filterHandle, filterOperator);
546         }
547
548         int ret = calendar_filter_add_str(__filterHandle, viewPropertyId, match, pCharArray.get());
549         SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
550
551         __isEmpty = false;
552
553         return E_SUCCESS;
554 }
555
556 result
557 _CalendarbookFilterImpl::AppendFilter(FilterConjunctiveOperator conjunctiveOperator, const _CalendarbookFilterImpl& filter)
558 {
559         SysTryReturn(NID_SCL, !filter.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified filter should not be empty.", GetErrorMessage(E_INVALID_ARG));
560         SysTryReturn(NID_SCL, __filterType == filter.__filterType, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the filter must be same with the type of this filter.", GetErrorMessage(E_INVALID_ARG));
561         SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
562                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
563                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
564         SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
565                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
566                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
567
568         if (!__isEmpty)
569         {
570                 calendar_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CALENDAR_FILTER_OPERATOR_AND : CALENDAR_FILTER_OPERATOR_OR;
571                 calendar_filter_add_operator(__filterHandle, filterOperator);
572         }
573
574         int ret = calendar_filter_add_filter(__filterHandle, filter.__filterHandle);
575         SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
576
577         __isEmpty = false;
578
579         return E_SUCCESS;
580 }
581
582 calendar_filter_h
583 _CalendarbookFilterImpl::GetFilterHandle(void) const
584 {
585         if (!__isEmpty)
586         {
587                 return __filterHandle;
588         }
589
590         return null;
591 }
592
593 CalendarbookFilterType
594 _CalendarbookFilterImpl::GetType(void) const
595 {
596         return __filterType;
597 }
598
599 bool
600 _CalendarbookFilterImpl::IsEmpty(void) const
601 {
602         return __isEmpty;
603 }
604
605 const _CalendarbookFilterImpl*
606 _CalendarbookFilterImpl::GetInstance(const CalendarbookFilter& filter)
607 {
608         return filter.__pCalendarbookFilterImpl;
609 }
610
611 _CalendarbookFilterImpl*
612 _CalendarbookFilterImpl::GetInstance(CalendarbookFilter& filter)
613 {
614         return filter.__pCalendarbookFilterImpl;
615 }
616
617 }} // Tizen::Social