/**
  *   
  * @param \DLigo\Animaltool\Domain\Model\Animal $oldAnimal
  * @param \DLigo\Animaltool\Domain\Model\Animal $animal
  * @param string $tryagain   
  * @param string $tryagain   
  * @param boolean $external   
  * @param boolean $cancel   
  * @return void
  */
 public function mergeAction(Animal $oldAnimal, Animal $animal, $tryagain = '', $external = false, $cancel = null)
 {
     if ($cancel) {
         $isrfid = substr($animal->getRFID(), 0, 7) === '_DUMMY_';
         if ($isrfid) {
             $animal->setRFID(null);
         } else {
             $animal->setEarTag(null);
         }
         $animal->setIsDummy(false);
         $this->animalRepository->update($animal);
         $this->addFlashMessage('Animals not merged. RFID/Eartag removed', '', \TYPO3\Flow\Error\Message::SEVERITY_OK, array(), 'flash.animal.merge.cancel');
         $this->redirect('index');
     }
     if (!empty($tryagain)) {
         $isrfid = substr($animal->getRFID(), 0, 7) === '_DUMMY_';
         if ($isrfid) {
             $old = $this->animalRepository->findByRFID($tryagain)->getFirst();
         } else {
             $old = $this->animalRepository->findByEarTag($tryagain)->getFirst();
         }
         if (empty($old)) {
             if ($isrfid) {
                 $animal->setRFID($tryagain);
             } else {
                 $animal->setEarTag($tryagain);
             }
             $animal->setIsDummy(false);
             $this->animalRepository->update($animal);
             $this->redirect('index');
         } else {
             $tag = "_DUMMY_" . $tryagain . "_" . time();
             if ($isrfid) {
                 $animal->setRFID($tag);
             } else {
                 $animal->setEarTag($tag);
             }
             $this->animalRepository->update($animal);
             $this->redirect('duplicate', null, null, array('animal' => $animal, 'oldAnimal' => $old, 'external' => $external));
         }
     }
     $overrideif = array('Weight', 'Birthday', 'Photo', 'Gender', 'Bread', 'Color', 'EarTag');
     $merge = array('Comment', 'SpecialProperties');
     $add = array('Actions', 'Treatments');
     foreach ($overrideif as $key) {
         $get = 'get' . $key;
         $set = 'set' . $key;
         $val = $animal->{$get}();
         if (empty($val)) {
             $animal->{$set}($oldAnimal->{$get}());
         }
     }
     foreach ($merge as $key) {
         $get = 'get' . $key;
         $set = 'set' . $key;
         $nl = '';
         $o = $animal->{$get}();
         $n = $oldAnimal->{$get}();
         if (!empty($o) && !empty($n)) {
             $nl = "\n";
         }
         $animal->{$set}($animal->{$get}() . $nl . $oldAnimal->{$get}());
     }
     foreach ($add as $key) {
         $get = 'get' . $key;
         $set = 'set' . $key;
         $oldCol = $oldAnimal->{$get}();
         $newCol = $animal->{$get}();
         $col = new \Doctrine\Common\Collections\ArrayCollection();
         foreach ($oldCol as $obj) {
             $nobj = clone $obj;
             $col->add($nobj);
             $nobj->setAnimal($animal);
         }
         $animal->stopOpenTreatments();
         foreach ($newCol as $obj) {
             $nobj = $obj;
             $col->add($nobj);
             $nobj->setAnimal($animal);
         }
         $animal->{$set}($col);
     }
     if ($oldAnimal->getIsPrivate() || $animal->getIsPrivate()) {
         $animal->setIsPrivate(true);
         $owner = $oldAnimal->getOwner();
         if ($animal->getOwner() == null && $oldAnimal->getOwner() != null) {
             $owner = clone $oldAnimal->getOwner();
         }
     }
     $animal->setRFID($oldAnimal->getRFID());
     $this->animalRepository->remove($oldAnimal);
     $this->persistenceManager->persistAll();
     $animal->setIsDummy(false);
     $this->animalRepository->update($animal);
     if ($external) {
         $this->redirect('open', 'External', null, array('animal' => $animal));
     }
     $this->redirect('index');
 }