Release 4.0.0-preview1-00051
[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 BulkData class for 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 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 bulk data.
84         /// </summary>
85         /// <param name="data">Bulk data</param>
86         /// <exception cref="ArgumentException">Thrown in case of Invalid parmaeter.</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 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">Target data index</param>
124         /// <exception cref="ArgumentException">Thrown in case of Invalid parmaeter.</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 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         ~BulkData()
173         {
174             Dispose(false);
175         }
176     }
177
178     /// <summary>
179     /// Represents BulkResultData class for DataControl bulk request.
180     /// </summary>
181     public class BulkResultData : IDisposable
182     {
183         private const string LogTag = "Tizen.Applications.DataControl";
184         private bool _disposed = false;
185         private Interop.DataControl.SafeBulkResultDataHandle _handle;
186         /// <summary>
187         /// Initializes BulkResultData class.
188         /// </summary>
189         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
190         public BulkResultData()
191         {
192             ResultType ret;
193
194             ret = Interop.DataControl.BulkResultCreate(out _handle);
195             if (ret != ResultType.Success)
196             {
197                 ErrorFactory.ThrowException(ret, true,"BulkResultCreate");
198             }
199         }
200
201         internal BulkResultData(Interop.DataControl.SafeBulkResultDataHandle handle)
202         {
203             ResultType ret;
204
205             ret = Interop.DataControl.BulkResultCreate(out _handle);
206             if (ret != ResultType.Success)
207             {
208                 ErrorFactory.ThrowException(ret, true,"BulkResultCreate");
209             }
210
211             int count;
212             ret = Interop.DataControl.BulkResultGetCount(handle, out count);
213             for (int i = 0; i < count; i++)
214             {
215                 IntPtr bundleHandle;
216                 Bundle bundle;
217                 int result;
218
219                 ret = Interop.DataControl.BulkResultGetData(handle, i, out bundleHandle, out result);
220                 if (ret != ResultType.Success)
221                 {
222                     ErrorFactory.ThrowException(ret, true, "BulkResultGetData");
223                 }
224
225                 bundle = new Bundle(new SafeBundleHandle(bundleHandle, false));
226                 ret = Interop.DataControl.BulkResultAdd(_handle, bundle.SafeBundleHandle, result);
227                 if (ret != ResultType.Success)
228                 {
229                     ErrorFactory.ThrowException(ret, true, "BulkResultAdd");
230                 }
231             }
232         }
233
234         /// <summary>
235         /// Adds bulk operation result data.
236         /// </summary>
237         /// <param name="data">Result data</param>
238         /// <param name="result">Result</param>
239         /// <exception cref="ArgumentException">Thrown in case of Invalid parmaeter.</exception>
240         public void Add(Bundle data, int result)
241         {
242             ResultType ret;
243
244             if (data == null || data.SafeBundleHandle.IsInvalid)
245             {
246                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "data");
247             }
248
249             ret = Interop.DataControl.BulkResultAdd(_handle, data.SafeBundleHandle, result);
250             if (ret != ResultType.Success)
251             {
252                 ErrorFactory.ThrowException(ret, true, "BulkResultAdd");
253             }
254         }
255
256         internal Interop.DataControl.SafeBulkResultDataHandle SafeBulkDataHandle
257         {
258             get { return _handle; }
259         }
260
261         /// <summary>
262         /// Gets current result data count.
263         /// </summary>
264         public int GetCount()
265         {
266             int count;
267             ResultType ret;
268
269             ret = Interop.DataControl.BulkResultGetCount(_handle, out count);
270             if (ret != ResultType.Success)
271             {
272                 ErrorFactory.ThrowException(ret, true,"BulkResultGetCount");
273             }
274
275             return count;
276         }
277
278         /// <summary>
279         /// Returns the result data at the given zero-based data index.
280         /// </summary>
281         /// <param name="index">Target result data index</param>
282         /// <exception cref="ArgumentException">Thrown in case of Invalid parmaeter.</exception>
283         public Bundle GetData(int index)
284         {
285             IntPtr bundlePtr;
286             Bundle bundle;
287             ResultType ret;
288             int result;
289
290             if (index < 0)
291             {
292                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "index");
293             }
294
295             ret = Interop.DataControl.BulkResultGetData(_handle, index, out bundlePtr, out result);
296             if (ret != ResultType.Success)
297             {
298                 ErrorFactory.ThrowException(ret, true, "BulkResultGetData");
299             }
300
301             bundle = new Bundle(new SafeBundleHandle(bundlePtr, false));
302             return bundle;
303         }
304
305         /// <summary>
306         /// Returns the result at the given zero-based data index.
307         /// </summary>
308         /// <param name="index">Target result index</param>
309         /// <exception cref="ArgumentException">Thrown in case of Invalid parmaeter.</exception>
310         public int GetResult(int index)
311         {
312             IntPtr bundlePtr;
313             ResultType ret;
314             int result;
315
316             if (index < 0)
317             {
318                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "index");
319             }
320
321             ret = Interop.DataControl.BulkResultGetData(_handle, index, out bundlePtr, out result);
322             if (ret != ResultType.Success)
323             {
324                 ErrorFactory.ThrowException(ret, true, "BulkResultGetData");
325             }
326
327             return result;
328         }
329
330         /// <summary>
331         /// Releases all resources used by the BulkResultData class.
332         /// </summary>
333         public void Dispose()
334         {
335             Dispose(true);
336             GC.SuppressFinalize(this);
337         }
338
339         /// <summary>
340         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
341         /// </summary>
342         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
343         protected virtual void Dispose(bool disposing)
344         {
345             if (!_disposed)
346             {
347                 if (_handle != null && !_handle.IsInvalid)
348                 {
349                     _handle.Dispose();
350                 }
351
352                 _disposed = true;
353             }
354         }
355
356         ~BulkResultData()
357         {
358             Dispose(false);
359         }
360     }
361 }