From 80237736b467c927787dbad009d93d6bb2e5706b Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <andreas.romeyke@slub-dresden.de>
Date: Mon, 20 Jan 2014 17:14:08 +0000
Subject: [PATCH] - added exLogger support - getStreamRelativePath() rewritten
 using IEPID and Date (needs further tests)

---
 .../plugin/storage/nfs/SLUBStoragePlugin.java | 63 ++++++++++---------
 1 file changed, 33 insertions(+), 30 deletions(-)

diff --git a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java
index 0133410..5026c2b 100644
--- a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java
+++ b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java
@@ -4,30 +4,35 @@ package org.slub.rosetta.dps.repository.plugin.storage.nfs;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.InputStream;
+import java.util.Calendar;
+import java.text.SimpleDateFormat;
 
+import com.exlibris.core.infra.common.exceptions.logging.ExLogger;
+import com.exlibris.digitool.common.dnx.DnxDocument;
 import com.exlibris.dps.repository.plugin.storage.nfs.NFSStoragePlugin;
 import com.exlibris.core.infra.common.util.IOUtil;
 import com.exlibris.core.sdk.storage.containers.StoredEntityMetaData;
 import com.exlibris.digitool.common.storage.Fixity;
 import com.exlibris.digitool.infrastructure.utils.Checksummer;
- 
+
+/**
+ * SLUBStoragePlugin
+ * writes all IEs, files and so on into the same dir under yyyy/mm/dd/IEpid/
+ *
+ * @author andreas.romeyke@slub-dresden.de (Andreas Romeyke)
+ */
 public class SLUBStoragePlugin extends NFSStoragePlugin {
- 
     private static final String DIR_ROOT = "DIR_ROOT";
- 
+    private static final ExLogger log = ExLogger.getExLogger(NFSStoragePlugin.class);
     public SLUBStoragePlugin() {
         super();
     }
  
- 
     @Override
     public String storeEntity(InputStream is, StoredEntityMetaData storedEntityMetadata) throws Exception {
- 
         String fileName = createFileName(storedEntityMetadata);
- 
         String relativeDirectoryPath = getStreamRelativePath(storedEntityMetadata);
         File destFile = getStreamDirectory(relativeDirectoryPath, fileName);
- 
         // better move/link
         if (canHandleSourcePath(storedEntityMetadata.getCurrentFilePath())) {
             is.close(); // close input stream so that 'move' can work, we don't use it anyway
@@ -38,42 +43,40 @@ public class SLUBStoragePlugin extends NFSStoragePlugin {
             IOUtil.copy(is, new FileOutputStream(destFile));
         }
         String storedEntityIdentifier = relativeDirectoryPath + File.separator + fileName;
- 
         if(!checkFixity(storedEntityMetadata.getFixities(), storedEntityIdentifier)) {
             deleteEntity(storedEntityIdentifier); // delete corrupt files
             return null;
         }
- 
         // return only relative (not absolute) path
         return storedEntityIdentifier;
     }
  
- 
-    private String getStreamRelativePath(StoredEntityMetaData storedEntityMetaData ) throws Exception {
- 
+    // path should be of form yyyy/mm/dd/IE-PID/ 
+    protected String getStreamRelativePath(StoredEntityMetaData storedEntityMetaData ) throws Exception {
         String relativeDirectoryPath = File.separator;
-        String pid = storedEntityMetaData.getEntityPid();
-        Checksummer checksummer = new Checksummer(pid, true, false, false);
-        Fixity fixity = new Fixity(Fixity.FixityAlgorithm.MD5.toString(), checksummer.getMD5());
-        String value = fixity.getValue();
- 
-        for(int i=0 ; i<value.length() && i<32 ; i= i+2) {
-            if(i+1 < value.length()) {
-                relativeDirectoryPath += value.substring(i, i+2) + File.separator;
-            } else {
-                relativeDirectoryPath += value.substring(i, i+1) + File.separator;
-            }
-        }
- 
-        if(32 < value.length()) {
-            relativeDirectoryPath += value.substring(32);
-        }
- 
+        // get IE PID by calling IE-DNX record and search for ""internalIdentifierType" == "PID"
+        DnxDocument iedoc = storedEntityMetaData.getIeDnx();
+        String iepid = iedoc.getSectionKeyValue("internalIdentifierType", "PID");
+        log.debug("SLUBStoragePlugin iepid=" + iepid);
+        String datestring = iedoc.getSectionKeyValue("objectCharacteristics", "creationDate");
+        Calendar date = Calendar.getInstance();
+        // date: 2014-01-15 14:28:01
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
+        date.setTime(sdf.parse(datestring));
+        log.debug("SLUBStoragePlugin creation Date read=" + datestring + " parsed=" + date.toString());
+        relativeDirectoryPath = relativeDirectoryPath + new SimpleDateFormat("yyyy").format(date);
+        relativeDirectoryPath = relativeDirectoryPath + File.separator;
+        relativeDirectoryPath = relativeDirectoryPath + new SimpleDateFormat("MM").format(date);
+        relativeDirectoryPath = relativeDirectoryPath + File.separator;
+        relativeDirectoryPath = relativeDirectoryPath + new SimpleDateFormat("dd").format(date);
+        relativeDirectoryPath = relativeDirectoryPath + File.separator;
+        relativeDirectoryPath = relativeDirectoryPath + iepid;
+        relativeDirectoryPath = relativeDirectoryPath + File.separator;
+        log.debug("SLUBStoragePlugin relativeDirectoryPath=" + relativeDirectoryPath);
         return relativeDirectoryPath;
     }
  
     protected File getStreamDirectory(String path, String fileName) {
- 
         File newDir = new File(parameters.get(DIR_ROOT) + File.separator + path);
         newDir.mkdirs();
         return new File(newDir.getAbsolutePath() + File.separator + fileName);
-- 
GitLab