[Contact] Refactoring API structures
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Calendar / Calendar.cpp
index 35e9edd..ffd20b6 100755 (executable)
@@ -85,21 +85,21 @@ void Calendar::OnRequestReceived(const IEventAddEventsPtr &events)
     Try
     {
         if (events->getEvents()->empty()) {
-            ThrowMsg(NullPointerException, "event vector parameter is empty");
+            ThrowMsg(NullPointerException, "Items array is empty.");
+        }
+
+        int returnValue = calendar_svc_begin_trans();
+        if(CAL_SUCCESS!=returnValue) {
+            ThrowMsg(PlatformException, "Begin transaction failed with error code: "<<returnValue);
+        } else {
+            LogInfo("Begin transaction succeeded.");
         }
 
-        // Save the vector of events iteratively
         bool failedAdding = false;
         for(unsigned int i=0; i<events->getEvents()->size(); i++)
         {
-            if (events->checkCancelled()) {
-                events->setCancelAllowed(true);
-                events->setResult(true);
-                return;
-            }
-
             if (events->getEvents()->at(i)->getIdIsSet()) {
-                LogWarning("adding event that is already added: index " << i);
+                LogWarning("Item has index: " << i << ". Resetting it.");
                 events->getEvents()->at(i)->resetId();
             }
             DPL::ScopedPtr<EventWrapper> eventWrapper(new EventWrapper(events->getEvents()->at(i), getType()));
@@ -107,7 +107,6 @@ void Calendar::OnRequestReceived(const IEventAddEventsPtr &events)
             eventWrapper->convertAbstractEventToPlatformEvent();
 
             Try {
-                // Set the account and calendar id before saving the item.
                 eventWrapper->setCalendarId(getId());
                 eventWrapper->setCalendarAccountId(getAccountId());
                 eventWrapper->saveEvent();
@@ -115,13 +114,18 @@ void Calendar::OnRequestReceived(const IEventAddEventsPtr &events)
                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
                 failedAdding = true;
             }
-            //getAddEmitter()->emit(eventPtr);
         }
 
         if( false==failedAdding ) {
             events->setResult(true);
+
+            returnValue = calendar_svc_end_trans(true);
+            LogInfo("End transaction return value with true flag: "<<returnValue);
         } else {
             events->setResult(false);
+
+            returnValue = calendar_svc_end_trans(false);
+            LogInfo("End transaction return value with false flag: "<<returnValue);
         }
     }
     Catch (Exception)
@@ -130,7 +134,6 @@ void Calendar::OnRequestReceived(const IEventAddEventsPtr &events)
         events->setResult(false);
         events->setExceptionCode(ExceptionCodes::UnknownException);
     }
-    events->setCancelAllowed(false);
 }
 
 void Calendar::OnRequestReceived(const IEventUpdateEventPtr &event)
@@ -172,12 +175,8 @@ void Calendar::OnRequestReceived(const IEventUpdateEventPtr &event)
                 }
 
                 int detachedEventId = calendar_svc_struct_get_int(platformEvent, CAL_VALUE_INT_INDEX);
-                std::time_t detachedStartTime = calendar_svc_struct_get_time(platformEvent,
-                                                                            CAL_VALUE_GMT_START_DATE_TIME,
-                                                                            CAL_TZ_FLAG_GMT);
-                std::time_t detachedEndTime = calendar_svc_struct_get_time(platformEvent,
-                                                                            CAL_VALUE_GMT_END_DATE_TIME,
-                                                                            CAL_TZ_FLAG_GMT);
+                long long int detachedStartTime = calendar_svc_struct_get_lli(platformEvent, CALS_VALUE_LLI_DTSTART_UTIME);
+                long long int detachedEndTime = calendar_svc_struct_get_lli(platformEvent, CALS_VALUE_LLI_DTEND_UTIME);
 
                 DPL::ScopedPtr<EventWrapper> eventWrapperChild(new EventWrapper(event->getEvent(), getType()));
                 eventWrapperChild->convertAbstractEventToPlatformEvent();
@@ -191,20 +190,18 @@ void Calendar::OnRequestReceived(const IEventUpdateEventPtr &event)
                                                                parentId)) {
                     ThrowMsg(PlatformException, "Can't set parent Id.");
                 }
