Commit 9fe4b813 authored by Chivy Lim's avatar Chivy Lim

[TAKS] Add Model validation on importing service

refs KIME-3603
parent c1d31909
...@@ -69,7 +69,7 @@ class SpreadsheetImportCommandController extends CommandController { ...@@ -69,7 +69,7 @@ class SpreadsheetImportCommandController extends CommandController {
$spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_COMPLETED); $spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_COMPLETED);
$this->spreadsheetImportRepository->update($spreadsheetImport); $this->spreadsheetImportRepository->update($spreadsheetImport);
$this->outputFormatted('Spreadsheet has been exported. (totalInserted: %d, totalUpdated: %d, totalDeleted: %d, totalSkipped: %d)', $this->outputFormatted('Spreadsheet has been imported. (totalInserted: %d, totalUpdated: %d, totalDeleted: %d, totalSkipped: %d)',
array($spreadsheetImport->getTotalInserted(), $spreadsheetImport->getTotalUpdated(), $spreadsheetImport->getTotalDeleted(), $spreadsheetImport->getTotalSkipped())); array($spreadsheetImport->getTotalInserted(), $spreadsheetImport->getTotalUpdated(), $spreadsheetImport->getTotalDeleted(), $spreadsheetImport->getTotalSkipped()));
} else { } else {
$this->outputFormatted('There is no spreadsheet importing in queue.'); $this->outputFormatted('There is no spreadsheet importing in queue.');
......
...@@ -70,6 +70,12 @@ class SpreadsheetImportService { ...@@ -70,6 +70,12 @@ class SpreadsheetImportService {
*/ */
protected $propertyMapper; protected $propertyMapper;
/**
* @Flow\Inject
* @var \TYPO3\Flow\Validation\ValidatorResolver
*/
protected $validatorResolver;
/** /**
* @param \WE\SpreadsheetImport\Domain\Model\SpreadsheetImport $spreadsheetImport * @param \WE\SpreadsheetImport\Domain\Model\SpreadsheetImport $spreadsheetImport
* *
...@@ -187,6 +193,7 @@ class SpreadsheetImportService { ...@@ -187,6 +193,7 @@ class SpreadsheetImportService {
$objectIds = array(); $objectIds = array();
$domain = $this->domain; $domain = $this->domain;
$objectRepository = $this->getDomainRepository(); $objectRepository = $this->getDomainRepository();
$objectValidator = $this->validatorResolver->getBaseValidatorConjunction($domain);
$identifierProperties = $this->getDomainMappingIdentifierProperties(); $identifierProperties = $this->getDomainMappingIdentifierProperties();
$file = $this->spreadsheetImport->getFile()->createTemporaryLocalCopy(); $file = $this->spreadsheetImport->getFile()->createTemporaryLocalCopy();
$reader = \PHPExcel_IOFactory::load($file); $reader = \PHPExcel_IOFactory::load($file);
...@@ -199,21 +206,33 @@ class SpreadsheetImportService { ...@@ -199,21 +206,33 @@ class SpreadsheetImportService {
$objectIds[] = $this->persistenceManager->getIdentifierByObject($object); $objectIds[] = $this->persistenceManager->getIdentifierByObject($object);
if ($this->spreadsheetImport->isUpdating()) { if ($this->spreadsheetImport->isUpdating()) {
$this->setObjectPropertiesByRow($object, $row); $this->setObjectPropertiesByRow($object, $row);
$validationResult = $objectValidator->validate($object);
if ($validationResult->hasErrors()) {
$totalSkipped++;
continue;
}
$objectRepository->update($object); $objectRepository->update($object);
$totalUpdated++; $totalUpdated++;
$i++; $i++;
} else { } else {
$totalSkipped++; $totalSkipped++;
continue;
} }
} elseif ($this->spreadsheetImport->isInserting()) { } elseif ($this->spreadsheetImport->isInserting()) {
$newObject = new $domain; $newObject = new $domain;
$this->setObjectPropertiesByRow($newObject, $row); $this->setObjectPropertiesByRow($newObject, $row);
$validationResult = $objectValidator->validate($newObject);
if ($validationResult->hasErrors()) {
$totalSkipped++;
continue;
}
$objectRepository->add($newObject); $objectRepository->add($newObject);
$objectIds[] = $this->persistenceManager->getIdentifierByObject($newObject); $objectIds[] = $this->persistenceManager->getIdentifierByObject($newObject);
$totalInserted++; $totalInserted++;
$i++; $i++;
} else { } else {
$totalSkipped++; $totalSkipped++;
continue;
} }
if ($i >= $numberOfRecordsToPersist) { if ($i >= $numberOfRecordsToPersist) {
$this->persistenceManager->persistAll(); $this->persistenceManager->persistAll();
...@@ -224,7 +243,6 @@ class SpreadsheetImportService { ...@@ -224,7 +243,6 @@ class SpreadsheetImportService {
// remove objects which are not exist on the spreadsheet // remove objects which are not exist on the spreadsheet
if ($this->spreadsheetImport->isDeleting()) { if ($this->spreadsheetImport->isDeleting()) {
$notExistingObjects = $this->findObjectsByExcludedIds($objectIds); $notExistingObjects = $this->findObjectsByExcludedIds($objectIds);
$i = 0;
foreach ($notExistingObjects as $object) { foreach ($notExistingObjects as $object) {
$objectRepository->remove($object); $objectRepository->remove($object);
$totalDeleted++; $totalDeleted++;
...@@ -246,8 +264,7 @@ class SpreadsheetImportService { ...@@ -246,8 +264,7 @@ class SpreadsheetImportService {
* @return \TYPO3\Flow\Persistence\RepositoryInterface * @return \TYPO3\Flow\Persistence\RepositoryInterface
*/ */
private function getDomainRepository() { private function getDomainRepository() {
$domainClassName = $this->domain; $repositoryClassName = preg_replace(array('/\\\Model\\\/', '/$/'), array('\\Repository\\', 'Repository'), $this->domain);
$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;
......
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