From 2df20ffbb7083e259345028f4f46e604f4d27eb0 Mon Sep 17 00:00:00 2001
From: ribeaudc <ribeaudc>
Date: Mon, 10 Dec 2007 15:12:20 +0000
Subject: [PATCH] [LMS-107] - Separation of concerns.

SVN: 3008
---
 .../java/ch/systemsx/cisd/bds/Utilities.java  |  1 +
 .../cisd/bds/hcs/HCSImageFormattedData.java   | 64 ++++++++++---------
 .../cisd/bds/hcs/IHCSImageFormattedData.java  | 10 +--
 .../systemsx/cisd/bds/storage/IDirectory.java | 11 +---
 .../bds/storage/filesystem/Directory.java     | 22 +++----
 .../bds/hcs/HCSDataStructureV1_0Test.java     |  9 +--
 .../bds/hcs/HCSImageFormattedDataTest.java    | 34 +++++-----
 .../bds/storage/filesystem/DirectoryTest.java |  2 +-
 8 files changed, 74 insertions(+), 79 deletions(-)

diff --git a/bds/source/java/ch/systemsx/cisd/bds/Utilities.java b/bds/source/java/ch/systemsx/cisd/bds/Utilities.java
index 3adf9dea87a..beda0d1501b 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/Utilities.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/Utilities.java
@@ -61,6 +61,7 @@ public class Utilities
      * @throws DataStructureException if requested directory not found.
      */
     public final static IDirectory getSubDirectory(final IDirectory directory, final String name)
+            throws DataStructureException
     {
         INode node = directory.tryGetNode(name);
         if (node == null)
diff --git a/bds/source/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedData.java b/bds/source/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedData.java
index 0ee084daf16..ea865fc9c27 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedData.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedData.java
@@ -16,6 +16,7 @@
 
 package ch.systemsx.cisd.bds.hcs;
 
+import java.io.File;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -107,12 +108,12 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement
         return ROW + wellLocation.y + "_" + COLUMN + wellLocation.x + ".tiff";
     }
 
-    private final IDirectory getStandardDataDirectory()
+    private final IDirectory getStandardDataDirectory() throws DataStructureException
     {
         return Utilities.getSubDirectory(dataDirectory, DataStructureV1_0.DIR_STANDARD);
     }
 
-    private final IDirectory getOriginalDataDirectory()
+    private final IDirectory getOriginalDataDirectory() throws DataStructureException
     {
         return Utilities.getSubDirectory(dataDirectory, DataStructureV1_0.DIR_ORIGINAL);
     }
@@ -132,62 +133,64 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement
         return Channel.CHANNEL + channel;
     }
 
-    //
-    // IHCSFormattedData
-    //
-
-    public final INode tryGetStandardNodeAt(final int channel, final Location plateLocation, final Location wellLocation)
+    private void checkCoordinates(final int channel, final Location plateLocation, final Location wellLocation)
     {
         checkChannel(channel);
         assert plateLocation != null : "Plate location can not be null.";
         assert wellLocation != null : "Well location can not be null.";
         checkLocation(getPlateGeometry(), plateLocation);
         checkLocation(getWellGeometry(), wellLocation);
-        final IDirectory standardDir = getStandardDataDirectory();
-        final IDirectory channelDir = Utilities.getSubDirectory(standardDir, getChannelName(channel));
-        final IDirectory plateRowDir = Utilities.getSubDirectory(channelDir, getPlateRowDirName(plateLocation));
-        final IDirectory plateColumnDir = Utilities.getSubDirectory(plateRowDir, getPlateColumnDir(plateLocation));
-        return plateColumnDir.tryGetNode(createWellFileName(wellLocation));
     }
 
-    public final NodePath addStandardNode(final String originalFilePath, int channel, final Location plateLocation,
-            final Location wellLocation)
+    //
+    // IHCSFormattedData
+    //
+
+    public final INode tryGetStandardNodeAt(final int channel, final Location plateLocation, final Location wellLocation)
     {
-        // This will check all parameters but originalFilePath.
-        INode node = null;
-        // TODO 2007-12-05, Christian Ribeaud: Improve this.
+        checkCoordinates(channel, plateLocation, wellLocation);
         try
         {
-            node = tryGetStandardNodeAt(channel, plateLocation, wellLocation);
-        } catch (DataStructureException ex)
+            final IDirectory standardDir = getStandardDataDirectory();
+            final IDirectory channelDir = Utilities.getSubDirectory(standardDir, getChannelName(channel));
+            final IDirectory plateRowDir = Utilities.getSubDirectory(channelDir, getPlateRowDirName(plateLocation));
+            final IDirectory plateColumnDir = Utilities.getSubDirectory(plateRowDir, getPlateColumnDir(plateLocation));
+            return plateColumnDir.tryGetNode(createWellFileName(wellLocation));
+        } catch (final DataStructureException e)
         {
-            // Nothing to do here.
+            return null;
         }
