From 79455516e4ae837410036cd3733e17a1714c6cfe Mon Sep 17 00:00:00 2001
From: pkupczyk <pkupczyk>
Date: Thu, 26 Jul 2012 15:06:53 +0000
Subject: [PATCH] SP-156 / BIS-38 - Show checksum in file browser of data set
 detailed view

SVN: 26230
---
 .../server/DatasetDownloadServlet.java        |  5 ++-
 .../generic/server/DirectoryRendererUtil.java | 45 +++++++++++++++++++
 .../generic/server/IDirectoryRenderer.java    |  2 +-
 .../server/PlainTextRendererFactory.java      |  7 ++-
 .../server/SimpleHTMLDirectoryRenderer.java   | 22 +++++----
 5 files changed, 63 insertions(+), 18 deletions(-)
 create mode 100644 datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DirectoryRendererUtil.java

diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DatasetDownloadServlet.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DatasetDownloadServlet.java
index 8b4cfd4f90d..2fab9d8fd50 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DatasetDownloadServlet.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DatasetDownloadServlet.java
@@ -464,8 +464,11 @@ public class DatasetDownloadServlet extends AbstractDatasetDownloadServlet
                     directoryRenderer.printDirectory(name, normalizedRelativePath);
                 } else
                 {
+                    Integer checksumOrNull =
+                            childNode.isChecksumCRC32Precalculated() ? childNode.getChecksumCRC32()
+                                    : null;
                     directoryRenderer.printFile(name, normalizedRelativePath,
-                            childNode.getFileLength());
+                            childNode.getFileLength(), checksumOrNull);
                 }
             }
             directoryRenderer.printFooter();
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DirectoryRendererUtil.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DirectoryRendererUtil.java
new file mode 100644
index 00000000000..9491d59a0b7
--- /dev/null
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DirectoryRendererUtil.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2012 ETH Zuerich, CISD
+ *
+ * Licensed 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 ch.systemsx.cisd.openbis.dss.generic.server;
+
+import org.apache.commons.io.FileUtils;
+
+import ch.systemsx.cisd.common.io.IOUtilities;
+
+/**
+ * @author pkupczyk
+ */
+public class DirectoryRendererUtil
+{
+
+    public static String renderFileSize(long size)
+    {
+        return FileUtils.byteCountToDisplaySize(size);
+    }
+
+    public static String renderCRC32Checksum(Integer checksumOrNull)
+    {
+        if (checksumOrNull == null)
+        {
+            return "-";
+        } else
+        {
+            return IOUtilities.crc32ToString(checksumOrNull);
+        }
+    }
+
+}
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDirectoryRenderer.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDirectoryRenderer.java
index e3edcb1c939..2da5b0dd17d 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDirectoryRenderer.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDirectoryRenderer.java
@@ -29,7 +29,7 @@ public interface IDirectoryRenderer extends IWriterInjector
 
     public void printDirectory(String name, String relativePath);
 
-    public void printFile(String name, String relativePath, long size);
+    public void printFile(String name, String relativePath, long size, Integer checksumOrNull);
 
     public void printFooter();
 
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/PlainTextRendererFactory.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/PlainTextRendererFactory.java
index b3433189321..2fc00e153c6 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/PlainTextRendererFactory.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/PlainTextRendererFactory.java
@@ -18,8 +18,6 @@ package ch.systemsx.cisd.openbis.dss.generic.server;
 
 import java.io.PrintWriter;
 
-import org.apache.commons.io.FileUtils;
-
 /**
  * Factory for rendering file system view in plain text.
  * 
@@ -73,9 +71,10 @@ public class PlainTextRendererFactory implements IRendererFactory
         }
 
         @Override
-        public void printFile(String name, String relativePath, long size)
+        public void printFile(String name, String relativePath, long size, Integer checksumOrNull)
         {
-            writer.format("%s\t%s\n", name, FileUtils.byteCountToDisplaySize(size));
+            writer.format("%s\t%s\t%s\n", name, DirectoryRendererUtil.renderFileSize(size),
+                    DirectoryRendererUtil.renderCRC32Checksum(checksumOrNull));
         }
 
         @Override
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/SimpleHTMLDirectoryRenderer.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/SimpleHTMLDirectoryRenderer.java
index 42a9e437274..124e2fa531d 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/SimpleHTMLDirectoryRenderer.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/SimpleHTMLDirectoryRenderer.java
@@ -20,7 +20,6 @@ import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 
-import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringUtils;
 
 import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
@@ -50,7 +49,7 @@ final class SimpleHTMLDirectoryRenderer implements IDirectoryRenderer
 
     private static final Template ROW_TEMPLATE =
             new Template(
-                    "<tr><td class='td_file'><a href='${path}?mode=simpleHtml${sessionId}'>${name}</td><td>${size}</td></tr>");
+                    "<tr><td class='td_file'><a href='${path}?mode=simpleHtml${sessionId}'>${name}</td><td>${size}</td><td>${checksum}</td></tr>");
 
     private static final Template HEADER_TEMPLATE = new Template("<html><head>" + CSS
             + "</head><body>" + "<table> " + "${folder}" + "");
@@ -97,27 +96,31 @@ final class SimpleHTMLDirectoryRenderer implements IDirectoryRenderer
     @Override
     public void printLinkToParentDirectory(final String aRelativePath)
     {
-        printRow("..", aRelativePath, "");
+        printRow("..", aRelativePath, "", "");
     }
 
     @Override
     public void printDirectory(final String name, final String aRelativePath)
     {
-        printRow(name, aRelativePath, "");
+        printRow(name, aRelativePath, "", "");
     }
 
     @Override
-    public void printFile(final String name, final String aRelativePath, final long size)
+    public void printFile(final String name, final String aRelativePath, final long size,
+            final Integer checksumOrNull)
     {
-        printRow(name, aRelativePath, renderFileSize(size));
+        printRow(name, aRelativePath, DirectoryRendererUtil.renderFileSize(size),
+                DirectoryRendererUtil.renderCRC32Checksum(checksumOrNull));
     }
 
-    private void printRow(final String name, final String aRelativePath, final String fileSize)
+    private void printRow(final String name, final String aRelativePath, final String fileSize,
+            final String checksum)
     {
         final Template template = ROW_TEMPLATE.createFreshCopy();
         template.bind("path", urlPrefix + encodeURL(aRelativePath));
         template.bind("name", name);
         template.bind("size", fileSize);
+        template.bind("checksum", checksum);
         template.bind("sessionId", Utils.createUrlParameterForSessionId("&", sessionIdOrNull));
         writer.println(template.createText());
     }
@@ -133,11 +136,6 @@ final class SimpleHTMLDirectoryRenderer implements IDirectoryRenderer
         }
     }
 
-    private final static String renderFileSize(final long size)
-    {
-        return FileUtils.byteCountToDisplaySize(size);
-    }
-
     @Override
     public void printFooter()
     {
-- 
GitLab