package org.alfresco.filesys.repo.rules; import java.util.ArrayList; import java.util.Date; import org.alfresco.filesys.repo.ResultCallback; import org.alfresco.filesys.repo.rules.commands.CloseFileCommand; import org.alfresco.filesys.repo.rules.commands.CompoundCommand; import org.alfresco.filesys.repo.rules.commands.CopyContentCommand; import org.alfresco.filesys.repo.rules.commands.DeleteFileCommand; import org.alfresco.filesys.repo.rules.commands.RenameFileCommand; import org.alfresco.filesys.repo.rules.commands.RestoreFileCommand; import org.alfresco.filesys.repo.rules.operations.CloseFileOperation; import org.alfresco.filesys.repo.rules.operations.DeleteFileOperation; import org.alfresco.filesys.repo.rules.operations.MoveFileOperation; import org.alfresco.filesys.repo.rules.operations.RenameFileOperation; import org.alfresco.jlan.server.filesys.FileName; import org.alfresco.repo.transaction.AlfrescoTransactionSupport.TxnReadState; import org.alfresco.service.cmr.repository.NodeRef; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * This is an instance of a rename, delete, move scenario triggered by a rename of a * file matching a specified pattern. *
* a) Original file is renamed. Typically with an obscure name. * b) Renamed file is deleted via delete command or via deleteOnClose flag and close operation. * c) Temp file is moved into original file location. * *
* If this filter is active then this is what happens.
* a) Original file is renamed:
* - File is renamed.
* b) Renamed file is deleted via delete command or via deleteOnClose flag and close operation:
* - File is deleted.
* c) Temp file is moved into original file location - Scenario fires
* - Deleted file is restored.
* - Restored file is renamed to it's original name.
* - Content from file that must be moved is copied to restored file.
* - File that must be moved is deleted.
*/
public class ScenarioRenameDeleteMoveInstance implements ScenarioInstance
{
private static Log logger = LogFactory.getLog(ScenarioRenameDeleteMoveInstance.class);
enum InternalState
{
NONE, DELETE, MOVE
}
InternalState internalState = InternalState.NONE;
private Date startTime = new Date();
private String fileMiddle;
private String fileFrom;
private String fileEnd;
private Ranking ranking;
private boolean deleteBackup;
/**
* Timeout in ms. Default 30 seconds.
*/
private long timeout = 30000;
private boolean isComplete;
private String folderMiddle;
private String folderEnd;
private NodeRef originalNodeRef;
/**
* Evaluate the next operation
*
* @param operation Operation
*/
public Command evaluate(Operation operation)
{
/**
* Anti-pattern : timeout
*/
Date now = new Date();
if (now.getTime() > startTime.getTime() + getTimeout())
{
if (logger.isDebugEnabled())
{
logger.debug("Instance timed out");
}
isComplete = true;
return null;
}
switch (internalState)
{
case NONE:
if (operation instanceof RenameFileOperation)
{
RenameFileOperation r = (RenameFileOperation) operation;
fileMiddle = r.getFrom();
fileEnd = r.getTo();
String[] paths = FileName.splitPath(r.getFromPath());
folderMiddle = paths[0];
String[] paths2 = FileName.splitPath(r.getToPath());
folderEnd = paths2[0];
internalState = InternalState.DELETE;
}
else
{
// anything else bomb out
if (logger.isDebugEnabled())
{
logger.debug("State error, expected a RENAME");
}
isComplete = true;
}
case DELETE:
if (operation instanceof DeleteFileOperation)
{
internalState = InternalState.MOVE;
DeleteFileOperation d = (DeleteFileOperation) operation;
if (d.getName().equalsIgnoreCase(fileEnd))
{
ArrayList