+    }
+
+    public final NodePath addStandardNode(final File imageFile, int channel, final Location plateLocation,
+            final Location wellLocation) throws DataStructureException
+    {
+        INode node = tryGetStandardNodeAt(channel, plateLocation, wellLocation);
         if (node != null)
         {
             throw new DataStructureException(String.format(
                     "A node already exists at channel %d, plate location '%s' and well location '%s'.", channel,
                     plateLocation, wellLocation));
         }
-        assert originalFilePath != null : "Given original file name can not be null.";
+        assert imageFile != null : "Given original file name can not be null.";
         final IDirectory standardDir = getStandardDataDirectory();
         final IDirectory channelDir = Utilities.getOrCreateSubDirectory(standardDir, getChannelName(channel));
         final IDirectory plateRowDir = Utilities.getOrCreateSubDirectory(channelDir, getPlateRowDirName(plateLocation));
         final IDirectory plateColumnDir =
                 Utilities.getOrCreateSubDirectory(plateRowDir, getPlateColumnDir(plateLocation));
         final String wellFileName = createWellFileName(wellLocation);
-        final INode originalNode = getOriginalDataDirectory().tryGetNode(originalFilePath);
-        if (originalNode == null)
-        {
-            throw new DataStructureException(String.format("No original node with name '%s' could be found.",
-                    originalFilePath));
-        }
         if (containsOriginalData())
         {
+            final INode originalNode = getOriginalDataDirectory().addFile(imageFile, null, true);
+            if (originalNode == null)
+            {
+                throw new DataStructureException(String.format("No original node with name '%s' could be found.",
+                        imageFile));
+            }
             node = plateColumnDir.tryAddLink(wellFileName, originalNode);
         } else
         {
-            node = plateColumnDir.tryAddNode(wellFileName, originalNode);
+            node = plateColumnDir.addFile(imageFile, wellFileName, true);
         }
         if (node == null)
         {
@@ -195,11 +198,12 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement
                     String
                             .format(
                                     "Original file name '%s' could not be added at channel %d, plate location '%s' and well location '%s'.",
-                                    originalFilePath, channel, plateLocation, wellLocation));
+                                    imageFile, channel, plateLocation, wellLocation));
         }
         final char sep = Constants.PATH_SEPARATOR;
         final String standardNodePath =
-                channelDir.getName() + sep + plateRowDir.getName() + sep + plateColumnDir.getName() + sep + wellFileName;
+                channelDir.getName() + sep + plateRowDir.getName() + sep + plateColumnDir.getName() + sep
+                        + wellFileName;
         return new NodePath(node, standardNodePath);
     }
 
diff --git a/bds/source/java/ch/systemsx/cisd/bds/hcs/IHCSImageFormattedData.java b/bds/source/java/ch/systemsx/cisd/bds/hcs/IHCSImageFormattedData.java
index afe3b771290..bd3eaf39cec 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/hcs/IHCSImageFormattedData.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/hcs/IHCSImageFormattedData.java
@@ -16,6 +16,8 @@
 
 package ch.systemsx.cisd.bds.hcs;
 
+import java.io.File;
+
 import ch.systemsx.cisd.bds.IFormattedData;
 import ch.systemsx.cisd.bds.exception.DataStructureException;
 import ch.systemsx.cisd.bds.storage.IFile;
@@ -40,14 +42,14 @@ public interface IHCSImageFormattedData extends IFormattedData
     public INode tryGetStandardNodeAt(final int channel, final Location plateLocation, final Location wellLocation);
 
     /**
-     * Adds a new node at given coordinates.
+     * Adds a new image file at given coordinates.
      * 
      * @return the new <code>INode</code> just added (encapsulated in returned <code>NodePath</code>) with its path
      *         in the <code>standard</code> directory. Never returns <code>null</code>.
-     * @param originalFilePath name of the <code>original</code> directory file path that is going to be added in the
-     *            <code>standard</code> directory.
+     * @param imageFile name of the image file that is going to be added in the <code>standard</code> directory.
+     * @throws DataStructureException if a node already exists at given coordinates.
      */