-                if (CAL_SUCCESS != calendar_svc_struct_set_time(eventWrapperChild->getPlatformEvent(),
-                                                               CAL_VALUE_GMT_START_DATE_TIME,
-                                                               CAL_TZ_FLAG_GMT,
+                if (CAL_SUCCESS != calendar_svc_struct_set_lli(eventWrapperChild->getPlatformEvent(),
+                                                               CALS_VALUE_LLI_DTSTART_UTIME,
                                                                detachedStartTime)) {
                     ThrowMsg(PlatformException, "Can't set start time.");
                 }
-                if (CAL_SUCCESS != calendar_svc_struct_set_time(eventWrapperChild->getPlatformEvent(),
-                                                               CAL_VALUE_GMT_END_DATE_TIME,
-                                                               CAL_TZ_FLAG_GMT,
+                if (CAL_SUCCESS != calendar_svc_struct_set_lli(eventWrapperChild->getPlatformEvent(),
+                                                               CALS_VALUE_LLI_DTEND_UTIME,
                                                                detachedEndTime)) {
                     ThrowMsg(PlatformException, "Can't set end time.");
                 }
                 if (CAL_SUCCESS != calendar_svc_struct_set_int(eventWrapperChild->getPlatformEvent(),
-                                                               CAL_VALUE_INT_REPEAT_TERM,
+                                                               CALS_VALUE_INT_RRULE_FREQ,
                                                                CAL_REPEAT_NONE)) {
                     ThrowMsg(PlatformException, "Can't set repeat term.");
                 }
@@ -222,15 +219,15 @@ void Calendar::OnRequestReceived(const IEventUpdateEventPtr &event)
                 CAL_VALUE_INT_ORIGINAL_EVENT_ID, event->getEvent()->getId()) ) {
                 ThrowMsg(PlatformException, "cannot save exception event");
             } 
-            // Use "CAL_VALUE_GMT_COMPLETED_DATE_TIME" as the recurrence id saving point.
-            if ( CAL_SUCCESS!=calendar_svc_struct_set_time(eventWrapperChild->getPlatformEvent(),
-                CAL_VALUE_GMT_COMPLETED_DATE_TIME, CAL_TZ_FLAG_GMT, event->getEvent()->getRecurrenceId()) ) {
+            // Use "CAL_VALUE_LLI_COMPLETED_TIME" as the recurrence id saving point.
+            if ( CAL_SUCCESS!=calendar_svc_struct_set_lli(eventWrapperChild->getPlatformEvent(),
+                CAL_VALUE_LLI_COMPLETED_TIME, event->getEvent()->getRecurrenceId()) ) {
                 ThrowMsg(PlatformException, "cannot save recurrence id");
             } else {
                 LogInfo("Saved the rid for the child: "<<event->getEvent()->getRecurrenceId());
             }
             if (CAL_SUCCESS!=calendar_svc_struct_set_int(eventWrapperChild->getPlatformEvent(),
-                CAL_VALUE_INT_REPEAT_TERM, CAL_REPEAT_NONE) ) {
+                CALS_VALUE_INT_RRULE_FREQ, CAL_REPEAT_NONE) ) {
                 ThrowMsg(PlatformException, "cannot save exception event");
             }
             int childId = calendar_svc_insert(eventWrapperChild->getPlatformEvent());
@@ -238,25 +235,10 @@ void Calendar::OnRequestReceived(const IEventUpdateEventPtr &event)
                 ThrowMsg(PlatformException, "cannot save exception event");
             }
 
-            GList* list = NULL;
-            cal_value *value = calendar_svc_value_new(CAL_VALUE_LST_EXCEPTION_DATE);
-            if (CAL_SUCCESS!=calendar_svc_value_set_time(value, CAL_VALUE_GMT_EXCEPTION_DATE_TIME, CAL_TZ_FLAG_GMT, event->getEvent()->getRecurrenceId()) ) {
-                ThrowMsg(PlatformException, "cannot save the exception date");
-            } else {
-                LogInfo("Saved the exception date: "<<event->getEvent()->getRecurrenceId());
-            }
-            if (CAL_SUCCESS!=calendar_svc_value_set_int(value,  CAL_VALUE_INT_EXCEPTION_DATE_ID, childId)) {
-                ThrowMsg(PlatformException, "cannot save the exception parent event");
-            } else {
-                LogInfo("Saved the exception id: "<<childId);
-            }
-            eventWrapper->loadEvent(event->getEvent()->getId());
-            calendar_svc_struct_get_list(eventWrapper->getPlatformEvent(), CAL_VALUE_LST_EXCEPTION_DATE, &list);
-            list = g_list_append(list, value);
-            if (CAL_SUCCESS!=calendar_svc_struct_store_list(eventWrapper->getPlatformEvent(), CAL_VALUE_LST_EXCEPTION_DATE, list)) {
-                ThrowMsg(PlatformException, "cannot save the exception parent event");
+            int error = calendar_svc_event_delete_normal_instance(event->getEvent()->getId(), event->getEvent()->getRecurrenceId());
+            if(error!=CAL_SUCCESS) {
+                ThrowMsg(PlatformException, "Can't delete the instance. Error code " << error);
             }
-            calendar_svc_update(eventWrapper->getPlatformEvent());
 
             event->getEvent()->setIsDetached(true);
         }
@@ -283,19 +265,19 @@ void Calendar::OnRequestReceived(const IEventUpdateEventsPtr &events)
     Try
     {
         if (events->getEvents()->empty()) {
-            ThrowMsg(NullPointerException, "Item vector is empty.");
+            ThrowMsg(NullPointerException, "Item array is empty.");
+        }
+
+        int returnValue = calendar_svc_begin_trans();
+        if(CAL_SUCCESS!=returnValue) {
+            ThrowMsg(PlatformException, "Begin transaction failed with error code: "<<returnValue);
+        } else {
+            LogInfo("Begin transaction succeeded.");
         }
 
-        // Update the vector of events iteratively
         bool failedUpdating = false;
         for(unsigned int i=0; i<events->getEvents()->size(); i++)
         {
-            if (events->checkCancelled()) {
-                events->setCancelAllowed(true);
-                events->setResult(true);
-                return;
-            }
-
             CalendarEventPtr thisEvent = events->getEvents()->at(i);
             if (!thisEvent->getIdIsSet()) {
                 ThrowMsg(InvalidArgumentException, "Item id is not set.");
@@ -326,12 +308,10 @@ void Calendar::OnRequestReceived(const IEventUpdateEventsPtr &events)
                         }
 
                         int detachedEventId = calendar_svc_struct_get_int(platformEvent, CAL_VALUE_INT_INDEX);
-                        std::time_t detachedStartTime = calendar_svc_struct_get_time(platformEvent,
-                                                                                    CAL_VALUE_GMT_START_DATE_TIME,
-                                                                                    CAL_TZ_FLAG_GMT);
-                        std::time_t detachedEndTime = calendar_svc_struct_get_time(platformEvent,
-                                                                                    CAL_VALUE_GMT_END_DATE_TIME,
-                                                                                    CAL_TZ_FLAG_GMT);
+                        long long int detachedStartTime = calendar_svc_struct_get_lli(platformEvent,
+                                                                                    CALS_VALUE_LLI_DTSTART_UTIME);
+                        long long int detachedEndTime = calendar_svc_struct_get_lli(platformEvent,
+                                                                                    CALS_VALUE_LLI_DTEND_UTIME);
 
                         DPL::ScopedPtr<EventWrapper> eventWrapperChild(new EventWrapper(thisEvent, getType()));
                         eventWrapperChild->convertAbstractEventToPlatformEvent();
@@ -340,15 +320,13 @@ void Calendar::OnRequestReceived(const IEventUpdateEventsPtr &events)
                                                                        detachedEventId)) {
                             ThrowMsg(PlatformException, "Can't set event Id.");
                         }
-                        if (CAL_SUCCESS != calendar_svc_struct_set_time(eventWrapperChild->getPlatformEvent(),
-                                                                       CAL_VALUE_GMT_START_DATE_TIME,
-                                                                       CAL_TZ_FLAG_GMT,
+                        if (CAL_SUCCESS != calendar_svc_struct_set_lli(eventWrapperChild->getPlatformEvent(),
+                                                                       CALS_VALUE_LLI_DTSTART_UTIME,
                                                                        detachedStartTime)) {
                             ThrowMsg(PlatformException, "Can't set start time.");
                         }
-                        if (CAL_SUCCESS != calendar_svc_struct_set_time(eventWrapperChild->getPlatformEvent(),
-                                                                       CAL_VALUE_GMT_END_DATE_TIME,
-                                                                       CAL_TZ_FLAG_GMT,
+                        if (CAL_SUCCESS != calendar_svc_struct_set_lli(eventWrapperChild->getPlatformEvent(),
+                                                                       CALS_VALUE_LLI_DTEND_UTIME,
                                                                        detachedEndTime)) {
                             ThrowMsg(PlatformException, "Can't set end time.");
                         }
@@ -367,15 +345,15 @@ void Calendar::OnRequestReceived(const IEventUpdateEventsPtr &events)
                         CAL_VALUE_INT_ORIGINAL_EVENT_ID, thisEvent->getId()) ) {
                         ThrowMsg(PlatformException, "cannot save parent id.");
                     }
-                    // Use "CAL_VALUE_GMT_COMPLETED_DATE_TIME" as the recurrence id saving point.
-                    if ( CAL_SUCCESS!=calendar_svc_struct_set_time(eventWrapperChild->getPlatformEvent(),
-                        CAL_VALUE_GMT_COMPLETED_DATE_TIME, CAL_TZ_FLAG_GMT, thisEvent->getRecurrenceId()) ) {
+                    // Use "CAL_VALUE_LLI_COMPLETED_TIME" as the recurrence id saving point.
+                    if ( CAL_SUCCESS!=calendar_svc_struct_set_lli(eventWrapperChild->getPlatformEvent(),
+                        CAL_VALUE_LLI_COMPLETED_TIME, thisEvent->getRecurrenceId()) ) {
                         ThrowMsg(PlatformException, "cannot save recurrence id.");
                     } else {
                         LogInfo("Saved the rid for the child: "<<thisEvent->getRecurrenceId());
                     }
                     if (CAL_SUCCESS!=calendar_svc_struct_set_int(eventWrapperChild->getPlatformEvent(),
-                        CAL_VALUE_INT_REPEAT_TERM, CAL_REPEAT_NONE) ) {
+                        CALS_VALUE_INT_RRULE_FREQ, CAL_REPEAT_NONE) ) {
                         ThrowMsg(PlatformException, "cannot save repeat term.");
                     }
                     int childId = calendar_svc_insert(eventWrapperChild->getPlatformEvent());
@@ -383,24 +361,10 @@ void Calendar::OnRequestReceived(const IEventUpdateEventsPtr &events)
                         ThrowMsg(PlatformException, "cannot save exception event");
                     }
 
-                    GList* list = NULL;
-                    cal_value *value = calendar_svc_value_new(CAL_VALUE_LST_EXCEPTION_DATE);
-                    if (CAL_SUCCESS!=calendar_svc_value_set_time(value, CAL_VALUE_GMT_EXCEPTION_DATE_TIME, CAL_TZ_FLAG_GMT, thisEvent->getRecurrenceId()) ) {
-                        ThrowMsg(PlatformException, "cannot save the exception parent event");
-                    } else {
-                        LogInfo("Saved the exception date: "<<thisEvent->getRecurrenceId());
+                    int error = calendar_svc_event_delete_normal_instance(thisEvent->getId(), thisEvent->getRecurrenceId());
+                    if(error!=CAL_SUCCESS) {
+                        ThrowMsg(PlatformException, "Can't delete the instance. Error code " << error);
                     }
-                    if (CAL_SUCCESS!=calendar_svc_value_set_int(value,  CAL_VALUE_INT_EXCEPTION_DATE_ID, childId)) {
-                        ThrowMsg(PlatformException, "cannot save the exception parent event");
-                    } else {
-                        LogInfo("Saved the exception id: "<<childId);
-                    }
-                    calendar_svc_struct_get_list(eventWrapper->getPlatformEvent(), CAL_VALUE_LST_EXCEPTION_DATE, &list);
-                    list = g_list_append(list, value);
-                    if (CAL_SUCCESS!=calendar_svc_struct_store_list(eventWrapper->getPlatformEvent(), CAL_VALUE_LST_EXCEPTION_DATE, list)) {
-                        ThrowMsg(PlatformException, "cannot save the exception parent event");
-                    }
-                    calendar_svc_update(eventWrapper->getPlatformEvent());
 
                     thisEvent->setIsDetached(true);
                 }
@@ -413,8 +377,14 @@ void Calendar::OnRequestReceived(const IEventUpdateEventsPtr &events)
 
         if( false==failedUpdating ) {
             events->setResult(true);
+
+            returnValue = calendar_svc_end_trans(true);
+            LogInfo("End transaction return value with true flag: "<<returnValue);
         } else {
             events->setResult(false);
+
+            returnValue = calendar_svc_end_trans(false);
+            LogInfo("End transaction return value with false flag: "<<returnValue);
         }
     }
     Catch (Exception)
@@ -427,7 +397,6 @@ void Calendar::OnRequestReceived(const IEventUpdateEventsPtr &events)
     if(iter) {
         calendar_svc_iter_remove(&iter);
     }
-    events->setCancelAllowed(false);
 }
 
 void Calendar::OnRequestReceived(const IEventDeleteEventPtr &event)
