From 0c023e80fa4fa4425241e4ce3e0f7aabe77e056f Mon Sep 17 00:00:00 2001 From: Mike Cantelon Date: Fri, 2 Aug 2019 14:35:34 -0700 Subject: [PATCH] Round trip mode for CSV import, refs #12281 Added round trip mode to CSV information object import. Round trip mode treats the values in the legacyID column as internal AtoM IDs and doesn't create keymap entries. --- lib/QubitFlatfileImport.class.php | 26 +++++++++++------ lib/task/import/csvImportTask.class.php | 38 +++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/lib/QubitFlatfileImport.class.php b/lib/QubitFlatfileImport.class.php index a20ef2796b..762d4c0715 100644 --- a/lib/QubitFlatfileImport.class.php +++ b/lib/QubitFlatfileImport.class.php @@ -38,6 +38,7 @@ class QubitFlatfileImport public $deleteAndReplace = false; // delete matching records & replace them public $skipMatched = false; // skip creating new record if matching one is found public $skipUnmatched = false; // skip creating new record if matching one is not found + public $roundtrip = false; // treat legacy ID as internal ID public $keepDigitalObjects = false; // skip deletion of DOs when set. Works when --update set. public $limitToId = 0; // id of repository or TLD to limit our update matching under public $status = array(); // place to store data related to overall import @@ -162,6 +163,7 @@ public function setUpdateOptions($options) $this->skipMatched = $options['skip-matched']; $this->skipUnmatched = $options['skip-unmatched']; + $this->roundtrip = $options['roundtrip']; } /* @@ -829,20 +831,28 @@ private function handleInformationObjectRow() $legacyId = $this->columnExists('legacyId') ? trim($this->columnValue('legacyId')) : null; - // Try to match on legacyId in keymap - $this->setInformationObjectByKeymap($legacyId); - - if (null === $this->object) + // Allow roundtripping (treating legacyId as an existing AtoM ID) + if ($this->roundtrip && null === $this->object) + { + $this->object = QubitInformationObject::getById($legacyId); + } + else { - // No match found in keymap, try to match on title, repository and identifier. - $this->setInformationObjectByFields(); + // Try to match on legacyId in keymap + $this->setInformationObjectByKeymap($legacyId); + + if (null === $this->object) + { + // No match found in keymap, try to match on title, repository and identifier. + $this->setInformationObjectByFields(); + } } $this->checkInformationObjectMatchLimit(); // Handle --limit option. if (null === $this->object) { - // Still no match found, create information object if --skip-unmatched is not set in options. + // Still no match found, create IO if not roundtripping or if --skip-unmatched is not set in options. return $this->createNewInformationObject(); } @@ -917,7 +927,7 @@ private function handleDeleteAndReplace() */ private function createNewInformationObject() { - if ($this->skipUnmatched) + if ($this->skipUnmatched || $this->roundtrip) { $msg = sprintf('Unable to match row. Skipping record: %s (id: %s)', $this->columnExists('title') ? trim($this->columnValue('title')) : '', diff --git a/lib/task/import/csvImportTask.class.php b/lib/task/import/csvImportTask.class.php index 6386895114..a788ca2d08 100644 --- a/lib/task/import/csvImportTask.class.php +++ b/lib/task/import/csvImportTask.class.php @@ -114,6 +114,18 @@ protected function configure() null, sfCommandOption::PARAMETER_NONE, 'Skip the deletion of existing digital objects and their derivatives when using --update with "match-and-update".' + ), + new sfCommandOption( + 'roundtrip', + null, + sfCommandOption::PARAMETER_NONE, + 'Treat legacy IDs as internal IDs.' + ), + new sfCommandOption( + 'no-confirmation', + null, + sfCommandOption::PARAMETER_NONE, + 'Do not ask for confirmation' ) )); } @@ -174,6 +186,25 @@ public function execute($arguments = array(), $options = array()) QubitTaxonomy::PHYSICAL_OBJECT_TYPE_ID => 'physicalObjectTypes' )); + if ( + $options['roundtrip'] + && + !$options['no-confirmation'] + && + !$this->askConfirmation(array( + 'WARNING: In round trip mode legacy IDs will be treated as internal IDs.', + 'Please back-up your database manually before you proceed.', + '', + 'Have you done a manual backup and wish to proceed? (y/N)' + ), + 'QUESTION_LARGE', false) + ) + { + $this->log('Task aborted.'); + + return 1; + } + // Define import $import = new QubitFlatfileImport(array( // Pass context @@ -590,8 +621,11 @@ public function execute($arguments = array(), $options = array()) throw new sfException('Information object save failed'); } - // Add keymap entry - $self->createKeymapEntry($self->getStatus('sourceName'), $self->rowStatusVars['legacyId']); + // Add keymap entry if not in round trip mode + if (!$self->roundtrip) + { + $self->createKeymapEntry($self->getStatus('sourceName'), $self->rowStatusVars['legacyId']); + } // Inherit repository instead of duplicating the association to it if applicable if ($self->object->canInheritRepository($self->object->repositoryId))