Author: mattmann
Date: Sun Mar 20 04:48:28 2011
New Revision: 1083367

URL: http://svn.apache.org/viewvc?rev=1083367&view=rev
Log:
- fix for OODT-164 AcqusitionDate Versioner

Added:
    
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/versioning/AcquisitionDateVersioner.java
    
oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/versioning/TestAcquisitionDateVersioner.java
Modified:
    oodt/trunk/CHANGES.txt

Modified: oodt/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/oodt/trunk/CHANGES.txt?rev=1083367&r1=1083366&r2=1083367&view=diff
==============================================================================
--- oodt/trunk/CHANGES.txt (original)
+++ oodt/trunk/CHANGES.txt Sun Mar 20 04:48:28 2011
@@ -4,6 +4,8 @@ Apache OODT Change Log
 Release 0.3-SNAPSHOT (in progress)
 --------------------------------------------
 
+* OODT-164 AcqusitionDate Versioner (mattmann)
+
 * OODT-163 DirectoryProduct Versioner for the File Manager (mattmann)
 
 * OODT-160 Allow number of session protocol connections in 

Added: 
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/versioning/AcquisitionDateVersioner.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/versioning/AcquisitionDateVersioner.java?rev=1083367&view=auto
==============================================================================
--- 
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/versioning/AcquisitionDateVersioner.java
 (added)
+++ 
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/versioning/AcquisitionDateVersioner.java
 Sun Mar 20 04:48:28 2011