-    public NodePath addStandardNode(final String originalFilePath, final int channel, final Location plateLocation,
+    public NodePath addStandardNode(final File imageFile, final int channel, final Location plateLocation,
             final Location wellLocation) throws DataStructureException;
 
     //
diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/IDirectory.java b/bds/source/java/ch/systemsx/cisd/bds/storage/IDirectory.java
index 4965eb31033..fbe2ab1bd94 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/storage/IDirectory.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/storage/IDirectory.java
@@ -44,6 +44,7 @@ public interface IDirectory extends INode, Iterable<INode>
      * Adds the specified real file to this directory. The content of <code>file</code> will be copied. If it is a
      * folder also its complete content including all subfolders will be copied.
      * 
+     * @param nameOrNull the name of the returned node. If <code>null</code>, then given <var>file</var> name is taken.
      * @param move whether given <var>file</var> should be copied or moved.
      * @return the new node. It will be a {@link ILink} if <code>file</code> is a symbolic link, a {@link IDirectory}
      *         if <code>file</code> is a folder, or {@link IFile} if <code>file</code> is a plain file.
@@ -51,7 +52,7 @@ public interface IDirectory extends INode, Iterable<INode>
     // TODO 2007-12-03 Tomasz Pylak review: this generic interface should not use java.io.File. Is the 'move' parameter
     // possible to implement in HDF5? Maybe those operations should be done before, depending on the implementation
     // which is used?
-    public INode addFile(final File file, final boolean move);
+    public INode addFile(final File file, final String nameOrNull, final boolean move);
 
     /**
      * Removes given <var>node</var> from this directory.
@@ -69,12 +70,4 @@ public interface IDirectory extends INode, Iterable<INode>
      * @return <code>null</code> if the operation did not succeed.
      */
     public ILink tryAddLink(final String name, final INode node);
-
-    /**
-     * Adds given <code>node</code> to this <code>IDirectory</code> and returns a new node with given
-     * <code>name</code> (rename operation).
-     * 
-     * @return <code>null</code> if the operation did not succeed.
-     */
-    public INode tryAddNode(final String name, final INode node);
 }
diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java
index 16192f56aa1..39ab104fcb7 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java
@@ -146,13 +146,21 @@ final class Directory extends AbstractNode implements IDirectory
         return new File(file);
     }
 
-    public final INode addFile(final java.io.File file, final boolean move)
+    public final INode addFile(final java.io.File file, final String name, final boolean move)
     {
         checkFile(file);
-        final java.io.File newFile = new java.io.File(nodeFile, file.getName());
+        final String fileName;
+        if (name == null)
+        {
+            fileName = file.getName();
+        } else
+        {
+            fileName = name;
+        }
+        final java.io.File newFile = new java.io.File(nodeFile, fileName);
         if (move)
         {
-            moveFileToDirectory(file, nodeFile, null);
+            moveFileToDirectory(file, nodeFile, name);
         } else
         {
             try
@@ -231,14 +239,6 @@ final class Directory extends AbstractNode implements IDirectory
         }
     }
 
