Commit 9af7082e authored by Simon Gadient's avatar Simon Gadient

[RFT] Label changes and code style

refs KIME-4583
parent 36f56676
...@@ -46,12 +46,12 @@ class SpreadsheetImportCommandController extends CommandController { ...@@ -46,12 +46,12 @@ class SpreadsheetImportCommandController extends CommandController {
protected $settings; protected $settings;
/** /**
* Import one pending queued spreadsheet into Domain data, and it will import the next one if it is done * Import next queued spreadsheets into domain objects asynchronously.
*/ */
public function importCommand() { public function importCommand() {
$currentImportingCount = $this->spreadsheetImportRepository->countByImportingStatus(SpreadsheetImport::IMPORTING_STATUS_IN_PROGRESS); $currentImportingCount = $this->spreadsheetImportRepository->countByImportingStatus(SpreadsheetImport::IMPORTING_STATUS_IN_PROGRESS);
if ($currentImportingCount > 0) { if ($currentImportingCount > 0) {
$this->outputFormatted('There is a progressing importing spreadsheet.'); $this->outputFormatted('Previous spreadsheet import is still in progress.');
$this->quit(); $this->quit();
} }
/** @var SpreadsheetImport $spreadsheetImport */ /** @var SpreadsheetImport $spreadsheetImport */
...@@ -67,25 +67,25 @@ class SpreadsheetImportCommandController extends CommandController { ...@@ -67,25 +67,25 @@ class SpreadsheetImportCommandController extends CommandController {
try { try {
$this->spreadsheetImportService->import(); $this->spreadsheetImportService->import();
$spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_COMPLETED); $spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_COMPLETED);
$this->outputFormatted('Spreadsheet has been imported. (totalInserted: %d, totalUpdated: %d, totalDeleted: %d, totalSkipped: %d)', $this->outputFormatted('Spreadsheet has been imported. %d inserted, %d updated, %d deleted, %d skipped',
array($spreadsheetImport->getTotalInserted(), $spreadsheetImport->getTotalUpdated(), $spreadsheetImport->getTotalDeleted(), $spreadsheetImport->getTotalSkipped())); array($spreadsheetImport->getTotalInserted(), $spreadsheetImport->getTotalUpdated(), $spreadsheetImport->getTotalDeleted(), $spreadsheetImport->getTotalSkipped()));
} catch (Exception $e) { } catch (Exception $e) {
$spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_FAILED); $spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_FAILED);
$this->outputFormatted('Spreadsheet imported failed.'); $this->outputFormatted('Spreadsheet import failed.');
} }
$this->spreadsheetImportRepository->update($spreadsheetImport); $this->spreadsheetImportRepository->update($spreadsheetImport);
} else { } else {
$this->outputFormatted('There is no spreadsheet importing in queue.'); $this->outputFormatted('No spreadsheet import in queue.');
} }
} }
/** /**
* Cleanup previous spreadsheet imports by specific time (defined by settings) * Cleanup previous spreadsheet imports. Threashold defined in settings.
*/ */
public function cleanupCommand() { public function cleanupCommand() {
$cleanupImportFromPreviousDay = $this->settings['cleanupImportFromPreviousDay']; $cleanupImportsThreasholdDays = intval($this->settings['cleanupImportsThreasholdDays']);
$cleanupFromDate = new \DateTime(); $cleanupFromDate = new \DateTime();
$cleanupFromDate->sub(new \DateInterval('P' . $cleanupImportFromPreviousDay . 'D')); $cleanupFromDate->sub(new \DateInterval('P' . $cleanupImportsThreasholdDays . 'D'));
$oldSpreadsheetImports = $this->spreadsheetImportRepository->findPreviousImportsBySpecificDate($cleanupFromDate); $oldSpreadsheetImports = $this->spreadsheetImportRepository->findPreviousImportsBySpecificDate($cleanupFromDate);
if ($oldSpreadsheetImports->count() > 0) { if ($oldSpreadsheetImports->count() > 0) {
/** @var SpreadsheetImport $oldSpreadsheetImport */ /** @var SpreadsheetImport $oldSpreadsheetImport */
......
...@@ -137,8 +137,8 @@ class SpreadsheetImportService { ...@@ -137,8 +137,8 @@ class SpreadsheetImportService {
public function getObjectByRow($number) { public function getObjectByRow($number) {
$domain = $this->domain; $domain = $this->domain;
$newObject = new $domain; $newObject = new $domain;
// Plus one to skip the headings
$sheet = $this->getFileActiveSheet(); $sheet = $this->getFileActiveSheet();
// Plus one to skip the headings
$row = $sheet->getRowIterator($number + 1, $number + 1)->current(); $row = $sheet->getRowIterator($number + 1, $number + 1)->current();
$this->setObjectPropertiesByRow($newObject, $row); $this->setObjectPropertiesByRow($newObject, $row);
return $newObject; return $newObject;
...@@ -151,74 +151,62 @@ class SpreadsheetImportService { ...@@ -151,74 +151,62 @@ class SpreadsheetImportService {
$totalInserted = 0; $totalInserted = 0;
$totalUpdated = 0; $totalUpdated = 0;
$totalDeleted = 0; $totalDeleted = 0;
$totalSkipped = 0; $processedObjectIds = array();
$objectIds = array();
$domain = $this->domain;
$objectRepository = $this->getDomainRepository(); $objectRepository = $this->getDomainRepository();
$objectValidator = $this->validatorResolver->getBaseValidatorConjunction($domain); $objectValidator = $this->validatorResolver->getBaseValidatorConjunction($this->domain);
$identifierProperties = $this->getDomainMappingIdentifierProperties(); $identifierProperties = $this->getDomainMappingIdentifierProperties();
$sheet = $this->getFileActiveSheet(); $sheet = $this->getFileActiveSheet();
$numberOfRecordsToPersist = $this->settings['numberOfRecordsToPersist']; $persistRecordsChunkSize = intval($this->settings['persistRecordsChunkSize']);
$i = 0; $totalCount = 0;
/** @var \PHPExcel_Worksheet_Row $row */ /** @var \PHPExcel_Worksheet_Row $row */
foreach ($sheet->getRowIterator(2) as $row) { foreach ($sheet->getRowIterator(2) as $row) {
$totalCount++;
$object = $this->findObjectByIdentifierPropertiesPerRow($identifierProperties, $row); $object = $this->findObjectByIdentifierPropertiesPerRow($identifierProperties, $row);
if (is_object($object)) { if (is_object($object)) {
$objectIds[] = $this->persistenceManager->getIdentifierByObject($object); $processedObjectIds[] = $this->persistenceManager->getIdentifierByObject($object);
if ($this->spreadsheetImport->isUpdating()) { if ($this->spreadsheetImport->isUpdating()) {
$this->setObjectPropertiesByRow($object, $row); $this->setObjectPropertiesByRow($object, $row);
$validationResult = $objectValidator->validate($object); $validationResult = $objectValidator->validate($object);
if ($validationResult->hasErrors()) { if ($validationResult->hasErrors()) {
$totalSkipped++;
continue; continue;
} }
$objectRepository->update($object); $objectRepository->update($object);
$totalUpdated++; $totalUpdated++;
$i++;
} else { } else {
$totalSkipped++;
continue; continue;
} }
} elseif ($this->spreadsheetImport->isInserting()) { } elseif ($this->spreadsheetImport->isInserting()) {
$newObject = new $domain; $newObject = new $this->domain;
$this->setObjectPropertiesByRow($newObject, $row); $this->setObjectPropertiesByRow($newObject, $row);
$validationResult = $objectValidator->validate($newObject); $validationResult = $objectValidator->validate($newObject);
if ($validationResult->hasErrors()) { if ($validationResult->hasErrors()) {
$totalSkipped++;
continue; continue;
} }
$objectRepository->add($newObject); $objectRepository->add($newObject);
$objectIds[] = $this->persistenceManager->getIdentifierByObject($newObject); $processedObjectIds[] = $this->persistenceManager->getIdentifierByObject($newObject);
$totalInserted++; $totalInserted++;
$i++;
} else { } else {
$totalSkipped++;
continue; continue;
} }
if ($i >= $numberOfRecordsToPersist) { if ($totalCount % $persistRecordsChunkSize === 0) {
$this->persistenceManager->persistAll(); $this->persistenceManager->persistAll();
$i = 0;
} }
} }
$deleteCount = 0;
// remove objects which are not exist on the spreadsheet
if ($this->spreadsheetImport->isDeleting()) { if ($this->spreadsheetImport->isDeleting()) {
$notExistingObjects = $this->findObjectsByExcludedIds($objectIds); $notExistingObjects = $this->findObjectsByExcludedIds($processedObjectIds);
foreach ($notExistingObjects as $object) { foreach ($notExistingObjects as $object) {
$objectRepository->remove($object); $objectRepository->remove($object);
$totalDeleted++; if (++$deleteCount % $persistRecordsChunkSize === 0) {
$i++;
if ($i >= $numberOfRecordsToPersist) {
$this->persistenceManager->persistAll(); $this->persistenceManager->persistAll();
$i = 0;
} }
} }
} }
$this->persistenceManager->persistAll();
$this->spreadsheetImport->setTotalInserted($totalInserted); $this->spreadsheetImport->setTotalInserted($totalInserted);
$this->spreadsheetImport->setTotalUpdated($totalUpdated); $this->spreadsheetImport->setTotalUpdated($totalUpdated);
$this->spreadsheetImport->setTotalSkipped($totalCount - $totalInserted - $totalUpdated);
$this->spreadsheetImport->setTotalDeleted($totalDeleted); $this->spreadsheetImport->setTotalDeleted($totalDeleted);
$this->spreadsheetImport->setTotalSkipped($totalSkipped);
} }
/** /**
......
WE: WE:
SpreadsheetImport: SpreadsheetImport:
cleanupImportFromPreviousDay: 365 cleanupImportsThreasholdDays: 365
numberOfRecordsToPersist: 100 persistRecordsChunkSize: 100
WE: WE:
SpreadsheetImport: SpreadsheetImport:
cleanupImportFromPreviousDay: 365 cleanupImportsThreasholdDays: 365
numberOfRecordsToPersist: 100 persistRecordsChunkSize: 100
default: default:
domain: WE\Sample\Domain\Model\User domain: WE\Sample\Domain\Model\User
arguments: arguments:
......
...@@ -44,10 +44,10 @@ ...@@ -44,10 +44,10 @@
<source>Ungültige Daten. Dieser Record wird nicht übersprungen.</source> <source>Ungültige Daten. Dieser Record wird nicht übersprungen.</source>
</trans-unit> </trans-unit>
<trans-unit id="label.spreadsheet_import.preview.record"> <trans-unit id="label.spreadsheet_import.preview.record">
<source>Vorschau Zeile</source> <source>Zeile</source>
</trans-unit> </trans-unit>
<trans-unit id="label.spreadsheet_import.list"> <trans-unit id="label.spreadsheet_import.list">
<source>Spreadsheet Imports</source> <source>Importe</source>
</trans-unit> </trans-unit>
<!-- Status --> <!-- Status -->
......
...@@ -44,10 +44,10 @@ ...@@ -44,10 +44,10 @@
<source>Invalid data. This record will be skipped.</source> <source>Invalid data. This record will be skipped.</source>
</trans-unit> </trans-unit>
<trans-unit id="label.spreadsheet_import.preview.record"> <trans-unit id="label.spreadsheet_import.preview.record">
<source>Preview record</source> <source>Record</source>
</trans-unit> </trans-unit>
<trans-unit id="label.spreadsheet_import.list"> <trans-unit id="label.spreadsheet_import.list">
<source>Spreadsheet Imports</source> <source>Imports</source>
</trans-unit> </trans-unit>
<!-- Status --> <!-- Status -->
......
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