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