Commit 8185499e authored by Chhengleap Soem's avatar Chhengleap Soem

Merge branch 'KIME-3603' into 'master'

[TAKS] Add Model validation on importing service

refs KIME-3603

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