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