/**
  * Handle the assertion that the given result object has no errors
  *
  * @param \TYPO3\Flow\Error\Result $result
  * @param boolean $expectSuccess
  * @return void
  */
 protected function assertSuccess(\TYPO3\Flow\Error\Result $result, $expectSuccess = true)
 {
     if ($expectSuccess === true) {
         $this->assertFalse($result->hasErrors());
     } else {
         $this->assertTrue($result->hasErrors());
     }
 }
 /**
  * @param \Flowpack\ElasticSearch\Domain\Model\AbstractType $type
  * @param string $id
  * @param \Flowpack\ElasticSearch\Transfer\Response $response
  * @throws \Flowpack\ElasticSearch\Domain\Exception\DocumentPropertiesMismatchException
  * @return \Flowpack\ElasticSearch\Domain\Model\Document
  */
 public function createFromResponse(Model\AbstractType $type, $id = null, \Flowpack\ElasticSearch\Transfer\Response $response)
 {
     $content = $response->getTreatedContent();
     $verificationResults = new ErrorResult();
     if (isset($content['_index']) && $type->getIndex()->getName() !== $content['_index']) {
         $error = new Error('The received index name "%s" does not match the expected one "%s".', 1340264838, array($content['_index'], $type->getIndex()->getName()));
         $verificationResults->addError($error);
     }
     if (isset($content['_type']) && $type->getName() !== $content['_type']) {
         $error = new Error('The received type name "%s" does not match the expected one "%s".', 1340265103, array($content['_type'], $type->getName()));
         $verificationResults->addError($error);
     }
     if (isset($content['_id']) && $id !== null && $id !== $content['_id']) {
         $error = new Error('The received id "%s" does not match the expected one "%s".', 1340269758, array($content['_id'], $id));
         $verificationResults->addError($error);
     }
     if ($verificationResults->hasErrors()) {
         $exception = new \Flowpack\ElasticSearch\Domain\Exception\DocumentPropertiesMismatchException('The document\'s properties do not match the expected ones.', 1340265248);
         $exception->setErrorResult($verificationResults);
         throw $exception;
     }
     $version = $content['_version'];
     $data = $content['_source'];
     return new Model\Document($type, $data, $id, $version);
 }
 /**
  * @return boolean TRUE if the argument is valid, FALSE otherwise
  * @api
  */
 public function isValid()
 {
     return !$this->validationResults->hasErrors();
 }
 /**
  * Shows the status of the current mapping
  *
  * @param string $object Class name of a domain object. If given, will only work on this single object
  * @param boolean $conductUpdate Set to TRUE to conduct the required corrections
  * @param string $clientName The client name to use
  */
 public function statusCommand($object = null, $conductUpdate = false, $clientName = null)
 {
     $result = new ErrorResult();
     $client = $this->clientFactory->create($clientName);
     $classesAndAnnotations = $this->indexInformer->getClassesAndAnnotations();
     if ($object !== null) {
         if (!isset($classesAndAnnotations[$object])) {
             $this->outputFormatted("Error: Object '<b>%s</b>' is not configured correctly, check the Indexable annotation.", array($object));
             $this->quit(1);
         }
         $classesAndAnnotations = array($object => $classesAndAnnotations[$object]);
     }
     array_walk($classesAndAnnotations, function (Indexable $annotation, $className) use($result, $client, $conductUpdate) {
         $this->outputFormatted("Object %s", array($className), 4);
         $this->outputFormatted("Index <b>%s</b> Type <b>%s</b>", array($annotation->indexName, $annotation->typeName), 8);
         $count = $client->findIndex($annotation->indexName)->findType($annotation->typeName)->count();
         if ($count === null) {
             $result->forProperty($className)->addError(new Error('ElasticSearch was unable to retrieve a count for the type "%s" at index "%s". Probably these don\' exist.', 1340289921, array($annotation->typeName, $annotation->indexName)));
         }
         $this->outputFormatted("Documents in Search: <b>%s</b>", array($count !== null ? $count : "Error"), 8);
         try {
             $count = $this->persistenceManager->createQueryForType($className)->count();
         } catch (\Exception $exception) {
             $count = null;
             $result->forProperty($className)->addError(new Error('The persistence backend was unable to retrieve a count for the type "%s". The exception message was "%s".', 1340290088, array($className, $exception->getMessage())));
         }
         $this->outputFormatted("Documents in Persistence: <b>%s</b>", array($count !== null ? $count : "Error"), 8);
         if (!$result->forProperty($className)->hasErrors()) {
             $states = $this->getModificationsNeededStatesAndIdentifiers($client, $className);
             if ($conductUpdate) {
                 $inserted = 0;
                 $updated = 0;
                 foreach ($states[ObjectIndexer::ACTION_TYPE_CREATE] as $identifier) {
                     try {
                         $this->objectIndexer->indexObject($this->persistenceManager->getObjectByIdentifier($identifier, $className));
                         $inserted++;
                     } catch (\Exception $exception) {
                         $result->forProperty($className)->addError(new Error('An error occurred while trying to add an object to the ElasticSearch backend. The exception message was "%s".', 1340356330, array($exception->getMessage())));
                     }
                 }
                 foreach ($states[ObjectIndexer::ACTION_TYPE_UPDATE] as $identifier) {
                     try {
                         $this->objectIndexer->indexObject($this->persistenceManager->getObjectByIdentifier($identifier, $className));
                         $updated++;
                     } catch (\Exception $exception) {
                         $result->forProperty($className)->addError(new Error('An error occurred while trying to update an object to the ElasticSearch backend. The exception message was "%s".', 1340358590, array($exception->getMessage())));
                     }
                 }
                 $this->outputFormatted("Objects inserted: <b>%s</b>", array($inserted), 8);
                 $this->outputFormatted("Objects updated: <b>%s</b>", array($updated), 8);
             } else {
                 $this->outputFormatted("Modifications needed: <b>create</b> %d, <b>update</b> %d", array(count($states[ObjectIndexer::ACTION_TYPE_CREATE]), count($states[ObjectIndexer::ACTION_TYPE_UPDATE])), 8);
             }
         }
     });
     if ($result->hasErrors()) {
         $this->outputLine();
         $this->outputLine('The following errors occurred:');
         /** @var $error \TYPO3\Flow\Error\Error */
         foreach ($result->getFlattenedErrors() as $className => $errors) {
             foreach ($errors as $error) {
                 $this->outputLine();
                 $this->outputFormatted("<b>Error</b> for %s:", array($className), 8);
                 $this->outputFormatted((string) $error, array(), 4);
             }
         }
     }
 }
 /**
  * Assert that the validation result object has no errors
  *
  * @param \TYPO3\Flow\Error\Result $result
  * @param string $message
  */
 protected function assertNotHasErrors(\TYPO3\Flow\Error\Result $result, $message = '')
 {
     self::assertThat($result->hasErrors(), self::isFalse(), $message);
 }