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