Commit 5207e2db authored by Simon Gadient's avatar Simon Gadient

[IMP] Persist the mapping configuration

refs KIME-4583
parent 8185499e
...@@ -29,8 +29,8 @@ class FrontendMappingUtility { ...@@ -29,8 +29,8 @@ class FrontendMappingUtility {
$mappings = array(); $mappings = array();
$domainMappingProperties = $spreadsheetImportService->getMappingProperties(); $domainMappingProperties = $spreadsheetImportService->getMappingProperties();
foreach ($domainMappingProperties as $property => $mapping) { foreach ($domainMappingProperties as $property => $mapping) {
$column = $request->getArgument($property); $columnMapping = array('column' => $request->getArgument($property), 'mapping' => $mapping);
$mappings[$property] = $column; $mappings[$property] = $columnMapping;
} }
return $mappings; return $mappings;
} }
......
...@@ -38,7 +38,7 @@ class SpreadsheetImportService { ...@@ -38,7 +38,7 @@ class SpreadsheetImportService {
/** /**
* @var array * @var array
*/ */
protected $columnPropertyMapping; protected $inverseSpreadsheetImportMapping;
/** /**
* @Flow\InjectConfiguration * @Flow\InjectConfiguration
...@@ -85,7 +85,6 @@ class SpreadsheetImportService { ...@@ -85,7 +85,6 @@ class SpreadsheetImportService {
$this->spreadsheetImport = $spreadsheetImport; $this->spreadsheetImport = $spreadsheetImport;
$this->domain = $this->settings[$spreadsheetImport->getContext()]['domain']; $this->domain = $this->settings[$spreadsheetImport->getContext()]['domain'];
$this->initDomainMappingProperties(); $this->initDomainMappingProperties();
$this->initColumnPropertyMapping();
return $this; return $this;
} }
...@@ -102,25 +101,27 @@ class SpreadsheetImportService { ...@@ -102,25 +101,27 @@ class SpreadsheetImportService {
} }
/** /**
* Flip mapping and return it as a 2-dim array in case the same column is assigned to multiple properties * 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
* properties.
*/ */
private function initColumnPropertyMapping() { private function getInverseSpreadsheetImportMapping() {
$this->columnPropertyMapping = array(); if (empty($this->inverseSpreadsheetImportMapping)) {
foreach ($this->spreadsheetImport->getMapping() as $property => $column) { $this->inverseSpreadsheetImportMapping = array();
$this->columnPropertyMapping[$column][] = $property; foreach ($this->spreadsheetImport->getMapping() as $property => $columnMapping) {
$column = $columnMapping['column'];
$propertyMapping = array('property' => $property, 'mapping' => $columnMapping['mapping']);
$this->inverseSpreadsheetImportMapping[$column][] = $propertyMapping;
}
} }
return $this->inverseSpreadsheetImportMapping;
} }
/** /**
* Adds additional mapping properties to the domain mapping properties retrieved by annotations. This increases * Adds additional mapping properties to the domain mapping properties retrieved by annotations. This increases
* flexibility for dynamic property mapping. * flexibility for dynamic property mapping.
* *
* This was implemented for the single use case to support the Flow package Radmiraal.CouchDB * This is implemented for the single use case to support the Flow package Radmiraal.CouchDB
*
* Note: Those additional property configurations are not persisted and need to be added after each initialization
* of the service. The persisted mappings in the SpreadsheetImport object only contain the property without any
* configuration. Therefore, the import works but only for the default setters and without identifiers. To support
* all, the additional mapping properties need to be persisted together with the mappings.
* *
* @param array $additionalMappingProperties * @param array $additionalMappingProperties
*/ */
...@@ -274,6 +275,7 @@ class SpreadsheetImportService { ...@@ -274,6 +275,7 @@ class SpreadsheetImportService {
* @return array * @return array
*/ */
private function getDomainMappingIdentifierProperties() { private function getDomainMappingIdentifierProperties() {
// TODO: Don't use the annotation properties but the SpreadsheetImport mapping since we store the Mapping object there as well
$domainMappingProperties = array(); $domainMappingProperties = array();
$properties = $this->reflectionService->getPropertyNamesByAnnotation($this->domain, Mapping::class); $properties = $this->reflectionService->getPropertyNamesByAnnotation($this->domain, Mapping::class);
foreach ($properties as $property) { foreach ($properties as $property) {
...@@ -298,7 +300,7 @@ class SpreadsheetImportService { ...@@ -298,7 +300,7 @@ class SpreadsheetImportService {
$spreadsheetImportMapping = $this->spreadsheetImport->getMapping(); $spreadsheetImportMapping = $this->spreadsheetImport->getMapping();
/** @var Mapping $mapping */ /** @var Mapping $mapping */
foreach ($identifierProperties as $property => $mapping) { foreach ($identifierProperties as $property => $mapping) {
$column = $spreadsheetImportMapping[$property]; $column = $spreadsheetImportMapping[$property]['column'];
/** @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();
...@@ -328,20 +330,17 @@ class SpreadsheetImportService { ...@@ -328,20 +330,17 @@ class SpreadsheetImportService {
* @param \PHPExcel_Worksheet_Row $row * @param \PHPExcel_Worksheet_Row $row
*/ */
private function setObjectPropertiesByRow($object, $row) { private function setObjectPropertiesByRow($object, $row) {
$domainMappingProperties = $this->mappingProperties; $inverseSpreadsheetImportMapping = $this->getInverseSpreadsheetImportMapping();
/** @var \PHPExcel_Cell $cell */ /** @var \PHPExcel_Cell $cell */
foreach ($row->getCellIterator() as $cell) { foreach ($row->getCellIterator() as $cell) {
$column = $cell->getColumn(); $column = $cell->getColumn();
if (array_key_exists($column, $this->columnPropertyMapping)) { if (array_key_exists($column, $inverseSpreadsheetImportMapping)) {
$properties = $this->columnPropertyMapping[$column]; $properties = $inverseSpreadsheetImportMapping[$column];
foreach ($properties as $property) { foreach ($properties as $propertyMapping) {
if (array_key_exists($property, $domainMappingProperties)) { $property = $propertyMapping['property'];
/** @var Mapping $mapping */ /** @var Mapping $mapping */
$mapping = $domainMappingProperties[$property]; $mapping = $propertyMapping['mapping'];
$setter = empty($mapping->setter) ? 'set' . ucfirst($property) : $mapping->setter; $setter = empty($mapping->setter) ? 'set' . ucfirst($property) : $mapping->setter;
} else {
$setter = 'set' . ucfirst($property);
}
$object->$setter($cell->getValue()); $object->$setter($cell->getValue());
} }
} }
......
...@@ -62,7 +62,9 @@ class SpreadsheetImportServiceTest extends FunctionalTestCase { ...@@ -62,7 +62,9 @@ class SpreadsheetImportServiceTest extends FunctionalTestCase {
$spreadsheetImport->setContext('testing'); $spreadsheetImport->setContext('testing');
$resource = $this->resourceManager->importResource(__DIR__ . '/Fixtures/Resources/sample.xlsx'); $resource = $this->resourceManager->importResource(__DIR__ . '/Fixtures/Resources/sample.xlsx');
$spreadsheetImport->setFile($resource); $spreadsheetImport->setFile($resource);
$spreadsheetImport->setMapping(array('id' => 'C', 'name' => 'A')); $idMapping = array('column' => 'C', 'mapping' => new Mapping());
$nameMapping = array('column' => 'A', 'mapping' => new Mapping());
$spreadsheetImport->setMapping(array('id' => $idMapping, 'name' => $nameMapping));
$this->spreadsheetImportService->init($spreadsheetImport); $this->spreadsheetImportService->init($spreadsheetImport);
} }
......
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