expiredContent = users.get(userName);
startWorkflow(userName, storeName, expiredContent, workflowTitle);
}
}
}
/**
* Processes the given node.
*
* This method is called if the node has been identified as being expired,
* the date and time is checked to make sure it has actually passed i.e. the
* date maybe todat but the time set to later in the day. If the item is
* indeed expired it's added to the expired list and the date reset.
*
*
* @param storeName The name of the store the folder belongs to
* @param node The node to examine
*/
private void processNode(String storeName, AVMNodeDescriptor node)
{
// check supplied node is a file
if (node.isFile())
{
// check for existence of expires aspect
String nodePath = node.getPath();
PropertyValue expirationDateProp = this.avmService.getNodeProperty(-1, nodePath,
WCMAppModel.PROP_EXPIRATIONDATE);
if (logger.isDebugEnabled())
logger.debug("Examining expiration date for '" + nodePath + "': " +
expirationDateProp);
if (expirationDateProp != null)
{
Date now = new Date();
Date expirationDate = (Date)expirationDateProp.getValue(DataTypeDefinition.DATETIME);
if (expirationDate != null && expirationDate.before(now))
{
// before doing anything else see whether the item is locked by any user,
// if it is then just log a warning messge and wait until the next time around
String[] splitPath = nodePath.split(":");
String lockOwner = this.avmLockingService.getLockOwner(storeName, splitPath[1]);
if (logger.isDebugEnabled())
logger.debug("lock details for '" + nodePath + "': " + lockOwner);
if (lockOwner == null)
{
// get the map of expired content for the store
Map> storeExpiredContent = this.expiredContent.get(storeName);
if (storeExpiredContent == null)
{
storeExpiredContent = new HashMap>(4);
this.expiredContent.put(storeName, storeExpiredContent);
}
// get the list of expired content for the last modifier of the node
String modifier = node.getLastModifier();
List userExpiredContent = storeExpiredContent.get(modifier);
if (userExpiredContent == null)
{
userExpiredContent = new ArrayList(4);
storeExpiredContent.put(modifier, userExpiredContent);
}
// add the content to the user's list for the current store
userExpiredContent.add(nodePath);
if (logger.isDebugEnabled())
logger.debug("Added " + nodePath + " to " + modifier + "'s list of expired content");
// reset the expiration date
this.avmService.setNodeProperty(nodePath, WCMAppModel.PROP_EXPIRATIONDATE,
new PropertyValue(DataTypeDefinition.DATETIME, null));
if (logger.isDebugEnabled())
logger.debug("Reset expiration date for: " + nodePath);
}
else
{
if (logger.isWarnEnabled())
{
logger.warn("ignoring '" + nodePath + "', although it has expired, it's currently locked");
}
}
}
}
}
}
/**
* Starts a workflow for the given user prompting them to review the list of given
* expired content in the given store.
*
* @param userName The user the expired content should be sent to
* @param storeName The store the expired content is in
* @param expiredContent List of paths to expired content
* @param workflowTitle The title to apply to the workflow
*/
private void startWorkflow(String userName, String storeName, List expiredContent,
String workflowTitle)
{
// find the 'Change Request' workflow
WorkflowDefinition wfDef = workflowService.getDefinitionByName(this.workflowName);
WorkflowPath path = this.workflowService.startWorkflow(wfDef.id, null);
if (path != null)
{
// extract the start task
List tasks = this.workflowService.getTasksForWorkflowPath(path.id);
if (tasks.size() == 1)
{
WorkflowTask startTask = tasks.get(0);
if (startTask.state == WorkflowTaskState.IN_PROGRESS)
{
// determine the user to assign the workflow to
String userStore = storeName + STORE_SEPARATOR + userName;
if (this.avmService.getStore(userStore) == null)
{
// use the creator of the store (the web project creator) to assign the
// workflow to
String storeCreator = this.avmService.getStore(storeName).getCreator();
if (logger.isDebugEnabled())
logger.debug("'" + userName + "' is no longer assigned to web project. Using '" +
storeCreator + "' as they created store '" + storeName + "'");
userName = storeCreator;
userStore = storeName + STORE_SEPARATOR + userName;
}
// lookup the NodeRef for the user
NodeRef assignee = this.personService.getPerson(userName);
// create a workflow store layered over the users store
String workflowStoreName = sandboxFactory.createUserWorkflowSandbox(storeName, userStore);
// create a workflow package with all the expired items
NodeRef workflowPackage = setupWorkflowPackage(workflowStoreName, expiredContent);
// create the workflow parameters map
Map params = new HashMap(5);
params.put(WorkflowModel.ASSOC_PACKAGE, workflowPackage);
params.put(WorkflowModel.ASSOC_ASSIGNEE, assignee);
params.put(WorkflowModel.PROP_WORKFLOW_DESCRIPTION, workflowTitle);
// transition the workflow to send it to the users inbox
this.workflowService.updateTask(startTask.id, params, null, null);
this.workflowService.endTask(startTask.id, null);
// remember the root path of the workflow sandbox so we can inform
// the virtualisation server later
this.workflowStores.add(workflowStoreName + ":/" +
JNDIConstants.DIR_DEFAULT_WWW + "/" +
JNDIConstants.DIR_DEFAULT_APPBASE + "/ROOT");
if (logger.isDebugEnabled())
logger.debug("Started '" + this.workflowName + "' workflow for user '" +
userName + "' in store '" + storeName + "'");
}
}
}
}
/**
* Sets up a workflow package from the given main workflow store and applies
* the list of paths as modified items within the main workflow store.
*
* @param workflowStoreName The main workflow store to setup
* @param expiredContent The expired content
* @return The NodeRef representing the workflow package
*/
private NodeRef setupWorkflowPackage(String workflowStoreName, List expiredContent)
{
// create package paths (layered to user sandbox area as target)
String packagesPath = workflowStoreName + ":/" + JNDIConstants.DIR_DEFAULT_WWW;
for (final String srcPath : expiredContent)
{
final Matcher m = STORE_RELATIVE_PATH_PATTERN.matcher(srcPath);
String relPath = m.matches() && m.group(1).length() != 0 ? m.group(1) : null;
String pathInWorkflowStore = workflowStoreName + ":" + relPath;
// call forceCopy to make sure the path appears modified in the workflow
// sandbox, if the item is already modified or deleted this call has no effect.
this.avmLockingAwareService.forceCopy(pathInWorkflowStore);
}
// convert package to workflow package
AVMNodeDescriptor packageDesc = avmService.lookup(-1, packagesPath);
NodeRef packageNodeRef = workflowService.createPackage(
AVMNodeConverter.ToNodeRef(-1, packageDesc.getPath()));
this.nodeService.setProperty(packageNodeRef, WorkflowModel.PROP_IS_SYSTEM_PACKAGE, true);
return packageNodeRef;
}
}