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