2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 using System.Collections.Generic;
18 using Tizen.Applications.DataControl.Core;
19 using System.Threading;
20 using System.Runtime.InteropServices;
22 namespace Tizen.Applications.DataControl
25 /// Represents the Consumer class for the DataControl consumer application.
27 /// <since_tizen> 3 </since_tizen>
28 public abstract class Consumer : IDisposable
31 private Interop.DataControl.SafeDataControlHandle _handle;
32 private string _dataID, _providerID;
33 private int _changeCallbackID = 0;
34 private const string LogTag = "Tizen.Applications.DataControl";
35 private bool _disposed = false;
36 private static Mutex _lock = new Mutex();
37 private Interop.DataControl.DataChangeCallback _dataChangeCallback;
38 private Interop.DataControl.AddCallbackResultCallback _addCallbackResultCallback;
40 private static class CallbackManager
42 private static IDictionary<string, Interop.DataControl.MapResponseCallbacks> _mapResponseCallbacks = new Dictionary<string, Interop.DataControl.MapResponseCallbacks>();
43 private static IDictionary<string, Interop.DataControl.MapBulkAddResponseCallback> _mapBulkResponseCallback = new Dictionary<string, Interop.DataControl.MapBulkAddResponseCallback>();
44 private static IDictionary<string, Interop.DataControl.SqlResponseCallbacks> _sqlResponseCallbacks = new Dictionary<string, Interop.DataControl.SqlResponseCallbacks>();
45 private static IDictionary<string, Interop.DataControl.SqlBulkInsertResponseCallback> _sqlBulkResponseCallback = new Dictionary<string, Interop.DataControl.SqlBulkInsertResponseCallback>();
46 private static IDictionary<int, Consumer> _reqConsumerDictionary = new Dictionary<int, Consumer>();
47 private static IDictionary<string, int> _reqProviderList = new Dictionary<string, int>();
48 private static void InsertResponse(int reqId, IntPtr provider, long insertedRowId, bool providerResult, string error, IntPtr userData)
50 Log.Debug(LogTag, $"InsertResponse {reqId.ToString()}");
51 if (!_reqConsumerDictionary.ContainsKey(reqId))
53 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
59 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}, rowID : {insertedRowId.ToString()}");
62 Consumer consumer = _reqConsumerDictionary[reqId];
63 consumer.OnInsertResult(new InsertResult(insertedRowId, providerResult));
64 _reqConsumerDictionary.Remove(reqId);
67 private static void BulkInsertResponse(int reqId, IntPtr provider, IntPtr bulkResults, bool providerResult, string error, IntPtr userData)
70 Log.Debug(LogTag, $"BulkInsertResponse {reqId.ToString()}");
71 if (!_reqConsumerDictionary.ContainsKey(reqId))
73 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
79 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
82 if (bulkResults != IntPtr.Zero)
84 brd = new BulkResultData(new Interop.DataControl.SafeBulkResultDataHandle(bulkResults, false));
88 brd = new BulkResultData();
89 Log.Error(LogTag, $"reqId {reqId.ToString()}, bulkResults is null");
92 Consumer consumer = _reqConsumerDictionary[reqId];
93 consumer.OnBulkInsertResult(new BulkInsertResult(brd, providerResult));
94 _reqConsumerDictionary.Remove(reqId);
97 private static void SelectResponse(int reqId, IntPtr provider, IntPtr cursor, bool providerResult, string error, IntPtr userData)
100 Log.Debug(LogTag, $"SelectResponse {reqId.ToString()}");
101 if (!_reqConsumerDictionary.ContainsKey(reqId))
103 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
109 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
112 if (cursor != IntPtr.Zero)
116 dmc = CloneCursor(new CloneCursorCore(new Interop.DataControl.SafeCursorHandle(cursor, true)));
120 dmc = new MatrixCursor();
121 Log.Error(LogTag, $"reqId {reqId.ToString()}, {ex.ToString()}");
126 dmc = new MatrixCursor();
127 Log.Error(LogTag, $"reqId {reqId.ToString()}, cursor is null");
129 Consumer consumer = _reqConsumerDictionary[reqId];
130 consumer.OnSelectResult(new SelectResult(dmc, providerResult));
131 _reqConsumerDictionary.Remove(reqId);
134 private static void UpdateResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
136 if (!_reqConsumerDictionary.ContainsKey(reqId))
138 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
144 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
147 Consumer consumer = _reqConsumerDictionary[reqId];
148 consumer.OnUpdateResult(new UpdateResult(providerResult));
149 _reqConsumerDictionary.Remove(reqId);
152 private static void DeleteResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
154 if (!_reqConsumerDictionary.ContainsKey(reqId))
156 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
162 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
165 Consumer consumer = _reqConsumerDictionary[reqId];
166 consumer.OnDeleteResult(new DeleteResult(providerResult));
167 _reqConsumerDictionary.Remove(reqId);
170 static void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray)
172 managedArray = new string[size];
173 IntPtr[] IntPtrArray = new IntPtr[size];
174 Marshal.Copy(unmanagedArray, IntPtrArray, 0, size);
175 for (int iterator = 0; iterator < size; iterator++)
177 managedArray[iterator] = Marshal.PtrToStringAnsi(IntPtrArray[iterator]);
181 private static void MapGetResponse(int reqId, IntPtr provider, IntPtr valueList, int valueCount, bool providerResult, string error, IntPtr userData)
184 Log.Debug(LogTag, $"MapGetResponse {reqId.ToString()}");
185 if (!_reqConsumerDictionary.ContainsKey(reqId))
187 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
193 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
196 if (valueList != IntPtr.Zero)
198 string[] stringArray;
199 IntPtrToStringArray(valueList, valueCount, out stringArray);
200 mgr = new MapGetResult(stringArray, providerResult);
204 mgr = new MapGetResult(new string[0], providerResult);
205 Log.Error(LogTag, $"reqId {reqId.ToString()}, valueList is null");
208 Consumer consumer = _reqConsumerDictionary[reqId];
209 consumer.OnMapGetResult(mgr);
210 _reqConsumerDictionary.Remove(reqId);
213 private static void MapBulkAddResponse(int reqId, IntPtr provider, IntPtr bulkResults, bool providerResult, string error, IntPtr userData)
216 Log.Debug(LogTag, $"MapBulkAddResponse {reqId.ToString()}");
217 if (!_reqConsumerDictionary.ContainsKey(reqId))
219 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
225 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
228 if (bulkResults != IntPtr.Zero)
230 brd = new BulkResultData(new Interop.DataControl.SafeBulkResultDataHandle(bulkResults, false));
234 brd = new BulkResultData();
235 Log.Error(LogTag, $"reqId {reqId.ToString()}, bulkResults is null");
238 Consumer consumer = _reqConsumerDictionary[reqId];
239 consumer.OnMapBulkAddResult(new MapBulkAddResult(brd, providerResult));
240 _reqConsumerDictionary.Remove(reqId);
243 private static void MapAddResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
245 Log.Debug(LogTag, $"MapAddResponse {reqId.ToString()}");
246 if (!_reqConsumerDictionary.ContainsKey(reqId))
248 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
254 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
257 Consumer consumer = _reqConsumerDictionary[reqId];
258 consumer.OnMapAddResult(new MapAddResult(providerResult));
259 _reqConsumerDictionary.Remove(reqId);
262 private static void MapSetResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
264 Log.Debug(LogTag, $"MapSetResponse {reqId.ToString()}");
265 if (!_reqConsumerDictionary.ContainsKey(reqId))
267 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
273 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
276 Consumer consumer = _reqConsumerDictionary[reqId];
277 consumer.OnMapSetResult(new MapSetResult(providerResult));
278 _reqConsumerDictionary.Remove(reqId);
281 private static void MapRemoveResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
283 if (!_reqConsumerDictionary.ContainsKey(reqId))
285 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
291 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
294 Consumer consumer = _reqConsumerDictionary[reqId];
295 consumer.OnMapRemoveResult(new MapRemoveResult(providerResult));
296 _reqConsumerDictionary.Remove(reqId);
299 private static MatrixCursor CloneCursor(CloneCursorCore coreCursor)
301 int size = coreCursor.GetColumnCount();
303 string[] name = new string[size];
304 object[] newRow = new object[size];
305 ColumnType[] type = new ColumnType[size];
307 for (i = 0; i < size; i++)
309 name[i] = coreCursor.GetColumnName(i);
310 type[i] = coreCursor.GetColumnType(i);
313 MatrixCursor dmc = new MatrixCursor(name, type);
315 if (coreCursor.GetRowCount() <= 0)
323 for (i = 0; i < size; i++)
327 case ColumnType.ColumnTypeInt:
328 newRow[i] = coreCursor.GetInt64Value(i);
330 case ColumnType.ColumnTypeDouble:
331 newRow[i] = coreCursor.GetDoubleValue(i);
333 case ColumnType.ColumnTypeBlob:
334 newRow[i] = coreCursor.GetBlobValue(i);
336 case ColumnType.ColumnTypeString:
337 newRow[i] = coreCursor.GetStringValue(i);
344 while (coreCursor.Next());
349 internal static void RegisterReqId(int reqId, Consumer consumer)
352 _reqConsumerDictionary.Add(reqId, consumer);
353 _lock.ReleaseMutex();
356 internal static void RegisterCallback(Interop.DataControl.SafeDataControlHandle handle, string providerId)
359 Interop.DataControl.SqlResponseCallbacks sqlCallbacks;
360 Interop.DataControl.SqlBulkInsertResponseCallback sqlBulkCallbacks;
361 Interop.DataControl.MapResponseCallbacks mapCallbacks;
362 Interop.DataControl.MapBulkAddResponseCallback mapBulkCallbacks;
363 bool sqlRegistered = false;
364 bool mapRegistered = false;
366 if (_reqProviderList.ContainsKey(providerId))
368 _reqProviderList[providerId]++;
369 Log.Error(LogTag, "The data control is already set");
373 sqlCallbacks.Insert = new Interop.DataControl.SqlInsertResponseCallback(InsertResponse);
374 sqlCallbacks.Select = new Interop.DataControl.SqlSelectResponseCallback(SelectResponse);
375 sqlCallbacks.Update = new Interop.DataControl.SqlUpdateResponseCallback(UpdateResponse);
376 sqlCallbacks.Delete = new Interop.DataControl.SqlDeleteResponseCallback(DeleteResponse);
377 ret = Interop.DataControl.RegisterSqlResponseCallback(handle, ref sqlCallbacks, IntPtr.Zero);
378 if (ret != ResultType.Success)
380 Log.Error(LogTag, "Registering the sql callback function is failed : " + ret);
384 _sqlResponseCallbacks.Add(providerId, sqlCallbacks);
385 sqlRegistered = true;
388 sqlBulkCallbacks = new Interop.DataControl.SqlBulkInsertResponseCallback(BulkInsertResponse);
389 ret = Interop.DataControl.RegisterSqlBulkResponseCallback(handle, sqlBulkCallbacks, IntPtr.Zero);
390 if (ret != ResultType.Success)
392 Log.Error(LogTag, "Registering the sql bulk callback function is failed : " + ret);
396 _sqlBulkResponseCallback.Add(providerId, sqlBulkCallbacks);
399 mapCallbacks.Add = new Interop.DataControl.MapAddResponseCallback(MapAddResponse);
400 mapCallbacks.Set = new Interop.DataControl.MapSetResponseCallback(MapSetResponse);
401 mapCallbacks.Get = new Interop.DataControl.MapGetResponseCallback(MapGetResponse);
402 mapCallbacks.Remove = new Interop.DataControl.MapRemoveResponseCallback(MapRemoveResponse);
403 ret = Interop.DataControl.RegisterMapResponse(handle, ref mapCallbacks, IntPtr.Zero);
405 if (ret != ResultType.Success)
407 Log.Error(LogTag, "Registering the map callback function is failed : " + ret);
411 _mapResponseCallbacks.Add(providerId, mapCallbacks);
412 mapRegistered = true;
415 mapBulkCallbacks = new Interop.DataControl.MapBulkAddResponseCallback(MapBulkAddResponse);
416 ret = Interop.DataControl.RegisterMapBulkResponseCallback(handle, mapBulkCallbacks, IntPtr.Zero);
417 if (ret != ResultType.Success)
419 Log.Error(LogTag, "Registering the map bulk callback function is failed : " + ret);
423 _mapBulkResponseCallback.Add(providerId, mapBulkCallbacks);
426 if (!mapRegistered && !sqlRegistered)
428 ErrorFactory.ThrowException(ret, true, "Registering the response callback function is failed");
431 _reqProviderList.Add(providerId, 1);
434 internal static void UnregisterCallback(Interop.DataControl.SafeDataControlHandle handle, string providerId)
438 if (!_reqProviderList.ContainsKey(providerId))
440 Log.Error(LogTag, "The provider id is not contained : " + providerId);
444 _reqProviderList[providerId]--;
445 count = _reqProviderList[providerId];
448 _reqProviderList.Remove(providerId);
450 _mapResponseCallbacks.Remove(providerId);
451 Interop.DataControl.UnregisterMapResponse(handle);
453 _mapBulkResponseCallback.Remove(providerId);
454 Interop.DataControl.UnregisterMapBulkResponseCallback(handle);
456 _sqlResponseCallbacks.Remove(providerId);
457 Interop.DataControl.UnregisterSqlResponseCallback(handle);
459 _sqlBulkResponseCallback.Remove(providerId);
460 Interop.DataControl.UnregisterSqlBulkResponseCallback(handle);
467 /// Sends the insert request to the provider application.
469 /// <remarks>The OnInsertResult will recieve the result of this API.</remarks>
470 /// <param name="insertData">The insert data.</param>
471 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
472 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
473 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
474 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
475 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
476 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
477 /// <since_tizen> 3 </since_tizen>
478 public void Insert(Bundle insertData)
483 if (insertData == null || insertData.SafeBundleHandle.IsInvalid)
485 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "insertData");
489 ret = Interop.DataControl.Insert(_handle, insertData.SafeBundleHandle, out reqId);
490 _lock.ReleaseMutex();
491 if (ret != ResultType.Success)
493 ErrorFactory.ThrowException(ret, false, "Insert");
496 CallbackManager.RegisterReqId(reqId, this);
500 /// Sends the select request to the provider application.
502 /// <remarks>The OnSelectResult will recieve the result of this API.</remarks>
503 /// <param name="columnList">Select the target column list.</param>
504 /// <param name="where">The Where statement for the select query.</param>
505 /// <param name="order">The Order statement for the select query.</param>
506 /// <param name="pageNumber">Select the target page number.</param>
507 /// <param name="countPerPage">Select the row count per page.</param>
508 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
509 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied..</exception>
510 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
511 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
512 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
513 /// <since_tizen> 3 </since_tizen>
514 public void Select(string[] columnList, string where, string order, int pageNumber = 1, int countPerPage = 20)
518 if (columnList == null || columnList.Length == 0)
520 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "column_list");
523 for (i = 0; i < columnList.Length; i++)
525 if (string.IsNullOrEmpty(columnList[i]))
527 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "column_list index " + i.ToString());
532 ret = Interop.DataControl.Select(_handle, columnList, columnList.Length, where, order, pageNumber, countPerPage, out reqId);
533 _lock.ReleaseMutex();
534 if (ret != ResultType.Success)
536 ErrorFactory.ThrowException(ret, false, "Select");
538 Log.Info(LogTag, "select end. " + reqId.ToString());
540 CallbackManager.RegisterReqId(reqId, this);
544 /// Sends the delete request to the provider application.
546 /// <remarks>The OnDeleteResult will recieve the result of this API</remarks>
547 /// <param name="where">The Where statement for the delete query.</param>
548 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
549 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
550 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
551 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
552 /// <since_tizen> 3 </since_tizen>
553 public void Delete(string where)
559 ret = Interop.DataControl.Delete(_handle, where, out reqId);
560 _lock.ReleaseMutex();
561 if (ret != ResultType.Success)
563 ErrorFactory.ThrowException(ret, false, "Delete");
566 CallbackManager.RegisterReqId(reqId, this);
570 /// Sends the update request to the provider application.
572 /// <remarks>The OnUpdateResult will recieve result of this API.</remarks>
573 /// <param name="updateData">The update data.</param>
574 /// <param name="where">The Where statement for the query.</param>
575 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
576 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
577 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
578 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
579 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
580 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
581 /// <since_tizen> 3 </since_tizen>
582 public void Update(Bundle updateData, string where)
587 if (updateData == null || updateData.SafeBundleHandle.IsInvalid)
589 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "insertData");
592 if (string.IsNullOrEmpty(where))
594 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "where");
598 ret = Interop.DataControl.Update(_handle, updateData.SafeBundleHandle, where, out reqId);
599 _lock.ReleaseMutex();
600 if (ret != ResultType.Success)
602 ErrorFactory.ThrowException(ret, false, "Update");
605 CallbackManager.RegisterReqId(reqId, this);
609 /// Sends the bulk insert request to the provider application.
611 /// <remarks>The OnBulkInsertResult will recieve the result of this API.</remarks>
612 /// <param name="insertData">The bulk insert data.</param>
613 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
614 /// <exception cref="UnauthorizedAccessException">Thrown in case oif a permission is denied.</exception>
615 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
616 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
617 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
618 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
619 /// <since_tizen> 3 </since_tizen>
620 public void BulkInsert(BulkData insertData)
625 if (insertData == null || insertData.SafeBulkDataHandle.IsInvalid)
627 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "insertData");
631 ret = Interop.DataControl.BulkInsert(_handle, insertData.SafeBulkDataHandle, out reqId);
632 _lock.ReleaseMutex();
633 if (ret != ResultType.Success)
635 ErrorFactory.ThrowException(ret, false, "BulkInsert");
638 CallbackManager.RegisterReqId(reqId, this);
642 /// Sends the map add request to the provider application.
644 /// <remarks>The OnMapAddResult will recieve the result of this API.</remarks>
645 /// <param name="key">The key of the value to add.</param>
646 /// <param name="value">The value to add.</param>
647 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
648 /// <exception cref="UnauthorizedAccessException">Thrown in case of if a permission is denied.</exception>
649 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
650 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
651 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
652 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
653 /// <since_tizen> 3 </since_tizen>
654 public void MapAdd(string key, string value)
659 if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
661 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
665 ret = Interop.DataControl.MapAdd(_handle, key, value, out reqId);
666 _lock.ReleaseMutex();
667 if (ret != ResultType.Success)
669 ErrorFactory.ThrowException(ret, false, "MapAdd");
672 CallbackManager.RegisterReqId(reqId, this);
676 /// Sends the map get request to the provider application.
678 /// <remarks>The OnMapGetResult will recieve the result of this API.</remarks>
679 /// <param name="key">The key of the value list to obtain.</param>
680 /// <param name="pageNumber">The page number of the value set.</param>
681 /// <param name="countPerPage">The desired maximum count of the data items per page.</param>
682 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
683 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
684 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
685 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
686 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
687 /// <since_tizen> 3 </since_tizen>
688 public void MapGet(string key, int pageNumber = 1, int countPerPage = 20)
693 if (string.IsNullOrEmpty(key) || pageNumber <= 0 || countPerPage <= 0)
695 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
699 ret = Interop.DataControl.MapGet(_handle, key, out reqId, pageNumber, countPerPage);
700 _lock.ReleaseMutex();
701 if (ret != ResultType.Success)
703 ErrorFactory.ThrowException(ret, false, "MapGet");
706 CallbackManager.RegisterReqId(reqId, this);
710 /// Sends the map remove request to the provider application.
712 /// <remarks>The OnMapRemoveResult will recieve the result of this API.</remarks>
713 /// <param name="key">The key of the value to remove.</param>
714 /// <param name="value">The value to remove.</param>
715 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
716 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
717 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
718 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
719 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
720 /// <since_tizen> 3 </since_tizen>
721 public void MapRemove(string key, string value)
726 if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
728 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
732 ret = Interop.DataControl.MapRemove(_handle, key, value, out reqId);
733 _lock.ReleaseMutex();
734 if (ret != ResultType.Success)
736 ErrorFactory.ThrowException(ret, false, "MapRemove");
739 CallbackManager.RegisterReqId(reqId, this);
743 /// Sends the map set request to the provider application.
745 /// <remarks>The OnMapSetResult will recieve the result of this API.</remarks>
746 /// <param name="key">The key of the value to replace.</param>
747 /// <param name="oldValue">The value to be replaced.</param>
748 /// <param name="newValue"> The new value that replaces the existing value.</param>
749 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
750 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
751 /// <exception cref="ArgumentOutOfRangeException">Thrown when message has exceeded the maximum limit (1MB).</exception>
752 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
753 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
754 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
755 /// <since_tizen> 3 </since_tizen>
756 public void MapSet(string key, string oldValue, string newValue)
761 if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(oldValue) || string.IsNullOrEmpty(newValue))
763 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
767 ret = Interop.DataControl.MapSet(_handle, key, oldValue, newValue, out reqId);
768 _lock.ReleaseMutex();
769 if (ret != ResultType.Success)
771 ErrorFactory.ThrowException(ret, false, "MapSet");
774 CallbackManager.RegisterReqId(reqId, this);
778 /// Sends the map bulk add request to the provider application.
780 /// <remarks>The OnMapBulkAddResult will recieve the result of this API.</remarks>
781 /// <param name="addData">The map bulk add data.</param>
782 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
783 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
784 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
785 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
786 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
787 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
788 /// <since_tizen> 3 </since_tizen>
789 public void MapBulkAdd(BulkData addData)
794 if (addData == null || addData.SafeBulkDataHandle.IsInvalid)
796 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "addData");
800 ret = Interop.DataControl.BulkAdd(_handle, addData.SafeBulkDataHandle, out reqId);
801 _lock.ReleaseMutex();
802 if (ret != ResultType.Success)
804 ErrorFactory.ThrowException(ret, false, "BulkAdd");
807 CallbackManager.RegisterReqId(reqId, this);
810 private void DataChange(IntPtr handle, ChangeType type, IntPtr data, IntPtr userData)
812 OnDataChange(type, new Bundle(new SafeBundleHandle(data, false)));
815 private void DataChangeListenResult(IntPtr handle, ResultType type, int callbackId, IntPtr userData)
817 OnDataChangeListenResult(new DataChangeListenResult(type));
821 /// Listens the DataChange event.
823 /// <remarks>The OnDataChangeListenResult will recieve the result of this API.</remarks>
824 /// <remarks>If success, the OnDataChange will recieve the DataChange event.</remarks>
825 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
826 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
827 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
828 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
829 /// <since_tizen> 3 </since_tizen>
830 public void DataChangeListen()
834 /* Only one callback is allowed for every obejct */
835 if (_changeCallbackID > 0)
837 _lock.ReleaseMutex();
840 _dataChangeCallback = new Interop.DataControl.DataChangeCallback(DataChange);
841 _addCallbackResultCallback = new Interop.DataControl.AddCallbackResultCallback(DataChangeListenResult);
842 ret = Interop.DataControl.AddDataChangeCallback(_handle, _dataChangeCallback, IntPtr.Zero,
843 _addCallbackResultCallback , IntPtr.Zero, out _changeCallbackID);
844 _lock.ReleaseMutex();
845 if (ret != ResultType.Success)
847 ErrorFactory.ThrowException(ret, false, "DataChangeListen");
852 /// Initializes the Consumer class with the providerId and the ataId.
854 /// <param name="providerId">The DataControl Provider ID.</param>
855 /// <param name="dataId">The DataControl Data ID.</param>
856 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
857 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
858 /// <since_tizen> 3 </since_tizen>
859 public Consumer(string providerId, string dataId)
863 if (string.IsNullOrEmpty(providerId))
865 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "providerId");
868 if (string.IsNullOrEmpty(dataId))
870 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "dataId");
873 ret = Interop.DataControl.DataControlCreate(out _handle);
874 if (ret != ResultType.Success)
876 ErrorFactory.ThrowException(ret, false, "Creating data control handle is failed");
879 Interop.DataControl.DataControlSetProviderId(_handle, providerId);
880 Interop.DataControl.DataControlSetDataId(_handle, dataId);
882 CallbackManager.RegisterCallback(_handle, providerId);
883 _lock.ReleaseMutex();
885 _providerID = providerId;
889 /// Destructor of the Consumer class.
897 /// Overrides this method if you want to handle the behavior when the DataChangeListen result is received.
899 /// <since_tizen> 3 </since_tizen>
900 protected virtual void OnDataChangeListenResult(DataChangeListenResult result)
902 Log.Info(LogTag, "The OnDataChangeListenResult is not implemented.");
906 /// Overrides this method if you want to handle the behavior when the data change event is received.
908 /// <since_tizen> 3 </since_tizen>
909 protected virtual void OnDataChange(ChangeType type, Bundle data)
911 Log.Info(LogTag, "The OnDataChange is not implemented.");
915 /// Overrides this method if you want to handle the behavior when the select response is received.
917 /// <since_tizen> 3 </since_tizen>
918 protected abstract void OnSelectResult(SelectResult result);
921 /// Overrides this method if you want to handle the behavior when the insert response is received.
923 /// <since_tizen> 3 </since_tizen>
924 protected abstract void OnInsertResult(InsertResult result);
927 /// Overrides this method if you want to handle the behavior when the update response is received.
929 /// <since_tizen> 3 </since_tizen>
930 protected abstract void OnUpdateResult(UpdateResult result);
933 /// Overrides this method if want to handle the behavior when the delete response is received.
935 /// <since_tizen> 3 </since_tizen>
936 protected abstract void OnDeleteResult(DeleteResult result);
938 /// Overrides this method if you want to handle the behavior when the BulkInsert response is received.
940 /// <since_tizen> 3 </since_tizen>
941 protected virtual void OnBulkInsertResult(BulkInsertResult result)
943 Log.Info(LogTag, "The OnBulkInsertResult is not implemented.");
947 /// Overrides this method if you want to handle the behavior when the map get response is received.
949 /// <since_tizen> 3 </since_tizen>
950 protected virtual void OnMapGetResult(MapGetResult result)
952 Log.Info(LogTag, "The OnMapGetResult is not implemented.");
956 /// Overrides this method if you want to handle the behavior when the map add response is received.
958 /// <since_tizen> 3 </since_tizen>
959 protected virtual void OnMapAddResult(MapAddResult result)
961 Log.Info(LogTag, "The OnMapAddResult is not implemented.");
965 /// Overrides this method if you want to handle the behavior when the map set response is received.
967 /// <since_tizen> 3 </since_tizen>
968 protected virtual void OnMapSetResult(MapSetResult result)
970 Log.Info(LogTag, "The OnMapSetResult is not implemented.");
974 /// Overrides this method if you want to handle the behavior when the map remove response is received.
976 /// <since_tizen> 3 </since_tizen>
977 protected virtual void OnMapRemoveResult(MapRemoveResult result)
979 Log.Info(LogTag, "The OnMapRemoveResult is not implemented.");
983 /// Overrides this method if you want to handle the behavior when the BulkAdd response is received.
985 /// <since_tizen> 3 </since_tizen>
986 protected virtual void OnMapBulkAddResult(MapBulkAddResult result)
988 Log.Info(LogTag, "The OnMapBulkAddResult is not implemented.");
992 /// Releases the unmanaged resources used by the Consumer class specifying whether to perform a normal dispose operation.
994 /// <param name="disposing">true for a normal dispose operation; false to finalize the handle.</param>
995 /// <since_tizen> 3 </since_tizen>
996 protected virtual void Dispose(bool disposing)
1000 if (_changeCallbackID > 0)
1002 Interop.DataControl.RemoveDataChangeCallback(_handle, _changeCallbackID);
1006 CallbackManager.UnregisterCallback(_handle, _providerID);
1007 _lock.ReleaseMutex();
1014 GC.SuppressFinalize(this);
1019 /// Releases all resources used by the Consumer class.
1021 /// <since_tizen> 3 </since_tizen>
1022 public void Dispose()