getLogger(); $logger->debug('Tabular import: CSV parsing started'); $itemValidatorResults = $this->getParser()->parseFile($uploadedFile, $template); $logger->debug('Tabular import: CSV parsing finished'); $successReportsImport = 0; $importService = $this->getItemImportService(); $templateProcessor = $this->getTemplateProcessor(); $xmlItems = $templateProcessor->processResultSet($itemValidatorResults, $template); foreach ($xmlItems as $lineNumber => $xmlItem) { try { $metaData = $this->getMetadataResolver()->resolve($class, $xmlItem->getMetadata()); $itemImportReport = $importService->importQTIFile($xmlItem->getItemXML(), $class, true); if (Report::TYPE_SUCCESS !== $itemImportReport->getType()) { $itemValidatorResults->addException( $lineNumber, new ErrorValidationException($itemImportReport->getMessage()) ); continue; } $this->importMetadata($metaData, $itemImportReport); $itemValidatorResults->setFirstItem($itemImportReport->getData()); $logger->debug(sprintf('Tabular import: successful import of item from line %s', $lineNumber)); $successReportsImport++; } catch (Throwable $exception) { if (isset($itemImportReport)) { $this->rollbackItem($itemImportReport, $lineNumber); } $logger->error( sprintf( 'Tabular import: failed import of item from line %s due to %s', $lineNumber, $exception->getMessage() ) ); $itemValidatorResults->addException($lineNumber, $exception); } finally { if (isset($itemImportReport)) { unset($itemImportReport); } } } helpers_TimeOutHelper::reset(); $logger->debug( sprintf( 'Tabular import: successful import %s items from %s', $successReportsImport, count($xmlItems) ) ); $itemValidatorResults->setTotalSuccessfulImport($successReportsImport); return $itemValidatorResults; } private function getParser(): ParserInterface { /** @var CsvParser $parser */ $parser = $this->getServiceLocator()->get(CsvParser::class); $parser->setCsvSeparator($this->getCsvSeparator()); return $parser; } private function getItemImportService(): ImportService { return $this->getServiceLocator()->get(ImportService::SERVICE_ID); } private function getItemService(): taoItems_models_classes_ItemsService { return $this->getServiceLocator()->get(taoItems_models_classes_ItemsService::class); } private function getTemplateProcessor(): ItemsQtiTemplateRender { return $this->getServiceLocator()->get(ItemsQtiTemplateRender::class); } private function getMetadataResolver(): MetadataResolver { return $this->getServiceManager()->get(MetadataResolver::class); } /** * @throws tao_models_classes_dataBinding_GenerisFormDataBindingException */ private function importMetadata(array $metaData, ReportInterface $itemImportReport): void { $itemRdf = $itemImportReport->getData(); $binder = new tao_models_classes_dataBinding_GenerisFormDataBinder($itemRdf); $binder->bind($metaData); } private function rollbackItem(ReportInterface $itemImportReport, int $lineNumber): void { $item = $itemImportReport->getData(); if ($item instanceof core_kernel_classes_Resource) { $this->getItemService()->deleteResource($item); $this->getLogger()->warning( sprintf( 'Tabular import: line `%s` rollback item with uri `%s` and label %s', $lineNumber, $item->getUri(), $item->getLabel() ) ); } } }