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