30c95b2b05c886b55cbdc9df313b95cbcd50f45c
[platform/core/csapi/tizenfx.git] / src / Tizen.Pims.Calendar / Tizen.Pims.Calendar / CalendarDatabase.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.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Diagnostics.CodeAnalysis;
21
22 namespace Tizen.Pims.Calendar
23 {
24     /// <summary>
25     /// Delegate for detecting the calendar database changes.
26     /// </summary>
27     /// <param name="uri">The record uri</param>
28     /// <remarks>
29     /// The delegate must be registered using AddDBChangedDelegate.
30     /// It's invoked when the designated view changes.
31     /// </remarks>
32     public delegate void CalendarDBChanged(string uri);
33
34     /// <summary>
35     /// CalendarDatabase provides methods to manage calendar information from/to the database.
36     /// </summary>
37     /// <remarks>
38     /// This class allows user to access/create/update db operations for calendar information.
39     /// </remarks>
40     public class CalendarDatabase
41     {
42         private Object thisLock = new Object();
43         private Dictionary<string, CalendarDBChanged> _callbackMap = new Dictionary<string, CalendarDBChanged>();
44         private Dictionary<string, Interop.Database.DBChangedCallback> _delegateMap = new Dictionary<string, Interop.Database.DBChangedCallback>();
45         private Interop.Database.DBChangedCallback _dbChangedDelegate;
46
47         internal CalendarDatabase()
48         {
49             ///To be created in CalendarManager only
50         }
51
52         /// <summary>
53         /// The calendar database version.
54         /// </summary>
55         /// <value>The current calendar database version.</value>
56         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
57         public int Version
58         {
59             get
60             {
61                 int version = -1;
62                 int error = Interop.Database.GetCurrentVersion(out version);
63                 if (CalendarError.None != (CalendarError)error)
64                 {
65                     Log.Error(Globals.LogTag, "Version Failed with error " + error);
66                 }
67                 return version;
68             }
69         }
70
71         /// <summary>
72         /// Gets last successful changed calendar database version on the current connection.
73         /// </summary>
74         /// <returns>The last successful changed calendar database version on the current connection</returns>
75         /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
76         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
77         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
78         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
79         /// <value>The last successful changed calendar database version on the current connection.</value>
80         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
81         public int LastChangeVersion
82         {
83             get
84             {
85                 int version = -1;
86                 int error = Interop.Database.GetLastChangeVersion(out version);
87                 if (CalendarError.None != (CalendarError)error)
88                 {
89                     Log.Error(Globals.LogTag, "LastChangeVersion Failed with error " + error);
90                 }
91                 return version;
92             }
93         }
94
95         /// <summary>
96         /// Inserts a record into the calendar database.
97         /// </summary>
98         /// <param name="record">The record to be inserted</param>
99         /// <returns>The ID of inserted record</returns>
100         /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
101         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
102         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
103         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
104         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
105         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
106         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
107         public int Insert(CalendarRecord record)
108         {
109             int id = -1;
110             int error = Interop.Database.Insert(record._recordHandle, out id);
111             if (CalendarError.None != (CalendarError)error)
112             {
113                 Log.Error(Globals.LogTag, "Insert Failed with error " + error);
114                 throw CalendarErrorFactory.GetException(error);
115             }
116             return id;
117         }
118
119         /// <summary>
120         /// Gets a record from the calendar database.
121         /// </summary>
122         /// <param name="viewUri">The view URI of a record</param>
123         /// <param name="recordId">The record ID</param>
124         /// <returns>
125         /// The record associated with the record ID
126         /// </returns>
127         /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
128         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
129         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
130         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
131         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
132         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
133         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
134         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
135         public CalendarRecord Get(string viewUri, int recordId)
136         {
137             IntPtr handle;
138             int error = Interop.Database.Get(viewUri, recordId, out handle);
139             if (CalendarError.None != (CalendarError)error)
140             {
141                                 if (CalendarError.DBNotFound == (CalendarError)error)
142                                 {
143                                         Log.Error(Globals.LogTag, "No data" + error);
144                                         return null;
145                                 }
146                                 Log.Error(Globals.LogTag, "Get Failed with error " + error);
147                                 throw CalendarErrorFactory.GetException(error);
148             }
149             return new CalendarRecord(handle);
150         }
151
152         /// <summary>
153         /// Updates a record in the calendar database.
154         /// </summary>
155         /// <param name="record">The record to be updated</param>
156         /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
157         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
158         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
159         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
160         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
161         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
162         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
163         public void Update(CalendarRecord record)
164         {
165             int error = Interop.Database.Update(record._recordHandle);
166             if (CalendarError.None != (CalendarError)error)
167             {
168                 Log.Error(Globals.LogTag, "Update Failed with error " + error);
169                 throw CalendarErrorFactory.GetException(error);
170             }
171         }
172
173         /// <summary>
174         /// Deletes a record from the calendar database with related child records.
175         /// </summary>
176         /// <param name="viewUri">The view URI of a record</param>
177         /// <param name="recordId">The record ID to be deleted</param>
178         /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
179         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
180         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
181         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
182         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
183         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
184         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
185         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
186         public void Delete(string viewUri, int recordId)
187         {
188             int error = Interop.Database.Delete(viewUri, recordId);
189             if (CalendarError.None != (CalendarError)error)
190             {
191                 Log.Error(Globals.LogTag, "Delete Failed with error " + error);
192                 throw CalendarErrorFactory.GetException(error);
193             }
194         }
195
196         /// <summary>
197         /// Replaces a record in the calendar database.
198         /// </summary>
199         /// <param name="record">The record to be replaced</param>
200         /// <param name="id">the record id</param>
201         /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
202         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
203         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
204         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
205         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
206         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
207         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
208         public void Replace(CalendarRecord record, int id)
209         {
210             int error = Interop.Database.Replace(record._recordHandle, id);
211             if (CalendarError.None != (CalendarError)error)
212             {
213                 Log.Error(Globals.LogTag, "Replace Failed with error " + error);
214                 throw CalendarErrorFactory.GetException(error);
215             }
216         }
217
218         /// <summary>
219         /// Retrieves all records as a list.
220         /// </summary>
221         /// <param name="viewUri">The view URI to get records from</param>
222         /// <param name="offset">The index from which results are received</param>
223         /// <param name="limit">The maximum number of results(value 0 is used for all records)</param>
224         /// <returns>
225         /// The record list
226         /// </returns>
227         /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
228         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
229         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
230         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
231         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
232         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
233         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
234         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
235         public CalendarList GetAll(string viewUri, int offset, int limit)
236         {
237             IntPtr handle;
238             int error = Interop.Database.GetAllRecords(viewUri, offset, limit, out handle);
239             if (CalendarError.None != (CalendarError)error)
240             {
241                 Log.Error(Globals.LogTag, "GetAll Failed with error " + error);
242                 throw CalendarErrorFactory.GetException(error);
243             }
244             return new CalendarList(handle);
245         }
246
247         /// <summary>
248         /// Retrieves records using a query.
249         /// </summary>
250         /// <param name="query">The query used to filter results</param>
251         /// <param name="offset">The index from which results are received</param>
252         /// <param name="limit">The maximum number of results(value 0 is used for all records)</param>
253         /// <returns>
254         /// CalendarList
255         /// </returns>
256         /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
257         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
258         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
259         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
260         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
261         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
262         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
263         public CalendarList GetRecordsWithQuery(CalendarQuery query, int offset, int limit)
264         {
265             IntPtr handle;
266             int error = Interop.Database.GetRecords(query._queryHandle, offset, limit, out handle);
267             if (CalendarError.None != (CalendarError)error)
268             {
269                 Log.Error(Globals.LogTag, "GetAllWithQuery Failed with error " + error);
270                 throw CalendarErrorFactory.GetException(error);
271             }
272             return new CalendarList(handle);
273         }
274
275         /// <summary>
276         /// Inserts multiple records into the calendar database as a batch operation.
277         /// </summary>
278         /// <param name="list">The record list</param>
279         /// <returns>
280         /// The inserted record id array
281         /// </returns>
282         /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
283         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
284         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
285         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
286         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
287         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
288         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
289         public int[] Insert(CalendarList list)
290         {
291             IntPtr ids;
292             int count;
293             int error = Interop.Database.InsertRecords(list._listHandle, out ids, out count);
294             if (CalendarError.None != (CalendarError)error)
295             {
296                 Log.Error(Globals.LogTag, "Insert Failed with error " + error);
297                 throw CalendarErrorFactory.GetException(error);
298             }
299             int[] idArr = new int[count];
300             Marshal.Copy(ids, idArr, 0, count);
301
302             return idArr;
303         }
304
305         /// <summary>
306         /// Updates multiple records into the calendar database as a batch operation.
307         /// </summary>
308         /// <param name="list">The record list</param>
309         /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
310         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
311         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
312         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
313         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
314         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
315         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
316         public void Update(CalendarList list)
317         {
318             int error = Interop.Database.UpdateRecords(list._listHandle);
319             if (CalendarError.None != (CalendarError)error)
320             {
321                 Log.Error(Globals.LogTag, "Update Failed with error " + error);
322                 throw CalendarErrorFactory.GetException(error);
323             }
324         }
325
326         /// <summary>
327         /// Deletes multiple records with related child records from the calendar database as a batch operation.
328         /// </summary>
329         /// <param name="viewUri">The view URI of the records to delete</param>
330         /// <param name="idArray">The record IDs to delete</param>
331         /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
332         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
333         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
334         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
335         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
336         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
337         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
338         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
339         public void Delete(string viewUri, int[] idArray)
340         {
341             int error = Interop.Database.DeleteRecords(viewUri, idArray, idArray.Length);
342             if (CalendarError.None != (CalendarError)error)
343             {
344                 Log.Error(Globals.LogTag, "Delete Failed with error " + error);
345                 throw CalendarErrorFactory.GetException(error);
346             }
347         }
348
349         /// <summary>
350         /// Deletes multiple records with related child records from the calendar database as a batch operation.
351         /// </summary>
352         /// <param name="list">The record list</param>
353         /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
354         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
355         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
356         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
357         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
358         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
359         public void Delete(CalendarList list)
360         {
361             CalendarRecord record = null;
362             if (list.Count <= 0)
363                 return;
364
365             int[] ids = new int[list.Count];
366             int i;
367             uint propertyId = 0;
368             list.MoveFirst();
369             for (i = 0; i < list.Count; i++)
370             {
371                 record = list.GetCurrentRecord();
372                 if (0 == propertyId)
373                 {
374                     if (0 == String.Compare(CalendarViews.Book.Uri, record.Uri))
375                         propertyId = CalendarViews.Book.Id;
376                     else if (0 == String.Compare(CalendarViews.Event.Uri, record.Uri))
377                         propertyId = CalendarViews.Event.Id;
378                     else if (0 == String.Compare(CalendarViews.Todo.Uri, record.Uri))
379                         propertyId = CalendarViews.Todo.Id;
380                     else if (0 == String.Compare(CalendarViews.Timezone.Uri, record.Uri))
381                         propertyId = CalendarViews.Timezone.Id;
382                     else if (0 == String.Compare(CalendarViews.Extended.Uri, record.Uri))
383                         propertyId = CalendarViews.Extended.Id;
384                     else
385                     {
386                         Log.Error(Globals.LogTag, "Invalid uri [" + record.Uri + "]");
387                         continue;
388                     }
389                 }
390                 ids[i] = record.Get<int>(propertyId);
391                 list.MoveNext();
392             }
393             Delete(record.Uri, ids);
394         }
395
396         /// <summary>
397         /// Replaces multiple records in the calendar database as a batch operation.
398         /// </summary>
399         /// <param name="list">The record list</param>
400         /// <param name="idArray">The record IDs</param>
401         /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
402         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
403         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
404         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
405         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
406         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
407         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
408         public void Replace(CalendarList list, int[] idArray)
409         {
410             int error = Interop.Database.ReplaceRecords(list._listHandle, idArray, idArray.Length);
411             if (CalendarError.None != (CalendarError)error)
412             {
413                 Log.Error(Globals.LogTag, "Replace Failed with error " + error);
414                 throw CalendarErrorFactory.GetException(error);
415             }
416         }
417
418         /// <summary>
419         /// Retrieves records with the given calendar database version.
420         /// </summary>
421         /// <param name="viewUri">The view URI to get records from</param>
422         /// <param name="BookId">The calendar book ID to filter</param>
423         /// <param name="calendarDBVersion">The calendar database version</param>
424         /// <param name="currentDBVersion">The current calendar database version</param>
425         /// <returns>
426         /// The record list
427         /// </returns>
428         /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
429         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
430         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
431         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
432         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
433         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
434         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
435         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
436         public CalendarList GetChangesByVersion(string viewUri, int BookId, int calendarDBVersion, out int currentDBVersion)
437         {
438             IntPtr recordList;
439             int error = Interop.Database.GetChangesByVersion(viewUri, BookId, calendarDBVersion, out recordList, out currentDBVersion);
440             if (CalendarError.None != (CalendarError)error)
441             {
442                 Log.Error(Globals.LogTag, "GetChangesByVersion Failed with error " + error);
443                 throw CalendarErrorFactory.GetException(error);
444             }
445             return new CalendarList(recordList);
446         }
447
448         /// <summary>
449         /// Gets the record count of a specific view.
450         /// </summary>
451         /// <param name="viewUri">The view URI to get records from</param>
452         /// <returns>
453         /// The count of records
454         /// </returns>
455         /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
456         [SuppressMessage("Microsoft.Design", "CA1822:MarkMembersAsStatic")]
457         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
458         public int GetCount(string viewUri)
459         {
460             int count = -1;
461             int error = Interop.Database.GetCount(viewUri, out count);
462             if (CalendarError.None != (CalendarError)error)
463             {
464                 Log.Error(Globals.LogTag, "GetCount Failed with error " + error);
465                 throw CalendarErrorFactory.GetException(error);
466             }
467             return count;
468         }
469
470         /// <summary>
471         /// Gets the record count with a query.
472         /// </summary>
473         /// <param name="query">The query used for filtering the results</param>
474         /// <returns>
475         /// The count of records
476         /// </returns>
477         /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
478         [SuppressMessage("Microsoft.Design", "CA1822:MarkMembersAsStatic")]
479         public int GetCount(CalendarQuery query)
480         {
481             int count = -1;
482             int error = Interop.Database.GetCountWithQuery(query._queryHandle, out count);
483             if (CalendarError.None != (CalendarError)error)
484             {
485                 Log.Error(Globals.LogTag, "GetCount Failed with error " + error);
486                 throw CalendarErrorFactory.GetException(error);
487             }
488             return count;
489         }
490
491         /// <summary>
492         /// Registers a callback function to be invoked when a record changes.
493         /// </summary>
494         /// <param name="viewUri">The view URI of the record to subscribe for change notifications</param>
495         /// <param name="callback">The callback function to register</param>
496         /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
497         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
498         public void AddDBChangedDelegate(string viewUri, CalendarDBChanged callback)
499         {
500             Log.Debug(Globals.LogTag, "AddDBChangedDelegate");
501
502             _dbChangedDelegate = (string uri, IntPtr userData) =>
503             {
504                 _callbackMap[uri](uri);
505             };
506             int error = Interop.Database.AddChangedCallback(viewUri, _dbChangedDelegate, IntPtr.Zero);
507             if (CalendarError.None != (CalendarError)error)
508             {
509                 Log.Error(Globals.LogTag, "AddDBChangedDelegate Failed with error " + error);
510                 throw CalendarErrorFactory.GetException(error);
511             }
512             _callbackMap[viewUri] += callback;
513             _delegateMap[viewUri] = _dbChangedDelegate;
514         }
515
516         /// <summary>
517         /// Deregisters a callback function.
518         /// </summary>
519         /// <param name="viewUri">The view URI of the record to subscribe for change notifications</param>
520         /// <param name="callback">The callback function to register</param>
521         /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
522         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
523         public void RemoveDBChangedDelegate(string viewUri, CalendarDBChanged callback)
524         {
525             Log.Debug(Globals.LogTag, "RemoveDBChangedDelegate");
526
527             int error = Interop.Database.RemoveChangedCallback(viewUri, _delegateMap[viewUri], IntPtr.Zero);
528             if (CalendarError.None != (CalendarError)error)
529             {
530                 Log.Error(Globals.LogTag, "RemoveDBChangedDelegate Failed with error " + error);
531                 throw CalendarErrorFactory.GetException(error);
532             }
533             _callbackMap[viewUri] -= callback;
534             _delegateMap.Remove(viewUri);
535         }
536
537         /// <summary>
538         /// Link a record to another record.
539         /// </summary>
540         /// <param name="baseId">The base record ID</param>
541         /// <param name="recordId">The record ID to link to</param>
542         /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
543         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
544         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
545         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
546         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
547         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
548         [SuppressMessage("Microsoft.Design", "CA1822:MarkMembersAsStatic")]
549         public void LinkRecord(int baseId, int recordId)
550         {
551             Log.Debug(Globals.LogTag, "LinkRecord");
552             int error = Interop.Database.LinkRecord(baseId, recordId);
553             if (CalendarError.None != (CalendarError)error)
554             {
555                 Log.Error(Globals.LogTag, "LinkRecor Failed with error " + error);
556                 throw CalendarErrorFactory.GetException(error);
557             }
558         }
559
560         /// <summary>
561         /// Unlink a record from base record.
562         /// </summary>
563         /// <param name="recordId">The record ID to unlink</param>
564         /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
565         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
566         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
567         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
568         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
569         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
570         [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
571         public void UnlinkRecord(int recordId)
572         {
573             Log.Debug(Globals.LogTag, "UnlinkRecord");
574             int error = Interop.Database.UnlinkRecord(recordId);
575             if (CalendarError.None != (CalendarError)error)
576             {
577                 Log.Error(Globals.LogTag, "UnlinkRecor Failed with error " + error);
578                 throw CalendarErrorFactory.GetException(error);
579             }
580         }
581     }
582 }