Fixed MOB-426: Refactor CopyService: Apply new pattern to existing policy handlers

Fallout:
1. Policy handling for 'onCopy' has been changed to 'getCopyCallback'
2. All existing policy usage was refactored to control behaviour more closely
3. The default child association behaviour has changed:
3.1 Types and aspects control their own child association behaviour
3.2 cm:folder recurses into primary children, but merely copies the secondary association
3.3 cm:rule recurses into primary children
3.4 unless behaviour is defined for a child association, there is no recursion or copying
4. Node association behavior has changed
4.1 There is no copying of node associations.  Each type and aspect must handle this by
    recording nodes and fixing up the required associations in the onCopyComplete.
4.2 If there is a requirement, this can be added to the callback later

See 'org.alfresco.repo.copy.AbstractCopyBehaviourCallback' and derived classes for examples.

Areas to test with particular attention:
1. Normal copy behaviour
2. Copy of documents with discussions
3. Check-in check-out
4. Check-in, check-out of documents where a discussion was added to working copy
5. Copies of documents with thumbnails
6. Copies of documents with rules
7. Copying of hierarchies that contain rules to copy to another location within the hierarchy
8. Copying into folders where named children already exist


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@13915 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2009-04-09 14:48:02 +00:00
parent 0b57c06ad3
commit d6586351c1
29 changed files with 5920 additions and 4222 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -22,97 +22,88 @@
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.forum;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.alfresco.i18n.I18NUtil;
import org.alfresco.model.ApplicationModel;
import org.alfresco.model.ContentModel;
import org.alfresco.model.ForumModel;
import org.alfresco.repo.copy.CopyBehaviourCallback;
import org.alfresco.repo.copy.CopyDetails;
import org.alfresco.repo.copy.CopyServicePolicies;
import org.alfresco.repo.copy.DoNothingCopyBehaviourCallback;
import org.alfresco.repo.node.NodeServicePolicies;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.repo.policy.PolicyScope;
import org.alfresco.service.cmr.coci.CheckOutCheckInServiceException;
import org.alfresco.repo.transaction.TransactionalResourceHelper;
import org.alfresco.service.cmr.model.FileExistsException;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileNotFoundException;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.ConcurrencyFailureException;
public class DiscussableAspect
/**
* Discussion-specific behaviours.
*
* @author Derek Hulley
* @since 3.2
*/
public class DiscussableAspect implements
NodeServicePolicies.OnAddAspectPolicy,
CopyServicePolicies.OnCopyNodePolicy,
CopyServicePolicies.OnCopyCompletePolicy
{
private static final String KEY_WORKING_COPIES = DiscussableAspect.class.getName() + ".WorkingCopies";
private static final Log logger = LogFactory.getLog(DiscussableAspect.class);
/**
* Policy component
*/
private PolicyComponent policyComponent;
/**
* The node service
*/
private NodeService nodeService;
/**
* The file folder service
*/
private FileFolderService fileFolderService;
/**
* Sets the policy component
*
* @param policyComponent the policy component
*/
public void setPolicyComponent(PolicyComponent policyComponent)
{
this.policyComponent = policyComponent;
}
/**
* Set the node service
*
* @param nodeService the node service
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* Set the file folder service
*
* @param fileFolderService the file folder service
*/
public void setFileFolderService(FileFolderService fileFolderService)
public final void setFileFolderService(FileFolderService fileFolderService)
{
this.fileFolderService = fileFolderService;
}
/**
* Initialise method
*/
public void init()
{
// Register copy behaviour for the discussable aspect
// All forum-related copy behaviour uses the same copy callback
this.policyComponent.bindClassBehaviour(
QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyNode"),
QName.createQName(NamespaceService.ALFRESCO_URI, "onAddAspect"),
ForumModel.ASPECT_DISCUSSABLE,
new JavaBehaviour(this, "onCopy"));
new JavaBehaviour(this, "onAddAspect"));
this.policyComponent.bindClassBehaviour(
QName.createQName(NamespaceService.ALFRESCO_URI, "getCopyCallback"),
ForumModel.ASPECT_DISCUSSABLE,
new JavaBehaviour(this, "getCopyCallback"));
this.policyComponent.bindClassBehaviour(
QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyComplete"),
ForumModel.ASPECT_DISCUSSABLE,
@@ -120,148 +111,267 @@ public class DiscussableAspect
}
/**
* onCopy policy behaviour
*
* @see org.alfresco.repo.copy.CopyServicePolicies.OnCopyNodePolicy#onCopyNode(QName, NodeRef, StoreRef, boolean, PolicyScope)
* @return Returns {@link DiscussableAspectCopyBehaviourCallback}
*/
public void onCopy(
QName sourceClassRef,
NodeRef sourceNodeRef,
StoreRef destinationStoreRef,
boolean copyToNewNode,
PolicyScope copyDetails)
public CopyBehaviourCallback getCopyCallback(QName classRef, CopyDetails copyDetails)
{
// NOTE: we intentionally don't do anything in here, this stops the discussable
// aspect from being added to the new copied node - the behaviour we want.
return DiscussableAspectCopyBehaviourCallback.INSTANCE;
}
public void onCopyComplete(
QName classRef,
NodeRef sourceNodeRef,
NodeRef destinationRef,
boolean copyNewNode,
Map<NodeRef, NodeRef> copyMap)
/**
* Copy behaviour for the <b>fm:discussable</b> aspect.
* <p>
* Only the aspect is copied (to get the default behaviour). All topics are copied later.
*
* @author Derek Hulley
* @since 3.2
*/
private static class DiscussableAspectCopyBehaviourCallback extends DoNothingCopyBehaviourCallback
{
// if the copy is not a new node it is a checkin, we therefore
// need to copy any discussions from the working copy document
// to the document being checked in
if (copyNewNode == false)
private static final CopyBehaviourCallback INSTANCE = new DiscussableAspectCopyBehaviourCallback();
/**
* Copy the aspect over only if the source document has also been checked out
*/
@Override
public boolean getMustCopy(QName classQName, CopyDetails copyDetails)
{
List<ChildAssociationRef> sourceChildren = this.nodeService.getChildAssocs(sourceNodeRef,
ForumModel.ASSOC_DISCUSSION, RegexQNamePattern.MATCH_ALL);
if (sourceChildren.size() != 1)
if (copyDetails.getSourceNodeAspectQNames().contains(ContentModel.ASPECT_WORKING_COPY) &&
!copyDetails.isTargetNodeIsNew())
{
throw new CheckOutCheckInServiceException(
"The source node has the discussable aspect but does not have 1 child, it has " +
sourceChildren.size() + " children!");
// We are copying back from a working copy to the original node (probably)
// We need to do a full merge of the discussions. Keep track of the nodes
// that need this behaviour and complete the copy after the copy completes.
Set<NodeRef> nodeRefs = TransactionalResourceHelper.getSet(KEY_WORKING_COPIES);
nodeRefs.add(copyDetails.getSourceNodeRef());
}
NodeRef sourceForum = sourceChildren.get(0).getChildRef();
// get the forum for the destination node, it's created if necessary
NodeRef destinationForum = getDestinationForum(destinationRef);
// copy any topics from the source forum to the destination forum
int copied = 0;
List<ChildAssociationRef> sourceForums = this.nodeService.getChildAssocs(sourceForum);
for (ChildAssociationRef childRef : sourceForums)
{
String topicName = null;
NodeRef childNode = childRef.getChildRef();
if (this.nodeService.getType(childNode).equals(ForumModel.TYPE_TOPIC))
{
try
{
// work out the name for the copied topic
String childName = this.nodeService.getProperty(childNode,
ContentModel.PROP_NAME).toString();
Serializable labelProp = this.nodeService.getProperty(destinationRef,
ContentModel.PROP_VERSION_LABEL);
if (labelProp == null)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");
topicName = childName + " - " + dateFormat.format(new Date());
}
else
{
topicName = childName + " (" + labelProp.toString() + ")";
}
this.fileFolderService.copy(childNode, destinationForum, topicName);
copied++;
}
catch (FileNotFoundException fnfe)
{
throw new CheckOutCheckInServiceException(
"Failed to copy topic from working copy to checked out content", fnfe);
}
catch (FileExistsException fee)
{
throw new CheckOutCheckInServiceException("Failed to checkin content as a topic called " +
topicName + " already exists on the checked out content", fee);
}
}
}
if (logger.isDebugEnabled())
logger.debug("Copied " + copied + " topics from the working copy to the checked out content");
return false;
}
}
/**
* Retrieves or creates the forum node for the given destination node
*
* @param destNodeRef The node to get the forum for
* @return NodeRef representing the forum
* Ensure that the node has a <b>fm:forum</b> child node otherwise create one
*/
private NodeRef getDestinationForum(NodeRef destNodeRef)
public void onAddAspect(NodeRef discussableNodeRef, QName aspectTypeQName)
{
NodeRef destinationForum = null;
String name = (String)this.nodeService.getProperty(
discussableNodeRef,
ContentModel.PROP_NAME);
String forumName = I18NUtil.getMessage("discussion.discussion_for", new Object[] {name});
if (this.nodeService.hasAspect(destNodeRef, ForumModel.ASPECT_DISCUSSABLE))
NodeRef forumNodeRef = getForum(discussableNodeRef);
if (forumNodeRef == null)
{
List<ChildAssociationRef> destChildren = this.nodeService.getChildAssocs(destNodeRef,
ForumModel.ASSOC_DISCUSSION, RegexQNamePattern.MATCH_ALL);
Map<QName, Serializable> forumProps = new HashMap<QName, Serializable>(1);
forumProps.put(ContentModel.PROP_NAME, forumName);
if (destChildren.size() != 1)
{
throw new IllegalStateException("Locked node has the discussable aspect but does not have 1 child, it has " +
destChildren.size() + " children!");
}
destinationForum = destChildren.get(0).getChildRef();
ChildAssociationRef childRef = nodeService.createNode(
discussableNodeRef,
ForumModel.ASSOC_DISCUSSION,
QName.createQName(NamespaceService.FORUMS_MODEL_1_0_URI, "discussion"),
ForumModel.TYPE_FORUM, forumProps);
forumNodeRef = childRef.getChildRef();
}
else
{
// create the forum - TODO: Move this to a repo discussion service so that it can
// be shared between here and the discussion wizard
// add the discussable aspect
this.nodeService.addAspect(destNodeRef, ForumModel.ASPECT_DISCUSSABLE, null);
// create a child forum space using the child association just introduced by
// adding the discussable aspect
String name = (String)this.nodeService.getProperty(destNodeRef,
ContentModel.PROP_NAME);
String forumName = I18NUtil.getMessage("coci_service.discussion_for", new Object[] {name});
Map<QName, Serializable> forumProps = new HashMap<QName, Serializable>(1);
forumProps.put(ContentModel.PROP_NAME, forumName);
ChildAssociationRef childRef = this.nodeService.createNode(destNodeRef,
ForumModel.ASSOC_DISCUSSION,
QName.createQName(NamespaceService.FORUMS_MODEL_1_0_URI, "discussion"),
ForumModel.TYPE_FORUM, forumProps);
destinationForum = childRef.getChildRef();
// Just adjust the name
nodeService.setProperty(forumNodeRef, ContentModel.PROP_NAME, forumName);
}
// apply the uifacets aspect
Map<QName, Serializable> uiFacetsProps = new HashMap<QName, Serializable>(5);
uiFacetsProps.put(ApplicationModel.PROP_ICON, "forum");
this.nodeService.addAspect(forumNodeRef, ApplicationModel.ASPECT_UIFACETS, uiFacetsProps);
// Done
if (logger.isDebugEnabled())
{
logger.debug(
"Created forum node for discussion: \n" +
" Discussable Node: " + discussableNodeRef + "\n" +
" Forum Node: " + forumNodeRef);
}
}
// apply the uifacets aspect
Map<QName, Serializable> uiFacetsProps = new HashMap<QName, Serializable>(5);
uiFacetsProps.put(ApplicationModel.PROP_ICON, "forum");
this.nodeService.addAspect(destinationForum, ApplicationModel.ASPECT_UIFACETS, uiFacetsProps);
/**
* Retrieves the forum node the the given discussable
*
* @return Returns the <b>fm:forum</b> node or <tt>null</tt>
*/
private NodeRef getForum(NodeRef discussableNodeRef)
{
List<ChildAssociationRef> destChildren = nodeService.getChildAssocs(
discussableNodeRef,
ForumModel.ASSOC_DISCUSSION,
RegexQNamePattern.MATCH_ALL);
// Take the first one
if (destChildren.size() == 0)
{
return null;
}
else
{
// We just take the first one
ChildAssociationRef discussionAssoc = destChildren.get(0);
return discussionAssoc.getChildRef();
}
}
//
// /**
// * Copies the discussions to the new node, whilst being sensitive to any existing discussions.
// */
// public void onCopyComplete(
// QName classQName,
// NodeRef sourceNodeRef,
// NodeRef targetNodeRef,
// boolean copyToNewNode,
// Map<NodeRef,NodeRef> copyMap)
// {
// // if the copy is not a new node it is a checkin, we therefore
// // need to copy any discussions from the working copy document
// // to the document being checked in
// if (copyToNewNode)
// {
// // We don't care about new copies, just copies to existing nodes
// return;
// }
// List<ChildAssociationRef> sourceChildren = nodeService.getChildAssocs(sourceNodeRef,
// ForumModel.ASSOC_DISCUSSION, RegexQNamePattern.MATCH_ALL);
//
// if (sourceChildren.size() != 1)
// {
// throw new CheckOutCheckInServiceException(
// "The source node has the discussable aspect but does not have 1 child, it has " +
// sourceChildren.size() + " children!");
// }
//
// NodeRef sourceForum = sourceChildren.get(0).getChildRef();
//
// // get the forum for the destination node, it's created if necessary
// NodeRef destinationForum = getDestinationForum(targetNodeRef);
//
// // copy any topics from the source forum to the destination forum
// int copied = 0;
// List<ChildAssociationRef> sourceForums = nodeService.getChildAssocs(sourceForum);
// for (ChildAssociationRef childRef : sourceForums)
// {
// String topicName = null;
// NodeRef childNode = childRef.getChildRef();
// if (nodeService.getType(childNode).equals(ForumModel.TYPE_TOPIC))
// {
// try
// {
// // work out the name for the copied topic
// String childName = nodeService.getProperty(childNode,
// ContentModel.PROP_NAME).toString();
// Serializable labelProp = nodeService.getProperty(targetNodeRef,
// ContentModel.PROP_VERSION_LABEL);
// if (labelProp == null)
// {
// SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");
// topicName = childName + " - " + dateFormat.format(new Date());
// }
// else
// {
// topicName = childName + " (" + labelProp.toString() + ")";
// }
//
// fileFolderService.copy(childNode, destinationForum, topicName);
// copied++;
// }
// catch (FileNotFoundException fnfe)
// {
// throw new CheckOutCheckInServiceException(
// "Failed to copy topic from working copy to checked out content", fnfe);
// }
// catch (FileExistsException fee)
// {
// throw new CheckOutCheckInServiceException("Failed to checkin content as a topic called " +
// topicName + " already exists on the checked out content", fee);
// }
// }
// }
//
// if (logger.isDebugEnabled())
// logger.debug("Copied " + copied + " topics from the working copy to the checked out content");
// }
//
public void onCopyComplete(
QName classRef,
NodeRef sourceNodeRef,
NodeRef targetNodeRef,
boolean copyToNewNode,
Map<NodeRef, NodeRef> copyMap)
{
Set<NodeRef> workingCopyNodeRefs = TransactionalResourceHelper.getSet(KEY_WORKING_COPIES);
if (!workingCopyNodeRefs.contains(sourceNodeRef))
{
// This is not one of the nodes that needs to have discussions copied over
return;
}
return destinationForum;
// First check that the source node has forums
NodeRef sourceForumNodeRef = getForum(sourceNodeRef);
if (sourceForumNodeRef == null)
{
// Missing! Clean the source node up!
nodeService.removeAspect(sourceNodeRef, ForumModel.ASPECT_DISCUSSABLE);
return;
}
// The aspect may or may not exist on the target node
if (!nodeService.hasAspect(targetNodeRef, ForumModel.ASPECT_DISCUSSABLE))
{
// Add the aspect
nodeService.addAspect(targetNodeRef, ForumModel.ASPECT_DISCUSSABLE, null);
}
// Get the forum node
NodeRef targetForumNodeRef = getForum(targetNodeRef);
// Merge the forum topics
List<ChildAssociationRef> topicAssocRefs = nodeService.getChildAssocs(
sourceForumNodeRef,
Collections.singleton(ForumModel.TYPE_TOPIC));
int copied = 0;
for (ChildAssociationRef topicAssocRef : topicAssocRefs)
{
NodeRef topicNodeRef = topicAssocRef.getChildRef();
try
{
// work out the name for the copied topic
String topicName;
String topicNodeName = nodeService.getProperty(topicNodeRef, ContentModel.PROP_NAME).toString();
Serializable labelProp = nodeService.getProperty(targetNodeRef, ContentModel.PROP_VERSION_LABEL);
if (labelProp == null)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");
topicName = topicNodeName + " - " + dateFormat.format(new Date());
}
else
{
topicName = topicNodeName + " (" + labelProp.toString() + ")";
}
if (fileFolderService.searchSimple(targetForumNodeRef, topicName) != null)
{
// A topic with that name already exists
continue;
}
fileFolderService.copy(topicNodeRef, targetForumNodeRef, topicName);
copied++;
}
catch (FileExistsException e)
{
// We checked for this, so this is a concurrency condition
throw new ConcurrencyFailureException("Target topic exists: " + e.getMessage(), e);
}
catch (FileNotFoundException e)
{
// The node was there, but now it's gone
throw new ConcurrencyFailureException("Forum was deleted: " + e.getMessage(), e);
}
}
}
}