@@ -447,7 +416,7 @@ void Calendar::OnRequestReceived(const IEventDeleteEventPtr &event)
         eventWrapper->getAbstractEvent()->setId(id);
 
         std::stringstream ss(event->getEventId()->getRecurrenceId());
-        std::time_t rid;
+        long long int rid;
         ss>>rid;
         eventWrapper->getAbstractEvent()->setRecurrenceId(rid);
 
@@ -489,7 +458,7 @@ void Calendar::OnRequestReceived(const IEventGetPtr &event)
         ss>>id;
         eventWrapper->loadEvent(id);
 
-        event->setItem(eventWrapper->convertPlatformEventToAbstractEvent());
+        event->setItem(eventWrapper->getAbstractEvent());
         event->setResult(true);
     }
     Catch (NotFoundException)
@@ -512,10 +481,16 @@ void Calendar::OnRequestReceived(const IEventDeleteEventsPtr &events)
     Try
     {
         if (events->getEventIds()->empty()) {
-            ThrowMsg(NullPointerException, "event vector parameter is empty");
+            ThrowMsg(NullPointerException, "Item id array is empty.");
+        }
+
+        int returnValue = calendar_svc_begin_trans();
+        if(CAL_SUCCESS!=returnValue) {
+            ThrowMsg(PlatformException, "Begin transaction failed with error code: "<<returnValue);
+        } else {
+            LogInfo("Begin transaction succeeded.");
         }
 
-        // Delete the vector of events iteratively considering the recurrenceId
         bool failedDeleting = false;
         for(unsigned int i=0; i<events->getEventIds()->size(); i++)
         {
@@ -527,7 +502,7 @@ void Calendar::OnRequestReceived(const IEventDeleteEventsPtr &events)
             eventWrapper->getAbstractEvent()->setId(id);
 
             std::stringstream ss(events->getEventIds()->at(i)->getRecurrenceId());
-            std::time_t rid;
+            long long int rid;
             ss>>rid;
             eventWrapper->getAbstractEvent()->setRecurrenceId(rid);
             eventWrapper->convertAbstractEventToPlatformEvent();
@@ -549,9 +524,13 @@ void Calendar::OnRequestReceived(const IEventDeleteEventsPtr &events)
 
         if( false==failedDeleting ) {
             events->setResult(true);
-        }
-        else {
+            returnValue = calendar_svc_end_trans(true);
+            LogInfo("End transaction return value with true flag: "<<returnValue);
+        } else {
             events->setResult(false);
+
+            returnValue = calendar_svc_end_trans(false);
+            LogInfo("End transaction return value with false flag: "<<returnValue);
         }
     }
     Catch (NotFoundException)
@@ -604,19 +583,19 @@ void Calendar::OnRequestReceived(const IEventFindEventsPtr &event)
             query.append(std::string(" AND ") + CAL_VALUE_INT_ACCOUNT_ID + " = " + ssAccount.str());
             if (getType()==CalendarEvent::TASK_TYPE) {
                 ssType<<CALS_CALENDAR_TYPE_TODO;
-                query.append(std::string(" AND ") + CAL_VALUE_INT_TYPE + " = " + ssType.str() + " AND " + "is_deleted = 0");
+                query.append(std::string(" AND ") + CAL_VALUE_INT_TYPE + " = " + ssType.str());
             } else {
                 ssType<<CALS_CALENDAR_TYPE_EVENT;
-                query.append(std::string(" AND ") + CAL_VALUE_INT_TYPE + " = " + ssType.str() + " AND " + "is_deleted = 0");
+                query.append(std::string(" AND ") + CAL_VALUE_INT_TYPE + " = " + ssType.str());
             }
                } else {
             query.append(std::string(" WHERE ") + CAL_VALUE_INT_ACCOUNT_ID + " = " + ssAccount.str());
             if (getType()==CalendarEvent::TASK_TYPE) {
                 ssType<<CALS_CALENDAR_TYPE_TODO;
-                query.append(std::string(" AND ") + CAL_VALUE_INT_TYPE + " = " + ssType.str() + " AND " + "is_deleted = 0");
+                query.append(std::string(" AND ") + CAL_VALUE_INT_TYPE + " = " + ssType.str());
             } else {
                 ssType<<CALS_CALENDAR_TYPE_EVENT;
-                query.append(std::string(" AND ") + CAL_VALUE_INT_TYPE + " = " + ssType.str() + " AND " + "is_deleted = 0");
+                query.append(std::string(" AND ") + CAL_VALUE_INT_TYPE + " = " + ssType.str());
             }
                }
 
