Author: mj
Date: Fri Oct 28 13:35:07 2011
New Revision: 10227

Log:
Refactor & generalize parenting code for import; lots of fixes against #140. 
More to come for related form values for RAD & ISAD

Modified:
   trunk/apps/qubit/modules/informationobject/actions/editAction.class.php
   trunk/lib/QubitCsvImport.class.php
   trunk/lib/task/importBulkTask.class.php
   
trunk/plugins/sfInstallPlugin/modules/sfInstallPlugin/actions/clearCacheAction.class.php

Modified: 
trunk/apps/qubit/modules/informationobject/actions/editAction.class.php
==============================================================================
--- trunk/apps/qubit/modules/informationobject/actions/editAction.class.php     
Fri Oct 28 09:56:38 2011        (r10226)
+++ trunk/apps/qubit/modules/informationobject/actions/editAction.class.php     
Fri Oct 28 13:35:07 2011        (r10227)
@@ -530,7 +530,36 @@
   {
     parent::execute($request);
 
-    if ($request->isMethod('post'))
+    if ($request->hasParameter('csvimport'))
+    {
+      // if a parent ID is set, use that for parenting
+      $parentId = $request->getParameter('parent', null);
+
+      if (!empty($parentId))
+      {
+        $request->getParameterHolder()->remove('parent');
+      }
+
+      // make sure we don't pass the import ID
+      $request->getParameterHolder()->remove('id');
+
+      $this->form->bind($request->getParameterHolder()->getAll());
+      if ($this->form->isValid())
+      {
+        $this->processForm();
+
+        if (!empty($parentId))
+        {
+          $this->resource->parent = QubitInformationObject::getById($parentId);
+        }
+        else{
+          $this->resource->parent = 
QubitInformationObject::getById(QubitInformationObject::ROOT_ID);
+        }
+
+        $this->resource->save();
+      }
+    }
+    elseif ($request->isMethod('post'))
     {
       $this->form->bind($request->getPostParameters());
       if ($this->form->isValid())

Modified: trunk/lib/QubitCsvImport.class.php
==============================================================================
--- trunk/lib/QubitCsvImport.class.php  Fri Oct 28 09:56:38 2011        (r10226)
+++ trunk/lib/QubitCsvImport.class.php  Fri Oct 28 13:35:07 2011        (r10227)
@@ -34,11 +34,12 @@
   public function import($csvFile, $options = array())
   {
     // load the CSV document into an array
-    $data = $this->loadCSV($csvFile, $options);
+    $data = $this->loadCSV($csvFile);
 
-     // information object schema element names
-    $names['isad'] = array_flip(array_map(array('sfInflector', 'underscore'), 
sfIsadPluginEditAction::$NAMES));
+    // NOTE: ORDER MATTERS HERE
+    // information object schema element names
     $names['rad'] = array_flip(array_map(array('sfInflector', 'underscore'), 
sfRadPluginEditAction::$NAMES));
+    $names['isad'] = array_flip(array_map(array('sfInflector', 'underscore'), 
sfIsadPluginEditAction::$NAMES));
 
     // repository schema element names
     $names['isdiah'] = array_flip(array_map(array('sfInflector', 
'underscore'), sfIsdiahPluginEditAction::$NAMES));
@@ -49,7 +50,7 @@
     unset($data['header']);
 
     // see if any of these schemas match close enough
-    if (!in_array($options['schema'], array('isad', 'rad', 'isdiah')))
+    if (!in_array($options['schema'], array('rad', 'isad', 'isdiah')))
     {
       foreach ($names as $name => $elements)
       {
@@ -75,7 +76,7 @@
       throw new Exception($errorMsg);
     }
 
-    // switch context and create a new repository object using the ISDIAH 
schema
+    // switch context and create a new object using the import schema
     $this->context = sfContext::getInstance();
 
     switch ($importSchema)
@@ -83,29 +84,76 @@
       case 'isdiah':
         $action = new sfIsdiahPluginEditAction($this->context, 
'sfIsdiahPlugin', 'edit');
         break;
+      case 'rad':
+        $action = new sfRadPluginEditAction($this->context, 'sfRadPlugin', 
'edit');
+        break;
+      case 'isad':
+        $action = new sfIsadPluginEditAction($this->context, 'sfRadPlugin', 
'edit');
+        break;
     }
 
-    unset($action->getRoute()->resource); // we are not editing an existing 
object
+    // we are not editing an existing object
+    unset($action->getRoute()->resource);
+
+    // populate parameter holder with properties for the object
+    foreach ($data as $index => $row)
+    {
+      $parameters = array_combine(array_map('lcfirst', 
array_map(array('sfInflector', 'camelize'), array_keys($header))), $row);
+
+      // generic mapping for hierarchical data
+      if (isset($parameters['id']))
+      {
+        $ids[$index] = $parameters['id'];
+      }
+
+      if (isset($parameters['parent']))
+      {
+        $parents[$index] = $parameters['parent'];
+      }
+
+      // special cases for various schemas to map to parameter holders
+      switch ($importSchema)
+      {
+        case 'isdiah':
+          $parameters = $this->mapIsdiah($parameters);
+          break;
+        case 'rad':
+          $parameters = $this->mapRad($parameters);
+          break;
+        case 'isad':
+          $parameters = $this->mapIsad($parameters);
+          break;
+      }
+
+      $parameterArray[] = $parameters;
+    }
+
+    // if we have hierarchy information, re-order data rows
+    if (isset($ids) && isset($parents))
+    {
+      array_multisort($parents, SORT_ASC, $ids, SORT_ASC, $parameterArray);
+    }
 
     // emulate a POST form submission with given data
     $request = $this->context->getRequest();
     $request->setParameter('csvimport', true);
 
-    foreach ($data as $row)
+    // populate and submit the form for each data row
+    $insertIds = array();
+    foreach ($parameterArray as $index => $parameters)
     {
-
-      // populate parameter holder with properties for the object
-      $parameters = array_combine(array_map('lcfirst', 
array_map(array('sfInflector', 'camelize'), array_keys($header))), $row);
-
-      // special cases for various schemas to map to parameter holders
-      if ('isdiah' == $importSchema)
+      if (!empty($parameters['parent']))
       {
-        $parameters = $this->mapIsdiah($parameters);
+        $parameters['parent'] = $insertIds[$parameters['parent']];
       }
+
       // run the action to create and save the new object
       $request->getParameterHolder()->add($parameters);
       $action->execute($request);
 
+      // keep track of the insert ID for parenting
+      $insertIds[$parameters['id']] = $action->resource->id;
+
       // set the rootObject to use for initial display in successful import
       if (!$this->rootObject)
       {
@@ -116,14 +164,14 @@
     return $this;
   }
 
-  protected function loadCSV($csvFile, $options = array())
+  protected function loadCSV($csvFile)
   {
     // make sure we get the right EOL character
     ini_set('auto_detect_line_endings', true);
     $fh = fopen($csvFile, 'rb');
 
     // Get header (first) row
-    foreach (fgetcsv($fh, 1000) as $col => $label)
+    foreach (fgetcsv($fh) as $col => $label)
     {
         // normalize the column labels to match defined fields
         $header[$col] = preg_replace(array('/(\s|\/)+/', '/[^a-z_]/', '/_$/'), 
array('_', ''), strtolower(sfInflector::underscore($label)));
@@ -131,7 +179,7 @@
     $data['header'] = $header;
 
     // the rest of data is n-indexed
-    while ($row = fgetcsv($fh, 1000))
+    while ($row = fgetcsv($fh))
     {
         $data[] = $row;
     }
@@ -141,19 +189,8 @@
     return $data;
   }
 
-  protected function mapIsdiah($parameters)
+  protected function mapEdit($parameters)
   {
-    // remove duplicate values
-    if ($parameters['parallelName'] == $parameters['authorizedFormOfName'])
-    {
-      $parameters['parallelName'] = '';
-    }
-
-    if ($parameters['otherName'] == $parameters['authorizedFormOfName'])
-    {
-      $parameters['otherName'] = '';
-    }
-
     // convert pipe-delimited values into multi-value
     $n = 0;
     foreach (explode('|', $parameters['parallelName']) as $new_parallelName)
@@ -181,22 +218,104 @@
       $parameters['language'] = '';
     }
 
-    if (!in_array($parameters['script'], $this->culture->getScripts()))
+    // TODO: FIXME;
+    if (true || !in_array($parameters['script'], $this->culture->getScripts()))
     {
       $parameters['script'] = '';
     }
 
-    // TODO: FIX ME
-//    if (!in_array($parameters['descDetail'], 
QubitTaxonomy::getTermsById(QubitTaxonomy::DESCRIPTION_DETAIL_LEVEL_ID)))
-    if (!empty($parameters['descDetail']))
+    if (!isset($this->levelsOfDescription))
+    {
+      foreach 
(QubitTaxonomy::getTermsById(QubitTaxonomy::LEVEL_OF_DESCRIPTION_ID) as $term)
+      {
+        $this->levelsOfDescription[] = $term;
+      }
+    }
+
+    if (!in_array($parameters['levelOfDescription'], 
$this->levelsOfDescription))
     {
-      $parameters['descDetail'] = '';
+      $parameters['levelOfDescription'] = '';
     }
 
-//    if (!in_array($parameters['descStatus'], 
QubitTaxonomy::getTermsById(QubitTaxonomy::DESCRIPTION_STATUS_ID)))
+    if (!isset($this->descriptionDetailLevels))
+    {
+      foreach 
(QubitTaxonomy::getTermsById(QubitTaxonomy::DESCRIPTION_DETAIL_LEVEL_ID) as 
$term)
+      {
+        $this->descriptionDetailLevels[] = $term;
+      }
+    }
+
+    if (!in_array($parameters['descriptionDetail'], 
$this->descriptionDetailLevels))
+    {
+      $parameters['descriptionDetail'] = '';
+    }
+
+    if (!isset($this->descriptionStatuss))
+    {
+      foreach 
(QubitTaxonomy::getTermsById(QubitTaxonomy::DESCRIPTION_STATUS_ID) as $term)
+      {
+        $this->descriptionStatuss[] = $term;
+      }
+    }
+
+    if (!in_array($parameters['descriptionStatus'], $this->descriptionStatuss))
+    {
+      $parameters['descriptionStatus'] = '';
+    }
+
+    return $parameters;
+  }
+
+  protected function mapRad($parameters)
+  {
+    // TODO: refactor into here
+    return $this->mapEdit($parameters);
+  }
+
+  protected function mapIsad($parameters)
+  {
+    // TODO: refactor into here
+    return $this->mapEdit($parameters);
+  }
+
+  protected function mapIsdiah($parameters)
+  {
+    // remove duplicate values
+    if ($parameters['parallelName'] == $parameters['authorizedFormOfName'])
+    {
+      $parameters['parallelName'] = '';
+    }
+
+    if ($parameters['otherName'] == $parameters['authorizedFormOfName'])
+    {
+      $parameters['otherName'] = '';
+    }
+
+    // NB: this is hacky, but required for an inconsistency in repository 
naming
     if (!empty($parameters['descStatus']))
     {
-      $parameters['descStatus'] = '';
+      $parameters['descriptionStatus'] = $parameters['descStatus'];
+      unset($parameters['descStatus']);
+    }
+
+    if (!empty($parameters['descDetail']))
+    {
+      $parameters['descriptionDetail'] = $parameters['descDetail'];
+      unset($parameters['descDetail']);
+    }
+
+    $parameters = $this->mapEdit($parameters);
+
+    if (!empty($parameters['descriptionStatus']))
+    {
+      $parameters['descStatus'] = $parameters['descriptionStatus'];
+      unset($parameters['descriptionStatus']);
+    }
+
+    if (!empty($parameters['descriptionDetail']))
+    {
+      $parameters['descDetail'] = $parameters['descriptionDetail'];
+      unset($parameters['descriptionDetail']);
     }
 
     return $parameters;

Modified: trunk/lib/task/importBulkTask.class.php
==============================================================================
--- trunk/lib/task/importBulkTask.class.php     Fri Oct 28 09:56:38 2011        
(r10226)
+++ trunk/lib/task/importBulkTask.class.php     Fri Oct 28 13:35:07 2011        
(r10227)
@@ -29,6 +29,7 @@
       new sfCommandOption('application', null, 
sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'qubit'),
       new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 
'The environment', 'cli'),
       new sfCommandOption('noindex', null, 
sfCommandOption::PARAMETER_OPTIONAL, 'Set to \'true\' to skip indexing on 
imported objects'),
+      new sfCommandOption('schema', null, sfCommandOption::PARAMETER_OPTIONAL, 
'Schema to use if importing a CSV file'),
       new sfCommandOption('output', null, sfCommandOption::PARAMETER_OPTIONAL, 
'Filename to output results in CSV format'),
       new sfCommandOption('v', null, sfCommandOption::PARAMETER_OPTIONAL, 
'Verbose output'),
     ));
