Author: sevein
Date: Tue Oct 18 17:33:00 2011
New Revision: 10132

Log:
Factory patter for SWORD packaging formats

Added:
   trunk/plugins/qtSwordPlugin/lib/qtPackageExtractorFactory.class.php
      - copied, changed from r10131, 
trunk/plugins/qtSwordPlugin/lib/qtPackageExtractor.class.php
   
trunk/plugins/qtSwordPlugin/lib/qtPackageExtractorMETSArchivematicaDIP.class.php
Deleted:
   trunk/plugins/qtSwordPlugin/lib/qtPackageExtractor.class.php
Modified:
   
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/actions/depositAction.class.php

Copied and modified: 
trunk/plugins/qtSwordPlugin/lib/qtPackageExtractorFactory.class.php (from 
r10131, trunk/plugins/qtSwordPlugin/lib/qtPackageExtractor.class.php)
==============================================================================
--- trunk/plugins/qtSwordPlugin/lib/qtPackageExtractor.class.php        Mon Oct 
17 23:52:02 2011        (r10131, copy source)
+++ trunk/plugins/qtSwordPlugin/lib/qtPackageExtractorFactory.class.php Tue Oct 
18 17:33:00 2011        (r10132)
@@ -17,269 +17,26 @@
  * along with Qubit Toolkit.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-class qtPackageExtractor
