f65a41153d77b770951a4b236588f064008bcd30
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.DataControl / Tizen.Applications.DataControl / BulkData.cs
1 /*
2  * Copyright (c) 2017 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 using System;
17 using System.Collections.Generic;
18
19 namespace Tizen.Applications.DataControl
20 {
21     /// <summary>
22     /// Represents the BulkData class for the DataControl bulk request.
23     /// </summary>
24     public class BulkData : IDisposable
25     {
26         private bool _disposed = false;
27         private Interop.DataControl.SafeBulkDataHandle _handle;
28
29         /// <summary>
30         /// Initializes the BulkData class.
31         /// </summary>
32         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
33         public BulkData()
34         {
35             ResultType ret;
36
37             ret = Interop.DataControl.BulkCreate(out _handle);
38             if (ret != ResultType.Success)
39             {
40                 ErrorFactory.ThrowException(ret, true, "BulkCreate");
41             }
42
43         }
44
45         internal BulkData(Interop.DataControl.SafeBulkDataHandle handle)
46         {
47             ResultType ret;
48             int count, i;
49
50             ret = Interop.DataControl.BulkCreate(out _handle);
51             if (ret != ResultType.Success)
52             {
53                 ErrorFactory.ThrowException(ret, true, "BulkCreate");
54             }
55
56             ret = Interop.DataControl.BulkGetCount(handle, out count);
57             for ( i = 0; i < count; i++)
58             {
59                 IntPtr bundleHandle;
60                 Bundle bundle;
61
62                 ret = Interop.DataControl.BulkGetData(handle, i, out bundleHandle);
63                 if (ret != ResultType.Success)
64                 {
65                     ErrorFactory.ThrowException(ret, true, "BulkGetData");
66                 }
67
68                 bundle = new Bundle(new SafeBundleHandle(bundleHandle, false));
69                 ret = Interop.DataControl.BulkAdd(_handle, bundle.SafeBundleHandle);
70                 if (ret != ResultType.Success)
71                 {
72                     ErrorFactory.ThrowException(ret, true, "BulkAdd");
73                 }
74             }
75         }
76
77         internal Interop.DataControl.SafeBulkDataHandle SafeBulkDataHandle
78         {
79             get { return _handle; }
80         }
81
82         /// <summary>
83         /// Adds the bulk data.
84         /// </summary>
85         /// <param name="data">Bulk data</param>
86         /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
87         public void Add(Bundle data)
88         {
89             ResultType ret;
90
91             if (data == null || data.SafeBundleHandle.IsInvalid)
92             {
93                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "data");
94             }
95
96             ret = Interop.DataControl.BulkAdd(_handle, data.SafeBundleHandle);
97             if (ret != ResultType.Success)
98             {
99                 ErrorFactory.ThrowException(ret, true, "BulkAdd");
100             }
101         }
102
103         /// <summary>
104         /// Gets the current data count.
105         /// </summary>
106         public int GetCount()
107         {
108             int count;
109             ResultType ret;
110
111             ret = Interop.DataControl.BulkGetCount(_handle, out count);
112             if (ret != ResultType.Success)
113             {
114                 ErrorFactory.ThrowException(ret, true, "BulkGetCount");
115             }
116
117             return count;
118         }
119
120         /// <summary>
121         /// Returns the data at the given zero-based data index.
122         /// </summary>
123         /// <param name="index">The target data index.</param>
124         /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
125         public Bundle GetData(int index)
126         {
127             IntPtr bundlePtr;
128             Bundle bundle;
129             ResultType ret;
130
131             if (index < 0)
132             {
133                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "index");
134             }
135
136             ret = Interop.DataControl.BulkGetData(_handle, index, out bundlePtr);
137             if (ret != ResultType.Success)
138             {
139                 ErrorFactory.ThrowException(ret, true, "BulkGetData");
140             }
141
142             bundle = new Bundle(new SafeBundleHandle(bundlePtr, false));
143             return bundle;
144         }
145
146         /// <summary>
147         /// Releases all the resources used by the BulkData class.
148         /// </summary>
149         public void Dispose()
150         {
151             Dispose(true);
152             GC.SuppressFinalize(this);
153         }
154
155         /// <summary>
156         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
157         /// </summary>
158         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
159         protected virtual void Dispose(bool disposing)
160         {
161             if (!_disposed)
162             {
163                 if (_handle != null && !_handle.IsInvalid)
164                 {
165                     _handle.Dispose();
166                 }
167
168                 _disposed = true;
169             }
170         }
171
172         /// <summary>
173         /// Destructor of the BulkData class.
174         /// </summary>
175         ~BulkData()
176         {
177             Dispose(false);
178         }
179     }
180
181     /// <summary>
182     /// Represents the BulkResultData class for the DataControl bulk request.
183     /// </summary>
184     public class BulkResultData : IDisposable
185     {
186         private const string LogTag = "Tizen.Applications.DataControl";
187         private bool _disposed = false;
188         private Interop.DataControl.SafeBulkResultDataHandle _handle;
189         /// <summary>
190         /// Initializes the BulkResultData class.
191         /// </summary>
192         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
193         public BulkResultData()
194         {
195             ResultType ret;
196
197             ret = Interop.DataControl.BulkResultCreate(out _handle);
198             if (ret != ResultType.Success)
199             {
200                 ErrorFactory.ThrowException(ret, true,"BulkResultCreate");
201             }
202         }
203
204         internal BulkResultData(Interop.DataControl.SafeBulkResultDataHandle handle)
205         {
206             ResultType ret;
207
208             ret = Interop.DataControl.BulkResultCreate(out _handle);
209             if (ret != ResultType.Success)
210             {
211                 ErrorFactory.ThrowException(ret, true,"BulkResultCreate");
212             }
213
214             int count;
215             ret = Interop.DataControl.BulkResultGetCount(handle, out count);
216             for (int i = 0; i < count; i++)
217             {
218                 IntPtr bundleHandle;
219                 Bundle bundle;
220                 int result;
221
222                 ret = Interop.DataControl.BulkResultGetData(handle, i, out bundleHandle, out result);
223                 if (ret != ResultType.Success)
224                 {
225                     ErrorFactory.ThrowException(ret, true, "BulkResultGetData");
226                 }
227
228                 bundle = new Bundle(new SafeBundleHandle(bundleHandle, false));
229                 ret = Interop.DataControl.BulkResultAdd(_handle, bundle.SafeBundleHandle, result);
230                 if (ret != ResultType.Success)
231                 {
232                     ErrorFactory.ThrowException(ret, true, "BulkResultAdd");
233                 }
234             }
235         }
236
237         /// <summary>
238         /// Adds the bulk operation result data.
239         /// </summary>
240         /// <param name="data">The result data.</param>
241         /// <param name="result">Result.</param>
242         /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
243         public void Add(Bundle data, int result)
244         {
245             ResultType ret;
246
247             if (data == null || data.SafeBundleHandle.IsInvalid)
248             {
249                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "data");
250             }
251
252             ret = Interop.DataControl.BulkResultAdd(_handle, data.SafeBundleHandle, result);
253             if (ret != ResultType.Success)
254             {
255                 ErrorFactory.ThrowException(ret, true, "BulkResultAdd");
256             }
257         }
258
259         internal Interop.DataControl.SafeBulkResultDataHandle SafeBulkDataHandle
260         {
261             get { return _handle; }
262         }
263
264         /// <summary>
265         /// Gets the current result data count.
266         /// </summary>
267         public int GetCount()
268         {
269             int count;
270             ResultType ret;
271
272             ret = Interop.DataControl.BulkResultGetCount(_handle, out count);
273             if (ret != ResultType.Success)
274             {
275                 ErrorFactory.ThrowException(ret, true,"BulkResultGetCount");
276             }
277
278             return count;
279         }
280
281         /// <summary>
282         /// Returns the result data at the given zero-based data index.
283         /// </summary>
284         /// <param name="index">The target result data index.</param>
285         /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
286         public Bundle GetData(int index)
287         {
288             IntPtr bundlePtr;
289             Bundle bundle;
290             ResultType ret;
291             int result;
292
293             if (index < 0)
294             {
295                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "index");
296             }
297
298             ret = Interop.DataControl.BulkResultGetData(_handle, index, out bundlePtr, out result);
299             if (ret != ResultType.Success)
300             {
301                 ErrorFactory.ThrowException(ret, true, "BulkResultGetData");
302             }
303
304             bundle = new Bundle(new SafeBundleHandle(bundlePtr, false));
305             return bundle;
306         }
307
308         /// <summary>
309         /// Returns the result at the given zero-based data index.
310         /// </summary>
311         /// <param name="index">The target result index.</param>
312         /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
313         public int GetResult(int index)
314         {
315             IntPtr bundlePtr;
316             ResultType ret;
317             int result;
318
319             if (index < 0)
320             {
321                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "index");
322             }
323
324             ret = Interop.DataControl.BulkResultGetData(_handle, index, out bundlePtr, out result);
325             if (ret != ResultType.Success)
326             {
327                 ErrorFactory.ThrowException(ret, true, "BulkResultGetData");
328             }
329
330             return result;
331         }
332
333         /// <summary>
334         /// Releases all the resources used by the BulkResultData class.
335         /// </summary>
336         public void Dispose()
337         {
338             Dispose(true);
339             GC.SuppressFinalize(this);
340         }
341
342         /// <summary>
343         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
344         /// </summary>
345         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
346         protected virtual void Dispose(bool disposing)
347         {
348             if (!_disposed)
349             {
350                 if (_handle != null && !_handle.IsInvalid)
351                 {
352                     _handle.Dispose();
353                 }
354
355                 _disposed = true;
356             }
357         }
358
359         /// <summary>
360         /// Destructor of the BulkResultData class.
361         /// </summary>
362         ~BulkResultData()
363         {
364             Dispose(false);
365         }
366     }
367 }