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