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;
21 namespace Tizen.Applications.DataControl
24 /// Represents the Consumer class for the DataControl consumer application.
26 public abstract class Consumer : IDisposable
29 private Interop.DataControl.SafeDataControlHandle _handle;
30 private string _dataID, _providerID;
31 private int _changeCallbackID = 0;
32 private const string LogTag = "Tizen.Applications.DataControl";
33 private bool _disposed = false;
34 private static Mutex _lock = new Mutex();
35 private Interop.DataControl.DataChangeCallback _dataChangeCallback;
36 private Interop.DataControl.AddCallbackResultCallback _addCallbackResultCallback;
38 private static class CallbackManager
40 private static IDictionary<string, Interop.DataControl.MapResponseCallbacks> _mapResponseCallbacks = new Dictionary<string, Interop.DataControl.MapResponseCallbacks>();
41 private static IDictionary<string, Interop.DataControl.MapBulkAddResponseCallback> _mapBulkResponseCallback = new Dictionary<string, Interop.DataControl.MapBulkAddResponseCallback>();
42 private static IDictionary<string, Interop.DataControl.SqlResponseCallbacks> _sqlResponseCallbacks = new Dictionary<string, Interop.DataControl.SqlResponseCallbacks>();
43 private static IDictionary<string, Interop.DataControl.SqlBulkInsertResponseCallback> _sqlBulkResponseCallback = new Dictionary<string, Interop.DataControl.SqlBulkInsertResponseCallback>();
44 private static IDictionary<int, Consumer> _reqConsumerDictionary = new Dictionary<int, Consumer>();
45 private static IDictionary<string, int> _reqProviderList = new Dictionary<string, int>();
46 private static void InsertResponse(int reqId, IntPtr provider, long insertedRowId, bool providerResult, string error, IntPtr userData)
48 Log.Debug(LogTag, $"InsertResponse {reqId.ToString()}");
49 if (!_reqConsumerDictionary.ContainsKey(reqId))
51 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
57 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}, rowID : {insertedRowId.ToString()}");
60 Consumer consumer = _reqConsumerDictionary[reqId];
61 consumer.OnInsertResult(new InsertResult(insertedRowId, providerResult));
62 _reqConsumerDictionary.Remove(reqId);
65 private static void BulkInsertResponse(int reqId, IntPtr provider, IntPtr bulkResults, bool providerResult, string error, IntPtr userData)
68 Log.Debug(LogTag, $"BulkInsertResponse {reqId.ToString()}");
69 if (!_reqConsumerDictionary.ContainsKey(reqId))
71 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
77 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
80 if (bulkResults != IntPtr.Zero)
82 brd = new BulkResultData(new Interop.DataControl.SafeBulkResultDataHandle(bulkResults, false));
86 brd = new BulkResultData();
87 Log.Error(LogTag, $"reqId {reqId.ToString()}, bulkResults is null");
90 Consumer consumer = _reqConsumerDictionary[reqId];
91 consumer.OnBulkInsertResult(new BulkInsertResult(brd, providerResult));
92 _reqConsumerDictionary.Remove(reqId);
95 private static void SelectResponse(int reqId, IntPtr provider, IntPtr cursor, bool providerResult, string error, IntPtr userData)
98 Log.Debug(LogTag, $"SelectResponse {reqId.ToString()}");
99 if (!_reqConsumerDictionary.ContainsKey(reqId))
101 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
107 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
110 if (cursor != IntPtr.Zero)
114 dmc = CloneCursor(new CloneCursorCore(new Interop.DataControl.SafeCursorHandle(cursor, true)));
118 dmc = new MatrixCursor();
119 Log.Error(LogTag, $"reqId {reqId.ToString()}, {ex.ToString()}");
124 dmc = new MatrixCursor();
125 Log.Error(LogTag, $"reqId {reqId.ToString()}, cursor is null");
127 Consumer consumer = _reqConsumerDictionary[reqId];
128 consumer.OnSelectResult(new SelectResult(dmc, providerResult));
129 _reqConsumerDictionary.Remove(reqId);
132 private static void UpdateResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
134 if (!_reqConsumerDictionary.ContainsKey(reqId))
136 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
142 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
145 Consumer consumer = _reqConsumerDictionary[reqId];
146 consumer.OnUpdateResult(new UpdateResult(providerResult));
147 _reqConsumerDictionary.Remove(reqId);
150 private static void DeleteResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
152 if (!_reqConsumerDictionary.ContainsKey(reqId))
154 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
160 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
163 Consumer consumer = _reqConsumerDictionary[reqId];
164 consumer.OnDeleteResult(new DeleteResult(providerResult));
165 _reqConsumerDictionary.Remove(reqId);
168 private static void MapGetResponse(int reqId, IntPtr provider, string[] valueList, int valueCount, bool providerResult, string error, IntPtr userData)
171 Log.Debug(LogTag, $"MapGetResponse {reqId.ToString()}");
172 if (!_reqConsumerDictionary.ContainsKey(reqId))
174 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
180 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
183 if (valueList !=null)
185 mgr = new MapGetResult(valueList, providerResult);
189 mgr = new MapGetResult(new string[0], providerResult);
190 Log.Error(LogTag, $"reqId {reqId.ToString()}, valueList is null");
193 Consumer consumer = _reqConsumerDictionary[reqId];
194 consumer.OnMapGetResult(mgr);
195 _reqConsumerDictionary.Remove(reqId);
198 private static void MapBulkAddResponse(int reqId, IntPtr provider, IntPtr bulkResults, bool providerResult, string error, IntPtr userData)
201 Log.Debug(LogTag, $"MapBulkAddResponse {reqId.ToString()}");
202 if (!_reqConsumerDictionary.ContainsKey(reqId))
204 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
210 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
213 if (bulkResults != IntPtr.Zero)
215 brd = new BulkResultData(new Interop.DataControl.SafeBulkResultDataHandle(bulkResults, false));
219 brd = new BulkResultData();
220 Log.Error(LogTag, $"reqId {reqId.ToString()}, bulkResults is null");
223 Consumer consumer = _reqConsumerDictionary[reqId];
224 consumer.OnMapBulkAddResult(new MapBulkAddResult(brd, providerResult));
225 _reqConsumerDictionary.Remove(reqId);
228 private static void MapAddResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
230 Log.Debug(LogTag, $"MapAddResponse {reqId.ToString()}");
231 if (!_reqConsumerDictionary.ContainsKey(reqId))
233 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
239 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
242 Consumer consumer = _reqConsumerDictionary[reqId];
243 consumer.OnMapAddResult(new MapAddResult(providerResult));
244 _reqConsumerDictionary.Remove(reqId);
247 private static void MapSetResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
249 Log.Debug(LogTag, $"MapSetResponse {reqId.ToString()}");
250 if (!_reqConsumerDictionary.ContainsKey(reqId))
252 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
258 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
261 Consumer consumer = _reqConsumerDictionary[reqId];
262 consumer.OnMapSetResult(new MapSetResult(providerResult));
263 _reqConsumerDictionary.Remove(reqId);
266 private static void MapRemoveResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
268 if (!_reqConsumerDictionary.ContainsKey(reqId))
270 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
276 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
279 Consumer consumer = _reqConsumerDictionary[reqId];
280 consumer.OnMapRemoveResult(new MapRemoveResult(providerResult));
281 _reqConsumerDictionary.Remove(reqId);
284 private static MatrixCursor CloneCursor(CloneCursorCore coreCursor)
286 int size = coreCursor.GetColumnCount();
288 string[] name = new string[size];
289 object[] newRow = new object[size];
290 ColumnType[] type = new ColumnType[size];
292 for (i = 0; i < size; i++)
294 name[i] = coreCursor.GetColumnName(i);
295 type[i] = coreCursor.GetColumnType(i);
298 MatrixCursor dmc = new MatrixCursor(name, type);
300 if (coreCursor.GetRowCount() <= 0)
308 for (i = 0; i < size; i++)
312 case ColumnType.ColumnTypeInt:
313 newRow[i] = coreCursor.GetInt64Value(i);
315 case ColumnType.ColumnTypeDouble:
316 newRow[i] = coreCursor.GetDoubleValue(i);
318 case ColumnType.ColumnTypeBlob:
319 newRow[i] = coreCursor.GetBlobValue(i);
321 case ColumnType.ColumnTypeString:
322 newRow[i] = coreCursor.GetStringValue(i);
329 while (coreCursor.Next());
334 internal static void RegisterReqId(int reqId, Consumer consumer)
337 _reqConsumerDictionary.Add(reqId, consumer);
338 _lock.ReleaseMutex();
341 internal static void RegisterCallback(Interop.DataControl.SafeDataControlHandle handle, string providerId)
344 Interop.DataControl.SqlResponseCallbacks sqlCallbacks;
345 Interop.DataControl.SqlBulkInsertResponseCallback sqlBulkCallbacks;
346 Interop.DataControl.MapResponseCallbacks mapCallbacks;
347 Interop.DataControl.MapBulkAddResponseCallback mapBulkCallbacks;
348 bool sqlRegistered = false;
349 bool mapRegistered = false;
351 if (_reqProviderList.ContainsKey(providerId))
353 _reqProviderList[providerId]++;
354 Log.Error(LogTag, "The data control is already set");
358 sqlCallbacks.Insert = new Interop.DataControl.SqlInsertResponseCallback(InsertResponse);
359 sqlCallbacks.Select = new Interop.DataControl.SqlSelectResponseCallback(SelectResponse);
360 sqlCallbacks.Update = new Interop.DataControl.SqlUpdateResponseCallback(UpdateResponse);
361 sqlCallbacks.Delete = new Interop.DataControl.SqlDeleteResponseCallback(DeleteResponse);
362 ret = Interop.DataControl.RegisterSqlResponseCallback(handle, ref sqlCallbacks, IntPtr.Zero);
363 if (ret != ResultType.Success)
365 Log.Error(LogTag, "Registering the sql callback function is failed : " + ret);
369 _sqlResponseCallbacks.Add(providerId, sqlCallbacks);
370 sqlRegistered = true;
373 sqlBulkCallbacks = new Interop.DataControl.SqlBulkInsertResponseCallback(BulkInsertResponse);
374 ret = Interop.DataControl.RegisterSqlBulkResponseCallback(handle, sqlBulkCallbacks, IntPtr.Zero);
375 if (ret != ResultType.Success)
377 Log.Error(LogTag, "Registering the sql bulk callback function is failed : " + ret);
381 _sqlBulkResponseCallback.Add(providerId, sqlBulkCallbacks);
384 mapCallbacks.Add = new Interop.DataControl.MapAddResponseCallback(MapAddResponse);
385 mapCallbacks.Set = new Interop.DataControl.MapSetResponseCallback(MapSetResponse);
386 mapCallbacks.Get = new Interop.DataControl.MapGetResponseCallback(MapGetResponse);
387 mapCallbacks.Remove = new Interop.DataControl.MapRemoveResponseCallback(MapRemoveResponse);
388 ret = Interop.DataControl.RegisterMapResponse(handle, ref mapCallbacks, IntPtr.Zero);
390 if (ret != ResultType.Success)
392 Log.Error(LogTag, "Registering the map callback function is failed : " + ret);
396 _mapResponseCallbacks.Add(providerId, mapCallbacks);
397 mapRegistered = true;
400 mapBulkCallbacks = new Interop.DataControl.MapBulkAddResponseCallback(MapBulkAddResponse);
401 ret = Interop.DataControl.RegisterMapBulkResponseCallback(handle, mapBulkCallbacks, IntPtr.Zero);
402 if (ret != ResultType.Success)
404 Log.Error(LogTag, "Registering the map bulk callback function is failed : " + ret);
408 _mapBulkResponseCallback.Add(providerId, mapBulkCallbacks);
411 if (!mapRegistered && !sqlRegistered)
413 ErrorFactory.ThrowException(ret, true, "Registering the response callback function is failed");
416 _reqProviderList.Add(providerId, 1);
419 internal static void UnregisterCallback(Interop.DataControl.SafeDataControlHandle handle, string providerId)
423 _reqProviderList[providerId]--;
424 count = _reqProviderList[providerId];
427 _reqProviderList.Remove(providerId);
429 _mapResponseCallbacks.Remove(providerId);
430 Interop.DataControl.UnregisterMapResponse(handle);
432 _mapBulkResponseCallback.Remove(providerId);
433 Interop.DataControl.UnregisterMapBulkResponseCallback(handle);
435 _sqlResponseCallbacks.Remove(providerId);
436 Interop.DataControl.UnregisterSqlResponseCallback(handle);
438 _sqlBulkResponseCallback.Remove(providerId);
439 Interop.DataControl.UnregisterSqlBulkResponseCallback(handle);
446 /// Sends the insert request to the provider application.
448 /// <remarks>The OnInsertResult will recieve the result of this API.</remarks>
449 /// <param name="insertData">The insert data.</param>
450 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
451 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
452 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
453 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
454 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
455 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
456 public void Insert(Bundle insertData)
461 if (insertData == null || insertData.SafeBundleHandle.IsInvalid)
463 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "insertData");
467 ret = Interop.DataControl.Insert(_handle, insertData.SafeBundleHandle, out reqId);
468 _lock.ReleaseMutex();
469 if (ret != ResultType.Success)
471 ErrorFactory.ThrowException(ret, false, "Insert");
474 CallbackManager.RegisterReqId(reqId, this);
478 /// Sends the select request to the provider application.
480 /// <remarks>The OnSelectResult will recieve the result of this API.</remarks>
481 /// <param name="columnList">Select the target column list.</param>
482 /// <param name="where">The Where statement for the select query.</param>
483 /// <param name="order">The Order statement for the select query.</param>
484 /// <param name="pageNumber">Select the target page number.</param>
485 /// <param name="countPerPage">Select the row count per page.</param>
486 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
487 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied..</exception>
488 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
489 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
490 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
491 public void Select(string[] columnList, string where, string order, int pageNumber = 1, int countPerPage = 20)
495 if (columnList == null || columnList.Length == 0)
497 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "column_list");
500 for (i = 0; i < columnList.Length; i++)
502 if (string.IsNullOrEmpty(columnList[i]))
504 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "column_list index " + i.ToString());
509 ret = Interop.DataControl.Select(_handle, columnList, columnList.Length, where, order, pageNumber, countPerPage, out reqId);
510 _lock.ReleaseMutex();
511 if (ret != ResultType.Success)
513 ErrorFactory.ThrowException(ret, false, "Select");
515 Log.Info(LogTag, "select end. " + reqId.ToString());
517 CallbackManager.RegisterReqId(reqId, this);
521 /// Sends the delete request to the provider application.
523 /// <remarks>The OnDeleteResult will recieve the result of this API</remarks>
524 /// <param name="where">The Where statement for the delete query.</param>
525 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
526 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
527 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
528 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
529 public void Delete(string where)
535 ret = Interop.DataControl.Delete(_handle, where, out reqId);
536 _lock.ReleaseMutex();
537 if (ret != ResultType.Success)
539 ErrorFactory.ThrowException(ret, false, "Delete");
542 CallbackManager.RegisterReqId(reqId, this);
546 /// Sends the update request to the provider application.
548 /// <remarks>The OnUpdateResult will recieve result of this API.</remarks>
549 /// <param name="updateData">The update data.</param>
550 /// <param name="where">The Where statement for the query.</param>
551 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
552 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
553 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
554 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
555 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
556 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
557 public void Update(Bundle updateData, string where)
562 if (updateData == null || updateData.SafeBundleHandle.IsInvalid)
564 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "insertData");
567 if (string.IsNullOrEmpty(where))
569 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "where");
573 ret = Interop.DataControl.Update(_handle, updateData.SafeBundleHandle, where, out reqId);
574 _lock.ReleaseMutex();
575 if (ret != ResultType.Success)
577 ErrorFactory.ThrowException(ret, false, "Update");
580 CallbackManager.RegisterReqId(reqId, this);
584 /// Sends the bulk insert request to the provider application.
586 /// <remarks>The OnBulkInsertResult will recieve the result of this API.</remarks>
587 /// <param name="insertData">The bulk insert data.</param>
588 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
589 /// <exception cref="UnauthorizedAccessException">Thrown in case oif a permission is denied.</exception>
590 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
591 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
592 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
593 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
594 public void BulkInsert(BulkData insertData)
599 if (insertData == null || insertData.SafeBulkDataHandle.IsInvalid)
601 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "insertData");
605 ret = Interop.DataControl.BulkInsert(_handle, insertData.SafeBulkDataHandle, out reqId);
606 _lock.ReleaseMutex();
607 if (ret != ResultType.Success)
609 ErrorFactory.ThrowException(ret, false, "BulkInsert");
612 CallbackManager.RegisterReqId(reqId, this);
616 /// Sends the map add request to the provider application.
618 /// <remarks>The OnMapAddResult will recieve the result of this API.</remarks>
619 /// <param name="key">The key of the value to add.</param>
620 /// <param name="value">The value to add.</param>
621 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
622 /// <exception cref="UnauthorizedAccessException">Thrown in case of if a permission is denied.</exception>
623 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
624 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
625 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
626 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
627 public void MapAdd(string key, string value)
632 if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
634 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
638 ret = Interop.DataControl.MapAdd(_handle, key, value, out reqId);
639 _lock.ReleaseMutex();
640 if (ret != ResultType.Success)
642 ErrorFactory.ThrowException(ret, false, "MapAdd");
645 CallbackManager.RegisterReqId(reqId, this);
649 /// Sends the map get request to the provider application.
651 /// <remarks>The OnMapGetResult will recieve the result of this API.</remarks>
652 /// <param name="key">The key of the value list to obtain.</param>
653 /// <param name="pageNumber">The page number of the value set.</param>
654 /// <param name="countPerPage">The desired maximum count of the data items per page.</param>
655 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
656 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
657 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
658 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
659 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
660 public void MapGet(string key, int pageNumber = 1, int countPerPage = 20)
665 if (string.IsNullOrEmpty(key) || pageNumber <= 0 || countPerPage <= 0)
667 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
671 ret = Interop.DataControl.MapGet(_handle, key, out reqId, pageNumber, countPerPage);
672 _lock.ReleaseMutex();
673 if (ret != ResultType.Success)
675 ErrorFactory.ThrowException(ret, false, "MapGet");
678 CallbackManager.RegisterReqId(reqId, this);
682 /// Sends the map remove request to the provider application.
684 /// <remarks>The OnMapRemoveResult will recieve the result of this API.</remarks>
685 /// <param name="key">The key of the value to remove.</param>
686 /// <param name="value">The value to remove.</param>
687 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
688 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
689 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
690 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
691 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
692 public void MapRemove(string key, string value)
697 if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
699 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
703 ret = Interop.DataControl.MapRemove(_handle, key, value, out reqId);
704 _lock.ReleaseMutex();
705 if (ret != ResultType.Success)
707 ErrorFactory.ThrowException(ret, false, "MapRemove");
710 CallbackManager.RegisterReqId(reqId, this);
714 /// Sends the map set request to the provider application.
716 /// <remarks>The OnMapSetResult will recieve the result of this API.</remarks>
717 /// <param name="key">The key of the value to replace.</param>
718 /// <param name="oldValue">The value to be replaced.</param>
719 /// <param name="newValue"> The new value that replaces the existing value.</param>
720 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
721 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
722 /// <exception cref="ArgumentOutOfRangeException">Thrown when message has exceeded the maximum limit (1MB).</exception>
723 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
724 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
725 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
726 public void MapSet(string key, string oldValue, string newValue)
731 if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(oldValue) || string.IsNullOrEmpty(newValue))
733 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
737 ret = Interop.DataControl.MapSet(_handle, key, oldValue, newValue, out reqId);
738 _lock.ReleaseMutex();
739 if (ret != ResultType.Success)
741 ErrorFactory.ThrowException(ret, false, "MapSet");
744 CallbackManager.RegisterReqId(reqId, this);
748 /// Sends the map bulk add request to the provider application.
750 /// <remarks>The OnMapBulkAddResult will recieve the result of this API.</remarks>
751 /// <param name="addData">The map bulk add data.</param>
752 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
753 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
754 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
755 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
756 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
757 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
758 public void MapBulkAdd(BulkData addData)
763 if (addData == null || addData.SafeBulkDataHandle.IsInvalid)
765 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "addData");
769 ret = Interop.DataControl.BulkAdd(_handle, addData.SafeBulkDataHandle, out reqId);
770 _lock.ReleaseMutex();
771 if (ret != ResultType.Success)
773 ErrorFactory.ThrowException(ret, false, "BulkAdd");
776 CallbackManager.RegisterReqId(reqId, this);
779 private void DataChange(IntPtr handle, ChangeType type, IntPtr data, IntPtr userData)
781 OnDataChange(type, new Bundle(new SafeBundleHandle(data, false)));
784 private void DataChangeListenResult(IntPtr handle, ResultType type, int callbackId, IntPtr userData)
786 OnDataChangeListenResult(new DataChangeListenResult(type));
790 /// Listens the DataChange event.
792 /// <remarks>The OnDataChangeListenResult will recieve the result of this API.</remarks>
793 /// <remarks>If success, the OnDataChange will recieve the DataChange event.</remarks>
794 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
795 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
796 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
797 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
798 public void DataChangeListen()
802 /* Only one callback is allowed for every obejct */
803 if (_changeCallbackID > 0)
805 _lock.ReleaseMutex();
808 _dataChangeCallback = new Interop.DataControl.DataChangeCallback(DataChange);
809 _addCallbackResultCallback = new Interop.DataControl.AddCallbackResultCallback(DataChangeListenResult);
810 ret = Interop.DataControl.AddDataChangeCallback(_handle, _dataChangeCallback, IntPtr.Zero,
811 _addCallbackResultCallback , IntPtr.Zero, out _changeCallbackID);
812 _lock.ReleaseMutex();
813 if (ret != ResultType.Success)
815 ErrorFactory.ThrowException(ret, false, "DataChangeListen");
820 /// Initializes the Consumer class with the providerId and the ataId.
822 /// <param name="providerId">The DataControl Provider ID.</param>
823 /// <param name="dataId">The DataControl Data ID.</param>
824 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
825 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
826 public Consumer(string providerId, string dataId)
830 if (string.IsNullOrEmpty(providerId))
832 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "providerId");
835 if (string.IsNullOrEmpty(dataId))
837 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "dataId");
840 ret = Interop.DataControl.DataControlCreate(out _handle);
841 if (ret != ResultType.Success)
843 ErrorFactory.ThrowException(ret, false, "Creating data control handle is failed");
846 Interop.DataControl.DataControlSetProviderId(_handle, providerId);
847 Interop.DataControl.DataControlSetDataId(_handle, dataId);
848 CallbackManager.RegisterCallback(_handle, providerId);
850 _providerID = providerId;
854 /// Destructor of the Consumer class.
862 /// Overrides this method if you want to handle the behavior when the DataChangeListen result is received.
864 protected virtual void OnDataChangeListenResult(DataChangeListenResult result)
866 Log.Info(LogTag, "The OnDataChangeListenResult is not implemented.");
870 /// Overrides this method if you want to handle the behavior when the data change event is received.
872 protected virtual void OnDataChange(ChangeType type, Bundle data)
874 Log.Info(LogTag, "The OnDataChange is not implemented.");
878 /// Overrides this method if you want to handle the behavior when the select response is received.
880 protected abstract void OnSelectResult(SelectResult result);
883 /// Overrides this method if you want to handle the behavior when the insert response is received.
885 protected abstract void OnInsertResult(InsertResult result);
888 /// Overrides this method if you want to handle the behavior when the update response is received.
890 protected abstract void OnUpdateResult(UpdateResult result);
893 /// Overrides this method if want to handle the behavior when the delete response is received.
895 protected abstract void OnDeleteResult(DeleteResult result);
897 /// Overrides this method if you want to handle the behavior when the BulkInsert response is received.
899 protected virtual void OnBulkInsertResult(BulkInsertResult result)
901 Log.Info(LogTag, "The OnBulkInsertResult is not implemented.");
905 /// Overrides this method if you want to handle the behavior when the map get response is received.
907 protected virtual void OnMapGetResult(MapGetResult result)
909 Log.Info(LogTag, "The OnMapGetResult is not implemented.");
913 /// Overrides this method if you want to handle the behavior when the map add response is received.
915 protected virtual void OnMapAddResult(MapAddResult result)
917 Log.Info(LogTag, "The OnMapAddResult is not implemented.");
921 /// Overrides this method if you want to handle the behavior when the map set response is received.
923 protected virtual void OnMapSetResult(MapSetResult result)
925 Log.Info(LogTag, "The OnMapSetResult is not implemented.");
929 /// Overrides this method if you want to handle the behavior when the map remove response is received.
931 protected virtual void OnMapRemoveResult(MapRemoveResult result)
933 Log.Info(LogTag, "The OnMapRemoveResult is not implemented.");
937 /// Overrides this method if you want to handle the behavior when the BulkAdd response is received.
939 protected virtual void OnMapBulkAddResult(MapBulkAddResult result)
941 Log.Info(LogTag, "The OnMapBulkAddResult is not implemented.");
945 /// Releases the unmanaged resources used by the Consumer class specifying whether to perform a normal dispose operation.
947 /// <param name="disposing">true for a normal dispose operation; false to finalize the handle.</param>
948 protected virtual void Dispose(bool disposing)
952 if (_changeCallbackID > 0)
954 Interop.DataControl.RemoveDataChangeCallback(_handle, _changeCallbackID);
957 CallbackManager.UnregisterCallback(_handle, _providerID);
964 GC.SuppressFinalize(this);
969 /// Releases all resources used by the Consumer class.
971 public void Dispose()