/**
  * Test eZ\Publish\Core\Repository\NameSchemaService method
  * @covers \eZ\Publish\Core\Repository\NameSchemaService::resolve
  * @dataProvider providerForTestResolve
  */
 public function testResolve($nameSchema, $expectedName)
 {
     /** @var $service \eZ\Publish\Core\Repository\NameSchemaService */
     $service = new NameSchemaService($this->repository);
     list($content, $contentType) = $this->buildTestObjects();
     $name = $service->resolve($nameSchema, $contentType, $content->fields, $content->versionInfo->languageCodes);
     self::assertEquals($expectedName, $name);
 }
 /**
  * Creates a new content draft assigned to the authenticated user.
  *
  * If a different userId is given in $contentCreateStruct it is assigned to the given user
  * but this required special rights for the authenticated user
  * (this is useful for content staging where the transfer process does not
  * have to authenticate with the user which created the content object in the source server).
  * The user has to publish the draft if it should be visible.
  * In 4.x at least one location has to be provided in the location creation array.
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create the content in the given location
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is a provided remoteId which exists in the system
  *                                                                        or there is no location provided (4.x) or multiple locations
  *                                                                        are under the same parent or if the a field value is not accepted by the field type
  * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid
  * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is missing or is set to an empty value
  *
  * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct
  * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs For each location parent under which a location should be created for the content
  *
  * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft
  */
 public function createContent(APIContentCreateStruct $contentCreateStruct, array $locationCreateStructs = array())
 {
     if ($contentCreateStruct->mainLanguageCode === null) {
         throw new InvalidArgumentException("\$contentCreateStruct", "'mainLanguageCode' property must be set");
     }
     if ($contentCreateStruct->contentType === null) {
         throw new InvalidArgumentException("\$contentCreateStruct", "'contentType' property must be set");
     }
     $contentCreateStruct = clone $contentCreateStruct;
     if ($contentCreateStruct->ownerId === null) {
         $contentCreateStruct->ownerId = $this->repository->getCurrentUser()->id;
     }
     if ($contentCreateStruct->alwaysAvailable === null) {
         $contentCreateStruct->alwaysAvailable = false;
     }
     $contentCreateStruct->contentType = $this->repository->getContentTypeService()->loadContentType($contentCreateStruct->contentType->id);
     if (empty($contentCreateStruct->sectionId)) {
         if (isset($locationCreateStructs[0])) {
             $location = $this->repository->getLocationService()->loadLocation($locationCreateStructs[0]->parentLocationId);
             $contentCreateStruct->sectionId = $location->contentInfo->sectionId;
         } else {
             $contentCreateStruct->sectionId = 1;
         }
     }
     if (!$this->repository->canUser('content', 'create', $contentCreateStruct, $locationCreateStructs)) {
         throw new UnauthorizedException('content', 'create', array('parentLocationId' => isset($locationCreateStructs[0]) ? $locationCreateStructs[0]->parentLocationId : null, 'sectionId' => $contentCreateStruct->sectionId));
     }
     if (!empty($contentCreateStruct->remoteId)) {
         try {
             $this->loadContentByRemoteId($contentCreateStruct->remoteId);
             throw new InvalidArgumentException("\$contentCreateStruct", "Another content with remoteId '{$contentCreateStruct->remoteId}' exists");
         } catch (APINotFoundException $e) {
             // Do nothing
         }
     } else {
         $contentCreateStruct->remoteId = $this->domainMapper->getUniqueHash($contentCreateStruct);
     }
     $spiLocationCreateStructs = $this->buildSPILocationCreateStructs($locationCreateStructs);
     $languageCodes = $this->getLanguageCodesForCreate($contentCreateStruct);
     $fields = $this->mapFieldsForCreate($contentCreateStruct);
     $fieldValues = array();
     $spiFields = array();
     $allFieldErrors = array();
     $inputRelations = array();
     $locationIdToContentIdMapping = array();
     foreach ($contentCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) {
         /** @var $fieldType \eZ\Publish\Core\FieldType\FieldType */
         $fieldType = $this->repository->getFieldTypeService()->buildFieldType($fieldDefinition->fieldTypeIdentifier);
         foreach ($languageCodes as $languageCode) {
             $isEmptyValue = false;
             $valueLanguageCode = $fieldDefinition->isTranslatable ? $languageCode : $contentCreateStruct->mainLanguageCode;
             $isLanguageMain = $languageCode === $contentCreateStruct->mainLanguageCode;
             if (isset($fields[$fieldDefinition->identifier][$valueLanguageCode])) {
                 $fieldValue = $fields[$fieldDefinition->identifier][$valueLanguageCode]->value;
             } else {
                 $fieldValue = $fieldDefinition->defaultValue;
             }
             $fieldValue = $fieldType->acceptValue($fieldValue);
             if ($fieldType->isEmptyValue($fieldValue)) {
                 $isEmptyValue = true;
                 if ($fieldDefinition->isRequired) {
                     throw new ContentValidationException("Value for required field definition '{$fieldDefinition->identifier}' with language '{$languageCode}' is empty");
                 }
             } else {
                 $fieldErrors = $fieldType->validate($fieldDefinition, $fieldValue);
                 if (!empty($fieldErrors)) {
                     $allFieldErrors[$fieldDefinition->id][$languageCode] = $fieldErrors;
                 }
             }
             if (!empty($allFieldErrors)) {
                 continue;
             }
             $this->relationProcessor->appendFieldRelations($inputRelations, $locationIdToContentIdMapping, $fieldType, $fieldValue, $fieldDefinition->id);
             $fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue;
             // Only non-empty value for: translatable field or in main language
             if (!$isEmptyValue && $fieldDefinition->isTranslatable || !$isEmptyValue && $isLanguageMain) {
                 $spiFields[] = new SPIField(array("id" => null, "fieldDefinitionId" => $fieldDefinition->id, "type" => $fieldDefinition->fieldTypeIdentifier, "value" => $fieldType->toPersistenceValue($fieldValue), "languageCode" => $languageCode, "versionNo" => null));
             }
         }
     }
     if (!empty($allFieldErrors)) {
         throw new ContentFieldValidationException($allFieldErrors);
     }
     $spiContentCreateStruct = new SPIContentCreateStruct(array("name" => $this->nameSchemaService->resolve($contentCreateStruct->contentType->nameSchema, $contentCreateStruct->contentType, $fieldValues, $languageCodes), "typeId" => $contentCreateStruct->contentType->id, "sectionId" => $contentCreateStruct->sectionId, "ownerId" => $contentCreateStruct->ownerId, "locations" => $spiLocationCreateStructs, "fields" => $spiFields, "alwaysAvailable" => $contentCreateStruct->alwaysAvailable, "remoteId" => $contentCreateStruct->remoteId, "modified" => isset($contentCreateStruct->modificationDate) ? $contentCreateStruct->modificationDate->getTimestamp() : time(), "initialLanguageId" => $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode($contentCreateStruct->mainLanguageCode)->id));
     $defaultObjectStates = $this->getDefaultObjectStates();
     $this->repository->beginTransaction();
     try {
         $spiContent = $this->persistenceHandler->contentHandler()->create($spiContentCreateStruct);
         $this->relationProcessor->processFieldRelations($inputRelations, $spiContent->versionInfo->contentInfo->id, $spiContent->versionInfo->versionNo, $contentCreateStruct->contentType);
         foreach ($defaultObjectStates as $objectStateGroupId => $objectState) {
             $this->persistenceHandler->objectStateHandler()->setContentState($spiContent->versionInfo->contentInfo->id, $objectStateGroupId, $objectState->id);
         }
         $this->repository->commit();
     } catch (Exception $e) {
         $this->repository->rollback();
         throw $e;
     }
     return $this->domainMapper->buildContentDomainObject($spiContent);
 }
 /**
  * Creates a new content draft assigned to the authenticated user.
  *
  * If a different userId is given in $contentCreateStruct it is assigned to the given user
  * but this required special rights for the authenticated user
  * (this is useful for content staging where the transfer process does not
  * have to authenticate with the user which created the content object in the source server).
  * The user has to publish the draft if it should be visible.
  * In 4.x at least one location has to be provided in the location creation array.
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create the content in the given location
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is a provided remoteId which exists in the system
  *                                                                        or there is no location provided (4.x) or multiple locations
  *                                                                        are under the same parent or if the a field value is not accepted by the field type
  * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid
  * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is missing or is set to an empty value
  *
  * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct
  * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs For each location parent under which a location should be created for the content
  *
  * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft
  */
 public function createContent(APIContentCreateStruct $contentCreateStruct, array $locationCreateStructs = array())
 {
     #$startzeit=microtime(true);
     if ($contentCreateStruct->mainLanguageCode === null) {
         throw new InvalidArgumentException("\$contentCreateStruct", "'mainLanguageCode' property must be set");
     }
     if ($contentCreateStruct->contentType === null) {
         throw new InvalidArgumentException("\$contentCreateStruct", "'contentType' property must be set");
     }
     $contentCreateStruct = clone $contentCreateStruct;
     if ($contentCreateStruct->ownerId === null) {
         $contentCreateStruct->ownerId = $this->repository->getCurrentUser()->id;
     }
     if ($contentCreateStruct->alwaysAvailable === null) {
         $contentCreateStruct->alwaysAvailable = false;
     }
     $contentCreateStruct->contentType = $this->repository->getContentTypeService()->loadContentType($contentCreateStruct->contentType->id);
     $contentCreateStruct->sectionId = 1;
     if (empty($contentCreateStruct->sectionId)) {
         if (isset($locationCreateStructs[0])) {
             $location = $this->repository->getLocationService()->loadLocation($locationCreateStructs[0]->parentLocationId);
             $contentCreateStruct->sectionId = $location->contentInfo->sectionId;
         } else {
             $contentCreateStruct->sectionId = 1;
         }
     }
     /*
     if ( !$this->repository->canUser( 'content', 'create', $contentCreateStruct, $locationCreateStructs ) )
     {
         throw new UnauthorizedException( 'content', 'create' );
     }
     */
     /*
     if ( !empty( $contentCreateStruct->remoteId ) )
     {
         try
         {
             $this->loadContentByRemoteId( $contentCreateStruct->remoteId );
     
             throw new InvalidArgumentException(
                 "\$contentCreateStruct",
                 "Another content with remoteId '{$contentCreateStruct->remoteId}' exists"
             );
         }
         catch ( APINotFoundException $e )
         {
             // Do nothing
         }
     }
     else
     {
         $contentCreateStruct->remoteId = $this->domainMapper->getUniqueHash( $contentCreateStruct );
     }
     */
     #Expect RemoteID is unique
     $contentCreateStruct->remoteId = $this->domainMapper->getUniqueHash($contentCreateStruct);
     $spiLocationCreateStructs = $this->buildSPILocationCreateStructs($locationCreateStructs);
     $languageCodes = $this->getLanguageCodesForCreate($contentCreateStruct);
     $fields = $this->mapFieldsForCreate($contentCreateStruct);
     $fieldValues = array();
     $SolrDocFields = array();
     $spiFields = array();
     $allFieldErrors = array();
     $inputRelations = array();
     $locationIdToContentIdMapping = array();
     foreach ($contentCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) {
         /** @var $fieldType \eZ\Publish\Core\FieldType\FieldType */
         $fieldType = $this->repository->getFieldTypeService()->buildFieldType($fieldDefinition->fieldTypeIdentifier);
         foreach ($languageCodes as $languageCode) {
             $isEmptyValue = false;
             $valueLanguageCode = $fieldDefinition->isTranslatable ? $languageCode : $contentCreateStruct->mainLanguageCode;
             $isLanguageMain = $languageCode === $contentCreateStruct->mainLanguageCode;
             if (isset($fields[$fieldDefinition->identifier][$valueLanguageCode])) {
                 $fieldValue = $fields[$fieldDefinition->identifier][$valueLanguageCode]->value;
             } else {
                 $fieldValue = $fieldDefinition->defaultValue;
             }
             $fieldValue = $fieldType->acceptValue($fieldValue);
             if ($fieldType->isEmptyValue($fieldValue)) {
                 $isEmptyValue = true;
                 if ($fieldDefinition->isRequired) {
                     throw new ContentValidationException("Value for required field definition '{$fieldDefinition->identifier}' with language '{$languageCode}' is empty");
                 }
             } else {
                 $fieldErrors = $fieldType->validate($fieldDefinition, $fieldValue);
                 if (!empty($fieldErrors)) {
                     $allFieldErrors[$fieldDefinition->id][$languageCode] = $fieldErrors;
                 }
             }
             if (!empty($allFieldErrors)) {
                 continue;
             }
             $this->relationProcessor->appendFieldRelations($inputRelations, $locationIdToContentIdMapping, $fieldType, $fieldValue, $fieldDefinition->id);
             $fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue;
             // Only non-empty value for: translatable field or in main language
             if (!$isEmptyValue && $fieldDefinition->isTranslatable || !$isEmptyValue && $isLanguageMain) {
                 $spiFields[] = new SPIField(array("id" => null, "fieldDefinitionId" => $fieldDefinition->id, "type" => $fieldDefinition->fieldTypeIdentifier, "value" => $fieldType->toPersistenceValue($fieldValue), "languageCode" => $languageCode, "versionNo" => null));
                 $SolrDocFields[] = array("identifier" => $fieldDefinition->identifier, "type" => $fieldDefinition->fieldTypeIdentifier, "value" => $fields[$fieldDefinition->identifier][$valueLanguageCode]->value, "data" => $fieldType->toPersistenceValue($fieldValue)->data, "sortkey" => $fieldType->toPersistenceValue($fieldValue)->sortKey, "languageCode" => $languageCode, "searchable" => $fieldDefinition->isSearchable);
             }
         }
     }
     if (!empty($allFieldErrors)) {
         throw new ContentFieldValidationException($allFieldErrors);
     }
     $spiContentCreateStruct = new SPIContentCreateStruct(array("name" => $this->nameSchemaService->resolve($contentCreateStruct->contentType->nameSchema, $contentCreateStruct->contentType, $fieldValues, $languageCodes), "typeId" => $contentCreateStruct->contentType->id, "sectionId" => $contentCreateStruct->sectionId, "ownerId" => $contentCreateStruct->ownerId, "locations" => $spiLocationCreateStructs, "fields" => $spiFields, "alwaysAvailable" => $contentCreateStruct->alwaysAvailable, "remoteId" => $contentCreateStruct->remoteId, "modified" => isset($contentCreateStruct->modificationDate) ? $contentCreateStruct->modificationDate->getTimestamp() : time(), "initialLanguageId" => $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode($contentCreateStruct->mainLanguageCode)->id));
     if (!is_numeric($spiContentCreateStruct->locations[0]->parentId) && $spiContentCreateStruct->locations[0]->parentId != "") {
         $url = trim($spiContentCreateStruct->locations[0]->parentId, '/');
         $url_array = explode("/", $url);
         $url_array[] = $spiContentCreateStruct->remoteId;
         $url_alias_cats = array();
         foreach ($url_array as $depth => $part) {
             $fullpart = "";
             for ($i = 0; $i <= $depth; $i++) {
                 $fullpart .= $url_array[$i] . "/";
             }
             $url_alias_cats[] = $depth . "/" . $fullpart;
         }
         $url_alias = $url_alias_cats;
         $lasturlelement = array_pop($url_alias_cats);
         $parent_url_alias = $url_alias_cats;
     } else {
         $url_alias_cats = array("0/" . $spiContentCreateStruct->locations[0]->parentId, "1/" . $spiContentCreateStruct->locations[0]->parentId . "/" . $spiContentCreateStruct->remoteId);
         $url_alias = $url_alias_cats;
         $lasturlelement = array_pop($url_alias_cats);
         $parent_url_alias = $url_alias_cats;
     }
     # we ommit defaultObjectStates!!!!
     #$defaultObjectStates = $this->getDefaultObjectStates();
     #This is the point where a new version is created
     #We create a version 1 of solrDoc
     #$spiContent = $this->persistenceHandler->contentHandler()->create( $spiContentCreateStruct );
     #$startzeit=microtime(true);
     $solrserverconfig = Globals::getSolrServerConfig();
     $solrglobalconfig = Globals::getSolrGlobalConfig();
     $client = new \Solarium\Client($solrserverconfig);
     $update = $client->createUpdate();
     $doc1 = $update->createDocument();
     $doc1->meta_installation_id_ms = $solrglobalconfig["meta_installation_id_ms"];
     $doc1->meta_guid_ms = $spiContentCreateStruct->remoteId;
     $doc1->meta_name_t = $spiContentCreateStruct->name["ger-DE"];
     $doc1->meta_sort_name_ms = $spiContentCreateStruct->name["ger-DE"];
     $doc1->meta_remote_id_ms = $spiContentCreateStruct->remoteId;
     $doc1->meta_current_version_si = 1;
     $doc1->meta_class_identifier_ms = $contentCreateStruct->contentType->identifier;
     $doc1->meta_class_name_ms = $contentCreateStruct->contentType->names["ger-DE"];
     $doc1->meta_contentclass_id_si = $spiContentCreateStruct->typeId;
     $doc1->meta_url_alias_ms = $url_alias;
     $doc1->meta_parent_url_alias_ms = array_pop($parent_url_alias);
     $doc1->meta_main_url_alias_ms = array_pop($url_alias);
     $doc1->meta_language_code_ms = $solrglobalconfig["meta_language_code_ms"];
     $date = new DateTime();
     $date->setTimestamp($spiContentCreateStruct->modified);
     $doc1->meta_published_dt = $date->format("Y-m-d\\TH:i:s\\Z");
     $doc1->timestamp = $date->format("Y-m-d\\TH:i:s\\Z");
     $doc1->meta_id_si = 0;
     /*
             // is that really nessecary?
             #$doc1->meta_main_parent_node_id_si= 61;
             #$doc1->meta_node_id_si=array( 68 );
             $doc1->is_solrdoc_b = true;
             $doc1->meta_installation_url_ms = $solrglobalconfig["meta_installation_id_ms"];
             $doc1->meta_id_si = 0;
             $doc1->meta_section_id_si = $spiContentCreateStruct->sectionId;
             $doc1->meta_owner_id_si = $spiContentCreateStruct->ownerId;
             $doc1->meta_modified_dt=$date->format( "Y-m-d\\TH:i:s\\Z" );
             $doc1->meta_is_hidden_b=array( false );
             $doc1->meta_is_invisible_b=array( false );
             $doc1->meta_always_available_b = $contentCreateStruct->alwaysAvailable;
             $doc1->meta_sort_field_ms=array( "1" );
             $doc1->meta_sort_order_ms=array( "1");
             $doc1->meta_priority_si=array( 0 );
             $doc1->meta_view_count_si=array(0);
             $doc1->meta_owner_name_t = $solrglobalconfig["meta_owner_name_t"];
             $doc1->meta_owner_group_id_si = array( $solrglobalconfig["meta_owner_group_id_si"] );
             $doc1->meta_object_states_si=array( $solrglobalconfig["meta_object_states_si"] );
     */
     // LoopThroughFields
     #var_dump(count($SolrDocFields));
     #var_dump($SolrDocFields);
     #die("sdkfj");
     foreach ($SolrDocFields as $solrField) {
         if ($solrField["type"] == "eztext") {
             $ident = "attr_" . $solrField["identifier"] . "_t";
             $solrvalue = $solrField["value"];
         }
         if ($solrField["type"] == "ezstring") {
             $ident = "attr_" . $solrField["identifier"] . "_s";
             $solrvalue = $solrField["value"];
         }
         if ($solrField["type"] == "ezboolean") {
             $ident = "attr_" . $solrField["identifier"] . "_b";
             $solrvalue = $solrField["value"];
         }
         if ($solrField["type"] == "ezinteger") {
             $ident = "attr_" . $solrField["identifier"] . "_si";
             $solrvalue = $solrField["value"];
         }
         if ($solrField["type"] == "ezfloat") {
             $ident = "attr_" . $solrField["identifier"] . "_f";
             $solrvalue = (double) $solrField["value"];
         }
         if ($solrField["type"] == "ezkeyword") {
             $ident = "attr_" . $solrField["identifier"] . "____k";
             $solrvalue = $solrField["value"];
         }
         if ($solrField["type"] == "ezurl") {
             $ident = "attr_" . $solrField["identifier"] . "_ms";
             $solrvalue = $solrField["value"];
         }
         if ($solrField["type"] == "ezdatetime" or $solrField["type"] == "ezdate") {
             $ident = "attr_" . $solrField["identifier"] . "_dt";
             $date = new DateTime();
             $date->setTimestamp((int) $solrField["value"]);
             $solrvalue = $date->format("Y-m-d\\TH:i:s\\Z");
         }
         if ($solrField["type"] == "ezxmltext") {
             $ident = "attr_" . $solrField["identifier"] . "_ms";
             $textcontent = $solrField["data"]->textContent;
             $solrvalue = $solrField["value"];
             $tident = "attr_" . $solrField["identifier"] . "_s";
             $doc1->{$tident} = $textcontent;
         }
         if ($solrField["type"] == "ezgmaplocation") {
             $ident = "attr_" . $solrField["identifier"] . "_gpt";
             $solrvalue = $solrField["value"]["longitude"] . "," . $solrField["value"]["latitude"];
         }
         $doc1->{$ident} = $solrvalue;
     }
     $update->addDocuments(array($doc1));
     $result = $client->update($update);
     /*
     $durationInMilliseconds = (microtime(true) - $startzeit) * 1000;
     $timing = number_format($durationInMilliseconds, 3, '.', '') . "ms";
     if($durationInMilliseconds > 1000)
     {
         $timing = number_format($durationInMilliseconds / 1000, 1, '.', '') . "sec";
     }
     echo "\nI:" . $timing . "\n";
     */
     /* Orig creation
             
             $this->repository->beginTransaction();
             try
             {
                 $spiContent = $this->persistenceHandler->contentHandler()->create( $spiContentCreateStruct );
                 $this->relationProcessor->processFieldRelations(
                     $inputRelations,
                     $spiContent->versionInfo->contentInfo->id,
                     $spiContent->versionInfo->versionNo,
                     $contentCreateStruct->contentType
                 );
     
                 foreach ( $defaultObjectStates as $objectStateGroupId => $objectState )
                 {
                     $this->persistenceHandler->objectStateHandler()->setContentState(
                         $spiContent->versionInfo->contentInfo->id,
                         $objectStateGroupId,
                         $objectState->id
                     );
                 }
     
                 $this->repository->commit();
             }
             catch ( Exception $e )
             {
                 $this->repository->rollback();
                 throw $e;
             }
         
             return $this->domainMapper->buildContentDomainObject( $spiContent );
             */
 }