Commit 28a8edde authored by Chhengleap Soem's avatar Chhengleap Soem

Merge branch 'KIME-4583' into 'master'

Property mapping



See merge request !2
parents fed606d3 4dab0562
...@@ -20,16 +20,29 @@ namespace WE\SpreadsheetImport\Annotations; ...@@ -20,16 +20,29 @@ namespace WE\SpreadsheetImport\Annotations;
final class Mapping { final class Mapping {
/** /**
* Label id for the property mapping
*
* @var string * @var string
*/ */
public $labelId = ''; public $labelId = '';
/** /**
* Flag if property is handled as an identifier for updates
*
* @var boolean * @var boolean
*/ */
public $identifier = FALSE; public $identifier = FALSE;
/** /**
* Overwrite the default getter for previews
*
* @var string
*/
public $getter;
/**
* Overwrite the default setter
*
* @var string * @var string
*/ */
public $setter; public $setter;
......
<?php
namespace WE\SpreadsheetImport;
/* *
* This script belongs to the Flow package "SpreadsheetImport". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use TYPO3\Flow\Mvc\ActionRequest;
use WE\SpreadsheetImport\Annotations\Mapping;
/**
* Utility class of basic FE mapping functionality for simple usage on separate implementations.
*/
class FrontendMappingUtility {
/**
* @param \WE\SpreadsheetImport\SpreadsheetImportService $spreadsheetImportService
* @param \TYPO3\Flow\Mvc\ActionRequest $request
*
* @return array
*/
public static function getSpreadsheetImportMappingByRequest(SpreadsheetImportService $spreadsheetImportService, ActionRequest $request) {
$mappings = array();
$domainMappingProperties = $spreadsheetImportService->getMappingProperties();
foreach ($domainMappingProperties as $property => $mapping) {
$column = $request->getArgument($property);
$mappings[$property] = $column;
}
return $mappings;
}
/**
* @param \WE\SpreadsheetImport\SpreadsheetImportService $spreadsheetImportService
* @param int $record
*
* @return array
*/
public static function getMappingPreview(SpreadsheetImportService $spreadsheetImportService, $record) {
$domainMappingProperties = $spreadsheetImportService->getMappingProperties();
$previewObject = $spreadsheetImportService->getObjectByRow($record);
$preview = array();
/** @var Mapping $mapping */
foreach ($domainMappingProperties as $property => $mapping) {
$getter = empty($mapping->getter) ? 'get' . ucfirst($property) : $mapping->getter;
$preview[$property] = $previewObject->$getter();
}
return $preview;
}
}
...@@ -30,6 +30,16 @@ class SpreadsheetImportService { ...@@ -30,6 +30,16 @@ class SpreadsheetImportService {
*/ */
protected $context; protected $context;
/**
* @var array
*/
protected $mappingProperties;
/**
* @var array
*/
protected $columnPropertyMapping;
/** /**
* @Flow\InjectConfiguration * @Flow\InjectConfiguration
* @var array * @var array
...@@ -68,25 +78,54 @@ class SpreadsheetImportService { ...@@ -68,25 +78,54 @@ class SpreadsheetImportService {
public function init(SpreadsheetImport $spreadsheetImport) { public function init(SpreadsheetImport $spreadsheetImport) {
$this->spreadsheetImport = $spreadsheetImport; $this->spreadsheetImport = $spreadsheetImport;
$this->context = $this->settings[$spreadsheetImport->getContext()]; $this->context = $this->settings[$spreadsheetImport->getContext()];
$this->initDomainMappingProperties();
$this->initColumnPropertyMapping();
return $this; return $this;
} }
/** /**
* @return array * Initializes the properties declared by annotations.
*/ */
public function getDomainMappingProperties() { private function initDomainMappingProperties() {
$domainMappingProperties = array(); $this->mappingProperties = array();
$properties = $this->reflectionService->getPropertyNamesByAnnotation($this->context['domain'], Mapping::class); $properties = $this->reflectionService->getPropertyNamesByAnnotation($this->context['domain'], Mapping::class);
foreach ($properties as $property) { foreach ($properties as $property) {
$domainMappingProperties[$property] = $this->reflectionService->getPropertyAnnotation($this->context['domain'], $property, Mapping::class); $this->mappingProperties[$property] = $this->reflectionService->getPropertyAnnotation($this->context['domain'], $property, Mapping::class);
} }
return $domainMappingProperties; }
/**
* Flip mapping and return it as a 2-dim array in case the same column is assigned to multiple properties
*/
private function initColumnPropertyMapping() {
$this->columnPropertyMapping = array();
foreach ($this->spreadsheetImport->getMapping() as $property => $column) {
$this->columnPropertyMapping[$column][] = $property;
}
}
/**
* Adds additional mapping properties to the domain mapping properties retrieved by annotations. This increases
* flexibility for dynamic property mapping.
*
* @param array $additionalMappingProperties
*/
public function addAdditionalMappingProperties(array $additionalMappingProperties) {
$this->mappingProperties = array_merge($this->mappingProperties, $additionalMappingProperties);
} }
/** /**
* @return array * @return array
*/ */
public function getDomainMappingIdentifierProperties() { public function getMappingProperties() {
return $this->mappingProperties;
}
/**
* @return array
*/
private function getDomainMappingIdentifierProperties() {
$domainMappingProperties = array(); $domainMappingProperties = array();
$properties = $this->reflectionService->getPropertyNamesByAnnotation($this->context['domain'], Mapping::class); $properties = $this->reflectionService->getPropertyNamesByAnnotation($this->context['domain'], Mapping::class);
foreach ($properties as $property) { foreach ($properties as $property) {
...@@ -141,7 +180,6 @@ class SpreadsheetImportService { ...@@ -141,7 +180,6 @@ class SpreadsheetImportService {
* @return void * @return void
*/ */
public function import() { public function import() {
// TODO: This simply creates the objects for now without update or delete
$totalInserted = 0; $totalInserted = 0;
$totalUpdated = 0; $totalUpdated = 0;
$totalDeleted = 0; $totalDeleted = 0;
...@@ -210,7 +248,7 @@ class SpreadsheetImportService { ...@@ -210,7 +248,7 @@ class SpreadsheetImportService {
private function findObjectByIdentifierPropertiesPerRow(array $identifierProperties, \PHPExcel_Worksheet_Row $row) { private function findObjectByIdentifierPropertiesPerRow(array $identifierProperties, \PHPExcel_Worksheet_Row $row) {
$query = $this->getDomainRepository()->createQuery(); $query = $this->getDomainRepository()->createQuery();
$constraints = array(); $constraints = array();
$spreadsheetImportMapping = array_flip($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];
...@@ -243,14 +281,13 @@ class SpreadsheetImportService { ...@@ -243,14 +281,13 @@ class SpreadsheetImportService {
* @param \PHPExcel_Worksheet_Row $row * @param \PHPExcel_Worksheet_Row $row
*/ */
private function setObjectPropertiesByRow($newObject, $row) { private function setObjectPropertiesByRow($newObject, $row) {
// TODO: Cache $domainMappingProperties and $mappings $domainMappingProperties = $this->mappingProperties;
$domainMappingProperties = $this->getDomainMappingProperties();
$mappings = $this->spreadsheetImport->getMapping();
/** @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, $mappings)) { if (array_key_exists($column, $this->columnPropertyMapping)) {
$property = $mappings[$column]; $properties = $this->columnPropertyMapping[$column];
foreach ($properties as $property) {
/** @var Mapping $mapping */ /** @var Mapping $mapping */
$mapping = $domainMappingProperties[$property]; $mapping = $domainMappingProperties[$property];
$setter = empty($mapping->setter) ? 'set' . ucfirst($property) : $mapping->setter; $setter = empty($mapping->setter) ? 'set' . ucfirst($property) : $mapping->setter;
...@@ -258,4 +295,5 @@ class SpreadsheetImportService { ...@@ -258,4 +295,5 @@ class SpreadsheetImportService {
} }
} }
} }
}
} }
...@@ -2,7 +2,6 @@ WE: ...@@ -2,7 +2,6 @@ WE:
SpreadsheetImport: SpreadsheetImport:
testing: testing:
domain: WE\SpreadsheetImport\Tests\Functional\Fixtures\ImportTarget domain: WE\SpreadsheetImport\Tests\Functional\Fixtures\ImportTarget
labelPackageKey: 'WE.SpreadsheetImport'
Configuration: Configuration:
userProfileType: userProfileType:
setter: convertUserProfileTypeBySysValue setter: convertUserProfileTypeBySysValue
......
...@@ -12,7 +12,6 @@ namespace WE\SpreadsheetImport\Tests\Functional; ...@@ -12,7 +12,6 @@ namespace WE\SpreadsheetImport\Tests\Functional;
* */ * */
use TYPO3\Flow\Reflection\ReflectionService; use TYPO3\Flow\Reflection\ReflectionService;
use TYPO3\Flow\Resource\Resource;
use TYPO3\Flow\Resource\ResourceManager; use TYPO3\Flow\Resource\ResourceManager;
use TYPO3\Flow\Tests\FunctionalTestCase; use TYPO3\Flow\Tests\FunctionalTestCase;
use WE\SpreadsheetImport\Annotations\Mapping; use WE\SpreadsheetImport\Annotations\Mapping;
...@@ -63,7 +62,7 @@ class SpreadsheetImportServiceTest extends FunctionalTestCase { ...@@ -63,7 +62,7 @@ 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('C' => 'id', 'A' => 'name')); $spreadsheetImport->setMapping(array('id' => 'C', 'name' => 'A'));
$this->spreadsheetImportService->init($spreadsheetImport); $this->spreadsheetImportService->init($spreadsheetImport);
} }
...@@ -71,7 +70,7 @@ class SpreadsheetImportServiceTest extends FunctionalTestCase { ...@@ -71,7 +70,7 @@ class SpreadsheetImportServiceTest extends FunctionalTestCase {
* @test * @test
*/ */
public function getMappingPropertiesReturnsPropertiesWithMappingAnnotation() { public function getMappingPropertiesReturnsPropertiesWithMappingAnnotation() {
$properties = $this->spreadsheetImportService->getDomainMappingProperties(); $properties = $this->spreadsheetImportService->getMappingProperties();
$this->assertArrayHasKey('id', $properties); $this->assertArrayHasKey('id', $properties);
$this->assertArrayHasKey('name', $properties); $this->assertArrayHasKey('name', $properties);
/** @var Mapping $id */ /** @var Mapping $id */
......
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