@@ -635,14 +614,13 @@ void Calendar::OnRequestReceived(const IEventFindEventsPtr &event)
 
         // Now load the full calendar item using the retrieved id.
         for( unsigned int i=0; i<event->getEvents()->size(); i++) {
-            // platformEvent is freed when the wrapper destructor is called.
             DPL::ScopedPtr<EventWrapper> eventWrapper(new EventWrapper(event->getEvents()->at(i), getType()));
             eventWrapper->loadEvent(event->getEvents()->at(i)->getId());
 
             // If it's a detached event, set the saved rid.
             if (eventWrapper->getAbstractEvent()->getParentId()>0) {
-                LogInfo("Set the saved rid: "<<calendar_svc_struct_get_time(eventWrapper->getPlatformEvent(), CAL_VALUE_GMT_COMPLETED_DATE_TIME, CAL_TZ_FLAG_GMT));
-                event->getEvents()->at(i)->setRecurrenceId(calendar_svc_struct_get_time(eventWrapper->getPlatformEvent(), CAL_VALUE_GMT_COMPLETED_DATE_TIME, CAL_TZ_FLAG_GMT));
+                LogInfo("Set the saved rid: "<<calendar_svc_struct_get_lli(eventWrapper->getPlatformEvent(), CAL_VALUE_LLI_COMPLETED_TIME));
+                event->getEvents()->at(i)->setRecurrenceId(calendar_svc_struct_get_lli(eventWrapper->getPlatformEvent(), CAL_VALUE_LLI_COMPLETED_TIME));
             }
         }
     }