+class qtPackageExtractorFactory
 {
-  protected
-    $directory = null,
-    $filename = null,
-    $document = null,
-    $format = null,
-    $type = null,
-    $resource = null;
-
-  public
-    $informationObject;
-
-  public function __construct($filename, array $options = array())
-  {
-    $this->filename = $filename;
-
-    if (isset($options['format']))
-    {
-      $this->format = $options['format'];
-    }
-
-    if (isset($options['type']))
-    {
-      $this->type = $options['type'];
-    }
-
-    if (isset($options['resource']))
-    {
-      $this->resource = $options['resource'];
-    }
-  }
-
-  protected function processDmdSec($xml, $informationObject = null)
-  {
-    if (!isset($informationObject))
-    {
-      $informationObject = new QubitInformationObject;
-    }
-
-    foreach ($xml->xpath('.//mdWrap/xmlData/dublincore/dcterms:*') as $item)
-    {
-      switch($item->getName())
-      {
-        case 'title':
-          $informationObject->title = $item;
-
-          break;
-
-        case 'creator':
-
-          break;
-
-        case 'subject':
-
-          break;
-
-        case 'description':
-          $informationObject->scopeAndContent = $item;
-
-          break;
-
-        case 'publisher':
-
-          break;
-
-        case 'contributor':
-
-          break;
-
-        case 'date':
-
-          break;
-
-        case 'type':
-
-          break;
-
-        case 'format':
-
-          break;
-
-        case 'identifier':
-
-          break;
-
-        case 'source':
-
-          break;
-
-        case 'language':
-
-          break;
-
-        case 'isPartOf':
-
-          break;
-
-        case 'coverage':
-
-          break;
-
-        case 'rights':
-
-          break;
-      }
-    }
-
-    return $informationObject;
-  }
-
-  public function extract()
-  {
-    switch ($this->type)
-    {
-      case 'application/zip':
-        $this->directory = $this->filename.'_dir';
-        $command = vsprintf('unzip -d %s %s', array($this->directory, 
$this->filename));
-        exec($command, $output, $return);
-        if (0 == $return)
-        {
-          $this->document = @file_get_contents($this->directory.'/METS.xml');
-        }
-
-      break;
-    }
-
-    $this->document = new SimpleXMLElement($this->document);
-  }
-
-  public function process()
+  public static function build($type, array $options = array())
   {
-    $this->informationObject = new QubitInformationObject;
-    $this->informationObject->parentId = $this->resource->id;
-
-    $publicationStatus = sfConfig::get('app_defaultPubStatus', 
QubitTerm::PUBLICATION_STATUS_DRAFT_ID);
-    $this->informationObject->setPublicationStatus($publicationStatus);
+    preg_match_all('/^.*\/(.*)\/*$/', $type, $matches);
 
-    // Main object
-    $dmdSec = $this->getMainDmdSec();
-    $this->processDmdSec($dmdSec, $this->informationObject);
-
-    foreach ($this->getFilesFromDirectory() as $item)
+    if (isset($matches[1]))
     {
-      $parts = pathinfo($item);
-      $filename = $parts['basename'];
-      $uuid = $this->getUUID($filename);
-
-      $child = new QubitInformationObject;
-      $child->setPublicationStatus($publicationStatus);
+      $className = 'qtPackageExtractor'.$matches[1][0];
 
-      if (null !== ($dmdSec = $this->searchFileDmdSec($uuid)))
+      if (!class_exists($className))
       {
-        $this->processDmdSec($dmdSec, $child);
+        throw new Exception('Missing format class.');
       }
-
-      $digitalObject = new QubitDigitalObject;
-      $digitalObject->assets[] = new QubitAsset($filename, 
file_get_contents($item));
-      $digitalObject->usageId = QubitTerm::MASTER_ID;
-
-      $child->digitalObjects[] = $digitalObject;
-
-      $this->informationObject->informationObjectsRelatedByparentId[] = $child;
     }
-
-    $this->informationObject->save();
-}
-
-  public function clean()
-  {
-    unlink($this->filename);
-
-    $rrmdir = function($directory) use (&$rrmdir)
+    else
     {
-      $objects = scandir($directory);
-
-      foreach ($objects as $object)
-      {
-        if ($object != '.' && $object != '..')
-        {
-          if (filetype($directory.'/'.$object) == 'dir')
-          {
-            $rrmdir($directory."/".$object);
-          }
-          else
-          {
-            unlink($directory."/".$object);
-          }
-        }
-      }
-
-      reset($objects);
-      rmdir($directory);
-    };
-
-    $rrmdir($this->directory);
-  }
-
-  protected function getMainDmdSec()
-  {
-    $items = $this->document->xpath('//mets/structMap/div/div');
-
-    $id = $items[0]['DMDID'];
-
-    $dmdSec = $this->document->xpath('//mets/dmdSec[@ID="'.$id.'"]');
-
-    return $dmdSec[0];
-  }
-
-  protected function searchFileDmdSec($uuid)
-  {
-    $node = $this->document->xpath('//mets/fileSec/fileGrp[@USE="original"]');
-
-    foreach ($node[0] as $item)
-    {
-      if (false !== strstr($item['ID'], $uuid))
-      {
-        $dmdSec = 
$this->document->xpath('//mets/dmdSec[@ID="'.$item['DMDID'].'"]');
-        if (0 < count($dmdSec))
-        {
-          return $dmdSec[0];
-        }
-      }
-    }
-  }
-
-  protected function getFilesFromDirectory($dir = null)
-  {
-    $files = array();
-
-    if (!isset($dir))
-    {
-      $dir = $this->directory.'/objects';
+      throw new Exception('Format not recognized.');
     }
 
-    if ($handle = opendir($dir))
-    {
-      while (false !== ($file = readdir($handle)))
-      {
-        if ($file != "." && $file != "..")
-        {
-          if (is_dir($dir.'/'.$file))
-          {
-            $dir2 = $dir.'/'.$file;
-            $files[] = $this->getFilesFromDirectory($dir2);
-          }
-          else
-          {
-            $files[] = $dir.'/'.$file;
-          }
-        }
-      }
-
-      closedir($handle);
-    }
-
-    return $files;
-  }
-
-  protected function getUUID($subject)
-  {
-    
preg_match('/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/', 
$subject, $matches);
-
-    return @$matches[0];
+    return new $className($options);
   }
 }

Added: 
trunk/plugins/qtSwordPlugin/lib/qtPackageExtractorMETSArchivematicaDIP.class.php
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ 
trunk/plugins/qtSwordPlugin/lib/qtPackageExtractorMETSArchivematicaDIP.class.php
    Tue Oct 18 17:33:00 2011        (r10132)
