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