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