@@ -0,0 +1,293 @@
+<?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 General Public License as published by
+ * the Free Software Foundation, either version 2 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/>.
+ */
+
+class qtPackageExtractorMETSArchivematicaDIP
+{
+  protected
+    $directory = null,
+    $filename = null,
+    $document = null,
+    $format = null,
+    $type = null,
+    $resource = null;
+
+  public
+    $informationObject;
+
+  public function __construct(array $options = array())
+  {
+    if (isset($options['filename']))
+    {
+      $this->filename = $options['filename'];
+    }
+
+    if (isset($options['name']))
+    {
+      $this->name = $options['name'];
+    }
+
+    if (isset($options['format']))
+    {
+      $this->format = $options['format'];
+    }
+
+    if (isset($options['type']))
+    {
+      $this->type = $options['type'];
+    }
+
+    if (isset($options['resource']))
+    {
+      $this->resource = $options['resource'];
+    }
+  }
+
+  protected function processDmdSec($xml, $informationObject = null)
+  {
+    if (!isset($informationObject))
+    {
+      $informationObject = new QubitInformationObject;
+    }
+
+    foreach ($xml->xpath('.//mdWrap/xmlData/dublincore/dcterms:*') as $item)
+    {
+      switch($item->getName())
+      {
+        case 'title':
+          $informationObject->title = $item;
+
+          break;
+
+        case 'creator':
+
+          break;
+
+        case 'subject':
+
+          break;
+
+        case 'description':
+          $informationObject->scopeAndContent = $item;
+
+          break;
+
+        case 'publisher':
+
+          break;
+
+        case 'contributor':
+
+          break;
+
+        case 'date':
+
+          break;
+
+        case 'type':
+
+          break;
+
+        case 'format':
+
+          break;
+
+        case 'identifier':
+
+          break;
+
+        case 'source':
+
+          break;
+
+        case 'language':
+
+          break;
+
+        case 'isPartOf':
+
+          break;
+
+        case 'coverage':
+
+          break;
+
+        case 'rights':
+
+          break;
+      }
+    }
+
+    return $informationObject;
+  }
+
+  public function extract()
+  {
+    switch ($this->type)
+    {
+      case 'application/zip':
+        $this->directory = $this->filename.'_dir';
+        $command = vsprintf('unzip -d %s %s', array($this->directory, 
$this->filename));
+        exec($command, $output, $return);
+        if (0 == $return)
+        {
+          $this->document = @file_get_contents($this->directory.'/METS.xml');
+        }
+
+      break;
+    }
+
+    $this->document = new SimpleXMLElement($this->document);
+  }
+
+  public function process()
+  {
+    $this->informationObject = new QubitInformationObject;
+    $this->informationObject->parentId = $this->resource->id;
+
+    $publicationStatus = sfConfig::get('app_defaultPubStatus', 
QubitTerm::PUBLICATION_STATUS_DRAFT_ID);
+    $this->informationObject->setPublicationStatus($publicationStatus);
+
+    // Main object
+    $dmdSec = $this->getMainDmdSec();
+    $this->processDmdSec($dmdSec, $this->informationObject);
+
+    foreach ($this->getFilesFromDirectory() as $item)
+    {
+      $parts = pathinfo($item);
+      $filename = $parts['basename'];
+      $uuid = $this->getUUID($filename);
+
+      $child = new QubitInformationObject;
+      $child->setPublicationStatus($publicationStatus);
+
+      if (null !== ($dmdSec = $this->searchFileDmdSec($uuid)))
+      {
+        $this->processDmdSec($dmdSec, $child);
+      }
+
+      $digitalObject = new QubitDigitalObject;
+      $digitalObject->assets[] = new QubitAsset($filename, 
file_get_contents($item));
+      $digitalObject->usageId = QubitTerm::MASTER_ID;
+
+      $child->digitalObjects[] = $digitalObject;
+
+      $this->informationObject->informationObjectsRelatedByparentId[] = $child;
+    }
+
+    $this->informationObject->save();
+}
+
+  public function clean()
+  {
+    unlink($this->filename);
+
+    $rrmdir = function($directory) use (&$rrmdir)
+    {
+      $objects = scandir($directory);
+
+      foreach ($objects as $object)
+      {
+        if ($object != '.' && $object != '..')
+        {
+          if (filetype($directory.'/'.$object) == 'dir')
+          {
+            $rrmdir($directory."/".$object);
+          }
+          else
+          {
+            unlink($directory."/".$object);
+          }
+        }
+      }
+
+      reset($objects);
+      rmdir($directory);
+    };
+
+    $rrmdir($this->directory);
+  }
+
+  protected function getMainDmdSec()
+  {
+    $items = $this->document->xpath('//mets/structMap/div/div');
+
+    $id = $items[0]['DMDID'];
+
+    $dmdSec = $this->document->xpath('//mets/dmdSec[@ID="'.$id.'"]');
+
+    return $dmdSec[0];
+  }
+
+  protected function searchFileDmdSec($uuid)
+  {
+    $node = $this->document->xpath('//mets/fileSec/fileGrp[@USE="original"]');
+
+    foreach ($node[0] as $item)
+    {
+      if (false !== strstr($item['ID'], $uuid))
+      {
+        $dmdSec = 
$this->document->xpath('//mets/dmdSec[@ID="'.$item['DMDID'].'"]');
+        if (0 < count($dmdSec))
+        {
+          return $dmdSec[0];
+        }
+      }
+    }
+  }
+
+  protected function getFilesFromDirectory($dir = null)
+  {
+    $files = array();
+
+    if (!isset($dir))
+    {
+      $dir = $this->directory.'/objects';
+    }
+
+    if ($handle = opendir($dir))
+    {
+      while (false !== ($file = readdir($handle)))
+      {
+        if ($file != "." && $file != "..")
+        {
+          if (is_dir($dir.'/'.$file))
+          {
+            $dir2 = $dir.'/'.$file;
+            $files[] = $this->getFilesFromDirectory($dir2);
+          }
+          else
+          {
+            $files[] = $dir.'/'.$file;
+          }
+        }
+      }
+
+      closedir($handle);
+    }
+
+    return $files;
+  }
+
+  protected function getUUID($subject)
+  {
+    
preg_match('/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/', 
$subject, $matches);
+
+    return @$matches[0];
+  }
+}

