/**
  *
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
  *
  * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs
  *
  * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct[]
  */
 protected function buildSPILocationCreateStructs(array $locationCreateStructs)
 {
     $spiLocationCreateStructs = array();
     $parentLocationIdSet = array();
     $mainLocation = true;
     foreach ($locationCreateStructs as $locationCreateStruct) {
         if (isset($parentLocationIdSet[$locationCreateStruct->parentLocationId])) {
             throw new InvalidArgumentException("\$locationCreateStructs", "Multiple LocationCreateStructs with the same parent Location '{$locationCreateStruct->parentLocationId}' are given");
         }
         $parentLocationIdSet[$locationCreateStruct->parentLocationId] = true;
         $parentLocation = $this->repository->getLocationService()->loadLocation($locationCreateStruct->parentLocationId);
         $spiLocationCreateStructs[] = $this->domainMapper->buildSPILocationCreateStruct($locationCreateStruct, $parentLocation, $mainLocation, null, null);
         // First Location in the list will be created as main Location
         $mainLocation = false;
     }
     return $spiLocationCreateStructs;
 }
 /**
  * Creates the new $location in the content repository for the given content
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create this location
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the content is already below the specified parent
  *                                        or the parent is a sub location of the location of the content
  *                                        or if set the remoteId exists already
  *
  * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
  *
  * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct
  *
  * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created Location
  */
 public function createLocation(ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct)
 {
     $content = $this->repository->getContentService()->loadContent($contentInfo->id);
     $parentLocation = $this->loadLocation($locationCreateStruct->parentLocationId);
     if (!$this->repository->canUser('content', 'create', $content->contentInfo, $parentLocation)) {
         throw new UnauthorizedException('content', 'create');
     }
     // Check if the parent is a sub location of one of the existing content locations (this also solves the
     // situation where parent location actually one of the content locations),
     // or if the content already has location below given location create struct parent
     $existingContentLocations = $this->loadLocations($content->contentInfo);
     if (!empty($existingContentLocations)) {
         foreach ($existingContentLocations as $existingContentLocation) {
             if (stripos($parentLocation->pathString, $existingContentLocation->pathString) !== false) {
                 throw new InvalidArgumentException("\$locationCreateStruct", "Specified parent is a sub location of one of the existing content locations.");
             }
             if ($parentLocation->id == $existingContentLocation->parentLocationId) {
                 throw new InvalidArgumentException("\$locationCreateStruct", "Content is already below the specified parent.");
             }
         }
     }
     $spiLocationCreateStruct = $this->domainMapper->buildSPILocationCreateStruct($locationCreateStruct, $parentLocation, $content->contentInfo->mainLocationId !== null ? $content->contentInfo->mainLocationId : true, $content->contentInfo->id, $content->contentInfo->currentVersionNo);
     $this->repository->beginTransaction();
     try {
         $newLocation = $this->persistenceHandler->locationHandler()->create($spiLocationCreateStruct);
         $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content);
         foreach ($urlAliasNames as $languageCode => $name) {
             $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation($newLocation->id, $newLocation->parentId, $name, $languageCode, $content->contentInfo->alwaysAvailable, $languageCode === $content->contentInfo->mainLanguageCode);
         }
         $this->repository->commit();
     } catch (Exception $e) {
         $this->repository->rollback();
         throw $e;
     }
     return $this->domainMapper->buildLocationDomainObject($newLocation);
 }