Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Pims.Calendar / Tizen.Pims.Calendar / CalendarFilter.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18 using System.Collections.Generic;
19
20 namespace Tizen.Pims.Calendar
21 {
22     /// <summary>
23     /// A filter includes the conditions for the search.
24     /// </summary>
25     public class CalendarFilter:IDisposable
26     {
27         internal IntPtr _filterHandle;
28
29         /// <summary>
30         /// Creates a filter with a condition for a string type.
31         /// </summary>
32         /// <param name="viewUri">The view URI of a filter</param>
33         /// <param name="propertyId">The property ID to add a condition</param>
34         /// <param name="matchType">The match flag</param>
35         /// <param name="matchValue">The match value</param>
36         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
37         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
38         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
39         public CalendarFilter(string viewUri, uint propertyId, StringMatchType matchType, string matchValue)
40         {
41             int error = 0;
42             error = Interop.Calendar.Filter.Create(viewUri, out _filterHandle);
43             if (CalendarError.None != (CalendarError)error)
44             {
45                 Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error);
46                 throw CalendarErrorFactory.GetException(error);
47             }
48
49             error = Interop.Calendar.Filter.AddString(_filterHandle, propertyId, matchType, matchValue);
50             if (CalendarError.None != (CalendarError)error)
51             {
52                 Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error);
53                 throw CalendarErrorFactory.GetException(error);
54             }
55         }
56
57         /// <summary>
58         /// Creates a filter with a condition for an integer type.
59         /// </summary>
60         /// <param name="viewUri">The view URI of a filter</param>
61         /// <param name="propertyId">The property ID to add a condition</param>
62         /// <param name="matchType">The match flag</param>
63         /// <param name="matchValue">The match value</param>
64         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
65         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
66         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
67         public CalendarFilter(string viewUri, uint propertyId, IntegerMatchType matchType, int matchValue)
68         {
69             int error = 0;
70             error = Interop.Calendar.Filter.Create(viewUri, out _filterHandle);
71             if (CalendarError.None != (CalendarError)error)
72             {
73                 Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error);
74                 throw CalendarErrorFactory.GetException(error);
75             }
76
77             error = Interop.Calendar.Filter.AddInteger(_filterHandle, propertyId, matchType, matchValue);
78             if (CalendarError.None != (CalendarError)error)
79             {
80                 Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error);
81                 throw CalendarErrorFactory.GetException(error);
82             }
83         }
84
85         /// <summary>
86         /// Creates a filter with a condition for long type.
87         /// </summary>
88         /// <param name="viewUri">The view URI of a filter</param>
89         /// <param name="propertyId">The property ID to add a condition</param>
90         /// <param name="matchType">The match flag</param>
91         /// <param name="matchValue">The match value</param>
92         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
93         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
94         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
95         public CalendarFilter(string viewUri, uint propertyId, IntegerMatchType matchType, long matchValue)
96         {
97             int error = 0;
98             error = Interop.Calendar.Filter.Create(viewUri, out _filterHandle);
99             if (CalendarError.None != (CalendarError)error)
100             {
101                 Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error);
102                 throw CalendarErrorFactory.GetException(error);
103             }
104
105             error = Interop.Calendar.Filter.AddLong(_filterHandle, propertyId, matchType, matchValue);
106             if (CalendarError.None != (CalendarError)error)
107             {
108                 Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error);
109                 throw CalendarErrorFactory.GetException(error);
110             }
111         }
112
113         /// <summary>
114         /// Creates a filter with a condition for double type.
115         /// </summary>
116         /// <param name="viewUri">The view URI of a filter</param>
117         /// <param name="propertyId">The property ID to add a condition</param>
118         /// <param name="matchType">The match flag</param>
119         /// <param name="matchValue">The match value</param>
120         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
121         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
122         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
123         public CalendarFilter(string viewUri, uint propertyId, IntegerMatchType matchType, double matchValue)
124         {
125             int error = 0;
126             error = Interop.Calendar.Filter.Create(viewUri, out _filterHandle);
127             if (CalendarError.None != (CalendarError)error)
128             {
129                 Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error);
130                 throw CalendarErrorFactory.GetException(error);
131             }
132
133             error = Interop.Calendar.Filter.AddDouble(_filterHandle, propertyId, matchType, matchValue);
134             if (CalendarError.None != (CalendarError)error)
135             {
136                 Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error);
137                 throw CalendarErrorFactory.GetException(error);
138             }
139         }
140
141         /// <summary>
142         /// Creates a filter with a condition for CalendarTime type.
143         /// </summary>
144         /// <param name="viewUri">The view URI of a filter</param>
145         /// <param name="propertyId">The property ID to add a condition</param>
146         /// <param name="matchType">The match flag</param>
147         /// <param name="matchValue">The match value</param>
148         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
149         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
150         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
151         public CalendarFilter(string viewUri, uint propertyId, IntegerMatchType matchType, CalendarTime matchValue)
152         {
153             int error = 0;
154             error = Interop.Calendar.Filter.Create(viewUri, out _filterHandle);
155             if (CalendarError.None != (CalendarError)error)
156             {
157                 Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error);
158                 throw CalendarErrorFactory.GetException(error);
159             }
160
161             Interop.Calendar.Record.DateTime time = CalendarRecord.ConvertCalendarTimeToStruct(matchValue);
162             error = Interop.Calendar.Filter.AddCalendarTime(_filterHandle, propertyId, matchType, time);
163             if (CalendarError.None != (CalendarError)error)
164             {
165                 Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error);
166                 throw CalendarErrorFactory.GetException(error);
167             }
168         }
169
170         ~CalendarFilter()
171         {
172             Dispose(false);
173         }
174
175         /// <summary>
176         /// Enumeration for the filter match type of a string.
177         /// </summary>
178         public enum StringMatchType
179         {
180             /// <summary>
181             /// Full string, case-sensitive
182             /// </summary>
183             Exactly,
184             /// <summary>
185             /// Full string, case-insensitive
186             /// </summary>
187             Fullstring,
188             /// <summary>
189             /// Sub string, case-insensitive
190             /// </summary>
191             Contains,
192             /// <summary>
193             /// Start with, case-insensitive
194             /// </summary>
195             Startswith,
196             /// <summary>
197             /// End with, case-insensitive
198             /// </summary>
199             Endswith,
200             /// <summary>
201             /// IS NOT NUL
202             /// </summary>
203             Exists,
204         }
205
206         /// <summary>
207         /// Enumeration for the filter match type of an integer.
208         /// </summary>
209         public enum IntegerMatchType
210         {
211             /// <summary>
212             /// '='
213             /// </summary>
214             Equal,
215             /// <summary>
216             /// '>'
217             /// </summary>
218             GreaterThan,
219             /// <summary>
220             /// '>='
221             /// </summary>
222             GreaterThanOrEqual,
223             /// <summary>
224             /// &lt;
225             /// </summary>
226             LessThan,
227             /// <summary>
228             /// &lt;=
229             /// </summary>
230             LessThanOrEqual,
231             /// <summary>
232             /// &lt;>, this flag can yield poor performance
233             /// </summary>
234             NotEqual,
235             /// <summary>
236             /// IS NULL
237             /// </summary>
238             None,
239         }
240
241         /// <summary>
242         /// Enumeration for a filter operator.
243         /// </summary>
244         public enum LogicalOperator
245         {
246             /// <summary>
247             /// AND
248             /// </summary>
249             And,
250             /// <summary>
251             /// OR
252             /// </summary>
253             Or,
254         }
255
256 #region IDisposable Support
257         private bool disposedValue = false; // To detect redundant calls
258
259         protected virtual void Dispose(bool disposing)
260         {
261             if (!disposedValue)
262             {
263                 Log.Debug(Globals.LogTag, "Dispose :" + disposing);
264
265                 int error = Interop.Calendar.Filter.Destroy(_filterHandle);
266                 if (CalendarError.None != (CalendarError)error)
267                 {
268                     Log.Error(Globals.LogTag, "Destroy Failed with error " + error);
269                     throw CalendarErrorFactory.GetException(error);
270                 }
271                 disposedValue = true;
272             }
273         }
274
275         /// <summary>
276         /// Releases all resources used by the CalendarFilter.
277         /// It should be called after finished using of the object.
278         /// </summary>
279         public void Dispose()
280         {
281             Dispose(true);
282         }
283 #endregion
284
285         /// <summary>
286         /// Adds a condition for the string type.
287         /// </summary>
288         /// <param name="logicalOperator">The operator type</param>
289         /// <param name="propertyId">The property ID to add a condition</param>
290         /// <param name="matchType">The match flag</param>
291         /// <param name="matchValue">The match valu</param>
292         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
293         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
294         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, StringMatchType matchType, string matchValue)
295         {
296             int error = Interop.Calendar.Filter.AddOperator(_filterHandle, logicalOperator);
297             if (CalendarError.None != (CalendarError)error)
298             {
299                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
300                 throw CalendarErrorFactory.GetException(error);
301             }
302
303             error = Interop.Calendar.Filter.AddString(_filterHandle, propertyId, matchType, matchValue);
304             if (CalendarError.None != (CalendarError)error)
305             {
306                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
307                 throw CalendarErrorFactory.GetException(error);
308             }
309         }
310
311         /// <summary>
312         /// Adds a condition for the integer type.
313         /// </summary>
314         /// <param name="logicalOperator">The operator type</param>
315         /// <param name="propertyId">The property ID to add a condition</param>
316         /// <param name="matchType">The match flag</param>
317         /// <param name="matchValue">The match valu</param>
318         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
319         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
320         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, int matchValue)
321         {
322             int error = Interop.Calendar.Filter.AddOperator(_filterHandle, logicalOperator);
323             if (CalendarError.None != (CalendarError)error)
324             {
325                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
326                 throw CalendarErrorFactory.GetException(error);
327             }
328
329             error = Interop.Calendar.Filter.AddInteger(_filterHandle, propertyId, matchType, matchValue);
330             if (CalendarError.None != (CalendarError)error)
331             {
332                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
333                 throw CalendarErrorFactory.GetException(error);
334             }
335         }
336
337         /// <summary>
338         /// Adds a condition for the long type.
339         /// </summary>
340         /// <param name="logicalOperator">The operator type</param>
341         /// <param name="propertyId">The property ID to add a condition</param>
342         /// <param name="matchType">The match flag</param>
343         /// <param name="matchValue">The match valu</param>
344         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
345         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
346         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, long matchValue)
347         {
348             int error = Interop.Calendar.Filter.AddOperator(_filterHandle, logicalOperator);
349             if (CalendarError.None != (CalendarError)error)
350             {
351                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
352                 throw CalendarErrorFactory.GetException(error);
353             }
354
355             error = Interop.Calendar.Filter.AddLong(_filterHandle, propertyId, matchType, matchValue);
356             if (CalendarError.None != (CalendarError)error)
357             {
358                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
359                 throw CalendarErrorFactory.GetException(error);
360             }
361         }
362
363         /// <summary>
364         /// Adds a condition for the double type.
365         /// </summary>
366         /// <param name="logicalOperator">The operator type</param>
367         /// <param name="propertyId">The property ID to add a condition</param>
368         /// <param name="matchType">The match flag</param>
369         /// <param name="matchValue">The match valu</param>
370         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
371         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
372         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, double matchValue)
373         {
374             int error = Interop.Calendar.Filter.AddOperator(_filterHandle, logicalOperator);
375             if (CalendarError.None != (CalendarError)error)
376             {
377                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
378                 throw CalendarErrorFactory.GetException(error);
379             }
380
381             error = Interop.Calendar.Filter.AddDouble(_filterHandle, propertyId, matchType, matchValue);
382             if (CalendarError.None != (CalendarError)error)
383             {
384                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
385                 throw CalendarErrorFactory.GetException(error);
386             }
387         }
388
389         /// <summary>
390         /// Adds a condition for the CalendarTime type.
391         /// </summary>
392         /// <param name="logicalOperator">The operator type</param>
393         /// <param name="propertyId">The property ID to add a condition</param>
394         /// <param name="matchType">The match flag</param>
395         /// <param name="matchValue">The match valu</param>
396         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
397         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
398         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, CalendarTime matchValue)
399         {
400             int error = Interop.Calendar.Filter.AddOperator(_filterHandle, logicalOperator);
401             if (CalendarError.None != (CalendarError)error)
402             {
403                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
404                 throw CalendarErrorFactory.GetException(error);
405             }
406
407             Interop.Calendar.Record.DateTime time = CalendarRecord.ConvertCalendarTimeToStruct(matchValue);
408             error = Interop.Calendar.Filter.AddCalendarTime(_filterHandle, propertyId, matchType, time);
409             if (CalendarError.None != (CalendarError)error)
410             {
411                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
412                 throw CalendarErrorFactory.GetException(error);
413             }
414         }
415
416         /// <summary>
417         /// Adds a child filter to a parent filter.
418         /// </summary>
419         /// <param name="logicalOperator">The operator type</param>
420         /// <param name="filter">The child filter</param>
421         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
422         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
423         public void AddFilter(LogicalOperator logicalOperator, CalendarFilter filter)
424         {
425             int error = Interop.Calendar.Filter.AddOperator(_filterHandle, logicalOperator);
426             if (CalendarError.None != (CalendarError)error)
427             {
428                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
429                 throw CalendarErrorFactory.GetException(error);
430             }
431
432             error = Interop.Calendar.Filter.AddFilter(_filterHandle, filter._filterHandle);
433             if (CalendarError.None != (CalendarError)error)
434             {
435                 Log.Error(Globals.LogTag, "AddFilter Failed with error " + error);
436                 throw CalendarErrorFactory.GetException(error);
437             }
438         }
439     }
440 }