Release 4.0.0-preview1-00147
[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         /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
271         protected virtual void Dispose(bool disposing)
272         {
273             if (!disposedValue)
274             {
275                 Log.Debug(Globals.LogTag, "Dispose :" + disposing);
276
277                 int error = Interop.Filter.Destroy(_filterHandle);
278                 if (CalendarError.None != (CalendarError)error)
279                 {
280                     Log.Error(Globals.LogTag, "Destroy Failed with error " + error);
281                     throw CalendarErrorFactory.GetException(error);
282                 }
283                 disposedValue = true;
284             }
285         }
286
287         /// <summary>
288         /// Releases all resources used by the CalendarFilter.
289         /// It should be called after having finished using of the object.
290         /// </summary>
291         public void Dispose()
292         {
293             Dispose(true);
294             GC.SuppressFinalize(this);
295         }
296 #endregion
297
298         /// <summary>
299         /// Adds a condition for the string type.
300         /// </summary>
301         /// <param name="logicalOperator">The operator type</param>
302         /// <param name="propertyId">The property ID to add a condition</param>
303         /// <param name="matchType">The match flag</param>
304         /// <param name="matchValue">The match value</param>
305         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
306         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
307         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, StringMatchType matchType, string matchValue)
308         {
309             int error = Interop.Filter.AddOperator(_filterHandle, logicalOperator);
310             if (CalendarError.None != (CalendarError)error)
311             {
312                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
313                 throw CalendarErrorFactory.GetException(error);
314             }
315
316             error = Interop.Filter.AddString(_filterHandle, propertyId, matchType, matchValue);
317             if (CalendarError.None != (CalendarError)error)
318             {
319                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
320                 throw CalendarErrorFactory.GetException(error);
321             }
322         }
323
324         /// <summary>
325         /// Adds a condition for the integer type.
326         /// </summary>
327         /// <param name="logicalOperator">The operator type</param>
328         /// <param name="propertyId">The property ID to add a condition</param>
329         /// <param name="matchType">The match flag</param>
330         /// <param name="matchValue">The match value</param>
331         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
332         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
333         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, int matchValue)
334         {
335             int error = Interop.Filter.AddOperator(_filterHandle, logicalOperator);
336             if (CalendarError.None != (CalendarError)error)
337             {
338                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
339                 throw CalendarErrorFactory.GetException(error);
340             }
341
342             error = Interop.Filter.AddInteger(_filterHandle, propertyId, matchType, matchValue);
343             if (CalendarError.None != (CalendarError)error)
344             {
345                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
346                 throw CalendarErrorFactory.GetException(error);
347             }
348         }
349
350         /// <summary>
351         /// Adds a condition for the long type.
352         /// </summary>
353         /// <param name="logicalOperator">The operator type</param>
354         /// <param name="propertyId">The property ID to add a condition</param>
355         /// <param name="matchType">The match flag</param>
356         /// <param name="matchValue">The match value</param>
357         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
358         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
359         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, long matchValue)
360         {
361             int error = Interop.Filter.AddOperator(_filterHandle, logicalOperator);
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             error = Interop.Filter.AddLong(_filterHandle, propertyId, matchType, matchValue);
369             if (CalendarError.None != (CalendarError)error)
370             {
371                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
372                 throw CalendarErrorFactory.GetException(error);
373             }
374         }
375
376         /// <summary>
377         /// Adds a condition for the double type.
378         /// </summary>
379         /// <param name="logicalOperator">The operator type</param>
380         /// <param name="propertyId">The property ID to add a condition</param>
381         /// <param name="matchType">The match flag</param>
382         /// <param name="matchValue">The match value</param>
383         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
384         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
385         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, double matchValue)
386         {
387             int error = Interop.Filter.AddOperator(_filterHandle, logicalOperator);
388             if (CalendarError.None != (CalendarError)error)
389             {
390                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
391                 throw CalendarErrorFactory.GetException(error);
392             }
393
394             error = Interop.Filter.AddDouble(_filterHandle, propertyId, matchType, matchValue);
395             if (CalendarError.None != (CalendarError)error)
396             {
397                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
398                 throw CalendarErrorFactory.GetException(error);
399             }
400         }
401
402         /// <summary>
403         /// Adds a condition for the CalendarTime type.
404         /// </summary>
405         /// <param name="logicalOperator">The operator type</param>
406         /// <param name="propertyId">The property ID to add a condition</param>
407         /// <param name="matchType">The match flag</param>
408         /// <param name="matchValue">The match value</param>
409         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
410         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
411         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, CalendarTime matchValue)
412         {
413             int error = Interop.Filter.AddOperator(_filterHandle, logicalOperator);
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             Interop.Record.DateTime time = CalendarRecord.ConvertCalendarTimeToStruct(matchValue);
421             error = Interop.Filter.AddCalendarTime(_filterHandle, propertyId, matchType, time);
422             if (CalendarError.None != (CalendarError)error)
423             {
424                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
425                 throw CalendarErrorFactory.GetException(error);
426             }
427         }
428
429         /// <summary>
430         /// Adds a child filter to a parent filter.
431         /// </summary>
432         /// <param name="logicalOperator">The operator type</param>
433         /// <param name="filter">The child filter</param>
434         /// <exception cref="NotSupportedException">Thrown when an invoked method 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 AddFilter(LogicalOperator logicalOperator, CalendarFilter filter)
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             error = Interop.Filter.AddFilter(_filterHandle, filter._filterHandle);
446             if (CalendarError.None != (CalendarError)error)
447             {
448                 Log.Error(Globals.LogTag, "AddFilter Failed with error " + error);
449                 throw CalendarErrorFactory.GetException(error);
450             }
451         }
452     }
453 }