Author: david
Date: Wed Mar  2 10:09:22 2011
New Revision: 9017

Log:
Create child information objects to maintain one-to-one relationship between 
info objects and digital objects

Modified:
   trunk/lib/task/digitalObjectLoadTask.class.php

Modified: trunk/lib/task/digitalObjectLoadTask.class.php
==============================================================================
--- trunk/lib/task/digitalObjectLoadTask.class.php      Wed Mar  2 00:02:53 
2011        (r9016)
+++ trunk/lib/task/digitalObjectLoadTask.class.php      Wed Mar  2 10:09:22 
2011        (r9017)
@@ -27,6 +27,9 @@
  */
 class digitalObjectLoadTask extends sfBaseTask
 {
+  protected static
+    $count = 0;
+
   /**
    * @see sfTask
    */
@@ -79,38 +82,75 @@
     $idKey = array_search('information_object_id', $header);
     $fileKey = array_search('filename', $header);
 
-    $count = 0;
+    // Build hash on information_object.id, with array value if information
+    // object has multiple digital objects attached 
     while ($item = fgetcsv($fh, 1000))
     {
-      if (null === $informationObject = 
QubitInformationObject::getById($item[$idKey]))
+      if (!isset($digitalObjects[$item[$idKey]]))
       {
-        $this->log("Invalid information_object id {$item[$idKey]}");
-
-        continue;
+        $digitalObjects[$item[$idKey]] = $item[$fileKey];
       }
+      else
+      {
+        $digitalObjects[$item[$idKey]] = array($digitalObjects[$item[$idKey]], 
$item[$fileKey]);
+      }
+    }
 
-      // read file contents
-      if (false === $content = file_get_contents($item[$fileKey]))
+    // Loop through $digitalObject hash and add digital objects to db
+    foreach ($digitalObjects as $key => $item)
+    {
+      if (null === $informationObject = QubitInformationObject::getById($key))
       {
-        $this->log("Couldn't read file '{$item[$fileKey]}'");
+        $this->log("Invalid information_object id $key");
 
         continue;
       }
 
-      $filename = basename($item[$fileKey]);
-      $this->log("Loading '$filename'");
+      if (!is_array($item))
+      {
+        self::addDigitalObject($informationObject, $item, $conn);
+      }
+      else
+      {
+        // If more than one digital object linked to this information object
+        for ($i=0; $i < count($item); $i++)
+        {
+          // Create new information objects, to maintain one-to-one
+          // relationship with digital objects
+          $informationObject = new QubitInformationObject;
+          $informationObject->parent = QubitInformationObject::getById($key);
+          $informationObject->title = basename($item[$i]);
+          $informationObject->save($conn);
+          
+          self::addDigitalObject($informationObject, $item[$i], $conn);
+        }
+      }
+    }
+
+    $this->logSection('Successfully Loaded '.self::$count.' digital objects.');
+  }
 
-      // Create digital object
-      $do = new QubitDigitalObject;
-      $do->informationObject = $informationObject;
-      $do->usageId = QubitTerm::MASTER_ID;
-      $do->assets[] = new QubitAsset($filename, $content);
-      $do->save($conn);
+  protected function addDigitalObject($informationObject, $filepath, $conn)
+  {
+    // read file contents
+    if (false === $content = file_get_contents($filepath))
+    {
+      $this->log("Couldn't read file '$filepath'");
 
-      $count++;
+      return;
     }
 
-    $this->logSection("Successfully Loaded $count digital objects.");
+    $filename = basename($filepath);
+    $this->log("Loading '$filename'");
+
+    // Create digital object
+    $do = new QubitDigitalObject;
+    $do->informationObject = $informationObject;
+    $do->usageId = QubitTerm::MASTER_ID;
+    $do->assets[] = new QubitAsset($filename, $content);
+    $do->save($conn);
+
+    self::$count++;
   }
 
   protected function getUploadDir($conn)

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