Commit c6ecbfd1 authored by Chivy Lim's avatar Chivy Lim

Merge branch 'KIME-4583' into 'master'

[IMP] Argument support for static parameter assignment

refs KIME-4583

See merge request !5
parents 70f31a75 f686dc55
...@@ -47,6 +47,12 @@ class SpreadsheetImport { ...@@ -47,6 +47,12 @@ class SpreadsheetImport {
*/ */
protected $mapping = ''; protected $mapping = '';
/**
* @var string
* @ORM\Column(type="text")
*/
protected $arguments = '';
/** /**
* @var boolean * @var boolean
*/ */
...@@ -141,7 +147,11 @@ class SpreadsheetImport { ...@@ -141,7 +147,11 @@ class SpreadsheetImport {
* @return array * @return array
*/ */
public function getMapping() { public function getMapping() {
return unserialize($this->mapping); $mapping = unserialize($this->mapping);
if (! is_array($mapping)) {
$mapping = array();
}
return $mapping;
} }
/** /**
...@@ -151,6 +161,24 @@ class SpreadsheetImport { ...@@ -151,6 +161,24 @@ class SpreadsheetImport {
$this->mapping = serialize($mapping); $this->mapping = serialize($mapping);
} }
/**
* @return array
*/
public function getArguments() {
return unserialize($this->arguments);
}
/**
* @param string|array $arguments
*/
public function setArguments($arguments) {
if (is_array($arguments)) {
$this->arguments = serialize($arguments);
} else {
$this->arguments = $arguments;
}
}
/** /**
* @return boolean * @return boolean
*/ */
......
...@@ -26,9 +26,9 @@ class SpreadsheetImportService { ...@@ -26,9 +26,9 @@ class SpreadsheetImportService {
protected $spreadsheetImport; protected $spreadsheetImport;
/** /**
* @var array * @var string
*/ */
protected $context; protected $domain;
/** /**
* @var array * @var array
...@@ -52,12 +52,6 @@ class SpreadsheetImportService { ...@@ -52,12 +52,6 @@ class SpreadsheetImportService {
*/ */
protected $reflectionService; protected $reflectionService;
/**
* @Flow\Inject
* @var \TYPO3\Flow\Resource\ResourceManager
*/
protected $resourceManager;
/** /**
* @Flow\Inject * @Flow\Inject
* @var \TYPO3\Flow\Persistence\PersistenceManagerInterface * @var \TYPO3\Flow\Persistence\PersistenceManagerInterface
...@@ -70,6 +64,12 @@ class SpreadsheetImportService { ...@@ -70,6 +64,12 @@ class SpreadsheetImportService {
*/ */
protected $objectManager; protected $objectManager;
/**
* @Flow\Inject
* @var \TYPO3\Flow\Property\PropertyMapper
*/
protected $propertyMapper;
/** /**
* @param \WE\SpreadsheetImport\Domain\Model\SpreadsheetImport $spreadsheetImport * @param \WE\SpreadsheetImport\Domain\Model\SpreadsheetImport $spreadsheetImport
* *
...@@ -77,7 +77,7 @@ class SpreadsheetImportService { ...@@ -77,7 +77,7 @@ 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->domain = $this->settings[$spreadsheetImport->getContext()]['domain'];
$this->initDomainMappingProperties(); $this->initDomainMappingProperties();
$this->initColumnPropertyMapping(); $this->initColumnPropertyMapping();
...@@ -89,9 +89,9 @@ class SpreadsheetImportService { ...@@ -89,9 +89,9 @@ class SpreadsheetImportService {
*/ */
private function initDomainMappingProperties() { private function initDomainMappingProperties() {
$this->mappingProperties = array(); $this->mappingProperties = array();
$properties = $this->reflectionService->getPropertyNamesByAnnotation($this->context['domain'], Mapping::class); $properties = $this->reflectionService->getPropertyNamesByAnnotation($this->domain, Mapping::class);
foreach ($properties as $property) { foreach ($properties as $property) {
$this->mappingProperties[$property] = $this->reflectionService->getPropertyAnnotation($this->context['domain'], $property, Mapping::class); $this->mappingProperties[$property] = $this->reflectionService->getPropertyAnnotation($this->domain, $property, Mapping::class);
} }
} }
...@@ -109,6 +109,13 @@ class SpreadsheetImportService { ...@@ -109,6 +109,13 @@ class SpreadsheetImportService {
* 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
*
* 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
*/ */
public function addAdditionalMappingProperties(array $additionalMappingProperties) { public function addAdditionalMappingProperties(array $additionalMappingProperties) {
...@@ -123,19 +130,12 @@ class SpreadsheetImportService { ...@@ -123,19 +130,12 @@ class SpreadsheetImportService {
} }
/** /**
* @param string $context
*
* @return array * @return array
*/ */
private function getDomainMappingIdentifierProperties() { public function getArgumentsByContext($context) {
$domainMappingProperties = array(); return $this->settings[$context]['arguments'];
$properties = $this->reflectionService->getPropertyNamesByAnnotation($this->context['domain'], Mapping::class);
foreach ($properties as $property) {
/** @var Mapping $mapping */
$mapping = $this->reflectionService->getPropertyAnnotation($this->context['domain'], $property, Mapping::class);
if ($mapping->identifier) {
$domainMappingProperties[$property] = $mapping;
}
}
return $domainMappingProperties;
} }
/** /**
...@@ -166,7 +166,7 @@ class SpreadsheetImportService { ...@@ -166,7 +166,7 @@ class SpreadsheetImportService {
* @return object * @return object
*/ */
public function getObjectByRow($number) { public function getObjectByRow($number) {
$domain = $this->context['domain']; $domain = $this->domain;
$newObject = new $domain; $newObject = new $domain;
// Plus one to skip the headings // Plus one to skip the headings
$file = $this->spreadsheetImport->getFile()->createTemporaryLocalCopy(); $file = $this->spreadsheetImport->getFile()->createTemporaryLocalCopy();
...@@ -185,7 +185,7 @@ class SpreadsheetImportService { ...@@ -185,7 +185,7 @@ class SpreadsheetImportService {
$totalDeleted = 0; $totalDeleted = 0;
$totalSkipped = 0; $totalSkipped = 0;
$objectIds = array(); $objectIds = array();
$domain = $this->context['domain']; $domain = $this->domain;
$objectRepository = $this->getDomainRepository(); $objectRepository = $this->getDomainRepository();
$identifierProperties = $this->getDomainMappingIdentifierProperties(); $identifierProperties = $this->getDomainMappingIdentifierProperties();
$file = $this->spreadsheetImport->getFile()->createTemporaryLocalCopy(); $file = $this->spreadsheetImport->getFile()->createTemporaryLocalCopy();
...@@ -232,13 +232,29 @@ class SpreadsheetImportService { ...@@ -232,13 +232,29 @@ class SpreadsheetImportService {
* @return \TYPO3\Flow\Persistence\RepositoryInterface * @return \TYPO3\Flow\Persistence\RepositoryInterface
*/ */
private function getDomainRepository() { private function getDomainRepository() {
$domainClassName = $this->context['domain']; $domainClassName = $this->domain;
$repositoryClassName = preg_replace(array('/\\\Model\\\/', '/$/'), array('\\Repository\\', 'Repository'), $domainClassName); $repositoryClassName = preg_replace(array('/\\\Model\\\/', '/$/'), array('\\Repository\\', 'Repository'), $domainClassName);
/** @var RepositoryInterface $repository */ /** @var RepositoryInterface $repository */
$repository = $this->objectManager->get($repositoryClassName); $repository = $this->objectManager->get($repositoryClassName);
return $repository; return $repository;
} }
/**
* @return array
*/
private function getDomainMappingIdentifierProperties() {
$domainMappingProperties = array();
$properties = $this->reflectionService->getPropertyNamesByAnnotation($this->domain, Mapping::class);
foreach ($properties as $property) {
/** @var Mapping $mapping */
$mapping = $this->reflectionService->getPropertyAnnotation($this->domain, $property, Mapping::class);
if ($mapping->identifier) {
$domainMappingProperties[$property] = $mapping;
}
}
return $domainMappingProperties;
}
/** /**
* @param array $identifierProperties * @param array $identifierProperties
* @param \PHPExcel_Worksheet_Row $row * @param \PHPExcel_Worksheet_Row $row
...@@ -277,10 +293,10 @@ class SpreadsheetImportService { ...@@ -277,10 +293,10 @@ class SpreadsheetImportService {
} }
/** /**
* @param object $newObject * @param object $object
* @param \PHPExcel_Worksheet_Row $row * @param \PHPExcel_Worksheet_Row $row
*/ */
private function setObjectPropertiesByRow($newObject, $row) { private function setObjectPropertiesByRow($object, $row) {
$domainMappingProperties = $this->mappingProperties; $domainMappingProperties = $this->mappingProperties;
/** @var \PHPExcel_Cell $cell */ /** @var \PHPExcel_Cell $cell */
foreach ($row->getCellIterator() as $cell) { foreach ($row->getCellIterator() as $cell) {
...@@ -288,11 +304,40 @@ class SpreadsheetImportService { ...@@ -288,11 +304,40 @@ class SpreadsheetImportService {
if (array_key_exists($column, $this->columnPropertyMapping)) { if (array_key_exists($column, $this->columnPropertyMapping)) {
$properties = $this->columnPropertyMapping[$column]; $properties = $this->columnPropertyMapping[$column];
foreach ($properties as $property) { foreach ($properties as $property) {
/** @var Mapping $mapping */ if (array_key_exists($property, $domainMappingProperties)) {
$mapping = $domainMappingProperties[$property]; /** @var Mapping $mapping */
$setter = empty($mapping->setter) ? 'set' . ucfirst($property) : $mapping->setter; $mapping = $domainMappingProperties[$property];
$newObject->$setter($cell->getValue()); $setter = empty($mapping->setter) ? 'set' . ucfirst($property) : $mapping->setter;
} else {
$setter = 'set' . ucfirst($property);
}
$object->$setter($cell->getValue());
}
}
}
$this->setObjectArgumentProperties($object);
}
/**
* @param $object
*/
private function setObjectArgumentProperties($object) {
$contextArguments = $this->getArgumentsByContext($this->spreadsheetImport->getContext());
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];
}
} else {
$value = array_key_exists('default', $contextArgument) ? $contextArgument['default'] : NULL;
} }
$setter = 'set' . ucfirst($name);
$object->$setter($value);
} }
} }
} }
......
...@@ -2,9 +2,4 @@ WE: ...@@ -2,9 +2,4 @@ WE:
SpreadsheetImport: SpreadsheetImport:
testing: testing:
domain: WE\SpreadsheetImport\Tests\Functional\Fixtures\ImportTarget domain: WE\SpreadsheetImport\Tests\Functional\Fixtures\ImportTarget
Configuration: arguments: ~
userProfileType:
setter: convertUserProfileTypeBySysValue
value: 'swisscomGRB'
privacy:
value: 'private'
<?php
namespace TYPO3\Flow\Persistence\Doctrine\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Add arguments property for serialized values
*/
class Version20161026173055 extends AbstractMigration
{
/**
* @return string
*/
public function getDescription() {
return '';
}
/**
* @param Schema $schema
* @return void
*/
public function up(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on "mysql".');
$this->addSql('ALTER TABLE we_spreadsheetimport_domain_model_spreadsheetimport ADD arguments LONGTEXT NOT NULL');
$this->addSql('ALTER TABLE we_spreadsheetimport_domain_model_spreadsheetimport ADD CONSTRAINT FK_19518FA38C9F3610 FOREIGN KEY (file) REFERENCES typo3_flow_resource_resource (persistence_object_identifier)');
}
/**
* @param Schema $schema
* @return void
*/
public function down(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on "mysql".');
$this->addSql('ALTER TABLE we_spreadsheetimport_domain_model_spreadsheetimport DROP FOREIGN KEY FK_19518FA38C9F3610');
$this->addSql('ALTER TABLE we_spreadsheetimport_domain_model_spreadsheetimport DROP arguments');
}
}
...@@ -50,7 +50,7 @@ class ImportTarget implements SpreadsheetImportTargetInterface { ...@@ -50,7 +50,7 @@ class ImportTarget implements SpreadsheetImportTargetInterface {
* @param string $id * @param string $id
*/ */
public function setRawId($id) { public function setRawId($id) {
$this->id = sprintf("%05d", $id); $this->id = sprintf('%05d', $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