Modified: 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/actions/depositAction.class.php
==============================================================================
--- 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/actions/depositAction.class.php
   Mon Oct 17 23:52:02 2011        (r10131)
+++ 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/actions/depositAction.class.php
   Tue Oct 18 17:33:00 2011        (r10132)
@@ -38,10 +38,12 @@
 
     if ($request->isMethod('post'))
     {
+      /*
       if (QubitAcl::check(QubitInformationObject::getRoot(), 'create', 
array('user' => $this->user)))
       {
         return $this->generateResponse(403, 'error/ErrorBadRequest', 
array('summary' => $this->context->i18n->__('Forbidden')));
       }
+      */
 
       $this->packageFormat = $request->getHttpHeader('X-Packaging');
       $this->packageContentType = $request->getContentType();
@@ -77,12 +79,19 @@
         return $this->generateResponse(412, 
'error/ErrorChecksumMismatchSuccess', array('summary' => 
$this->context->i18n->__('Checksum sent does not match the calculated 
checksum')));
       }
 
-      // Open the file, parse xml, get objects
-      $extractor = new qtPackageExtractor($filename, array(
-        'name' => $this->packageName,
-        'format' => $this->packageFormat,
-        'resource' => $this->resource,
-        'type' => $this->packageContentType));
+      try
+      {
+        $extractor = qtPackageExtractorFactory::build($this->packageFormat, 
array(
+          'filename' => $filename,
+          'name' => $this->packageName,
+          'format' => $this->packageFormat,
+          'resource' => $this->resource,
+          'type' => $this->packageContentType));
+      }
+      catch (Exception $e)
+      {
+        return $this->generateResponse(415, 'error/ErrorContent', 
array('summary' => $e->getMessage())); 
+      }
 
       // Open package and XML document
       $extractor->extract();
@@ -95,9 +104,6 @@
       // Remove temporary files
       $extractor->clean();
 
-      // Location
-      // $this->response->setHttpHeader('Location', '...');
-
       return $this->generateResponse(201, 'deposit', array('headers' =>
         array('Location' => url_for(array($this->informationObject, 'module' 
=> 'informationobject')))));
     }

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