Skip to content
Snippets Groups Projects
Commit 2df20ffb authored by ribeaudc's avatar ribeaudc
Browse files

[LMS-107]

- Separation of concerns.

SVN: 3008
parent 6579e361
No related branches found
No related tags found
No related merge requests found
...@@ -61,6 +61,7 @@ public class Utilities ...@@ -61,6 +61,7 @@ public class Utilities
* @throws DataStructureException if requested directory not found. * @throws DataStructureException if requested directory not found.
*/ */
public final static IDirectory getSubDirectory(final IDirectory directory, final String name) public final static IDirectory getSubDirectory(final IDirectory directory, final String name)
throws DataStructureException
{ {
INode node = directory.tryGetNode(name); INode node = directory.tryGetNode(name);
if (node == null) if (node == null)
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package ch.systemsx.cisd.bds.hcs; package ch.systemsx.cisd.bds.hcs;
import java.io.File;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
...@@ -107,12 +108,12 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement ...@@ -107,12 +108,12 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement
return ROW + wellLocation.y + "_" + COLUMN + wellLocation.x + ".tiff"; 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); 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); return Utilities.getSubDirectory(dataDirectory, DataStructureV1_0.DIR_ORIGINAL);
} }
...@@ -132,62 +133,64 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement ...@@ -132,62 +133,64 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement
return Channel.CHANNEL + channel; return Channel.CHANNEL + channel;
} }
// private void checkCoordinates(final int channel, final Location plateLocation, final Location wellLocation)
// IHCSFormattedData
//
public final INode tryGetStandardNodeAt(final int channel, final Location plateLocation, final Location wellLocation)
{ {
checkChannel(channel); checkChannel(channel);
assert plateLocation != null : "Plate location can not be null."; assert plateLocation != null : "Plate location can not be null.";
assert wellLocation != null : "Well location can not be null."; assert wellLocation != null : "Well location can not be null.";
checkLocation(getPlateGeometry(), plateLocation); checkLocation(getPlateGeometry(), plateLocation);
checkLocation(getWellGeometry(), wellLocation); 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. checkCoordinates(channel, plateLocation, wellLocation);
INode node = null;
// TODO 2007-12-05, Christian Ribeaud: Improve this.
try try
{ {
node = tryGetStandardNodeAt(channel, plateLocation, wellLocation); final IDirectory standardDir = getStandardDataDirectory();
} catch (DataStructureException ex) 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) if (node != null)
{ {
throw new DataStructureException(String.format( throw new DataStructureException(String.format(
"A node already exists at channel %d, plate location '%s' and well location '%s'.", channel, "A node already exists at channel %d, plate location '%s' and well location '%s'.", channel,
plateLocation, wellLocation)); 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 standardDir = getStandardDataDirectory();
final IDirectory channelDir = Utilities.getOrCreateSubDirectory(standardDir, getChannelName(channel)); final IDirectory channelDir = Utilities.getOrCreateSubDirectory(standardDir, getChannelName(channel));
final IDirectory plateRowDir = Utilities.getOrCreateSubDirectory(channelDir, getPlateRowDirName(plateLocation)); final IDirectory plateRowDir = Utilities.getOrCreateSubDirectory(channelDir, getPlateRowDirName(plateLocation));
final IDirectory plateColumnDir = final IDirectory plateColumnDir =
Utilities.getOrCreateSubDirectory(plateRowDir, getPlateColumnDir(plateLocation)); Utilities.getOrCreateSubDirectory(plateRowDir, getPlateColumnDir(plateLocation));
final String wellFileName = createWellFileName(wellLocation); 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()) 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); node = plateColumnDir.tryAddLink(wellFileName, originalNode);
} else } else
{ {
node = plateColumnDir.tryAddNode(wellFileName, originalNode); node = plateColumnDir.addFile(imageFile, wellFileName, true);
} }
if (node == null) if (node == null)
{ {
...@@ -195,11 +198,12 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement ...@@ -195,11 +198,12 @@ public final class HCSImageFormattedData extends AbstractFormattedData implement
String String
.format( .format(
"Original file name '%s' could not be added at channel %d, plate location '%s' and well location '%s'.", "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 char sep = Constants.PATH_SEPARATOR;
final String standardNodePath = 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); return new NodePath(node, standardNodePath);
} }
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package ch.systemsx.cisd.bds.hcs; package ch.systemsx.cisd.bds.hcs;
import java.io.File;
import ch.systemsx.cisd.bds.IFormattedData; import ch.systemsx.cisd.bds.IFormattedData;
import ch.systemsx.cisd.bds.exception.DataStructureException; import ch.systemsx.cisd.bds.exception.DataStructureException;
import ch.systemsx.cisd.bds.storage.IFile; import ch.systemsx.cisd.bds.storage.IFile;
...@@ -40,14 +42,14 @@ public interface IHCSImageFormattedData extends IFormattedData ...@@ -40,14 +42,14 @@ public interface IHCSImageFormattedData extends IFormattedData
public INode tryGetStandardNodeAt(final int channel, final Location plateLocation, final Location wellLocation); 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 * @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>. * 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 * @param imageFile name of the image file that is going to be added in the <code>standard</code> directory.
* <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; final Location wellLocation) throws DataStructureException;
// //
......
...@@ -44,6 +44,7 @@ public interface IDirectory extends INode, Iterable<INode> ...@@ -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 * 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. * 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. * @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} * @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. * 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> ...@@ -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 // 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 // possible to implement in HDF5? Maybe those operations should be done before, depending on the implementation
// which is used? // 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. * Removes given <var>node</var> from this directory.
...@@ -69,12 +70,4 @@ public interface IDirectory extends INode, Iterable<INode> ...@@ -69,12 +70,4 @@ public interface IDirectory extends INode, Iterable<INode>
* @return <code>null</code> if the operation did not succeed. * @return <code>null</code> if the operation did not succeed.
*/ */
public ILink tryAddLink(final String name, final INode node); 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);
} }
...@@ -146,13 +146,21 @@ final class Directory extends AbstractNode implements IDirectory ...@@ -146,13 +146,21 @@ final class Directory extends AbstractNode implements IDirectory
return new File(file); 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); 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) if (move)
{ {
moveFileToDirectory(file, nodeFile, null); moveFileToDirectory(file, nodeFile, name);
} else } else
{ {
try try
...@@ -231,14 +239,6 @@ final class Directory extends AbstractNode implements IDirectory ...@@ -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) public final void removeNode(final INode node)
{ {
assert node != null : "Node could not be null"; assert node != null : "Node could not be null";
......
...@@ -50,11 +50,6 @@ public final class HCSDataStructureV1_0Test extends AbstractFileSystemTestCase ...@@ -50,11 +50,6 @@ public final class HCSDataStructureV1_0Test extends AbstractFileSystemTestCase
private DataStructureV1_0 dataStructure; private DataStructureV1_0 dataStructure;
public HCSDataStructureV1_0Test()
{
super(false);
}
private final static ChannelList createChannelList() private final static ChannelList createChannelList()
{ {
final List<Channel> list = new ArrayList<Channel>(); final List<Channel> list = new ArrayList<Channel>();
...@@ -79,9 +74,9 @@ public final class HCSDataStructureV1_0Test extends AbstractFileSystemTestCase ...@@ -79,9 +74,9 @@ public final class HCSDataStructureV1_0Test extends AbstractFileSystemTestCase
@Override @Override
@BeforeMethod @BeforeMethod
public final void setup() throws IOException public final void setUp() throws IOException
{ {
super.setup(); super.setUp();
storage = new FileStorage(workingDirectory); storage = new FileStorage(workingDirectory);
dataStructure = new DataStructureV1_0(storage); dataStructure = new DataStructureV1_0(storage);
} }
......
...@@ -116,9 +116,9 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase ...@@ -116,9 +116,9 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase
@Override @Override
@BeforeMethod @BeforeMethod
public final void setup() throws IOException public final void setUp() throws IOException
{ {
super.setup(); super.setUp();
context = new Mockery(); context = new Mockery();
prepareAndCreateFormattedData(); prepareAndCreateFormattedData();
prepareStandardNode(); prepareStandardNode();
...@@ -170,7 +170,8 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase ...@@ -170,7 +170,8 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase
public final void testAddStandardNodeWithOriginalData() throws IOException public final void testAddStandardNodeWithOriginalData() throws IOException
{ {
final String originalFilePath = "original.txt"; 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 int channelIndex = 1;
final IDirectory originalNode = NodeFactory.createDirectoryNode(workingDirectory); final IDirectory originalNode = NodeFactory.createDirectoryNode(workingDirectory);
context.checking(new Expectations() context.checking(new Expectations()
...@@ -185,21 +186,22 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase ...@@ -185,21 +186,22 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase
will(returnValue(originalNode)); will(returnValue(originalNode));
one(formatParameters).getValue(HCSImageFormat1_0.CONTAINS_ORIGINAL_DATA); one(formatParameters).getValue(HCSImageFormat1_0.CONTAINS_ORIGINAL_DATA);
will(returnValue(Boolean.FALSE)); will(returnValue(Boolean.TRUE));
} }
}); });
final NodePath nodePath = 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"; final String standardFileName = "row2_column1.tiff";
assertNotNull(nodePath); assertNotNull(nodePath);
assertTrue(nodePath.node instanceof ILink);
assertEquals(standardFileName, nodePath.node.getName()); assertEquals(standardFileName, nodePath.node.getName());
// Look at the standard leaf directory if the node is there as well. // Look at the standard leaf directory if the node is there as well.
final INode standardFile = leafDirectory.tryGetNode(standardFileName); final INode standardFile = leafDirectory.tryGetNode(standardFileName);
assertNotNull(standardFile); assertNotNull(standardFile);
assertTrue(standardFile instanceof IFile); assertTrue(standardFile instanceof IFile);
assertEquals(standardFileName, standardFile.getName()); assertEquals(standardFileName, standardFile.getName());
// Node should no longer be in original directory // Node should still be in the 'original' directory.
assertNull(originalNode.tryGetNode(originalFilePath)); assertNotNull(originalNode.tryGetNode(originalFilePath));
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
...@@ -207,7 +209,8 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase ...@@ -207,7 +209,8 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase
public final void testAddStandardNodeWithoutOriginalData() throws IOException public final void testAddStandardNodeWithoutOriginalData() throws IOException
{ {
final String originalFilePath = "original.tiff"; 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 int channelIndex = 1;
final IDirectory originalNode = NodeFactory.createDirectoryNode(workingDirectory); final IDirectory originalNode = NodeFactory.createDirectoryNode(workingDirectory);
context.checking(new Expectations() context.checking(new Expectations()
...@@ -218,26 +221,23 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase ...@@ -218,26 +221,23 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase
exactly(2).of(directory).tryGetNode(DataStructureV1_0.DIR_STANDARD); exactly(2).of(directory).tryGetNode(DataStructureV1_0.DIR_STANDARD);
will(returnValue(standardNode)); will(returnValue(standardNode));
one(directory).tryGetNode(DataStructureV1_0.DIR_ORIGINAL);
will(returnValue(originalNode));
one(formatParameters).getValue(HCSImageFormat1_0.CONTAINS_ORIGINAL_DATA); one(formatParameters).getValue(HCSImageFormat1_0.CONTAINS_ORIGINAL_DATA);
will(returnValue(Boolean.TRUE)); will(returnValue(Boolean.FALSE));
} }
}); });
final NodePath nodePath = 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"; final String standardFileName = "row2_column1.tiff";
assertNotNull(nodePath); assertNotNull(nodePath);
assertTrue(nodePath.node instanceof ILink); assertTrue(nodePath.node instanceof IFile);
assertEquals(standardFileName, nodePath.node.getName()); assertEquals(standardFileName, nodePath.node.getName());
// Look at the standard leaf directory if the node is there as well. // Look at the standard leaf directory if the node is there as well.
final INode standardFile = leafDirectory.tryGetNode(standardFileName); final INode standardFile = leafDirectory.tryGetNode(standardFileName);
assertNotNull(standardFile); assertNotNull(standardFile);
assertTrue(standardFile instanceof IFile); assertTrue(standardFile instanceof IFile);
assertEquals(standardFileName, standardFile.getName()); assertEquals(standardFileName, standardFile.getName());
// Node should still be in original directory // Node should no longer be in the 'original' directory.
assertNotNull(originalNode.tryGetNode(originalFilePath)); assertNull(originalNode.tryGetNode(originalFilePath));
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
...@@ -265,7 +265,7 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase ...@@ -265,7 +265,7 @@ public class HCSImageFormattedDataTest extends AbstractFileSystemTestCase
{ {
final File file = new File(workingDirectory, "row2_column1.tiff"); final File file = new File(workingDirectory, "row2_column1.tiff");
FileUtils.writeStringToFile(file, "This is my original file..."); FileUtils.writeStringToFile(file, "This is my original file...");
leafDirectory.addFile(file, true); leafDirectory.addFile(file, null, true);
final int channelIndex = 1; final int channelIndex = 1;
context.checking(new Expectations() context.checking(new Expectations()
{ {
......
...@@ -53,7 +53,7 @@ public final class DirectoryTest extends AbstractFileSystemTestCase ...@@ -53,7 +53,7 @@ public final class DirectoryTest extends AbstractFileSystemTestCase
File dest = new File(workingDirectory, "destination"); File dest = new File(workingDirectory, "destination");
dest.mkdir(); dest.mkdir();
Directory directory = new Directory(dest); Directory directory = new Directory(dest);
INode copiedDir = directory.addFile(dir, move); INode copiedDir = directory.addFile(dir, null, move);
assertEquals("dir", copiedDir.getName()); assertEquals("dir", copiedDir.getName());
assertTrue(copiedDir instanceof IDirectory); assertTrue(copiedDir instanceof IDirectory);
File copiedRealDir = new File(dest, "dir"); File copiedRealDir = new File(dest, "dir");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment