/**
  * creates an exception instance of a recurring event
  *
  * NOTE: deleting persistent exceptions is done via a normal delete action
  *       and handled in the deleteInspection
  * 
  * @param  Calendar_Model_Event  $_event
  * @param  bool                  $_deleteInstance
  * @param  bool                  $_allFollowing
  * @param  bool                  $_checkBusyConflicts
  * @return Calendar_Model_Event  exception Event | updated baseEvent
  * 
  * @todo replace $_allFollowing param with $range
  * @deprecated replace with create/update/delete
  */
 public function createRecurException($_event, $_deleteInstance = FALSE, $_allFollowing = FALSE, $_checkBusyConflicts = FALSE)
 {
     $baseEvent = $this->getRecurBaseEvent($_event);
     if ($baseEvent->last_modified_time != $_event->last_modified_time) {
         if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) {
             Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . " It is not allowed to create recur instance if it is clone of base event");
         }
         throw new Tinebase_Timemachine_Exception_ConcurrencyConflict('concurrency conflict!');
     }
     //        // Maybe Later
     //        // exdates needs to stay in baseEvents container
     //        if ($_event->container_id != $baseEvent->container_id) {
     //            throw new Calendar_Exception_ExdateContainer();
     //        }
     // check if this is an exception to the first occurence
     if ($baseEvent->getId() == $_event->getId()) {
         if ($_allFollowing) {
             throw new Exception('please edit or delete complete series!');
         }
         // NOTE: if the baseEvent gets a time change, we can't compute the recurdid w.o. knowing the original dtstart
         $recurid = $baseEvent->setRecurId($baseEvent->getId());
         unset($baseEvent->recurid);
         $_event->recurid = $recurid;
     }
     // just do attender status update if user has no edit grant
     if ($this->_doContainerACLChecks && !$baseEvent->{Tinebase_Model_Grants::GRANT_EDIT}) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " user has no editGrant for event: '{$baseEvent->getId()}'. Only creating exception for attendee status");
         }
         if ($_event->attendee instanceof Tinebase_Record_RecordSet) {
             foreach ($_event->attendee as $attender) {
                 if ($attender->status_authkey) {
                     $exceptionAttender = $this->attenderStatusCreateRecurException($_event, $attender, $attender->status_authkey, $_allFollowing);
                 }
             }
         }
         if (!isset($exceptionAttender)) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG) && $_event->attendee instanceof Tinebase_Record_RecordSet) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Failed to update attendee: " . print_r($_event->attendee->toArray(), true));
             }
             throw new Tinebase_Exception_AccessDenied('Failed to update attendee, status authkey might be missing');
         }
         return $this->get($exceptionAttender->cal_event_id);
     }
     // NOTE: recurid is computed by rrule recur computations and therefore is already part of the event.
     if (empty($_event->recurid)) {
         throw new Exception('recurid must be present to create exceptions!');
     }
     // we do notifications ourself
     $sendNotifications = $this->sendNotifications(FALSE);
     // EDIT for baseEvent is checked above, CREATE, DELETE for recur exceptions is implied with it
     $doContainerACLChecks = $this->doContainerACLChecks(FALSE);
     $db = $this->_backend->getAdapter();
     $transactionId = Tinebase_TransactionManager::getInstance()->startTransaction($db);
     $exdate = new Tinebase_DateTime(substr($_event->recurid, -19));
     $exdates = is_array($baseEvent->exdate) ? $baseEvent->exdate : array();
     $originalDtstart = $_event->getOriginalDtStart();
     $originalEvent = Calendar_Model_Rrule::computeNextOccurrence($baseEvent, new Tinebase_Record_RecordSet('Calendar_Model_Event'), $originalDtstart);
     if ($_allFollowing != TRUE) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Adding exdate for: '{$_event->recurid}'");
         }
         array_push($exdates, $exdate);
         $baseEvent->exdate = $exdates;
         $updatedBaseEvent = $this->update($baseEvent, FALSE);
         if ($_deleteInstance == FALSE) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Creating persistent exception for: '{$_event->recurid}'");
             }
             if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
                 Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . " Recur exception: " . print_r($_event->toArray(), TRUE));
             }
             $_event->base_event_id = $baseEvent->getId();
             $_event->setId(NULL);
             unset($_event->rrule);
             unset($_event->exdate);
             foreach (array('attendee', 'notes', 'alarms') as $prop) {
                 if ($_event->{$prop} instanceof Tinebase_Record_RecordSet) {
                     $_event->{$prop}->setId(NULL);
                 }
             }
             $originalDtstart = $_event->getOriginalDtStart();
             $dtStartHasDiff = $originalDtstart->compare($_event->dtstart) != 0;
             // php52 compat
             if (!$dtStartHasDiff) {
                 $attendees = $_event->attendee;
                 unset($_event->attendee);
             }
             $note = $_event->notes;
             unset($_event->notes);
             $persistentExceptionEvent = $this->create($_event, $_checkBusyConflicts);
             if (!$dtStartHasDiff) {
                 // we save attendee seperatly to preserve their attributes
                 if ($attendees instanceof Tinebase_Record_RecordSet) {
                     $attendees->cal_event_id = $persistentExceptionEvent->getId();
                     $calendar = Tinebase_Container::getInstance()->getContainerById($_event->container_id);
                     foreach ($attendees as $attendee) {
                         $this->_createAttender($attendee, $_event, TRUE, $calendar);
                         $this->_increaseDisplayContainerContentSequence($attendee, $persistentExceptionEvent, Tinebase_Model_ContainerContent::ACTION_CREATE);
                     }
                 }
             }
             // @todo save notes and add a update note -> what was updated? -> modlog is also missing
             $persistentExceptionEvent = $this->get($persistentExceptionEvent->getId());
         }
     } else {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " shorten recur series for/to: '{$_event->recurid}'");
         }
         // split past/future exceptions
         $pastExdates = array();
         $futureExdates = array();
         foreach ($exdates as $exdate) {
             $exdate->isLater($_event->dtstart) ? $futureExdates[] = $exdate : ($pastExdates[] = $exdate);
         }
         $persistentExceptionEvents = $this->getRecurExceptions($_event);
         $pastPersistentExceptionEvents = new Tinebase_Record_RecordSet('Calendar_Model_Event');
         $futurePersistentExceptionEvents = new Tinebase_Record_RecordSet('Calendar_Model_Event');
         foreach ($persistentExceptionEvents as $persistentExceptionEvent) {
             $persistentExceptionEvent->getOriginalDtStart()->isLater($_event->dtstart) ? $futurePersistentExceptionEvents->addRecord($persistentExceptionEvent) : $pastPersistentExceptionEvents->addRecord($persistentExceptionEvent);
         }
         // update baseEvent
         $rrule = Calendar_Model_Rrule::getRruleFromString($baseEvent->rrule);
         if (isset($rrule->count)) {
             // get all occurences and find the split
             $exdate = $baseEvent->exdate;
             $baseEvent->exdate = NULL;
             //$baseCountOccurrence = Calendar_Model_Rrule::computeNextOccurrence($baseEvent, new Tinebase_Record_RecordSet('Calendar_Model_Event'), $baseEvent->rrule_until, $baseCount);
             $recurSet = Calendar_Model_Rrule::computeRecurrenceSet($baseEvent, new Tinebase_Record_RecordSet('Calendar_Model_Event'), $baseEvent->dtstart, $baseEvent->rrule_until);
             $baseEvent->exdate = $exdate;
             $originalDtstart = $_event->getOriginalDtStart();
             foreach ($recurSet as $idx => $rInstance) {
                 if ($rInstance->dtstart >= $originalDtstart) {
                     break;
                 }
             }
             $rrule->count = $idx + 1;
         } else {
             $lastBaseOccurence = Calendar_Model_Rrule::computeNextOccurrence($baseEvent, new Tinebase_Record_RecordSet('Calendar_Model_Event'), $_event->getOriginalDtStart()->subSecond(1), -1);
             $rrule->until = $lastBaseOccurence ? $lastBaseOccurence->getOriginalDtStart() : $baseEvent->dtstart;
         }
         $baseEvent->rrule = (string) $rrule;
         $baseEvent->exdate = $pastExdates;
         // NOTE: we don't want implicit attendee updates
         //$updatedBaseEvent = $this->update($baseEvent, FALSE);
         $this->_inspectEvent($baseEvent);
         $updatedBaseEvent = parent::update($baseEvent);
         if ($_deleteInstance == TRUE) {
             // delete all future persistent events
             $this->delete($futurePersistentExceptionEvents->getId());
         } else {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " create new recur series for/at: '{$_event->recurid}'");
             }
             // NOTE: in order to move exceptions correctly in time we need to find out the original dtstart
             //       and create the new baseEvent with this time. A following update also updates its exceptions
             $originalDtstart = new Tinebase_DateTime(substr($_event->recurid, -19));
             $adoptedDtstart = clone $_event->dtstart;
             $dtStartHasDiff = $adoptedDtstart->compare($originalDtstart) != 0;
             // php52 compat
             $eventLength = $_event->dtstart->diff($_event->dtend);
             $_event->dtstart = clone $originalDtstart;
             $_event->dtend = clone $originalDtstart;
             $_event->dtend->add($eventLength);
             // adopt count
             if (isset($rrule->count)) {
                 $baseCount = $rrule->count;
                 $rrule = Calendar_Model_Rrule::getRruleFromString($_event->rrule);
                 $rrule->count = $rrule->count - $baseCount;
                 $_event->rrule = (string) $rrule;
             }
             $_event->setId(Tinebase_Record_Abstract::generateUID());
             $_event->uid = $futurePersistentExceptionEvents->uid = Tinebase_Record_Abstract::generateUID();
             $_event->setId(Tinebase_Record_Abstract::generateUID());
             $futurePersistentExceptionEvents->setRecurId($_event->getId());
             unset($_event->recurid);
             unset($_event->base_event_id);
             foreach (array('attendee', 'notes', 'alarms') as $prop) {
                 if ($_event->{$prop} instanceof Tinebase_Record_RecordSet) {
                     $_event->{$prop}->setId(NULL);
                 }
             }
             $_event->exdate = $futureExdates;
             $attendees = $_event->attendee;
             unset($_event->attendee);
             $note = $_event->notes;
             unset($_event->notes);
             $persistentExceptionEvent = $this->create($_event, $_checkBusyConflicts && $dtStartHasDiff);
             // we save attendee separately to preserve their attributes
             if ($attendees instanceof Tinebase_Record_RecordSet) {
                 foreach ($attendees as $attendee) {
                     $this->_createAttender($attendee, $persistentExceptionEvent, true);
                 }
             }
             // @todo save notes and add a update note -> what was updated? -> modlog is also missing
             $persistentExceptionEvent = $this->get($persistentExceptionEvent->getId());
             foreach ($futurePersistentExceptionEvents as $futurePersistentExceptionEvent) {
                 $this->update($futurePersistentExceptionEvent, FALSE);
             }
             if ($dtStartHasDiff) {
                 if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                     Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " new recur series has adpted dtstart -> update to adopt exceptions'");
                 }
                 $persistentExceptionEvent->dtstart = clone $adoptedDtstart;
                 $persistentExceptionEvent->dtend = clone $adoptedDtstart;
                 $persistentExceptionEvent->dtend->add($eventLength);
                 $persistentExceptionEvent = $this->update($persistentExceptionEvent, $_checkBusyConflicts);
             }
         }
     }
     Tinebase_TransactionManager::getInstance()->commitTransaction($transactionId);
     // restore original notification handling
     $this->sendNotifications($sendNotifications);
     $notificationAction = $_deleteInstance ? 'deleted' : 'changed';
     $notificationEvent = $_deleteInstance ? $_event : $persistentExceptionEvent;
     // restore acl
     $this->doContainerACLChecks($doContainerACLChecks);
     // send notifications
     if ($this->_sendNotifications && $_event->mute != 1) {
         // NOTE: recur exception is a fake event from client.
         //       this might lead to problems, so we wrap the calls
         try {
             if (count($_event->attendee) > 0) {
                 $_event->attendee->bypassFilters = TRUE;
             }
             $_event->created_by = $baseEvent->created_by;
             $this->doSendNotifications($notificationEvent, Tinebase_Core::getUser(), $notificationAction, $originalEvent);
         } catch (Exception $e) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " " . $e->getTraceAsString());
             }
             Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . " could not send notification {$e->getMessage()}");
         }
     }
     return $_deleteInstance ? $updatedBaseEvent : $persistentExceptionEvent;
 }