Commit ed623bd8 authored by Simon Gadient's avatar Simon Gadient

[RFT] Get identifier mapping properties function

refs KIME-4583
parent 0f755ee6
...@@ -69,18 +69,29 @@ class SpreadsheetImportService { ...@@ -69,18 +69,29 @@ class SpreadsheetImportService {
protected $validatorResolver; protected $validatorResolver;
/** /**
* Inverse SpreadsheetImport mapping array * Inverse array of SpreadsheetDomain mapping array property. Always use the getter function, which will process the
* property in case it is not set.
* *
* @var array * @var array
*/ */
private $inverseSpreadsheetImportMapping; private $inverseSpreadsheetImportMapping;
/**
* Identifier properties of SpreadsheetDomain mapping array. Always use the getter function, which will process the
* property in case it is not set.
*
* @var array
*/
private $mappingIdentifierProperties;
/** /**
* @param \WE\SpreadsheetImport\Domain\Model\SpreadsheetImport $spreadsheetImport * @param \WE\SpreadsheetImport\Domain\Model\SpreadsheetImport $spreadsheetImport
* *
* @return $this * @return $this
*/ */
public function init(SpreadsheetImport $spreadsheetImport) { public function init(SpreadsheetImport $spreadsheetImport) {
$this->inverseSpreadsheetImportMapping = array();
$this->mappingIdentifierProperties = array();
$this->spreadsheetImport = $spreadsheetImport; $this->spreadsheetImport = $spreadsheetImport;
$this->domain = $this->settings[$spreadsheetImport->getContext()]['domain']; $this->domain = $this->settings[$spreadsheetImport->getContext()]['domain'];
...@@ -154,14 +165,13 @@ class SpreadsheetImportService { ...@@ -154,14 +165,13 @@ class SpreadsheetImportService {
$processedObjectIds = array(); $processedObjectIds = array();
$objectRepository = $this->getDomainRepository(); $objectRepository = $this->getDomainRepository();
$objectValidator = $this->validatorResolver->getBaseValidatorConjunction($this->domain); $objectValidator = $this->validatorResolver->getBaseValidatorConjunction($this->domain);
$identifierProperties = $this->getDomainMappingIdentifierProperties();
$sheet = $this->getFileActiveSheet(); $sheet = $this->getFileActiveSheet();
$persistRecordsChunkSize = intval($this->settings['persistRecordsChunkSize']); $persistRecordsChunkSize = intval($this->settings['persistRecordsChunkSize']);
$totalCount = 0; $totalCount = 0;
/** @var \PHPExcel_Worksheet_Row $row */ /** @var \PHPExcel_Worksheet_Row $row */
foreach ($sheet->getRowIterator(2) as $row) { foreach ($sheet->getRowIterator(2) as $row) {
$totalCount++; $totalCount++;
$object = $this->findObjectByIdentifierPropertiesPerRow($identifierProperties, $row); $object = $this->findObjectByIdentifierPropertiesPerRow($row);
if (is_object($object)) { if (is_object($object)) {
$processedObjectIds[] = $this->persistenceManager->getIdentifierByObject($object); $processedObjectIds[] = $this->persistenceManager->getIdentifierByObject($object);
if ($this->spreadsheetImport->isUpdating()) { if ($this->spreadsheetImport->isUpdating()) {
...@@ -232,37 +242,36 @@ class SpreadsheetImportService { ...@@ -232,37 +242,36 @@ class SpreadsheetImportService {
/** /**
* @return array * @return array
*/ */
private function getDomainMappingIdentifierProperties() { private function getMappingIdentifierProperties() {
// TODO: Don't use the annotation properties but the SpreadsheetImport mapping since we store the Mapping object there as well if (empty($this->mappingIdentifierProperties)) {
$domainMappingProperties = array(); foreach ($this->spreadsheetImport->getMapping() as $property => $columnMapping) {
$properties = $this->reflectionService->getPropertyNamesByAnnotation($this->domain, Mapping::class);
foreach ($properties as $property) {
/** @var Mapping $mapping */ /** @var Mapping $mapping */
$mapping = $this->reflectionService->getPropertyAnnotation($this->domain, $property, Mapping::class); $mapping = $columnMapping['mapping'];
if ($mapping->identifier) { if ($mapping->identifier) {
$domainMappingProperties[$property] = $mapping; $this->mappingIdentifierProperties[$property] = $columnMapping;
} }
} }
return $domainMappingProperties; }
return $this->mappingIdentifierProperties;
} }
/** /**
* @param array $identifierProperties
* @param \PHPExcel_Worksheet_Row $row * @param \PHPExcel_Worksheet_Row $row
* *
* @return null|object * @return null|object
*/ */
private function findObjectByIdentifierPropertiesPerRow(array $identifierProperties, \PHPExcel_Worksheet_Row $row) { private function findObjectByIdentifierPropertiesPerRow(\PHPExcel_Worksheet_Row $row) {
$query = $this->getDomainRepository()->createQuery(); $query = $this->getDomainRepository()->createQuery();
$constraints = array(); $constraints = array();
$spreadsheetImportMapping = $this->spreadsheetImport->getMapping(); $identifierProperties = $this->getMappingIdentifierProperties();
foreach ($identifierProperties as $property => $columnMapping) {
$column = $columnMapping['column'];
/** @var Mapping $mapping */ /** @var Mapping $mapping */
foreach ($identifierProperties as $property => $mapping) { $mapping = $columnMapping['mapping'];
$column = $spreadsheetImportMapping[$property]['column']; $propertyName = $mapping->queryPropertyName ?: $property;
/** @var \PHPExcel_Worksheet_RowCellIterator $cellIterator */ /** @var \PHPExcel_Worksheet_RowCellIterator $cellIterator */
$cellIterator = $row->getCellIterator($column, $column); $cellIterator = $row->getCellIterator($column, $column);
$value = $cellIterator->current()->getValue(); $value = $cellIterator->current()->getValue();
$propertyName = $mapping->queryPropertyName ?: $property;
$constraints[] = $query->equals($propertyName, $value); $constraints[] = $query->equals($propertyName, $value);
} }
$this->mergeQueryConstraintsWithArgumentIdentifiers($query, $constraints); $this->mergeQueryConstraintsWithArgumentIdentifiers($query, $constraints);
...@@ -293,6 +302,18 @@ class SpreadsheetImportService { ...@@ -293,6 +302,18 @@ class SpreadsheetImportService {
} }
} }
/**
* @param array $identifiers
*
* @return \TYPO3\Flow\Persistence\QueryResultInterface
*/
private function findObjectsByExcludedIds(array $identifiers) {
$query = $this->getDomainRepository()->createQuery();
$constraints[] = $query->logicalNot($query->in('Persistence_Object_Identifier', $identifiers));
$this->mergeQueryConstraintsWithArguments($query, $constraints);
return $query->matching($query->logicalAnd($constraints))->execute();
}
/** /**
* @param \TYPO3\Flow\Persistence\QueryInterface $query * @param \TYPO3\Flow\Persistence\QueryInterface $query
* @param array $constraints * @param array $constraints
...@@ -312,24 +333,43 @@ class SpreadsheetImportService { ...@@ -312,24 +333,43 @@ class SpreadsheetImportService {
} }
/** /**
* @param array $identifiers * @param object $object
* * @param \PHPExcel_Worksheet_Row $row
* @return \TYPO3\Flow\Persistence\QueryResultInterface
*/ */
private function findObjectsByExcludedIds(array $identifiers) { private function setObjectPropertiesByRow($object, $row) {
$query = $this->getDomainRepository()->createQuery(); /* Set the argument properties before the mapping properties, as mapping property setters might be dependent on
$constraints[] = $query->logicalNot($query->in('Persistence_Object_Identifier', $identifiers)); argument property values */
$this->mergeQueryConstraintsWithArguments($query, $constraints); $this->setObjectArgumentProperties($object);
return $query->matching($query->logicalAnd($constraints))->execute(); $this->setObjectSpreadsheetImportMappingProperties($object, $row);
}
/**
* @param $object
*/
private function setObjectArgumentProperties($object) {
$contextArguments = $this->settings[$this->spreadsheetImport->getContext()]['arguments'];
if (is_array($contextArguments)) {
$arguments = $this->spreadsheetImport->getArguments();
foreach ($contextArguments as $contextArgument) {
$name = $contextArgument['name'];
if (array_key_exists($name, $arguments)) {
if (array_key_exists('domain', $contextArgument)) {
$value = $this->propertyMapper->convert($arguments[$name], $contextArgument['domain']);
} else {
$value = $arguments[$name];
}
$setter = 'set' . ucfirst($name);
$object->$setter($value);
}
}
}
} }
/** /**
* @param object $object * @param object $object
* @param \PHPExcel_Worksheet_Row $row * @param \PHPExcel_Worksheet_Row $row
*/ */
private function setObjectPropertiesByRow($object, $row) { private function setObjectSpreadsheetImportMappingProperties($object, $row) {
// Set the arguments first as mapping property setters might be dependent on argument properties
$this->setObjectArgumentProperties($object);
$inverseSpreadsheetImportMapping = $this->getInverseSpreadsheetImportMapping(); $inverseSpreadsheetImportMapping = $this->getInverseSpreadsheetImportMapping();
/** @var \PHPExcel_Cell $cell */ /** @var \PHPExcel_Cell $cell */
foreach ($row->getCellIterator() as $cell) { foreach ($row->getCellIterator() as $cell) {
...@@ -349,7 +389,7 @@ class SpreadsheetImportService { ...@@ -349,7 +389,7 @@ class SpreadsheetImportService {
/** /**
* Return an inverse SpreadsheetImport mapping array. It flips the property and column attribute and returns it as a * Return an inverse SpreadsheetImport mapping array. It flips the property and column attribute and returns it as a
* 3-dim array instead of a 2-dim array. The reason for that is the case when the same column is assigned to multiple * 3-dim array instead of a 2-dim array. This is done for the case when the same column is assigned to multiple
* properties. * properties.
*/ */
private function getInverseSpreadsheetImportMapping() { private function getInverseSpreadsheetImportMapping() {
...@@ -363,26 +403,4 @@ class SpreadsheetImportService { ...@@ -363,26 +403,4 @@ class SpreadsheetImportService {
} }
return $this->inverseSpreadsheetImportMapping; return $this->inverseSpreadsheetImportMapping;
} }
/**
* @param $object
*/
private function setObjectArgumentProperties($object) {
$contextArguments = $this->settings[$this->spreadsheetImport->getContext()]['arguments'];
if (is_array($contextArguments)) {
$arguments = $this->spreadsheetImport->getArguments();
foreach ($contextArguments as $contextArgument) {
$name = $contextArgument['name'];
if (array_key_exists($name, $arguments)) {
if (array_key_exists('domain', $contextArgument)) {
$value = $this->propertyMapper->convert($arguments[$name], $contextArgument['domain']);
} else {
$value = $arguments[$name];
}
$setter = 'set' . ucfirst($name);
$object->$setter($value);
}
}
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment