Forums › Forums › OroPlatform › OroPlatform – Programming Questions › Import CsvFileReader
This topic contains 6 replies, has 2 voices, and was last updated by stalxed 9 years, 5 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- November 5, 2014 at 2:53 pm #33526
Hello!
I have several Import Processors.
They must have different settings of delimiter, enclosure and escape here:
https://github.com/orocrm/platform/blob/master/src/Oro/Bundle/ImportExportBundle/Reader/CsvFileReader.phpHow I can do this?
- CreatorTopic
- AuthorReplies
- November 6, 2014 at 2:44 am #33527
Hello, stalxed.
There are two ways to do that:
1) preferable solution: you can pass these variables to execution context – see this code;
2) another solution: you can extend this reader, override variables and use it in batch job configuration.
November 6, 2014 at 5:41 am #33528Thank you for answer!
I use importexport configuration like in this bundle:
https://github.com/orocrm/crm/blob/master/src/OroCRM/Bundle/ContactBundle/Resources/config/importexport.ymlFor example, how from OroCRMContactBundle pass this variables to execution context?
I spent a few hours, but did not understand how to do it.November 6, 2014 at 5:51 am #33529I think I cant do it from here
https://github.com/orocrm/platform/blob/master/src/Oro/Bundle/ImportExportBundle/Processor/ImportProcessor.php
In documentation he called after the read operation.
Or am I wrong?
If I’m not mistaken, where else can I set the context variables?(before call the function initializeFromContext in CsvFileReader).November 6, 2014 at 4:12 pm #33530I found solution:
1) Need create listener:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051<?phpnamespace Acme\ImportExportBundle\Listener;use Akeneo\Bundle\BatchBundle\Entity\JobInstance;use Akeneo\Bundle\BatchBundle\Event\StepExecutionEvent;use Oro\Bundle\ImportExportBundle\Job\JobExecutor;class ImportExportListener{public function changeCsv(StepExecutionEvent $event){$jobInstance = $event->getStepExecution()->getJobExecution()->getJobInstance();if (! $this->isImport($jobInstance)) {return;}$rawConfiguration = $jobInstance->getRawConfiguration();$rawConfiguration = $this->changeRawConfiguration($rawConfiguration);$jobInstance->setRawConfiguration($rawConfiguration);}private function isImport(JobInstance $jobInstance){if ($jobInstance->getAlias() == JobExecutor::JOB_IMPORT_FROM_CSV) {return true;}if ($jobInstance->getAlias() == JobExecutor::JOB_VALIDATE_IMPORT_FROM_CSV) {return true;}return false;}private function changeRawConfiguration($rawConfiguration){$processorAlias = $rawConfiguration['import_validation']['processorAlias'];if ($processorAlias == 'my_processor') {$rawConfiguration['import_validation']['delimiter'] = '|';$rawConfiguration['import_validation']['enclosure'] = '"';$rawConfiguration['import_validation']['escape'] = '\\';}return $rawConfiguration;}}2) Need register listener:
123456listener.import_export_listener:class: Acme\ImportExportBundle\Listener\ImportExportListenertags:- { name: kernel.event_listener, event: akeneo_batch.before_step_execution, method: changeCsv }November 7, 2014 at 3:18 am #33531Good job, stalxed!
Just a several tips:
– alongside with “import_validation” type you should alter configuration for “import” type
– instead of hardcoded types you can use constants ProcessorRegistry::TYPE_IMPORT_VALIDATION and ProcessorRegistry::TYPE_IMPORTNovember 7, 2014 at 9:40 am #33532Thanks Yevhen Shyshkin!
I improved the code:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182<?phpnamespace Acme\ImportExportBundle\Listener;use Akeneo\Bundle\BatchBundle\Entity\JobInstance;use Akeneo\Bundle\BatchBundle\Event\StepExecutionEvent;use Oro\Bundle\ImportExportBundle\Job\JobExecutor;use Oro\Bundle\ImportExportBundle\Processor\ProcessorRegistry;class ImportExportListener{private $processorAlias;private $delimiter;private $enclosure;private $escape;public function __construct($processorAlias, $delimiter, $enclosure, $escape){$this->processorAlias = $processorAlias;$this->delimiter = $delimiter;$this->enclosure = $enclosure;$this->escape = $escape;}public function changeCsv(StepExecutionEvent $event){$jobInstance = $event->getStepExecution()->getJobExecution()->getJobInstance();if (! $this->isImport($jobInstance) && ! $this->isValidate($jobInstance)) {return;}$rawConfiguration = $jobInstance->getRawConfiguration();if ($this->isImport($jobInstance)) {$rawConfiguration = $this->changeImport($rawConfiguration);}if ($this->isValidate($jobInstance)) {$rawConfiguration = $this->changeValidate($rawConfiguration);}$jobInstance->setRawConfiguration($rawConfiguration);}private function isImport(JobInstance $jobInstance){return $jobInstance->getAlias() == JobExecutor::JOB_IMPORT_FROM_CSV;}private function changeImport($rawConfiguration){$type = ProcessorRegistry::TYPE_IMPORT;$processorAlias = $rawConfiguration[$type]['processorAlias'];if ($processorAlias == $this->processorAlias) {$rawConfiguration[$type]['delimiter'] = $this->delimiter;$rawConfiguration[$type]['enclosure'] = $this->enclosure;$rawConfiguration[$type]['escape'] = $this->escape;}return $rawConfiguration;}private function isValidate(JobInstance $jobInstance){return $jobInstance->getAlias() == JobExecutor::JOB_VALIDATE_IMPORT_FROM_CSV;}private function changeValidate($rawConfiguration){$type = ProcessorRegistry::TYPE_IMPORT_VALIDATION;$processorAlias = $rawConfiguration[$type]['processorAlias'];if ($processorAlias == $this->processorAlias) {$rawConfiguration[$type]['delimiter'] = $this->delimiter;$rawConfiguration[$type]['enclosure'] = $this->enclosure;$rawConfiguration[$type]['escape'] = $this->escape;}return $rawConfiguration;}}Now can registe few services:
123456789101112131415161718192021listener.import_export_listener.my_processor1:class: Acme\ImportExportBundle\Listener\ImportExportListenerarguments:- 'my_processor1'- '|'- '"'- '\\'tags:- { name: kernel.event_listener, event: akeneo_batch.before_step_execution, method: changeCsv }listener.import_export_listener.my_processor2:class: Acme\ImportExportBundle\Listener\ImportExportListenerarguments:- 'my_processor1'- '|'- '"'- '\\'tags:- { name: kernel.event_listener, event: akeneo_batch.before_step_execution, method: changeCsv }I hope someone can help.
I would be glad if you publish in a cookbook documentation. - AuthorReplies
The forum ‘OroPlatform – Programming Questions’ is closed to new topics and replies.