Author: mcantelon
Date: Tue Jan 17 11:32:44 2012
New Revision: 10696

Log:
Moved import-related CLI tools into their own subdirectory.

Added:
   trunk/lib/task/import/csvAccessionImportTask.class.php
      - copied unchanged from r10694, 
trunk/lib/task/csvAccessionImportTask.class.php
   trunk/lib/task/import/csvImportTask.class.php
      - copied unchanged from r10695, trunk/lib/task/csvImportTask.class.php
   trunk/lib/task/import/csvIsaarImportTask.class.php
      - copied unchanged from r10694, 
trunk/lib/task/csvIsaarImportTask.class.php

Copied: trunk/lib/task/import/csvAccessionImportTask.class.php (from r10694, 
trunk/lib/task/csvAccessionImportTask.class.php)
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/lib/task/import/csvAccessionImportTask.class.php      Tue Jan 17 
11:32:44 2012        (r10696, copy of r10694, 
trunk/lib/task/csvAccessionImportTask.class.php)
@@ -0,0 +1,246 @@
+<?php
+
+/*
+ * This file is part of Qubit Toolkit.
+ *
+ * Qubit Toolkit is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Qubit Toolkit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Qubit Toolkit.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Import csv accession data
+ *
+ * @package    symfony
+ * @subpackage task
+ * @author     Mike Cantelon <[email protected]>
+ * @version    SVN: $Id: csvImportTask.class.php 10666 2012-01-13 01:13:48Z 
mcantelon $
+ */
+class csvAccessionImportTask extends sfBaseTask
+{
+  /**
+   * @see sfTask
+   */
+  protected function configure()
+  {
+    $this->addArguments(array(
+      new sfCommandArgument('filename', sfCommandArgument::REQUIRED, 'The 
input file (csv format).')
+    ));
+
+    $this->addOptions(array(
+      new sfCommandOption('rows-until-update', null, 
sfCommandOption::PARAMETER_OPTIONAL, 'Output total rows imported every n 
rows.'),
+      new sfCommandOption('skip-rows', null, 
sfCommandOption::PARAMETER_OPTIONAL, 'Skip n rows before importing.'),
+      new sfCommandOption('error-log', null, 
sfCommandOption::PARAMETER_OPTIONAL, 'File to log errors to.'),
+      new sfCommandOption('source-name', null, 
sfCommandOption::PARAMETER_OPTIONAL, 'Source name to use when inserting keymap 
entries.')
+    ));
+
+    $this->namespace        = 'csv';
+    $this->name             = 'accession-import';
+    $this->briefDescription = 'Import csv acession data';
+
+    $this->detailedDescription = <<<EOF
+Import CSV data
+EOF;
+  }
+
+  protected function validateOptions($options)
+  {
+    $numericOptions = array('rows-until-update', 'skip-rows');
+
+    foreach($numericOptions as $option)
+    {
+      if ($options[$option] && !is_numeric($options[$option]))
+      {
+        throw new sfException($option .' must be an integer');
+      }
+    }
+
+    if ($options['error-log'] && !is_dir(dirname($options['error-log'])))
+    {
+      throw new sfException('Path to error log is invalid.');
+    }
+  }
+
+  /**
+   * @see sfTask
+   */
+  public function execute($arguments = array(), $options = array())
+  {
+    $this->validateOptions($options);
+
+    $skipRows = ($options['skip-rows']) ? $options['skip-rows'] : 0;
+
+    if (!$options['source-name'])
+    {
+      print "WARNING: If you're importing multiple CSV files as part of the "
+        ."same import it's advisable to use the source-name CLI option to "
+        ."specify a source name (otherwise the filename will be used as a "
+        . "source name).\n";
+    }
+
+    $sourceName = ($options['source-name'])
+      ? $options['source-name']
+      : basename($arguments['filename']);
+
+    if (false === $fh = fopen($arguments['filename'], 'rb'))
+    {
+      throw new sfException('You must specify a valid filename');
+    }
+
+    $databaseManager = new sfDatabaseManager($this->configuration);
+    $conn = $databaseManager->getDatabase('propel')->getConnection();
+
+    // Load taxonomies into variables to avoid use of magic numbers
+    $termData = QubitFlatfileImport::loadTermsFromTaxonomies(array(
+      QubitTaxonomy::ACCESSION_ACQUISITION_TYPE_ID => 'acquisitionTypes',
+      QubitTaxonomy::ACCESSION_RESOURCE_TYPE_ID    => 'resourceTypes',
+    ));
+
+    // Define import
+    $import = new QubitFlatfileImport(array(
+      /* How many rows should import until we display an import status update? 
*/
+      'rowsUntilProgressDisplay' => $options['rows-until-update'],
+
+      /* Where to log errors to */
+      'errorLog' => $options['error-log'],
+
+      /* the status array is a place to put data that should be accessible
+         from closure logic using the getStatus method */
+      'status' => array(
+        'sourceName'       => $sourceName,
+        'acquisitionTypes' => $termData['acquisitionTypes'],
+        'resourceTypes'    => $termData['resourceTypes']
+      ),
+      'columnNames' => fgetcsv($fh, 60000), // 1st row supplies column 
names/order
+      'ignoreColumns' => array(
+      ),
+
+      /* import columns that map directory to QubitInformationObject 
properties */
+      'standardColumns' => array(
+      ),
+
+      /* import columns that should be redirected to QubitInformationObject
+         properties (and optionally transformed)
+      
+         Example:
+         'columnMap' => array(
+           'Archival History' => 'archivalHistory',
+           'Revision history' => array(
+             'column' => 'revision',
+             'transformationLogic' => function(&$self, $text)
+             {
+               return $self->appendWithLineBreakIfNeeded(
+                 $self->object->revision,
+                 $text
+               );
+             }
+           )
+         ),
+      */
+      'columnMap' => array(
+        'TITLE'          => 'title',
+        'LOCATION'       => 'locationInformation',
+        'ACCESSION NOTE' => 'processingNotes'
+      ),
+
+      /* these values get stored to the rowStatusVars array */
+      'variableColumns' => array(
+        'ACCESSION NUMBER',
+        'TYPE'
+      ),
+
+      /* import logic to load accession */
+      'rowInitLogic' => function(&$self)
+      {
+        $accessionNumber =  $self->rowStatusVars['ACCESSION NUMBER'];
+
+        // look up Qubit ID of accession using keymap
+        $statement = $self->sqlQuery(
+          "SELECT id FROM accession WHERE identifier=?",
+          $params = array($accessionNumber)
+        );
+
+        $result = $statement->fetch(PDO::FETCH_OBJ);
+        if ($result)
+        {
+          print 'Found '. $result->id ."\n";
+          $self->object = QubitAccession::getById($result->id);
+        } else {
+          $self->object = FALSE;
+          $error = "Couldn't find accession # ". $accessionNumber;
+          $self->printAndLogError($error);
+        }
+      },
+
+      /* import logic to execute before saving accession */
+      'preSaveLogic' => function(&$self)
+      {
+      },
+
+      /* import logic to save accession */
+      'saveLogic' => function(&$self)
+      {
+        if(isset($self->object) && is_object($self->object))
+        {
+          $self->object->save();
+//print 'Saved ID '. $self->object->id ."\n"; exit();
+        }
+      }
+    ));
+
+    $import->addColumnHandler('ACQUISITION METHOD', function(&$self, $data)
+    {
+      if ($data)
+      {
+        $cvaToQubit = array(
+          'Copy Loan'          => 'Deposit', // is this correct?
+          'Donation'           => 'Gift',
+          'Direct Transfer'    => 'Transfer',
+          'Scheduled Transfer' => 'Transfer'
+        );
+
+        if (isset($self->object) && is_object($self->object))
+        {
+          $self->object->resourceTypeId = $self->translateNameToTermId(
+            'acquisition type',
+            $data,
+            $cvaToQubit,
+            $self->getStatus('acquisitionTypes')
+          );
+        }
+      }
+    });
+
+    $import->addColumnHandler('TYPE', function(&$self, $data)
+    {
+      if ($data)
+      {
+        $cvaToQubit = array(
+          'Private records' => 'Private transfer',
+          'Public records'  => 'Public transfer'
+        );
+
+        if (isset($self->object) && is_object($self->object))
+        {
+          $self->object->resourceTypeId = $self->translateNameToTermId(
+            'transfer type',
+            $data,
+            $cvaToQubit,
+            $self->getStatus('resourceTypes')
+          );
+        }
+      }
+    });
+
+    $import->csv($fh, $skipRows);
+  }
+}

Copied: trunk/lib/task/import/csvImportTask.class.php (from r10695, 
trunk/lib/task/csvImportTask.class.php)
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/lib/task/import/csvImportTask.class.php       Tue Jan 17 11:32:44 
2012        (r10696, copy of r10695, trunk/lib/task/csvImportTask.class.php)
@@ -0,0 +1,559 @@
+<?php
+
+/*
+ * This file is part of Qubit Toolkit.
+ *
+ * Qubit Toolkit is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Qubit Toolkit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Qubit Toolkit.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Import csv data
+ *
+ * @package    symfony
+ * @subpackage task
+ * @author     Mike Cantelon <[email protected]>
+ * @version    SVN: $Id$
+ */
+class csvImportTask extends csvImportBaseTask
+{
+    protected $namespace        = 'csv';
+    protected $name             = 'import';
+    protected $briefDescription = 'Import csv information object data';
+
+    protected $detailedDescription = <<<EOF
+Import CSV data
+EOF;
+
+  /**
+   * @see sfTask
+   */
+  public function execute($arguments = array(), $options = array())
+  {
+    $this->validateOptions($options);
+
+    $skipRows = ($options['skip-rows']) ? $options['skip-rows'] : 0;
+
+    if (!$options['source-name'])
+    {
+      print "WARNING: If you're importing multiple CSV files as part of the "
+        ."same import it's advisable to use the source-name CLI option to "
+        ."specify a source name (otherwise the filename will be used as a "
+        . "source name).\n";
+    }
+
+    $sourceName = ($options['source-name'])
+      ? $options['source-name']
+      : basename($arguments['filename']);
+
+    if (false === $fh = fopen($arguments['filename'], 'rb'))
+    {
+      throw new sfException('You must specify a valid filename');
+    }
+
+    $databaseManager = new sfDatabaseManager($this->configuration);
+    $conn = $databaseManager->getDatabase('propel')->getConnection();
+
+    // set default publishing status
+    $defaultStatusId = sfConfig::get(
+      'app_defaultPubStatus',
+      QubitTerm::PUBLICATION_STATUS_PUBLISHED_ID
+    );
+    $defaultStatusTypeId = QubitTerm::STATUS_TYPE_PUBLICATION_ID;
+
+    // create note term if it doesn't yet exist
+    QubitFlatfileImport::createOrFetchTerm(
+      QubitTaxonomy::NOTE_TYPE_ID,
+      'Language note'
+    );
+
+    // Load taxonomies into variables to avoid use of magic numbers
+    $termData = QubitFlatfileImport::loadTermsFromTaxonomies(array(
+      QubitTaxonomy::NOTE_TYPE_ID        => 'noteTypes',
+      QubitTaxonomy::RAD_TITLE_NOTE_ID   => 'titleNoteTypes',
+      QubitTaxonomy::MATERIAL_TYPE_ID    => 'materialTypes',
+      QubitTaxonomy::COPYRIGHT_STATUS_ID => 'copyrightStatusTypes'
+    ));
+
+    // Define import
+    $import = new QubitFlatfileImport(array(
+      /* What type of object are we importing? */
+      'className' => 'QubitInformationObject',
+
+      /* How many rows should import until we display an import status update? 
*/
+      'rowsUntilProgressDisplay' => $options['rows-until-update'],
+
+      /* Where to log errors to */
+      'errorLog' => $options['error-log'],
+
+      /* the status array is a place to put data that should be accessible
+         from closure logic using the getStatus method */
+      'status' => array(
+        'options'              => $options,
+        'sourceName'           => $sourceName,
+        'defaultStatusId'      => $defaultStatusId,
+        'defaultStatusTypeId'  => $defaultStatusTypeId,
+        'materialTypes'        => $termData['materialTypes'],
+        'copyrightStatusTypes' => $termData['copyrightStatusTypes']
+      ),
+      'columnNames' => fgetcsv($fh, 60000), // 1st row supplies column 
names/order
+      'ignoreColumns' => array(
+        'RECORD_ID',
+        'PRI_REC_NO',
+        'sort'
+      ),
+
+      /* import columns that map directory to QubitInformationObject 
properties */
+      'standardColumns' => array(
+        'title',
+        'identifier',
+        'accruals',
+        'scopeAndContent',
+        'extentAndMedium',
+        'acquisition',
+        'accessConditions',
+        'locationOfCopies',
+        'locationOfOriginals',
+        'relatedUnitsOfDescription',
+        'edition',
+        'archivalHistory',
+        'arrangement',
+        'findingAids',
+        'sources',
+        'physicalCharacteristics',
+        'revisionHistory'
+      ),
+
+      /* import columns that should be redirected to QubitInformationObject
+         properties (and optionally transformed)
+      
+         Example:
+         'columnMap' => array(
+           'Archival History' => 'archivalHistory',
+           'Revision history' => array(
+             'column' => 'revision',
+             'transformationLogic' => function(&$self, $text)
+             {
+               return $self->appendWithLineBreakIfNeeded(
+                 $self->object->revision,
+                 $text
+               );
+             }
+           )
+         ),
+      */
+      'columnMap' => array(
+        'physicalStorageLocation' => ' locationOfOriginals'
+      ),
+
+      /* import columns that can be added using the
+         QubitInformationObject::addProperty method */
+      'propertyMap' => array(
+        'radNoteStatementOfResponsibility' => 
'statementOfResponsibilityRelatingToPublishersSeries',
+        'statementOfScaleArchitectural' => 'statementOfScaleArchitectural',
+        'statementOfScaleCartographic' => 'statementOfScaleCartographic',
+        'radTitleProperOfPublishersSeries' => 'titleProperOfPublishersSeries'
+      ),
+
+      /* import columns that can be added as QubitNote objects */
+      'noteMap' => array(
+        'languages' => array(
+          'typeId' => array_search('Language note', $termData['noteTypes'])
+        ),
+        'radNoteConservation' => array(
+          'typeId' => array_search('Conservation note', $termData['noteTypes'])
+        ),
+        'radNoteGeneral' => array(
+          'typeId' => array_search('General note', $termData['noteTypes'])
+        ),
+        'radNoteSourceOfTitleProper' => array(
+          'typeId' => array_search('Source of title proper', 
$termData['titleNoteTypes'])
+        ),
+        'radTitleVariationsInTitle' => array(
+          'typeId' => array_search('Variations in title', 
$termData['titleNoteTypes'])
+        ),
+        'radTitleNoteContinuationOfTitle' => array(
+          'typeId' => array_search('Continuation of title', 
$termData['titleNoteTypes'])
+        ),
+        'radNoteAlphaNumericDesignation' => array(
+          'typeId' => 247,
+          'transformationLogic' => function(&$self, $text)
+          {
+             return 'Old Photo Number: '. $text;
+          }
+        )
+      ),
+
+      /* these values get stored to the rowStatusVars array */
+      'variableColumns' => array(
+        'UNIQUE_ID',
+        'PARENT_ID',
+        'datesOfCreation',
+        'copyrightStatus',
+        'copyrightExpires',
+        'copyrightHolder',
+        'datesOfCreationNote',
+        'datesOfCreationStart',
+        'datesOfCreationEnd'
+      ),
+
+      /* these values get exploded and stored to the rowStatusVars array */
+      'arrayColumns' => array(
+        'accessionNumber'     => '|',
+        'creators'            => '|',
+        'creatorHistory'      => '|',
+        'subjectAccessPoints' => '|',
+        'placeAccessPoints'   => '|',
+        'nameAccessPoints'    => '|'
+      ),
+
+      /* import logic to execute before saving information object */
+      'preSaveLogic' => function(&$self)
+      {
+        // set to default status
+        $self->object->setStatus(array(
+          'statusId' => $self->getStatus('defaultStatusId'),
+          'typeId'   => $self->getStatus('defaultStatusTypeId')
+        ));
+
+        if (!$self->rowStatusVars['PARENT_ID'])
+        {
+          $parentId = QubitInformationObject::ROOT_ID;
+        } else {
+          if ($mapEntry = $self->fetchKeymapEntryBySourceAndTargetName(
+            $self->rowStatusVars['PARENT_ID'],
+            $self->getStatus('sourceName'),
+            'information_object'
+          ))
+          {
+            $parentId = $mapEntry->target_id;
+          } else {
+            $error = 'For UNIQUE_ID '
+              . $self->rowStatusVars['UNIQUE_ID']
+              .' Could not find PARENT_ID '
+              . $self->rowStatusVars['PARENT_ID']
+              .' in key_map table';
+            $self->printAndLogError($error);
+          }
+        }
+
+        if (isset($parentId))
+        {
+          $self->object->parentId = $parentId;
+        }
+      },
+
+      /* import logic to execute after saving information object */
+      'postSaveLogic' => function(&$self)
+      {
+        if (!$self->object->id)
+        {
+          throw new sfException('Information object save failed');
+        } else {
+          // add keymap entry
+          $keymap = new QubitKeymap;
+          $keymap->sourceId   = $self->rowStatusVars['UNIQUE_ID'];
+          $keymap->sourceName = $self->getStatus('sourceName');
+          $keymap->targetId   = $self->object->id;
+          $keymap->targetName = 'information_object';
+          $keymap->save();
+
+          // add subject access points
+          $accessPointColumns = array(
+            'subjectAccessPoints' => QubitTaxonomy::SUBJECT_ID,
+            'placeAccessPoints'   => QubitTaxonomy::PLACE_ID,
+          );
+
+          foreach($accessPointColumns as $columnName => $taxonomyId)
+          {
+            if (isset($self->rowStatusVars[$columnName]))
+            {
+              foreach($self->rowStatusVars[$columnName] as $subject)
+              {
+                $self->createAccessPoint($taxonomyId, $subject);
+              }
+            }
+          }
+
+          // add name access points
+          if (isset($self->rowStatusVars['nameAccessPoints']))
+          {
+            // add name access points
+            foreach($self->rowStatusVars['nameAccessPoints'] as $name)
+            {
+              $actor = $self->createOrFetchActor($name);
+              $relation = new QubitRelation;
+              $relation->subjectId = $self->object->id;
+              $relation->objectId = $actor->id;
+              $relation->typeId = QubitTerm::NAME_ACCESS_POINT_ID;
+              $relation->save();
+            }
+          }
+
+          // add accessions
+          if (
+            isset($self->rowStatusVars['accessionNumber'])
+            && count($self->rowStatusVars['accessionNumber'])
+          )
+          {
+            foreach($self->rowStatusVars['accessionNumber'] as 
$accessionNumber)
+            {
+              // attempt to fetch keymap entry
+              $accessionMapEntry = 
$self->fetchKeymapEntryBySourceAndTargetName(
+                $accessionNumber,
+                $self->getStatus('sourceName'),
+                'accession'
+              );
+
+              // if no entry found, create accession and entry
+              if (!$accessionMapEntry)
+              {
+                print "\nCreating accession # ". $accessionNumber ."\n";
+
+                // create new accession
+                $accession = new QubitAccession;
+                $accession->save();
+
+                // workaround to problem setting identifier
+                $query = "UPDATE accession SET identifier=? WHERE id=?";
+                $self->sqlQuery($query, $params = array($accessionNumber, 
$accession->id));
+
+                // create keymap entry for accession
+                $keymap = new QubitKeymap;
+                $keymap->sourceId   = $accessionNumber;
+                $keymap->sourceName = $self->getStatus('sourceName');
+                $keymap->targetId   = $accession->id;
+                $keymap->targetName = 'accession';
+                $keymap->save();
+
+                $accessionId = $accession->id;
+              } else {
+                $accessionId = $accessionMapEntry->target_id;
+              }
+
+              print "\nAssociating accession # ". $accessionNumber ." with ". 
$self->object->title ."\n";
+
+              // add relationship between information object and accession
+              $relation = new QubitRelation;
+              $relation->subjectId = $self->object->id;
+              $relation->objectId = $accessionId;
+              $relation->typeId = QubitTerm::ACCESSION_ID;
+              $relation->save();
+            }
+          }
+
+          // add material-related term relation
+          if (isset($self->rowStatusVars['radGeneralMaterialDesignation']))
+          {
+            $self->createObjectTermRelation(
+              $self->object->id,
+              $self->rowStatusVars['radGeneralMaterialDesignation']
+            );
+          }
+
+          // add copyright info
+          if (isset($self->rowStatusVars['copyrightStatus']) && 
$self->rowStatusVars['copyrightStatus'])
+          {
+            switch (strtolower($self->rowStatusVars['copyrightStatus']))
+            {
+              case 'under copyright':
+                print "Adding rights for ". $self->object->title ."...\n";
+                $rightsHolderId = FALSE;
+                if ($self->rowStatusVars['copyrightHolder'])
+                {
+                  // add rightsholder
+                  $actor = new QubitRightsHolder;
+                  $actor->parentId = QubitActor::ROOT_ID;
+                  $actor->authorizedFormOfName = 
$self->rowStatusVars['copyrightHolder'];
+                  $actor->save();
+
+                  $rightsHolderId = $actor->id;
+
+                }
+                $rightAndRelation = array(
+                  'restriction'       => 1,
+                  'basisId'           => 170,
+                  'actId'             => 305,
+                  'copyrightStatusId' => array_search(
+                    'Under copyright',
+                    $self->getStatus('copyrightStatusTypes')
+                  )
+                );
+                if ($rightsHolderId) $rightAndRelation['rightsHolderId'] = 
$rightsHolderId;
+                $self->createRightAndRelation($rightAndRelation);
+                break;
+
+              case 'unknown':
+                $self->createRightAndRelation(array(
+                  'restriction'       => 1,
+                  'basisId'           => 170,
+                  'actId'             => 305,
+                  'copyrightStatusId' => array_search(
+                    'Unknown',
+                    $self->getStatus('copyrightStatusTypes')
+                  )
+                ));
+                break;
+
+              case 'public domain':
+                $self->createRightAndRelation(array(
+                  'restriction'       => 1,
+                  'basisId'           => 170,
+                  'actId'             => 305,
+                  'copyrightStatusId' => array_search(
+                    'Public domain',
+                    $self->getStatus('copyrightStatusTypes')
+                  )
+                ));
+                break;
+
+              default:
+                throw new sfException('Copyright status "'
+                  . $self->rowStatusVars['copyrightStatus']
+                  .'" not handled: adjust script or import data');
+                break;
+            }
+          }
+
+          // add creators and create events
+          if (isset($self->rowStatusVars['creators'])
+            && count($self->rowStatusVars['creators']))
+          {
+            foreach($self->rowStatusVars['creators'] as $creator)
+            {
+              // add create event if specified
+              if (
+                isset($self->rowStatusVars['datesOfCreationStart'])
+                || (
+                  isset($self->rowStatusVars['datesOfCreationStart'])
+                  && isset($self->rowStatusVars['datesOfCreationEnd'])
+                )
+              )
+              {
+                $endDate = (isset($self->rowStatusVars['datesOfCreationEnd']))
+                  ? $self->rowStatusVars['datesOfCreationEnd'] .'-00-00'
+                  : false;
+
+                $eventData = array(
+                  'startDate' => $self->rowStatusVars['datesOfCreationStart'] 
.'-00-00',
+                  'endDate'   => $endDate,
+                  'actorName' => $creator
+                );
+
+                foreach(array(
+                    'datesOfCreationNote' => 'description',
+                    'datesOfCreation'     => 'date'
+                  )
+                  as $statusVar => $eventProperty
+                )
+                {
+                  $eventData[$eventProperty] = 
$self->rowStatusVars[$statusVar];
+                }
+
+                if(isset($self->rowStatusVars['creatorHistory']))
+                {
+                  $creatorPosition = array_search($creator, 
$self->rowStatusVars['creators']);
+                  if 
(isset($self->rowStatusVars['creatorHistory'][$creatorPosition]))
+                  {
+                    $eventData['actorHistory'] = 
$self->rowStatusVars['creatorHistory'][$creatorPosition];
+                  }
+                }
+
+                $event = $self->createEvent(
+                  QubitTerm::CREATION_ID,
+                  $eventData
+                );
+              }
+            }
+          }
+
+          // if a role is found, create term and actor if need be
+          if (isset($self->rowStatusVars['actorRoles']))
+          {
+            foreach($self->rowStatusVars['actorRoles'] as $actorRole)
+            {
+              // create/fetch term
+              $term = $self->createOrFetchTerm(
+                QubitTaxonomy::EVENT_TYPE_ID,
+                $actorRole['role']
+              );
+
+              // create/fetch actor
+              $self->createOrFetchActor($actorRole['actor']);
+
+              $self->createEvent($term->id, array('actorName' => 
$actorRole['actor']));
+            }
+          }
+        }
+      }
+    ));
+
+    $import->addColumnHandler('levelOfDescription', function(&$self, $data)
+    {
+      $self->object->setLevelOfDescriptionByName($data);
+    });
+
+    /* create function to deal with columns whose names begin with
+       "relatedActor", parsing out name of role from column name */
+    $relatedActorHandler = function(&$self, $data)
+    {
+      if ($data)
+      {
+        // parse out actor role from column name
+        $pattern = '/^relatedActor(.*)By$|^relatedActor(.*)$/';
+        preg_match($pattern, $self->status['currentColumn'], $matches);
+        $termName = (isset($matches[1]) && $matches[1] != '') ? $matches[1] : 
'';
+        $termName = (isset($matches[2]) && $matches[2] != '') ? $matches[2] : 
$termName;
+
+        // note that role and actor should be created after saving info object
+        $self->rowStatusVars['actorRoles'] = 
(isset($self->rowStatusVars['actorRoles']))
+          ? $self->rowStatusVars['actorRoles']
+          : array();
+
+        array_push(
+          $self->rowStatusVars['actorRoles'],
+          array('role' => $termName, 'actor' => $data)
+        );
+      }
+    };
+
+    $import->addColumnHandler('relatedActorCommissionedBy', 
$relatedActorHandler);
+    $import->addColumnHandler('relatedActorPhotographer', 
$relatedActorHandler);
+
+    // map value to taxonomy term name and take note of taxonomy term's ID
+    $import->addColumnHandler('radGeneralMaterialDesignation', 
function(&$self, $data)
+    {
+      if ($data)
+      {
+        $cvaToQubit = array(
+          'Text'              => 'Textual record',
+          'Photograph'        => 'Graphic material',
+          'OtherGraphical'    => 'Graphic material',
+          'ArchitecturalPlan' => 'Architectural drawing',
+          'Audio'             => 'Sound recording',
+          'MovingImage'       => 'Moving images',
+          'Map'               => 'Cartographic material'
+        );
+
+        $self->rowStatusVars['radGeneralMaterialDesignation'] = 
$self->translateNameToTermId(
+          'material types',
+          $data,
+          $cvaToQubit,
+          $self->getStatus('materialTypes')
+        );
+      }
+    });
+
+    $import->csv($fh, $skipRows);
+  }
+}

Copied: trunk/lib/task/import/csvIsaarImportTask.class.php (from r10694, 
trunk/lib/task/csvIsaarImportTask.class.php)
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/lib/task/import/csvIsaarImportTask.class.php  Tue Jan 17 11:32:44 
2012        (r10696, copy of r10694, 
trunk/lib/task/csvIsaarImportTask.class.php)
@@ -0,0 +1,143 @@
+<?php
+
+/*
+ * This file is part of Qubit Toolkit.
+ *
+ * Qubit Toolkit is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Qubit Toolkit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Qubit Toolkit.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Import ISAAR formatted csv data
+ *
+ * @package    symfony
+ * @subpackage task
+ * @author     Mike Cantelon <[email protected]>
+ * @author     David Juhasz <[email protected]>
+ * @version    SVN: $Id$
+ */
+class csvIsaarImportTask extends sfBaseTask
+{
+  protected static
+    $count = 0;
+
+  /**
+   * @see sfTask
+   */
+  protected function configure()
+  {
+    $this->addArguments(array(
+      new sfCommandArgument('filename', sfCommandArgument::REQUIRED, 'The 
input file (csv format).')
+    ));
+
+    $this->addOptions(array(
+      // http://trac.symfony-project.org/ticket/8352
+      new sfCommandOption('application', null, 
sfCommandOption::PARAMETER_REQUIRED, 'The application name', true),
+      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 
'The environment', 'cli'),
+      new sfCommandOption('rows-until-update', null, 
sfCommandOption::PARAMETER_OPTIONAL, 'Output total rows imported every n rows.')
+    ));
+
+    $this->namespace = 'csv';
+    $this->name = 'isaar-import';
+    $this->briefDescription = 'Import csv data';
+
+    $this->detailedDescription = <<<EOF
+Import ISAAR formatted CSV data
+EOF;
+  }
+
+  /**
+   * @see sfTask
+   */
+  public function execute($arguments = array(), $options = array())
+  {
+    if ($options['rows-until-update'] && 
!is_numeric($options['rows-until-update']))
+    {
+      throw new sfException('rows-until-update must be an integer');
+    }
+
+    if (false === $fh = fopen($arguments['filename'], 'rb'))
+    {
+      throw new sfException(sprintf('Could not open file "%s"', 
$arguments['filename']));
+    }
+
+    $databaseManager = new sfDatabaseManager($this->configuration);
+    $conn = $databaseManager->getDatabase('propel')->getConnection();
+
+    // First row is header
+    $header = fgetcsv($fh);
+
+    // camelCase header values
+    $header = array_map(array('QubitFlatFileImport', 'camelize'), $header);
+
+    // Set source file
+    qtIsaarCsv::$keymapSource = basename($arguments['filename']);
+
+    // Load entity type terms for lookup
+    qtIsaarCsv::$entityTypeLookup = 
self::getTermsForLookup(QubitTaxonomy::ACTOR_ENTITY_TYPE_ID, $conn);
+
+    while ($row = fgetcsv($fh))
+    {
+      $isaarCsv = new qtIsaarCsv(new QubitActor);
+
+      foreach (array_combine($header, $row) as $name => $value)
+      {
+        $isaarCsv->__set($name, $value);
+      }
+
+      $isaarCsv->save();
+
+      $this->count++;
+
+//DEBUGGING: stop after two rows
+      if (1 < $this->count)
+      {
+        break;
+      }
+    }
+
+    fclose($fh);
+  }
+
+  /**
+   * Get a hash table for looking up term ids based on the term name
+   *
+   * @param integer $taxonomyId              taxonomy foreign key
+   * @param sfPropelConnection $connnection  database connection
+   *
+   * @return array  hash table keyed on the English term name
+   */
+  protected function getTermsForLookup($taxonomyId, $connection)
+  {
+    $query = "SELECT term.id, i18n.name
+      FROM ".QubitTerm::TABLE_NAME." term JOIN ".QubitTermI18n::TABLE_NAME." 
i18n
+        ON term.id = i18n.id
+      WHERE term.taxonomy_id = ?
+        AND i18n.culture = 'en';";
+
+    $statement = $connection->prepare($query);
+    $statement->bindValue(1, $taxonomyId, PDO::PARAM_INT);
+    if (false === $statement->execute())
+    {
+      return;
+    }
+
+    while ($row = $statement->fetch(PDO::FETCH_NUM))
+    {
+      // key on lowercase term name
+      $terms[strtolower($row[1])] = $row[0];
+    }
+
+    return $terms;
+  }
+}

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to