Skip to content

Commit

Permalink
Round trip mode for CSV import, refs #12281
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mcantelon committed Aug 14, 2019
1 parent 1b2ed9c commit 0c023e8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
26 changes: 18 additions & 8 deletions lib/QubitFlatfileImport.class.php
Expand Up @@ -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
Expand Down Expand Up @@ -162,6 +163,7 @@ public function setUpdateOptions($options)

$this->skipMatched = $options['skip-matched'];
$this->skipUnmatched = $options['skip-unmatched'];
$this->roundtrip = $options['roundtrip'];
}

/*
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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')) : '',
Expand Down
38 changes: 36 additions & 2 deletions lib/task/import/csvImportTask.class.php
Expand Up @@ -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'
)
));
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 0c023e8

Please sign in to comment.