[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.Pims.Calendar / Tizen.Pims.Calendar / CalendarFilter.cs
1 /*
2  * Copyright (c) 2018 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/feature/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 the 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/feature/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 the 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 a long type.
96         /// </summary>
97         /// <since_tizen> 4 </since_tizen>
98         /// <feature>http://tizen.org/feature/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 the 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 a double type.
128         /// </summary>
129         /// <since_tizen> 4 </since_tizen>
130         /// <feature>http://tizen.org/feature/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 the 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 the CalendarTime type.
160         /// </summary>
161         /// <since_tizen> 4 </since_tizen>
162         /// <feature>http://tizen.org/feature/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 the 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             IntPtr 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         /// Destroys the 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         /// <since_tizen> 4 </since_tizen>
203         public enum StringMatchType
204         {
205             /// <summary>
206             /// Full string, case-sensitive.
207             /// </summary>
208             Exactly,
209             /// <summary>
210             /// Full string, case-insensitive.
211             /// </summary>
212             FullString,
213             /// <summary>
214             /// Sub-string, case-insensitive.
215             /// </summary>
216             Contains,
217             /// <summary>
218             /// Starts with, case-insensitive.
219             /// </summary>
220             StartsWith,
221             /// <summary>
222             /// Ends with, case-insensitive.
223             /// </summary>
224             EndsWith,
225             /// <summary>
226             /// IS NOT NULL.
227             /// </summary>
228             Exists,
229         }
230
231         /// <summary>
232         /// Enumeration for the filter match type of an integer.
233         /// </summary>
234         /// <since_tizen> 4 </since_tizen>
235         public enum IntegerMatchType
236         {
237             /// <summary>
238             /// '='
239             /// </summary>
240             Equal,
241             /// <summary>
242             /// '>'
243             /// </summary>
244             GreaterThan,
245             /// <summary>
246             /// '>='
247             /// </summary>
248             GreaterThanOrEqual,
249             /// <summary>
250             /// &lt;
251             /// </summary>
252             LessThan,
253             /// <summary>
254             /// &lt;=
255             /// </summary>
256             LessThanOrEqual,
257             /// <summary>
258             /// &lt;>, this flag can yield poor performance.
259             /// </summary>
260             NotEqual,
261             /// <summary>
262             /// IS NULL.
263             /// </summary>
264             None,
265         }
266
267         /// <summary>
268         /// Enumeration for a filter operator.
269         /// </summary>
270         /// <since_tizen> 4 </since_tizen>
271         public enum LogicalOperator
272         {
273             /// <summary>
274             /// And.
275             /// </summary>
276             And,
277             /// <summary>
278             /// Or.
279             /// </summary>
280             Or,
281         }
282
283 #region IDisposable Support
284         private bool disposedValue = false; // To detect redundant calls
285
286         /// <summary>
287         /// Dispose.
288         /// </summary>
289         /// <param name="disposing">true to release both managed and unmanaged resources, false to release only unmanaged resources.</param>
290         /// <since_tizen> 4 </since_tizen>
291         protected virtual void Dispose(bool disposing)
292         {
293             if (!disposedValue)
294             {
295                 Log.Debug(Globals.LogTag, "Dispose :" + disposing);
296
297                 int error = Interop.Filter.Destroy(_filterHandle);
298                 if (CalendarError.None != (CalendarError)error)
299                 {
300                     Log.Error(Globals.LogTag, "Destroy Failed with error " + error);
301                 }
302                 disposedValue = true;
303             }
304         }
305
306         /// <summary>
307         /// Releases all the resources used by the CalendarFilter.
308         /// It should be called after having finished using the object.
309         /// </summary>
310         /// <since_tizen> 4 </since_tizen>
311         public void Dispose()
312         {
313             Dispose(true);
314             GC.SuppressFinalize(this);
315         }
316 #endregion
317
318         /// <summary>
319         /// Adds a condition for the string type.
320         /// </summary>
321         /// <since_tizen> 4 </since_tizen>
322         /// <feature>http://tizen.org/feature/calendar</feature>
323         /// <param name="logicalOperator">The operator type.</param>
324         /// <param name="propertyId">The property ID to add a condition.</param>
325         /// <param name="matchType">The match flag.</param>
326         /// <param name="matchValue">The match value.</param>
327         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
328         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
329         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, StringMatchType matchType, string matchValue)
330         {
331             int error = Interop.Filter.AddOperator(_filterHandle, logicalOperator);
332             if (CalendarError.None != (CalendarError)error)
333             {
334                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
335                 throw CalendarErrorFactory.GetException(error);
336             }
337
338             error = Interop.Filter.AddString(_filterHandle, propertyId, matchType, matchValue);
339             if (CalendarError.None != (CalendarError)error)
340             {
341                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
342                 throw CalendarErrorFactory.GetException(error);
343             }
344         }
345
346         /// <summary>
347         /// Adds a condition for the integer type.
348         /// </summary>
349         /// <since_tizen> 4 </since_tizen>
350         /// <feature>http://tizen.org/feature/calendar</feature>
351         /// <param name="logicalOperator">The operator type.</param>
352         /// <param name="propertyId">The property ID to add a condition.</param>
353         /// <param name="matchType">The match flag.</param>
354         /// <param name="matchValue">The match value.</param>
355         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
356         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
357         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, int matchValue)
358         {
359             int error = Interop.Filter.AddOperator(_filterHandle, logicalOperator);
360             if (CalendarError.None != (CalendarError)error)
361             {
362                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
363                 throw CalendarErrorFactory.GetException(error);
364             }
365
366             error = Interop.Filter.AddInteger(_filterHandle, propertyId, matchType, matchValue);
367             if (CalendarError.None != (CalendarError)error)
368             {
369                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
370                 throw CalendarErrorFactory.GetException(error);
371             }
372         }
373
374         /// <summary>
375         /// Adds a condition for the long type.
376         /// </summary>
377         /// <since_tizen> 4 </since_tizen>
378         /// <feature>http://tizen.org/feature/calendar</feature>
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 the feature 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, long 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.AddLong(_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 double type.
404         /// </summary>
405         /// <since_tizen> 4 </since_tizen>
406         /// <feature>http://tizen.org/feature/calendar</feature>
407         /// <param name="logicalOperator">The operator type.</param>
408         /// <param name="propertyId">The property ID to add a condition.</param>
409         /// <param name="matchType">The match flag.</param>
410         /// <param name="matchValue">The match value.</param>
411         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
412         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
413         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, double matchValue)
414         {
415             int error = Interop.Filter.AddOperator(_filterHandle, logicalOperator);
416             if (CalendarError.None != (CalendarError)error)
417             {
418                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
419                 throw CalendarErrorFactory.GetException(error);
420             }
421
422             error = Interop.Filter.AddDouble(_filterHandle, propertyId, matchType, matchValue);
423             if (CalendarError.None != (CalendarError)error)
424             {
425                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
426                 throw CalendarErrorFactory.GetException(error);
427             }
428         }
429
430         /// <summary>
431         /// Adds a condition for the CalendarTime type.
432         /// </summary>
433         /// <since_tizen> 4 </since_tizen>
434         /// <feature>http://tizen.org/feature/calendar</feature>
435         /// <param name="logicalOperator">The operator type.</param>
436         /// <param name="propertyId">The property ID to add a condition.</param>
437         /// <param name="matchType">The match flag.</param>
438         /// <param name="matchValue">The match value.</param>
439         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
440         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
441         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, CalendarTime matchValue)
442         {
443             int error = Interop.Filter.AddOperator(_filterHandle, logicalOperator);
444             if (CalendarError.None != (CalendarError)error)
445             {
446                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
447                 throw CalendarErrorFactory.GetException(error);
448             }
449
450             IntPtr time = CalendarRecord.ConvertCalendarTimeToStruct(matchValue);
451             error = Interop.Filter.AddCalendarTime(_filterHandle, propertyId, matchType, time);
452             if (CalendarError.None != (CalendarError)error)
453             {
454                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
455                 throw CalendarErrorFactory.GetException(error);
456             }
457         }
458
459         /// <summary>
460         /// Adds a child filter to a parent filter.
461         /// </summary>
462         /// <since_tizen> 4 </since_tizen>
463         /// <feature>http://tizen.org/feature/calendar</feature>
464         /// <param name="logicalOperator">The operator type.</param>
465         /// <param name="filter">The child filter.</param>
466         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
467         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
468         public void AddFilter(LogicalOperator logicalOperator, CalendarFilter filter)
469         {
470             int error = Interop.Filter.AddOperator(_filterHandle, logicalOperator);
471             if (CalendarError.None != (CalendarError)error)
472             {
473                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
474                 throw CalendarErrorFactory.GetException(error);
475             }
476
477             error = Interop.Filter.AddFilter(_filterHandle, filter._filterHandle);
478             if (CalendarError.None != (CalendarError)error)
479             {
480                 Log.Error(Globals.LogTag, "AddFilter Failed with error " + error);
481                 throw CalendarErrorFactory.GetException(error);
482             }
483         }
484     }
485 }