-    public final INode tryAddNode(final String name, final INode node)
-    {
-        assert node != null : "Node can not be null";
-        assert name != null : "Name can not be null.";
-        final java.io.File file = getNodeFile(node);
-        return NodeFactory.createNode(moveFileToDirectory(file, nodeFile, name));
-    }
-
     public final void removeNode(final INode node)
     {
         assert node != null : "Node could not be null";
diff --git a/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/HCSDataStructureV1_0Test.java b/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/HCSDataStructureV1_0Test.java
index 8241204d938..ef06cdd479b 100644
--- a/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/HCSDataStructureV1_0Test.java
+++ b/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/HCSDataStructureV1_0Test.java
@@ -50,11 +50,6 @@ public final class HCSDataStructureV1_0Test extends AbstractFileSystemTestCase
 
     private DataStructureV1_0 dataStructure;
 
-    public HCSDataStructureV1_0Test()
-    {
-        super(false);
-    }
-
     private final static ChannelList createChannelList()
     {
         final List<Channel> list = new ArrayList<Channel>();
@@ -79,9 +74,9 @@ public final class HCSDataStructureV1_0Test extends AbstractFileSystemTestCase
 
     @Override
     @BeforeMethod
-    public final void setup() throws IOException
+    public final void setUp() throws IOException
     {
-        super.setup();
+        super.setUp();
         storage = new FileStorage(workingDirectory);
         dataStructure = new DataStructureV1_0(storage);
     }
diff --git a/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedDataTest.java b/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedDataTest.java
index 6ecb02788eb..9edccc78314 100644
--- a/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedDataTest.java
+++ b/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/HCSImageFormattedDataTest.java
@@ -116,9 +116,9 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase
 
     @Override
     @BeforeMethod
-    public final void setup() throws IOException
+    public final void setUp() throws IOException
     {
-        super.setup();
+        super.setUp();
         context = new Mockery();
         prepareAndCreateFormattedData();
         prepareStandardNode();
@@ -170,7 +170,8 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase
     public final void testAddStandardNodeWithOriginalData() throws IOException
     {
         final String originalFilePath = "original.txt";
-        FileUtils.writeStringToFile(new File(workingDirectory, originalFilePath), "This is my original file...");
+        final File originalFile = new File(workingDirectory, originalFilePath);
+        FileUtils.writeStringToFile(originalFile, "This is my original file...");
         final int channelIndex = 1;
         final IDirectory originalNode = NodeFactory.createDirectoryNode(workingDirectory);
         context.checking(new Expectations()
@@ -185,21 +186,22 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase
                     will(returnValue(originalNode));
 
                     one(formatParameters).getValue(HCSImageFormat1_0.CONTAINS_ORIGINAL_DATA);
-                    will(returnValue(Boolean.FALSE));
+                    will(returnValue(Boolean.TRUE));
                 }
             });
         final NodePath nodePath =
-                formattedData.addStandardNode(originalFilePath, channelIndex, PLATE_LOCATION, WELL_LOCATION);
+                formattedData.addStandardNode(originalFile, channelIndex, PLATE_LOCATION, WELL_LOCATION);
         final String standardFileName = "row2_column1.tiff";
         assertNotNull(nodePath);
+        assertTrue(nodePath.node instanceof ILink);
         assertEquals(standardFileName, nodePath.node.getName());
         // Look at the standard leaf directory if the node is there as well.
         final INode standardFile = leafDirectory.tryGetNode(standardFileName);
         assertNotNull(standardFile);
         assertTrue(standardFile instanceof IFile);
         assertEquals(standardFileName, standardFile.getName());
-        // Node should no longer be in original directory
-        assertNull(originalNode.tryGetNode(originalFilePath));
+        // Node should still be in the 'original' directory.
+        assertNotNull(originalNode.tryGetNode(originalFilePath));
         context.assertIsSatisfied();
     }
 
@@ -207,7 +209,8 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase
     public final void testAddStandardNodeWithoutOriginalData() throws IOException
     {
         final String originalFilePath = "original.tiff";
-        FileUtils.writeStringToFile(new File(workingDirectory, originalFilePath), "This is my original file...");
+        final File originalFile = new File(workingDirectory, originalFilePath);
+        FileUtils.writeStringToFile(originalFile, "This is my original file...");
         final int channelIndex = 1;
         final IDirectory originalNode = NodeFactory.createDirectoryNode(workingDirectory);
         context.checking(new Expectations()
@@ -218,26 +221,23 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase
                     exactly(2).of(directory).tryGetNode(DataStructureV1_0.DIR_STANDARD);
                     will(returnValue(standardNode));
 
-                    one(directory).tryGetNode(DataStructureV1_0.DIR_ORIGINAL);
-                    will(returnValue(originalNode));
-
                     one(formatParameters).getValue(HCSImageFormat1_0.CONTAINS_ORIGINAL_DATA);
-                    will(returnValue(Boolean.TRUE));
+                    will(returnValue(Boolean.FALSE));
                 }
             });
         final NodePath nodePath =
-                formattedData.addStandardNode(originalFilePath, channelIndex, PLATE_LOCATION, WELL_LOCATION);
+                formattedData.addStandardNode(originalFile, channelIndex, PLATE_LOCATION, WELL_LOCATION);
         final String standardFileName = "row2_column1.tiff";
         assertNotNull(nodePath);
-        assertTrue(nodePath.node instanceof ILink);
+        assertTrue(nodePath.node instanceof IFile);
         assertEquals(standardFileName, nodePath.node.getName());
         // Look at the standard leaf directory if the node is there as well.
         final INode standardFile = leafDirectory.tryGetNode(standardFileName);
         assertNotNull(standardFile);
         assertTrue(standardFile instanceof IFile);
         assertEquals(standardFileName, standardFile.getName());
-        // Node should still be in original directory
-        assertNotNull(originalNode.tryGetNode(originalFilePath));
+        // Node should no longer be in the 'original' directory.
+        assertNull(originalNode.tryGetNode(originalFilePath));
         context.assertIsSatisfied();
 
     }
@@ -265,7 +265,7 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase
     {
         final File file = new File(workingDirectory, "row2_column1.tiff");
         FileUtils.writeStringToFile(file, "This is my original file...");
-        leafDirectory.addFile(file, true);
+        leafDirectory.addFile(file, null, true);
         final int channelIndex = 1;
         context.checking(new Expectations()
             {
diff --git a/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/DirectoryTest.java b/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/DirectoryTest.java
index e59ecc893e6..d3d85b5c910 100644
--- a/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/DirectoryTest.java
+++ b/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/DirectoryTest.java
@@ -53,7 +53,7 @@ public final class DirectoryTest extends AbstractFileSystemTestCase
         File dest = new File(workingDirectory, "destination");
         dest.mkdir();
         Directory directory = new Directory(dest);
-        INode copiedDir = directory.addFile(dir, move);
+        INode copiedDir = directory.addFile(dir, null, move);
         assertEquals("dir", copiedDir.getName());
         assertTrue(copiedDir instanceof IDirectory);
         File copiedRealDir = new File(dest, "dir");
-- 
GitLab