@@ -0,0 +1,105 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.filemgr.versioning;
+
+//JDK imports
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+//OODT imports
+import org.apache.oodt.cas.filemgr.structs.Product;
+import org.apache.oodt.cas.filemgr.structs.exceptions.VersioningException;
+import org.apache.oodt.cas.metadata.Metadata;
+import org.apache.oodt.cas.metadata.util.PathUtils;
+
+/**
+ * 
+ * Store products under /[AcquisitionDate]/[Filename].
+ * 
+ * Assumes the existence of:
+ * 
+ * <pre>
+ *   &lt;code&gt;AcquisitionDate&lt;/code&gt; - if present, takes as is, and 
assumes 
+ *   it is in the yyMMdd format.&lt;br/&gt;
+ *   &lt;code&gt;StartDateTime&lt;/code&gt; - if present, assumes it is in 
yyyy-MM-ddTHH:mm:ss.000Z, 
+ *   or ISO8601 format, reformats it to yyMMdd, and then uses it for the value
+ *   of &lt;code&gt;AcquisitionDate&lt;/code&gt;&lt;br/&gt;
+ * </pre>
+ * 
+ * If neither of the above met fields are present, the Versioner will generate 
a
+ * new DateTime, and use the value of it (formatted as yyMMdd) for the
+ * AcquisitionDate.
+ * 
+ * @author mattmann
+ * @version $Revision$
+ * 
+ */
+public class AcquisitionDateVersioner extends MetadataBasedFileVersioner {
+
+  private String filePathSpec = "/[AcquisitionDate]/[Filename]";
+
+  protected final static String ACQUISITION_DATE = "AcquisitionDate";
+
+  protected static String ACQ_DATE_FORMAT = "yyMMdd";
+
+  protected final static String START_DATE_TIME = "StartDateTime";
+
+  public AcquisitionDateVersioner() {
+    setFilePathSpec(filePathSpec);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.oodt.cas.filemgr.versioning.MetadataBasedFileVersioner#
+   * createDataStoreReferences(org.apache.oodt.cas.filemgr.structs.Product,
+   * org.apache.oodt.cas.metadata.Metadata)
+   */
+  @Override
+  public void createDataStoreReferences(Product product, Metadata metadata)
+      throws VersioningException {
+
+    // compute AcquisitionDate
+    // grab CAS.ProductReceivedTime
+    // parse it as an ISO8601 date
+    // then reformat and add using ACQ_DATE_FORMAT
+    if (metadata.getMetadata(ACQUISITION_DATE) == null) {
+      SimpleDateFormat acqDateFormatter = new 
SimpleDateFormat(ACQ_DATE_FORMAT);
+      acqDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
+      Date casProdReceivedTime = new Date();
+      if (metadata.getMetadata(START_DATE_TIME) != null) {
+        try {
+          metadata.replaceMetadata(ACQUISITION_DATE, PathUtils
+              .doDynamicReplacement("[FORMAT(yyyy-MM-dd'T'HH:mm:ss.SSS'Z',"
+                  + metadata.getMetadata(START_DATE_TIME) + ","
+                  + ACQ_DATE_FORMAT + ")]"));
+        } catch (Exception e) {
+          throw new VersioningException("Failed to parse StartDateTime : "
+              + e.getMessage(), e);
+        }
+      } else {
+        metadata.replaceMetadata(ACQUISITION_DATE, acqDateFormatter
+            .format(casProdReceivedTime));
+      }
+    }
+
+    super.createDataStoreReferences(product, metadata);
+  }
+
+}

Added: 
oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/versioning/TestAcquisitionDateVersioner.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/versioning/TestAcquisitionDateVersioner.java?rev=1083367&view=auto
==============================================================================
--- 
oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/versioning/TestAcquisitionDateVersioner.java
 (added)
+++ 
oodt/trunk/filemgr/src/test/org/apache/oodt/cas/filemgr/versioning/TestAcquisitionDateVersioner.java
 Sun Mar 20 04:48:28 2011
@@ -0,0 +1,121 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.filemgr.versioning;
+
+//JDK imports
+import java.io.File;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+//OODT imports
+import org.apache.oodt.cas.filemgr.metadata.CoreMetKeys;
+import org.apache.oodt.cas.filemgr.structs.Product;
+import org.apache.oodt.cas.filemgr.structs.Reference;
+import org.apache.oodt.cas.metadata.Metadata;
+
+//Junit imports
+import junit.framework.TestCase;
+
+/**
+ * 
+ * Test harness for the {@link AcquisitionDateVersioner}.
+ * 
+ * @author mattmann
+ * @version $Revision$
+ * 
+ */
+public class TestAcquisitionDateVersioner extends TestCase {
+
+  public TestAcquisitionDateVersioner() {
+    System.setProperty("org.apache.oodt.cas.filemgr.mime.type.repository",
+        new File("./src/main/resources/mime-types.xml").getAbsolutePath());
+  }
+
+  public void testVersionerWithNoStartDateTimeAndNoAcqDate() {
+    AcquisitionDateVersioner versioner = new AcquisitionDateVersioner();
+    Product p = Product.getDefaultFlatProduct("test", "urn:oodt:GenericFile");
+    p.getProductType().setProductRepositoryPath("file:///home/files");
+    Reference r = new Reference("file:///tmp/dir1/file1.txt", null, 4L);
+    p.getProductReferences().add(r);
+    Metadata met = new Metadata();
+    met.addMetadata(CoreMetKeys.FILENAME, "file1.txt");
+    String expectedDateTimeStr = new SimpleDateFormat(
+        AcquisitionDateVersioner.ACQ_DATE_FORMAT).format(new Date());
+    String expectedPath = "file:/home/files/" + expectedDateTimeStr
+        + "/file1.txt";
+    try {
+      versioner.createDataStoreReferences(p, met);
+    } catch (Exception e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+
+    assertNotNull(p.getProductReferences());
+    assertEquals(1, p.getProductReferences().size());
+    assertEquals(expectedPath, p.getProductReferences().get(0)
+        .getDataStoreReference());
+  }
+
+  public void testVersionerWithNoStartDateTimeAndAcqDate() {
+    AcquisitionDateVersioner versioner = new AcquisitionDateVersioner();
+    Product p = Product.getDefaultFlatProduct("test", "urn:oodt:GenericFile");
+    p.getProductType().setProductRepositoryPath("file:///home/files");
+    Reference r = new Reference("file:///tmp/dir1/file1.txt", null, 4L);
+    p.getProductReferences().add(r);
+    Metadata met = new Metadata();
+    met.addMetadata(CoreMetKeys.FILENAME, "file1.txt");
+    met.addMetadata("AcquisitionDate", "090910");
+    String expectedPath = "file:/home/files/"
+        + met.getMetadata("AcquisitionDate") + "/file1.txt";
+    try {
+      versioner.createDataStoreReferences(p, met);
+    } catch (Exception e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+
+    assertNotNull(p.getProductReferences());
+    assertEquals(1, p.getProductReferences().size());
+    assertEquals(expectedPath, p.getProductReferences().get(0)
+        .getDataStoreReference());
+  }
+
+  public void testVersionerWithStartDateTime() {
+    AcquisitionDateVersioner versioner = new AcquisitionDateVersioner();
+    Product p = Product.getDefaultFlatProduct("test", "urn:oodt:GenericFile");
+    p.getProductType().setProductRepositoryPath("file:///home/files");
+    Reference r = new Reference("file:///tmp/dir1/file1.txt", null, 4L);
+    p.getProductReferences().add(r);
+    Metadata met = new Metadata();
+    met.addMetadata(CoreMetKeys.FILENAME, "file1.txt");
+    met.addMetadata("StartDateTime", "2006-09-10T00:00:01.000Z");
+    String expectedPath = "file:/home/files/060910/file1.txt";
+    try {
+      versioner.createDataStoreReferences(p, met);
+    } catch (Exception e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+
+    assertNotNull(p.getProductReferences());
+    assertEquals(1, p.getProductReferences().size());
+    assertEquals(expectedPath, p.getProductReferences().get(0)
+        .getDataStoreReference());
+  }
+
+}


Reply via email to