* Copy and paste the directory in directoryTreeNode to the current directory.
*
* @param directoryTreeNode
*/
private void copyPasteDirectory(DirectoryTreeNode directoryTreeNode) {
DirectoryModel copiedDirectoryModel = (DirectoryModel) directoryTreeNode.getDirectory();
// The configuration are not fully loaded when the configuration is first created to save
// time.
// We need to load all of the configurations contained in the directory and its
// subdirectories to copy them too.
loadConfigRecursive(copiedDirectoryModel);
// To copy the object, we serialize it to a byte array, then we recover a copy from it.
// This way, we can reuse the existing serialization rather than implement the clone
// method on every entity.
IDirectory copiedDirectory = AutoCopier.toImpl(copiedDirectoryModel, IDirectory.class);
DirectoryModel copyDirectoryModel;
IDirectory copyDirectory;
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(copiedDirectory);
byte[] buffer = byteArrayOutputStream.toByteArray();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
copyDirectory = (IDirectory) objectInputStream.readObject();
}
catch (IOException e) {
// The streams work on the RAM, so an IOException should never happen.
e.printStackTrace();
copyDirectory = null;
}
catch (ClassNotFoundException e) {
// This should never happen as well.
e.printStackTrace();
copyDirectory = null;
}
// copiedDirectoryModel needs to be rewrapped.
/*DirectoryModel restoreCopiedDirectoryModel = DirectoryModelWrapperFactory.getInstance()
.wrap(copiedDirectory);
if (copiedDirectoryModel == currentDirectory) {
currentDirectory = restoreCopiedDirectoryModel;
}*/
copyDirectory = AutoCopier.copyDirectoryToModel(copyDirectory);
// We need to set the identifiers of the new directories and configurations to null.
nullId(copyDirectory);
moveDirectory(copyDirectory, currentDirectory);
copyDirectoryModel = (DirectoryModel) copyDirectory;// DirectoryModelWrapperFactory.getInstance().wrap(copyDirectory);
DirectoryModel savedDirectory = saveDirectoryRecursive(copyDirectoryModel);
currentDirectory.getSubDirectoriesList().remove(copyDirectory);
currentDirectory.getSubDirectoriesList().add(savedDirectory);
directoryTreeNode.setDirectory(copiedDirectory);
}