@@ -728,28 +706,44 @@ static void eventChangedCb(void *user_data)
         // Determine the status of event change.
         cal_iter *iter = NULL;
         cal_struct *platformEvent = NULL;
-        int index;
-        int syncStatus;
-
-        if( CAL_SUCCESS != calendar_svc_get_updated_event_list(thisCalendar->getAccountId(),
-                thisCalendar->getLastChangeFetchTime(),
-                &iter) ) {
-            ThrowMsg(PlatformException, "Can't get the updated event list.");
+        int id;
+        int type;
+        int version = 0;
+        int errorCode;
+
+        int calendarId;
+        std::stringstream ss(thisCalendar->getId());
+        ss>>calendarId;
+
+        LogDebug("Getting items with calendar id: "<<calendarId<<", version: "<<thisCalendar->getLastChangedVersion()<<", type: "<<thisCalendar->getType());
+        if (CalendarEvent::TASK_TYPE==thisCalendar->getType()) {
+            errorCode = calendar_svc_todo_get_changes(calendarId,
+                    thisCalendar->getLastChangedVersion(),
+                    &iter);
+        } else {
+            errorCode = calendar_svc_event_get_changes(calendarId,
+                    thisCalendar->getLastChangedVersion(),
+                    &iter);
+        }
+        if( CAL_SUCCESS!=errorCode ) {
+            ThrowMsg(PlatformException, "Can't get the updated event list. Error code: "<<errorCode);
         }
+
         while( CAL_SUCCESS == calendar_svc_iter_next(iter) )
         {
             if ( CAL_SUCCESS != calendar_svc_iter_get_info(iter, &platformEvent) ) {
                 ThrowMsg(PlatformException, "Can't get the calendar info.");
             }
 
-            index = calendar_svc_struct_get_int(platformEvent, CAL_VALUE_INT_INDEX);
-            syncStatus = calendar_svc_struct_get_int(platformEvent, CAL_VALUE_INT_SYNC_STATUS);
-            LogDebug("index "<<index<<" , syncStatus "<<syncStatus);
-            if ( CAL_SYNC_STATUS_NEW==syncStatus ) {
+            id = calendar_svc_struct_get_int(platformEvent, CALS_STRUCT_UPDATED_INT_ID);
+            type = calendar_svc_struct_get_int(platformEvent, CALS_STRUCT_UPDATED_INT_TYPE);
+            version = calendar_svc_struct_get_int(platformEvent, CALS_STRUCT_UPDATED_INT_VERSION);
+            LogDebug("id "<<id<<", type "<<type<<", version "<<version);
+            if ( CALS_UPDATED_TYPE_INSERTED==type ) {
                 eventPtr->setStatus(OnEventsChanged::ON_ADD);
-            } else if ( CAL_SYNC_STATUS_UPDATED==syncStatus ) {
+            } else if ( CALS_UPDATED_TYPE_MODIFIED==type ) {
                 eventPtr->setStatus(OnEventsChanged::ON_UPDATE);
-            } else if ( CAL_SYNC_STATUS_DELETED==syncStatus ) {
+            } else if ( CALS_UPDATED_TYPE_DELETED==type ) {
                 eventPtr->setStatus(OnEventsChanged::ON_DELETE);
             } else {
                 if( platformEvent ) {
@@ -761,9 +755,9 @@ static void eventChangedCb(void *user_data)
 
             DPL::ScopedPtr<EventWrapper> eventWrapper(new EventWrapper(thisCalendar->getType()));
             Try {
-                eventWrapper->loadEvent(index);
+                eventWrapper->loadEvent(id);
             } Catch (NotFoundException){
-                LogDebug("Handling deleted event with index: "<<index);
+                LogDebug("Handling deleted event with index: "<<id);
             }
             eventPtr->addEvent(eventWrapper->getAbstractEvent());
 
@@ -776,9 +770,8 @@ static void eventChangedCb(void *user_data)
             ThrowMsg(PlatformException, "Can't remove the iter.");
         }
 
-        std::time_t localTime = time(NULL);
-        thisCalendar->setLastChangeFetchTime(localTime);
-        LogInfo("Last change fetch time: "<<thisCalendar->getLastChangeFetchTime());
+        thisCalendar->setLastChangedVersion(version);
+        LogInfo("Last change fetch version: "<<thisCalendar->getLastChangedVersion());
 
         eventPtr->setResult(true);
         eventPtr->setCalendarType(thisCalendar->getType());
@@ -803,13 +796,18 @@ void Calendar::OnRequestReceived(const IEventWatchChangesPtr &event)
         // Subscribe the watch to the platform just once.
         if( m_changeEmitters.size()==0 )
         {
-            if( CAL_SUCCESS!=calendar_svc_subscribe_change(eventChangedCb, this) ) {
-                ThrowMsg(PlatformException, "Can't subscribe the db change noti.");
+            if (CAL_SUCCESS!=calendar_svc_begin_trans() ) {
+                ThrowMsg(PlatformException, "Transaction failed.");
             } else {
-                // Save the last change fetch time to start watching.
-                std::time_t localTime = time(NULL);
-                setLastChangeFetchTime(localTime);
-                LogInfo("Last change fetch time: "<<getLastChangeFetchTime());
+                if( CAL_SUCCESS!=calendar_svc_subscribe_change(eventChangedCb, this) ) {
+                    calendar_svc_end_trans(true);
+                    ThrowMsg(PlatformException, "Can't subscribe the db change noti.");
+                } else {
+                    // Save the last change fetch version to start watching.
+                    int version = calendar_svc_end_trans(true);
+                    setLastChangedVersion(version);
+                    LogInfo("Last change fetch version: "<<getLastChangedVersion());
+                }
             }
         }
 
@@ -830,16 +828,19 @@ void Calendar::OnRequestReceived(const IEventClearWatchPtr &event)
     LogDebug("entered");
     Try
     {
-        m_changeEmitters.detach(event->getWatchId());
-
-        if( m_changeEmitters.size()==0 ) {
-            if( CAL_SUCCESS!=calendar_svc_unsubscribe_change(eventChangedCb) ) {
-                ThrowMsg(PlatformException, "Can't unsubscribe the db change noti.");
-            } else {
-                LogDebug("Platform watch cleared successfully.");
+        if( m_changeEmitters.detach(event->getWatchId()) ) {
+            if( m_changeEmitters.size()==0 ) {
+                if( CAL_SUCCESS!=calendar_svc_unsubscribe_change(eventChangedCb) ) {
+                    ThrowMsg(PlatformException, "Can't unsubscribe the db change noti.");
+                } else {
+                    LogDebug("Platform watch cleared successfully.");
+                }
             }
+            event->setResult(true);
+        } else {
+            LogInfo("Wrong watch Id.");
+            event->setResult(false);
         }
-        event->setResult(true);
     }
     Catch (Exception)
     {
@@ -853,8 +854,8 @@ void Calendar::OnRequestReceived(const IEventExpandEventRecurrencePtr &event)
 {
     LogDebug("entered");
     const CalendarEventPtr calEvent = event->getEvent();
-    const std::time_t startDate = event->getStartDate();
-    const std::time_t endDate = event->getEndDate();
+    const long long int startDate = event->getStartDate();
+    const long long int endDate = event->getEndDate();
     event->setResult(true);
 
     Try {
@@ -863,31 +864,52 @@ void Calendar::OnRequestReceived(const IEventExpandEventRecurrencePtr &event)
             ThrowMsg(PlatformException, "This is not a recurring event.");
         }
 
-        std::time_t nextStartTime = 0;
-        std::time_t nextEndTime = 0;
-
         DPL::ScopedPtr<EventWrapper> eventWrapper(new EventWrapper(CalendarEvent::EVENT_TYPE));
         // We must load the event from the DB in order to get the auto-calculated repeat end time.
-        eventWrapper->loadEvent(event->getEvent()->getId());
-        //eventWrapper->displayPlatformEvent();
+        eventWrapper->loadEvent(calEvent->getId());
 
-        while ( CAL_SUCCESS==calendar_svc_util_next_valid_event(eventWrapper->getPlatformEvent(), startDate, endDate, &nextStartTime, &nextEndTime) )
+        cal_iter *iter = NULL;
+        cal_struct *cs;
+        int candidateIndex;
+        long long int dtstart, dtend;
+        int calendarId;
+        std::stringstream ss(getId());
+        ss>>calendarId;
+
+        int returnVal = calendar_svc_event_get_normal_list_by_period(calendarId, CALS_LIST_PERIOD_NORMAL_BASIC, startDate, endDate, &iter);
+        if( CAL_SUCCESS!=returnVal ) {
+            ThrowMsg(PlatformException, "Getting event instances failed due to "<<returnVal);
+        }
+
+        while ( CAL_SUCCESS==calendar_svc_iter_next(iter) )
         {
-            LogInfo("Found a next valid event: "<<nextStartTime);
-            DPL::ScopedPtr<EventWrapper> recurringEventWrapper(new EventWrapper(getType()));
-            recurringEventWrapper->loadEvent(calEvent->getId());
+            cs = NULL;
+            returnVal = calendar_svc_iter_get_info(iter, &cs);
+            if( CAL_SUCCESS!=returnVal ){
+                LogDebug("Iterator get info failed due to "<<returnVal);
+                continue;
+            }
 
-            // Set distintive attributes of each instance.
-            recurringEventWrapper->getAbstractEvent()->setRecurrenceId(nextStartTime);
-            recurringEventWrapper->getAbstractEvent()->setStartTime(nextStartTime);
-            recurringEventWrapper->getAbstractEvent()->setEndTime(nextEndTime);
+            candidateIndex = calendar_svc_struct_get_int(cs, CALS_LIST_PERIOD_NORMAL_BASIC_INT_EVENTID);
+            if(candidateIndex==calEvent->getId()) {
+                DPL::ScopedPtr<EventWrapper> recurringEventWrapper(new EventWrapper(getType()));
+                recurringEventWrapper->loadEvent(calEvent->getId());
 
-            event->addExpandedEvent(recurringEventWrapper->getAbstractEvent());
+                // Set distintive attributes of each instance.
+                dtstart = calendar_svc_struct_get_lli(cs, CALS_LIST_PERIOD_NORMAL_BASIC_LLI_DTSTART_UTIME);
+                dtend = calendar_svc_struct_get_lli(cs, CALS_LIST_PERIOD_NORMAL_BASIC_LLI_DTEND_UTIME);
+                recurringEventWrapper->getAbstractEvent()->setRecurrenceId(dtstart);
+                recurringEventWrapper->getAbstractEvent()->setStartTime(dtstart);
+                recurringEventWrapper->getAbstractEvent()->setEndTime(dtend);
+                LogInfo("Found a valid event instance with dtstart: "<<dtstart<<" and dtend: "<<dtend);
+
+                event->addExpandedEvent(recurringEventWrapper->getAbstractEvent());
+            }
         }
         LogInfo("Length of expanded events from parent: "<<event->getExpandedEventList()->size());
 
         // Consider the detached events also.
-        cal_iter *iter = NULL;
+        iter = NULL;
         cal_struct *platformEvent = NULL;
         int parentId = event->getEvent()->getId();
         if(CAL_SUCCESS != calendar_svc_find_event_list(getAccountId(), CAL_VALUE_INT_ORIGINAL_EVENT_ID, (void*) parentId, &iter)) {
@@ -901,8 +923,8 @@ void Calendar::OnRequestReceived(const IEventExpandEventRecurrencePtr &event)
             }
 
             int detachedEventId = calendar_svc_struct_get_int(platformEvent, CAL_VALUE_INT_INDEX);
-            std::time_t detachedStartTime = calendar_svc_struct_get_time(platformEvent, CAL_VALUE_GMT_START_DATE_TIME, CAL_TZ_FLAG_GMT);
-            std::time_t detachedEndTime = calendar_svc_struct_get_time(platformEvent, CAL_VALUE_GMT_END_DATE_TIME, CAL_TZ_FLAG_GMT);
+            long long int detachedStartTime = calendar_svc_struct_get_lli(platformEvent, CALS_VALUE_LLI_DTSTART_UTIME);
+            long long int detachedEndTime = calendar_svc_struct_get_lli(platformEvent, CALS_VALUE_LLI_DTEND_UTIME);
             if (detachedStartTime>=startDate && detachedStartTime<=endDate) {
                 LogInfo("Found a valid detached event: "<<detachedStartTime);
                 DPL::ScopedPtr<EventWrapper> detachedEventWrapper(new EventWrapper(getType()));