Update change log and spec for wrt-plugins-tizen_0.4.70
[framework/web/wrt-plugins-tizen.git] / src / Calendar / JSCalendar.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18
19 #include "ICalendar.h"
20 #include "EventId.h"
21 #include "CalendarFactory.h"
22
23 #include <CommonsJavaScript/PrivateObject.h>
24 #include <CommonsJavaScript/Converter.h>
25 #include <CommonsJavaScript/JSUtils.h>
26 #include <CommonsJavaScript/Utils.h>
27 #include <CommonsJavaScript/Validator.h>
28 #include <CommonsJavaScript/ScopedJSStringRef.h>
29 #include <JSWebAPIErrorFactory.h>
30 #include <SecurityExceptions.h>
31 #include <TimeUtilConverter.h>
32 #include <TimeTracer.h>
33 #include <Logger.h>
34 #include <Export.h>
35 #include <GlobalContextManager.h>
36
37 #include "JSCalendarManager.h"
38 #include "CalendarConverter.h"
39 #include "JSCalendar.h"
40 #include "JSCalendarEvent.h"
41 #include "JSCalendarTask.h"
42 #include "JSCalendarItemProperties.h"
43 #include "JSCalendarEventId.h"
44 #include "plugin_config.h"
45 #include "CalendarResponseDispatcher.h"
46 #include "CalendarMultiCallback.h"
47 #include "CalendarFilterConverter.h"
48
49 #include "CalendarListenerManager.h"
50
51 using namespace WrtDeviceApis::Commons;
52 using namespace WrtDeviceApis::CommonsJavaScript;
53 using namespace DeviceAPI::Common;
54
55 #define TIZEN_CALENDAR_ATTRIBUTENAME "Calendar"
56
57 namespace DeviceAPI {
58 namespace Calendar {
59
60 using WrtDeviceApis::Commons::UnknownException;
61 using WrtDeviceApis::Commons::NotFoundException;
62
63 JSClassDefinition JSCalendar::m_classInfo = {
64     0,
65     kJSClassAttributeNone,
66     TIZEN_CALENDAR_ATTRIBUTENAME,
67     NULL,
68     m_property,
69     m_function,
70     initialize,
71     finalize,
72     NULL, //HasProperty,
73     NULL, //GetProperty,
74     NULL, //SetProperty,
75     NULL, //DeleteProperty,
76     NULL, //GetPropertyNames,
77     NULL, //CallAsFunction,
78     NULL, //CallAsConstructor,
79     NULL, //HasInstance,
80     NULL //ConvertToType
81 };
82
83 JSStaticValue JSCalendar::m_property[] = {
84     { TIZEN_CALENDAR_PROPERTY_NAME, JSCalendar::getPropertyName, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
85     /*{ TIZEN_CALENDAR_PROPERTY_ACCOUNT_SERVICE_ID, JSCalendar::getPropertyAccountServiceId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },*/
86     { TIZEN_CALENDAR_PROPERTY_ID, JSCalendar::getPropertyId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
87     { 0, 0, 0, 0 }
88 };
89
90 JSStaticFunction JSCalendar::m_function[] = {
91     { CALENDAR_FUNCTION_API_ADD, add, kJSPropertyAttributeNone },
92     { CALENDAR_FUNCTION_API_ADD_BATCH, addBatch, kJSPropertyAttributeNone },
93     { CALENDAR_FUNCTION_API_UPDATE, update, kJSPropertyAttributeNone },
94     { CALENDAR_FUNCTION_API_UPDATE_BATCH, updateBatch, kJSPropertyAttributeNone },
95     { CALENDAR_FUNCTION_API_REMOVE, remove, kJSPropertyAttributeNone },
96     { CALENDAR_FUNCTION_API_REMOVE_BATCH, removeBatch, kJSPropertyAttributeNone },
97     { CALENDAR_FUNCTION_API_FIND, find, kJSPropertyAttributeNone },
98     { CALENDAR_FUNCTION_API_ADD_CHANGE_LISTENER, addChangeListener, kJSPropertyAttributeNone },
99     { CALENDAR_FUNCTION_API_REMOVE_CHANGE_LISTENER, removeChangeListener, kJSPropertyAttributeNone },
100     { CALENDAR_FUNCTION_API_GET, get, kJSPropertyAttributeNone },
101
102     { 0, 0, 0 }
103 };
104
105 JSClassRef JSCalendar::m_jsClassRef = JSClassCreate(JSCalendar::getClassInfo());
106
107 void JSCalendar::initialize(JSContextRef context, JSObjectRef object)
108 {
109     if (!JSObjectGetPrivate(object)) {
110         LoggerD("Create calendar private object.");
111         ICalendarPtr calendar = CalendarFactory::getInstance().createCalendarObject();
112         CalendarPrivObject *priv = new CalendarPrivObject(context, calendar);
113         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
114             delete priv;
115         }
116     } else {
117         LoggerD("Private object already set.");
118     }
119 }
120
121 void JSCalendar::finalize(JSObjectRef object)
122 {
123     CalendarPrivObject *priv = static_cast<CalendarPrivObject*>(JSObjectGetPrivate(object));
124     if (priv) {
125         delete priv;
126         JSObjectSetPrivate(object, NULL);
127     }
128 }
129
130 const JSClassRef DLL_EXPORT JSCalendar::getClassRef()
131 {
132     if (!m_jsClassRef) {
133         m_jsClassRef = JSClassCreate(&m_classInfo);
134     }
135     return m_jsClassRef;
136 }
137
138 const JSClassDefinition* JSCalendar::getClassInfo()
139 {
140     return &m_classInfo;
141 }
142
143 JSValueRef JSCalendar::add(JSContextRef context,
144         JSObjectRef object,
145         JSObjectRef thisObject,
146         size_t argumentCount,
147         const JSValueRef arguments[],
148         JSValueRef* exception)
149 {
150         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
151     CalendarPrivObject *privateObject =
152         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
153
154
155     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_ADD);
156
157     Try
158     {
159         if (!privateObject) {
160             ThrowMsg(ConversionException, "Object is null.");
161         }
162
163         ICalendarPtr calendar = getCalendar(context, thisObject, NULL);
164
165         if (argumentCount<1) {
166             ThrowMsg(ConversionException, "Wrong parameter type.");
167         }
168
169         if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
170             if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarEvent::getClassRef())) {
171                 ThrowMsg(ConversionException, "Wrong first parameter type.");
172             }
173         } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
174             if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarTask::getClassRef())) {
175                 ThrowMsg(ConversionException, "Wrong first parameter type.");
176             }
177         } else {
178             ThrowMsg(ConversionException, "Wrong calendar type.");
179         }
180
181         CalendarConverter converter(context);
182
183         CalendarEventPtr item = converter.toItem(arguments[0], true);
184         if (!item) {
185             ThrowMsg(ConversionException, "Parameter conversion failed.");
186         }
187
188         IEventAddEventPtr dplEvent(new IEventAddEvent());
189         dplEvent->setEvent(item);
190         dplEvent->setForSynchronousCall();
191         calendar->addEvent(dplEvent);
192
193         if (dplEvent->getResult()) {
194             LoggerD("Add result successful.");
195                         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
196             return JSValueMakeUndefined(context);
197         } else {
198             ThrowMsg(UnknownException, "Adding failed by unknown reason.");
199         }
200     }
201     Catch(UnsupportedException)
202     {
203                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
204         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
205     }
206     Catch(InvalidArgumentException)
207     {
208                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
209         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
210     }
211     Catch(ConversionException)
212     {
213                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
214         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
215     }
216     Catch(Exception)
217     {
218                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
219         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
220     }
221 }
222
223 JSValueRef JSCalendar::addBatch(JSContextRef context,
224         JSObjectRef object,
225         JSObjectRef thisObject,
226         size_t argumentCount,
227         const JSValueRef arguments[],
228         JSValueRef* exception)
229 {
230         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
231     CalendarPrivObject *privateObject =
232         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
233
234     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_ADD_BATCH);
235
236     Try
237     {
238         if (!privateObject) {
239             ThrowMsg(ConversionException, "Object is null.");
240         }
241
242         ICalendarPtr calendar = getCalendar(context, thisObject, NULL);
243
244         JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
245         CalendarConverter converter(context);
246
247         if (argumentCount<1) {
248             ThrowMsg(ConversionException, "Wrong parameter type.");
249         }
250
251         CalendarEventListPtr events;
252         if (!JSIsArrayValue(context, arguments[0])) {
253             ThrowMsg(ConversionException, "Wrong first parameter type.");
254         }
255         events = converter.toVectorOfItemsFromDictionary(arguments[0], true);
256         if (!events) {
257             ThrowMsg(ConversionException, "First parameter conversion failed.");
258         }
259
260         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
261         if (argumentCount>=2) {
262             cbm->setOnSuccess(converter.toFunctionOrNull(arguments[1]));
263         }
264         if (argumentCount>=3) {
265             cbm->setOnError(converter.toFunctionOrNull(arguments[2]));
266         }
267
268                 cbm->setObject(thisObject);
269
270         LoggerD("Proceed the event to the platform.");
271
272         IEventAddEventsPtr dplEvent(new IEventAddEvents());
273         dplEvent->setCalendarType(calendar->getType());
274         dplEvent->setEvents(events);
275         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
276         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
277         dplEvent->copyAceCheckAccessFunction(privateObject);
278         calendar->addEvents(dplEvent);
279
280                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
281         return JSValueMakeUndefined(context);
282     }
283     Catch(UnsupportedException)
284     {
285                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
286         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
287     }
288     Catch(InvalidArgumentException)
289     {
290                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
291         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
292     }
293     Catch(ConversionException)
294     {
295                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
296         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
297     }
298     Catch(Exception)
299     {
300                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
301         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
302     }
303 }
304
305 JSValueRef JSCalendar::update(JSContextRef context,
306         JSObjectRef object,
307         JSObjectRef thisObject,
308         size_t argumentCount,
309         const JSValueRef arguments[],
310         JSValueRef* exception)
311 {
312         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
313     CalendarPrivObject *privateObject =
314         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
315
316     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_UPDATE);
317
318     Try
319     {
320         if (!privateObject) {
321             ThrowMsg(ConversionException, "Object is null.");
322         }
323
324         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
325
326         if (argumentCount<1) {
327             ThrowMsg(ConversionException, "Wrong parameter type.");
328         }
329
330         if (argumentCount>=1) {
331             if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarEvent::getClassRef()) &&
332             !JSValueIsObjectOfClass(context, arguments[0], JSCalendarTask::getClassRef())) {
333                 ThrowMsg(ConversionException, "Wrong first parameter type.");
334             }
335         } else {
336             ThrowMsg(ConversionException, "Wrong first parameter type.");
337         }
338
339         CalendarConverter converter(context);
340
341         CalendarEventPtr item = converter.toItem(arguments[0], true);
342         if (!item) {
343             ThrowMsg(ConversionException, "Parameter conversion failed.");
344         }
345
346         bool updateAllInstances = true; // Set the default value.
347         if (argumentCount>=2) {
348             updateAllInstances = converter.toBool(arguments[1]);
349         }
350
351         IEventUpdateEventPtr dplEvent(new IEventUpdateEvent());
352         dplEvent->setEvent(item);
353         dplEvent->setUpdateAllInstances(updateAllInstances);
354         dplEvent->setForSynchronousCall();
355         calendar->updateEvent(dplEvent);
356
357         if (dplEvent->getResult()) {
358                                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
359             return JSValueMakeUndefined(context);
360         } else {
361             ThrowMsg(UnknownException, "Updating failed by unknown reason.");
362         }
363     }
364     Catch(UnsupportedException)
365     {
366                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
367         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
368     }
369     Catch(InvalidArgumentException)
370     {
371                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
372         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
373     }
374     Catch(ConversionException)
375     {
376                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
377         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
378     }
379     Catch (NotFoundException)
380     {
381                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
382         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
383     }
384     Catch(Exception)
385     {
386                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
387         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
388     }
389 }
390
391 JSValueRef JSCalendar::updateBatch(JSContextRef context,
392         JSObjectRef object,
393         JSObjectRef thisObject,
394         size_t argumentCount,
395         const JSValueRef arguments[],
396         JSValueRef* exception)
397 {
398         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
399     CalendarPrivObject *privateObject =
400         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
401
402     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_UPDATE_BATCH);
403
404     Try
405     {
406         if (!privateObject) {
407             ThrowMsg(ConversionException, "Object is null.");
408         }
409
410         JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
411
412         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
413
414         CalendarConverter converter(context);
415
416         if (argumentCount>=1) {
417             if (!JSIsArrayValue(context, arguments[0])) {
418                 ThrowMsg(ConversionException, "Wrong first parameter type.");
419             }
420         } else {
421             ThrowMsg(ConversionException, "Wrong parameter type.");
422         }
423         CalendarEventListPtr items;
424         items = converter.toVectorOfItemsFromDictionary(arguments[0], true);
425         if (!items) {
426             ThrowMsg(ConversionException, "Third parameter conversion failed.");
427         }
428
429         JSValueRef onSuccess = NULL;
430         if (argumentCount>=2) {
431             onSuccess = converter.toFunctionOrNull(arguments[1]);
432         }
433         JSValueRef onError = NULL;
434         if (argumentCount>=3) {
435             onError = converter.toFunctionOrNull(arguments[2]);
436         }
437         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, onSuccess, onError);
438
439         bool updateAllInstances = true; // Set the default value.
440         if( argumentCount>=4 ) {
441             updateAllInstances = converter.toBool(arguments[3]);
442         }
443
444                 cbm->setObject(thisObject);
445
446         LoggerD("Proceed the event to the platform.");
447
448         IEventUpdateEventsPtr dplEvent(new IEventUpdateEvents());
449         dplEvent->setEvents(items);
450         dplEvent->setUpdateAllInstances(updateAllInstances);
451         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
452         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
453         calendar->updateEvents(dplEvent);
454
455                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
456         return JSValueMakeUndefined(context);
457     }
458     Catch(UnsupportedException)
459     {
460                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
461         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
462     }
463     Catch(InvalidArgumentException)
464     {
465                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
466         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
467     }
468     Catch(ConversionException)
469     {
470                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
471         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
472     }
473     Catch (NotFoundException)
474     {
475                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
476         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
477     }
478     Catch(Exception)
479     {
480                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
481         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
482     }
483 }
484
485 JSValueRef JSCalendar::remove(JSContextRef context,
486         JSObjectRef object,
487         JSObjectRef thisObject,
488         size_t argumentCount,
489         const JSValueRef arguments[],
490         JSValueRef* exception)
491 {
492         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
493     CalendarPrivObject *privateObject =
494         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
495
496     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_REMOVE);
497
498     Try
499     {
500         if (!privateObject) {
501             ThrowMsg(ConversionException, "Object is null.");
502         }
503
504         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
505
506         EventIdPtr itemId;
507         CalendarConverter converter(context);
508
509         if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
510             if (argumentCount<1) {
511                 ThrowMsg(ConversionException, "Wrong parameter type.");
512             }
513
514             if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarEventId::getClassRef())) {
515                 ThrowMsg(ConversionException, "Wrong parameter type.");
516             }
517             itemId = JSCalendarEventId::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
518             if (!itemId) {
519                 ThrowMsg(ConversionException, "Parameter conversion failed.");
520             }
521             itemId->setCalendarType(CalendarEvent::EVENT_TYPE);
522         } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
523             EventIdPtr result( new EventId() );
524             if (argumentCount>=1) {
525                 result->setUId(converter.toString(arguments[0]));
526             }
527             itemId = result;
528             itemId->setCalendarType(CalendarEvent::TASK_TYPE);
529         } else {
530             ThrowMsg(ConversionException, "Parameter conversion failed.");
531         }
532
533         IEventDeleteEventPtr dplEvent(new IEventDeleteEvent());
534         dplEvent->setEventId(itemId);
535         dplEvent->setForSynchronousCall();
536         calendar->deleteEvent(dplEvent);
537
538         if (dplEvent->getResult()) {
539             LoggerI("Successfully deleted.");
540                                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
541             return JSValueMakeUndefined(context);
542         } else {
543             if (dplEvent->getExceptionCode() == ExceptionCodes::NotFoundException) {
544                 ThrowMsg(NotFoundException, "Item not found.");
545             } else {
546                 ThrowMsg(UnknownException, "Removing failed by unnkown reason.");
547             }
548         }
549     }
550     Catch(UnsupportedException)
551     {
552                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
553         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
554     }
555     Catch(InvalidArgumentException)
556     {
557                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
558         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
559     }
560     Catch(ConversionException)
561     {
562                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
563         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
564     }
565     Catch (NotFoundException)
566     {
567                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
568         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
569     }
570     Catch(Exception)
571     {
572                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
573         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
574     }
575 }
576
577 JSValueRef JSCalendar::removeBatch(JSContextRef context,
578         JSObjectRef object,
579         JSObjectRef thisObject,
580         size_t argumentCount,
581         const JSValueRef arguments[],
582         JSValueRef* exception)
583 {
584         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
585     CalendarPrivObject *privateObject =
586         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
587
588     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_REMOVE_BATCH);
589
590     Try
591     {
592         if (!privateObject) {
593             ThrowMsg(ConversionException, "Object is null.");
594         }
595
596         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
597         JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
598
599         CalendarConverter converter(context);
600
601         if (argumentCount>=1) {
602             if (!JSIsArrayValue(context, arguments[0])) {
603                 ThrowMsg(ConversionException, "Wrong first parameter type.");
604             }
605         } else {
606                         ThrowMsg(ConversionException, "Wrong parameter type.");
607         }
608
609         // Pick one array element to determine its type.
610         JSObjectRef objArg = converter.toJSObjectRef(arguments[0]);
611         JSValueRef element;
612         if (JSGetArrayLength(context, objArg)>0) {
613             element = JSGetArrayElement(context, objArg, 0);
614         } else {
615             ThrowMsg(ConversionException, "Wrong first parameter type.");
616         }
617
618         EventIdListPtr itemIds;
619         if (JSValueIsObjectOfClass(context, element, JSCalendarEventId::getClassRef())) {
620             itemIds = converter.toVectorOfEventIds(arguments[0]);
621             if (!itemIds) {
622                 ThrowMsg(ConversionException, "Parameter conversion failed.");
623             } else {
624                 LoggerD("Array length: "<<itemIds->size());
625                 }
626         } else if (JSValueIsString(context, element)) {
627             std::vector<std::string> idStrings = converter.toVectorOfStrings(arguments[0]);
628             EventIdListPtr result(new EventIdList());
629             for (unsigned int i=0; i<idStrings.size(); i++) {
630                 EventIdPtr idPtr( new EventId() );
631                 idPtr->setUId(idStrings[i]);
632                 idPtr->setCalendarType(CalendarEvent::TASK_TYPE);
633                 result->push_back(idPtr);
634             }
635             itemIds = result;
636         } else {
637             ThrowMsg(ConversionException, "Wrong parameter type.");
638         }
639
640         JSValueRef onSuccess = NULL;
641         if (argumentCount>=2) {
642             onSuccess = converter.toFunctionOrNull(arguments[1]);
643         }
644         JSValueRef onError = NULL;
645         if (argumentCount>=3) {
646             onError = converter.toFunctionOrNull(arguments[2]);
647         }
648         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, onSuccess, onError);
649
650                 cbm->setObject(thisObject);
651
652         LoggerD("Proceed the event to the platform.");
653
654         IEventDeleteEventsPtr dplEvent(new IEventDeleteEvents());
655         dplEvent->setEventIds(itemIds);
656         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
657         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
658         calendar->deleteEvents(dplEvent);
659
660                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
661         return JSValueMakeUndefined(context);
662     }
663     Catch(UnsupportedException)
664     {
665                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
666         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
667     }
668     Catch(InvalidArgumentException)
669     {
670                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
671         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
672     }
673     Catch(ConversionException)
674     {
675                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
676         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
677     }
678     Catch (NotFoundException)
679     {
680                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
681         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
682     }
683     Catch(Exception)
684     {
685                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
686         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
687     }
688 }
689
690 JSValueRef JSCalendar::find(JSContextRef context,
691         JSObjectRef object,
692         JSObjectRef thisObject,
693         size_t argumentCount,
694         const JSValueRef arguments[],
695         JSValueRef* exception)
696 {
697         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
698     CalendarPrivObject *privateObject =
699         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
700
701     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_FIND);
702
703     Try
704     {
705         if (!privateObject) {
706             ThrowMsg(ConversionException, "Object is null.");
707         }
708
709         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
710
711         JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
712
713         Validator validator(context, exception);
714         CalendarConverter converter(context);
715         CalendarFilterConverterFactory::ConverterType filterConverter =
716                         CalendarFilterConverterFactory::ConverterType(new CalendarFilterConverter(context));
717
718         if (argumentCount<1) {
719             ThrowMsg(ConversionException, "Wrong parameter type.");
720         }
721
722         JSValueRef onSuccess = NULL;
723         if (argumentCount>=1) {
724             onSuccess = converter.toFunction(arguments[0]);
725         }
726         JSValueRef onError = NULL;
727         if (argumentCount>=2) {
728             onError = converter.toFunctionOrNull(arguments[1]);
729         }
730         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, onSuccess, onError);
731
732                 cbm->setObject(thisObject);
733
734         LoggerD("Proceed the find event to the platform.");
735
736         IEventFindEventsPtr dplEvent(new IEventFindEvents());
737         dplEvent->setCalendarType(calendar->getType());
738         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
739         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
740         dplEvent->copyAceCheckAccessFunction(privateObject);
741
742         if (argumentCount>=3) {
743             if (JSValueIsObject(context, arguments[2])) {
744                 dplEvent->setGenericFilter(filterConverter->toFilter(arguments[2]));
745             } else if (JSValueIsNull(context, arguments[2])) {
746                                 LoggerD("Use default filter.");
747             } else {
748                 ThrowMsg(ConversionException, "Wrong third parameter type.");
749             }
750         }
751         if (argumentCount>=4) {
752             if (JSValueIsObject(context, arguments[3])) {
753                 // Though the sortMode is a single type, we save it in an array internally.
754                 DeviceAPI::Tizen::SortModeArrayPtr sortModes(new DeviceAPI::Tizen::SortModeArray());
755                 sortModes->push_back(filterConverter->toSortMode(arguments[3]));
756                 dplEvent->setSortModes(sortModes);
757                         } else if (JSValueIsNull(context, arguments[3])) {
758                                 LoggerD("Use default sort mode.");
759             } else {
760                 ThrowMsg(ConversionException, "Wrong fourth parameter type.");
761             }
762         }
763
764         calendar->findEvents(dplEvent);
765
766                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
767                 return JSValueMakeUndefined(context);
768     }
769     Catch(UnsupportedException)
770     {
771                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
772         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
773     }
774     Catch(InvalidArgumentException)
775     {
776                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
777         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
778     }
779     Catch(ConversionException)
780     {
781                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
782         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
783     }
784     Catch (NotFoundException)
785     {
786                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
787         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
788     }
789     Catch(Exception)
790     {
791                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
792         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
793     }
794 }
795
796 JSValueRef JSCalendar::get(JSContextRef context,
797         JSObjectRef object,
798         JSObjectRef thisObject,
799         size_t argumentCount,
800         const JSValueRef arguments[],
801         JSValueRef* exception)
802 {
803         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
804     CalendarPrivObject *privateObject =
805         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
806
807     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_GET);
808
809     Try
810     {
811         if (!privateObject) {
812             ThrowMsg(ConversionException, "Object is null.");
813         }
814
815         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
816         JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
817
818         EventIdPtr itemId;
819         CalendarConverter converter(context);
820         if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
821             if (argumentCount<1) {
822                 ThrowMsg(ConversionException, "Wrong parameter type.");
823             }
824
825             if (JSValueIsObjectOfClass(context, arguments[0], JSCalendarEventId::getClassRef())) {
826                 itemId = JSCalendarEventId::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
827                 if (!itemId) {
828                     ThrowMsg(ConversionException, "Parameter conversion failed.");
829                 }
830                 itemId->setCalendarType(CalendarEvent::EVENT_TYPE);
831             } else {
832                 ThrowMsg(ConversionException, "Wrong parameter type.");
833             }
834         } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
835             EventIdPtr result( new EventId() );
836             itemId = result;
837             if (argumentCount>=1) {
838                 itemId->setUId(converter.toString(arguments[0]));
839             }
840             itemId->setCalendarType(CalendarEvent::TASK_TYPE);
841         } else {
842             ThrowMsg(ConversionException, "Wrong calendar type.");
843         }
844
845         IEventGetPtr dplEvent(new IEventGet());
846         dplEvent->setForSynchronousCall();
847         dplEvent->setItemId(itemId);
848         calendar->get(dplEvent);
849
850         // Process the found item.
851         if (dplEvent->getResult()) {
852                         LoggerD("Successfully got an item.");
853                         if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
854                                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
855                 // Use global context for the expandRecurrence api in event.
856                     return JSCalendarEvent::createJSCalendarEvent(globalContext, dplEvent->getItem(), privateObject);
857                         } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
858                     return JSCalendarTask::createJSCalendarTask(context, dplEvent->getItem(), privateObject);
859                         } else {
860                     ThrowMsg(UnknownException, "Wrong calendar type.");
861                         }
862         } else {
863             if(ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
864                 ThrowMsg(NotFoundException, "Item not found.");
865             } else {
866                 ThrowMsg(UnknownException, "Getting an item failed by unknown reason.");
867             }
868         }
869     }
870     Catch(UnsupportedException)
871     {
872                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
873         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
874     }
875     Catch(InvalidArgumentException)
876     {
877                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
878         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
879     }
880     Catch(ConversionException)
881     {
882                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
883         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
884     }
885     Catch (NotFoundException)
886     {
887                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
888         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
889     }
890     Catch(Exception)
891     {
892                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
893         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
894     }
895 }
896
897 JSValueRef JSCalendar::addChangeListener(JSContextRef context,
898         JSObjectRef object,
899         JSObjectRef thisObject,
900         size_t argumentCount,
901         const JSValueRef arguments[],
902         JSValueRef* exception)
903 {
904         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
905     CalendarPrivObject *privateObject =
906         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
907
908     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_ADD_CHANGE_LISTENER);
909
910     Try
911     {
912         if (!privateObject) {
913             ThrowMsg(ConversionException, "Object is null.");
914         }
915
916         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
917         JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
918
919         CalendarConverter converter(context);
920
921         if (argumentCount>=1) {
922             if (!JSValueIsObject(context, arguments[0])) {
923                 ThrowMsg(ConversionException, "Wrong first parameter type.");
924             }
925         } else {
926             ThrowMsg(ConversionException, "Wrong first parameter type.");
927         }
928
929         JSObjectRef objectCallbacks = converter.toJSObjectRef(arguments[0]);
930         CalendarChangeCallback result;
931         Validator validator(context);
932
933         result.onAdded = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onitemsadded");
934         if (validator.isNullOrUndefined(result.onAdded)) {
935             result.onAdded = NULL;
936         } else if(!validator.isCallback(result.onAdded)) {
937             ThrowMsg(ConversionException, "Wrong first parameter type.");
938         }
939
940         result.onUpdated = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onitemsupdated");
941         if (validator.isNullOrUndefined(result.onUpdated)) {
942             result.onUpdated = NULL;
943         } else if(!validator.isCallback(result.onUpdated)) {
944             ThrowMsg(ConversionException, "Wrong first parameter type.");
945         }
946
947         result.onDeleted = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onitemsremoved");
948         if (validator.isNullOrUndefined(result.onDeleted)) {
949             result.onDeleted = NULL;
950         } else if(!validator.isCallback(result.onDeleted)) {
951             ThrowMsg(ConversionException, "Wrong first parameter type.");
952         }
953
954         if (!result.onAdded && !result.onUpdated && !result.onDeleted) {
955             ThrowMsg(ConversionException, "Wrong second parameter type.");
956         }
957
958         JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(globalContext, result.onAdded, NULL);
959         JSCallbackManagerPtr onUpdatedCbm = JSCallbackManager::createObject(globalContext, result.onUpdated, NULL);
960         JSCallbackManagerPtr onDeletedCbm = JSCallbackManager::createObject(globalContext, result.onDeleted, NULL);
961
962         LoggerD("Make change emitter and process sync operation.");
963
964         CalendarChangeCallbackPrivateDataPtr privData(new CalendarChangeCallbackPrivateData(
965             onAddedCbm, onUpdatedCbm, onDeletedCbm));
966         privData->copyAceCheckAccessFunction(privateObject);
967         OnEventsChangedEmitterPtr emitter(new OnEventsChangedEmitter());
968         emitter->setListener(&CalendarResponseDispatcher::getInstance());
969         emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(privData));
970
971         IEventWatchChangesPtr dplEvent(new IEventWatchChanges());
972         dplEvent->setEmitter(emitter);
973         dplEvent->setForSynchronousCall();
974         calendar->watchChanges(dplEvent);
975
976         long watchId;
977         if (dplEvent->getResult()) {
978             watchId = dplEvent->getWatchId();
979
980                         CalendarListenerCancellerPtr canceller = CalendarListenerCancellerPtr(new CalendarListenerCanceller(globalContext, thisObject, watchId));
981                         DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
982                         CalendarListenerManagerSingleton::Instance().registerListener(listenerItem, globalContext);
983
984             LoggerD("Returning the watchId "<<watchId);
985                         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
986             return converter.toJSValueRefLong(watchId);
987         } else {
988             ThrowMsg(UnknownException, "Adding change listener failed by unknown reason.");
989         }
990     }
991     Catch(UnsupportedException)
992     {
993                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
994         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
995     }
996     Catch(InvalidArgumentException)
997     {
998                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
999         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1000     }
1001     Catch(ConversionException)
1002     {
1003                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1004         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1005     }
1006     Catch(Exception)
1007     {
1008                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1009         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1010     }
1011 }
1012
1013 JSValueRef JSCalendar::removeChangeListener(JSContextRef context,
1014         JSObjectRef object,
1015         JSObjectRef thisObject,
1016         size_t argumentCount,
1017         const JSValueRef arguments[],
1018         JSValueRef* exception)
1019 {
1020         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
1021     CalendarPrivObject *privateObject =
1022         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
1023
1024     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_REMOVE_CHANGE_LISTENER);
1025
1026     Try
1027     {
1028         if (!privateObject) {
1029             ThrowMsg(ConversionException, "Object is null.");
1030         }
1031
1032         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
1033
1034         JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
1035
1036         long watchId = -1;
1037         CalendarConverter converter(context);
1038         if (argumentCount>=1) {
1039             watchId = converter.toLong(arguments[0]);
1040         } else {
1041             watchId = converter.toLong(JSValueMakeUndefined(context));
1042         }
1043
1044         LoggerD("Make sync operation with watchId: "<<watchId);
1045
1046         IEventClearWatchPtr dplEvent(new IEventClearWatch());
1047         dplEvent->setWatchId(watchId);
1048         dplEvent->setForSynchronousCall();
1049         calendar->clearWatch(dplEvent);
1050
1051                 CalendarListenerCancellerPtr canceller = CalendarListenerCancellerPtr(new CalendarListenerCanceller(globalContext, thisObject, watchId));
1052                 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
1053                 CalendarListenerManagerSingleton::Instance().unregisterListener(listenerItem);
1054
1055                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
1056         return JSValueMakeUndefined(context);
1057     }
1058     Catch(UnsupportedException)
1059     {
1060                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1061         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
1062     }
1063     Catch(InvalidArgumentException)
1064     {
1065                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1066         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1067     }
1068     Catch(ConversionException)
1069     {
1070                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1071         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1072     }
1073     Catch (NotFoundException)
1074     {
1075                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1076         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
1077     }
1078     Catch(Exception)
1079     {
1080                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1081         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1082     }
1083 }
1084
1085 JSValueRef JSCalendar::getPropertyName(JSContextRef context,
1086         JSObjectRef object,
1087         JSStringRef propertyName,
1088         JSValueRef* exception)
1089 {
1090     Try
1091     {
1092         ICalendarPtr calendar = getCalendar(context, object, exception);
1093         Converter converter(context);
1094                 if(calendar->getIsUnified()) {
1095                         return JSValueMakeNull(context);
1096                 } else {
1097                 return converter.toJSValueRef(calendar->getName());
1098                 }
1099         }
1100     Catch(Exception)
1101     {
1102                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1103     }
1104     return JSValueMakeUndefined(context);
1105 }
1106
1107 JSValueRef JSCalendar::getPropertyAccountServiceId(JSContextRef context,
1108         JSObjectRef object,
1109         JSStringRef propertyName,
1110         JSValueRef* exception)
1111 {
1112     Try
1113     {
1114         ICalendarPtr calendar = getCalendar(context, object, exception);
1115         Converter converter(context);
1116                 if(calendar->getIsUnified()) {
1117                         return JSValueMakeNull(context);
1118                 } else {
1119                 return converter.toJSValueRefLong(calendar->getAccountId());
1120                 }
1121     }
1122     Catch(Exception)
1123     {
1124                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1125     }
1126     return JSValueMakeUndefined(context);
1127 }
1128
1129 JSValueRef JSCalendar::getPropertyId(JSContextRef context,
1130         JSObjectRef object,
1131         JSStringRef propertyName,
1132         JSValueRef* exception)
1133 {
1134     Try
1135     {
1136         ICalendarPtr calendar = getCalendar(context, object, exception);
1137         Converter converter(context);
1138                 if(calendar->getIsUnified()) {
1139                         return JSValueMakeNull(context);
1140                 } else {
1141                 return converter.toJSValueRef(calendar->getId());
1142                 }
1143     }
1144     Catch(Exception)
1145     {
1146                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1147     }
1148     return JSValueMakeUndefined(context);
1149 }
1150
1151 ICalendarPtr JSCalendar::getCalendar(JSContextRef ctx,
1152         const JSObjectRef object,
1153         JSValueRef* exception)
1154 {
1155     CalendarPrivObject *priv =
1156         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(object));
1157     if (priv) {
1158         return priv->getObject();
1159     }
1160     ThrowMsg(NullPointerException, "Private object is NULL.");
1161 }
1162
1163 }
1164 }