/**
  * Checks if number is unique if manual generated
  * 
  * @param Tinebase_Record_Interface $record
  * @param Boolean $update true if called un update
  * @throws Tinebase_Exception_Duplicate
  * @return boolean
  */
 protected function _checkNumberUniquity($record, $update = FALSE)
 {
     $filterArray = array(array('field' => 'number', 'operator' => 'equals', 'value' => $record->{$this->_numberProperty}));
     if ($update) {
         $filterArray[] = array('field' => 'id', 'operator' => 'notin', 'value' => $record->getId());
     }
     $filterName = $this->_modelName . 'Filter';
     $filter = new $filterName($filterArray);
     $existing = $this->search($filter);
     if (count($existing->toArray()) > 0) {
         $e = new Tinebase_Exception_Duplicate(_('The number you have tried to set is already in use!'));
         $e->setData($existing);
         $e->setClientRecord($record);
         throw $e;
     }
     return true;
 }
 /**
  * do duplicate check (before create)
  *
  * @param   Tinebase_Record_Interface $_record
  * @return  void
  * @throws Tinebase_Exception_Duplicate
  */
 protected function _duplicateCheck(Tinebase_Record_Interface $_record)
 {
     $duplicateFilter = $this->_getDuplicateFilter($_record);
     if ($duplicateFilter === NULL) {
         return;
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Doing duplicate check.');
     }
     $duplicates = $this->search($duplicateFilter, new Tinebase_Model_Pagination(array('limit' => 5)), true);
     if (count($duplicates) > 0) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Found ' . count($duplicates) . ' duplicate(s).');
         }
         $ted = new Tinebase_Exception_Duplicate('Duplicate record(s) found');
         $ted->setModelName($this->_modelName);
         $ted->setData($duplicates);
         $ted->setClientRecord($_record);
         throw $ted;
     } else {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' No duplicates found.');
         }
     }
 }
 /**
  * handle duplicate exceptions
  * 
  * @param Tinebase_Exception_Duplicate $ted
  * @param integer $recordIndex
  * @param Tinebase_Record_Abstract|array $record
  * @param boolean $allowToResolveDuplicates
  * @return array|null exception
  */
 protected function _handleDuplicateExceptions(Tinebase_Exception_Duplicate $ted, $recordIndex, $record = null, $allowToResolveDuplicates = true)
 {
     $firstDuplicateRecord = $ted->getData()->getFirstRecord();
     $resolveStrategy = isset($this->_options['duplicateResolveStrategy']) ? $this->_options['duplicateResolveStrategy'] : null;
     // switch to keep strategy for records of current import run
     if (in_array($firstDuplicateRecord->getId(), $this->_importResult['results']->getArrayOfIds())) {
         $allowToResolveDuplicates = true;
         $resolveStrategy = 'keep';
     }
     if ($resolveStrategy && $allowToResolveDuplicates) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Trying to resolve with configured strategy: ' . $resolveStrategy);
         }
         try {
             if ($resolveStrategy === 'keep') {
                 $updatedRecord = $this->_importAndResolveConflict($ted->getClientRecord(), $resolveStrategy);
                 $this->_importResult['totalcount']++;
             } else {
                 $updatedRecord = $this->_importAndResolveConflict($firstDuplicateRecord, $resolveStrategy, $ted->getClientRecord());
                 $this->_importResult['updatecount']++;
             }
             $this->_importResult['results']->addRecord($updatedRecord);
         } catch (Exception $newException) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Resolving failed. Don't try to resolve duplicates this time");
             }
             $this->_handleImportException($newException, $recordIndex, $record, false);
         }
         $result = null;
     } else {
         $this->_importResult['duplicatecount']++;
         $result = $ted->toArray();
     }
     return $result;
 }
 /**
  * handle duplicate exceptions
  * 
  * @param Tinebase_Exception_Duplicate $ted
  * @param integer $recordIndex
  * @param Tinebase_Record_Abstract|array $record
  * @param boolean $allowToResolveDuplicates
  * @return array|null exception
  */
 protected function _handleDuplicateExceptions(Tinebase_Exception_Duplicate $ted, $recordIndex, $record = null, $allowToResolveDuplicates = true)
 {
     if (!empty($this->_options['duplicateResolveStrategy']) && $allowToResolveDuplicates) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Trying to resolve with configured strategy: ' . $this->_options['duplicateResolveStrategy']);
         }
         try {
             $updatedRecord = $this->_importAndResolveConflict($ted->getData()->getFirstRecord(), $this->_options['duplicateResolveStrategy'], $ted->getClientRecord());
             $this->_importResult['updatecount']++;
             $this->_importResult['results']->addRecord($updatedRecord);
         } catch (Exception $newException) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Resolving failed. Don't try to resolve duplicates this time");
             }
             $this->_handleImportException($newException, $recordIndex, $record, false);
         }
         $result = null;
     } else {
         $this->_importResult['duplicatecount']++;
         $result = $ted->toArray();
     }
     return $result;
 }