Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Pims.Calendar / Tizen.Pims.Calendar / CalendarQuery.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 static Interop.Calendar.Query;
19
20 namespace Tizen.Pims.Calendar
21 {
22     /// A query is used to retrieve data which satisfies given criteria.
23     /// </summary>
24     /// <remarks>
25     /// A query is used to retrieve calendar data which satisfies a given criteria,
26     /// such as an integer property being greater than a given value,
27     /// or a string property containing a given substring.
28     /// A query needs a filter which can set the conditions for the search.
29     /// </remarks>
30     public class CalendarQuery : IDisposable
31     {
32         internal IntPtr _queryHandle;
33
34         /// <summary>
35         /// Creates a query.
36         /// </summary>
37         /// <param name="viewUri">The view URI of a query</param>
38         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
39         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
40         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
41         public CalendarQuery(string viewUri)
42         {
43             int error = Interop.Calendar.Query.Create(viewUri, out _queryHandle);
44             if (CalendarError.None != (CalendarError)error)
45             {
46                 Log.Error(Globals.LogTag, "CalendarQuery Failed with error " + error);
47                 throw CalendarErrorFactory.GetException(error);
48             }
49         }
50
51         internal CalendarQuery(IntPtr handle)
52         {
53             _queryHandle = handle;
54         }
55
56         ~CalendarQuery()
57         {
58             Dispose(false);
59         }
60
61 #region IDisposable Support
62         private bool disposedValue = false;
63
64         protected virtual void Dispose(bool disposing)
65         {
66             if (!disposedValue)
67             {
68                 Log.Debug(Globals.LogTag, "Dispose :" + disposing);
69
70                 int error = Interop.Calendar.Query.Destroy(_queryHandle);
71                 if (CalendarError.None != (CalendarError)error)
72                 {
73                     Log.Error(Globals.LogTag, "CalendarQueryDestroy Failed with error " + error);
74                     throw CalendarErrorFactory.GetException(error);
75                 }
76                 disposedValue = true;
77             }
78         }
79
80         /// <summary>
81         /// Releases all resources used by the CalendarQuery.
82         /// It should be called after finished using of the object.
83         /// </summary>
84         public void Dispose()
85         {
86             Dispose(true);
87         }
88 #endregion
89
90         /// <summary>
91         /// Adds property IDs for projection.
92         /// </summary>
93         /// <param name="propertyIdArray">The property ID array </param>
94         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
95         public void SetProjection(uint[] propertyIdArray)
96         {
97             int error = Interop.Calendar.Query.SetProjection(_queryHandle, propertyIdArray, propertyIdArray.Length);
98             if (CalendarError.None != (CalendarError)error)
99             {
100                 Log.Error(Globals.LogTag, "SetProjection Failed with error " + error);
101                 throw CalendarErrorFactory.GetException(error);
102             }
103         }
104
105         /// <summary>
106         /// Sets the "distinct" option for projection.
107         /// </summary>
108         /// <param name="set">If true it is set, otherwise if false it is unset</param>
109         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
110         public void SetDistinct(bool set)
111         {
112             int error = Interop.Calendar.Query.SetDistinct(_queryHandle, set);
113             if (CalendarError.None != (CalendarError)error)
114             {
115                 Log.Error(Globals.LogTag, "SetDistinct Failed with error " + error);
116                 throw CalendarErrorFactory.GetException(error);
117             }
118         }
119
120         /// <summary>
121         /// Sets the filter for a query.
122         /// </summary>
123         /// <param name="filter">The filter</param>
124         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
125         public void SetFilter(CalendarFilter filter)
126         {
127             int error = Interop.Calendar.Query.SetFilter(_queryHandle, filter._filterHandle);
128             if (CalendarError.None != (CalendarError)error)
129             {
130                 Log.Error(Globals.LogTag, "SetFilter Failed with error " + error);
131                 throw CalendarErrorFactory.GetException(error);
132             }
133         }
134
135         /// <summary>
136         /// Sets the sort mode for a query.
137         /// </summary>
138         /// <param name="propertyId">The property ID to sort</param>
139         /// <param name="isAscending">If true it sorts in the ascending order, otherwise if false it sorts in the descending order</param>
140         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
141         public void SetSort(uint propertyId, bool isAscending)
142         {
143             int error = Interop.Calendar.Query.SetSort(_queryHandle, propertyId, isAscending);
144             if (CalendarError.None != (CalendarError)error)
145             {
146                 Log.Error(Globals.LogTag, "SetSort Failed with error " + error);
147                 throw CalendarErrorFactory.GetException(error);
148             }
149         }
150     }
151 }