@@ -86,7 +87,7 @@
       if ('csv' == pathinfo($file, PATHINFO_EXTENSION))
       {
         $importer = new QubitCsvImport;
-        $importer->import($file);
+        $importer->import($file, $options);
       }
       elseif ('xml' == pathinfo($file, PATHINFO_EXTENSION))
       {

Modified: 
trunk/plugins/sfInstallPlugin/modules/sfInstallPlugin/actions/clearCacheAction.class.php
==============================================================================
--- 
trunk/plugins/sfInstallPlugin/modules/sfInstallPlugin/actions/clearCacheAction.class.php
    Fri Oct 28 09:56:38 2011        (r10226)
+++ 
trunk/plugins/sfInstallPlugin/modules/sfInstallPlugin/actions/clearCacheAction.class.php
    Fri Oct 28 13:35:07 2011        (r10227)
@@ -25,6 +25,10 @@
     $cacheClear = new 
sfCacheClearTask(sfContext::getInstance()->getEventDispatcher(), new 
sfFormatter());
     $cacheClear->run();
 
+    // Clear the search index
+    QubitSearch::getInstance()->getEngine()->erase();
+    QubitSearch::getInstance()->optimize();
+
     $this->redirect(array('module' => 'sfInstallPlugin', 'action' => 
'finishInstall'));
   }
 }

-- 
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