- Added getCopies method to CopyService

- Copy and transform actions will now (by default) overwrite an exiting copy.  A parameter can be set on both actions to revert to the previous, always copy, behaviour.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2755 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Roy Wetherall
2006-05-04 11:15:38 +00:00
parent 5b81614707
commit 5ba9ae0028
6 changed files with 229 additions and 39 deletions

View File

@@ -470,6 +470,9 @@
<property name="ruleService"> <property name="ruleService">
<ref bean="ruleService" /> <ref bean="ruleService" />
</property> </property>
<property name="searchService">
<ref bean="SearchService" />
</property>
</bean> </bean>
<!-- --> <!-- -->

View File

@@ -21,6 +21,7 @@ package org.alfresco.repo.action.executer;
import java.util.List; import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.ParameterDefinitionImpl; import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.service.cmr.action.Action; import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition; import org.alfresco.service.cmr.action.ParameterDefinition;
@@ -28,6 +29,7 @@ import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.repository.CopyService; import org.alfresco.service.cmr.repository.CopyService;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.rule.RuleServiceException;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
/** /**
@@ -39,11 +41,14 @@ import org.alfresco.service.namespace.QName;
*/ */
public class CopyActionExecuter extends ActionExecuterAbstractBase public class CopyActionExecuter extends ActionExecuterAbstractBase
{ {
public static final String ERR_OVERWRITE = "Unable to overwrite copy because more than one have been found.";
public static final String NAME = "copy"; public static final String NAME = "copy";
public static final String PARAM_DESTINATION_FOLDER = "destination-folder"; public static final String PARAM_DESTINATION_FOLDER = "destination-folder";
public static final String PARAM_ASSOC_TYPE_QNAME = "assoc-type"; public static final String PARAM_ASSOC_TYPE_QNAME = "assoc-type";
public static final String PARAM_ASSOC_QNAME = "assoc-name"; public static final String PARAM_ASSOC_QNAME = "assoc-name";
public static final String PARAM_DEEP_COPY = "deep-copy"; public static final String PARAM_DEEP_COPY = "deep-copy";
public static final String PARAM_OVERWRITE_COPY = "overwrite-copy";
/** /**
* Node operations service * Node operations service
@@ -55,17 +60,30 @@ public class CopyActionExecuter extends ActionExecuterAbstractBase
*/ */
private NodeService nodeService; private NodeService nodeService;
/**
* Sets the node service
*
* @param nodeService the node service
*/
public void setNodeService(NodeService nodeService) public void setNodeService(NodeService nodeService)
{ {
this.nodeService = nodeService; this.nodeService = nodeService;
} }
/**
* Sets the copy service
*
* @param copyService the copy service
*/
public void setCopyService(CopyService copyService) public void setCopyService(CopyService copyService)
{ {
this.copyService = copyService; this.copyService = copyService;
} }
/**
* @see org.alfresco.repo.action.ParameterizedItemAbstractBase#addParameterDefintions(java.util.List)
*/
@Override @Override
protected void addParameterDefintions(List<ParameterDefinition> paramList) protected void addParameterDefintions(List<ParameterDefinition> paramList)
{ {
@@ -73,6 +91,7 @@ public class CopyActionExecuter extends ActionExecuterAbstractBase
paramList.add(new ParameterDefinitionImpl(PARAM_ASSOC_TYPE_QNAME, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_ASSOC_TYPE_QNAME))); paramList.add(new ParameterDefinitionImpl(PARAM_ASSOC_TYPE_QNAME, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_ASSOC_TYPE_QNAME)));
paramList.add(new ParameterDefinitionImpl(PARAM_ASSOC_QNAME, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_ASSOC_QNAME))); paramList.add(new ParameterDefinitionImpl(PARAM_ASSOC_QNAME, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_ASSOC_QNAME)));
paramList.add(new ParameterDefinitionImpl(PARAM_DEEP_COPY, DataTypeDefinition.BOOLEAN, false, getParamDisplayLabel(PARAM_DEEP_COPY))); paramList.add(new ParameterDefinitionImpl(PARAM_DEEP_COPY, DataTypeDefinition.BOOLEAN, false, getParamDisplayLabel(PARAM_DEEP_COPY)));
paramList.add(new ParameterDefinitionImpl(PARAM_OVERWRITE_COPY, DataTypeDefinition.BOOLEAN, false, getParamDisplayLabel(PARAM_OVERWRITE_COPY)));
} }
/** /**
@@ -86,9 +105,63 @@ public class CopyActionExecuter extends ActionExecuterAbstractBase
QName destinationAssocTypeQName = (QName)ruleAction.getParameterValue(PARAM_ASSOC_TYPE_QNAME); QName destinationAssocTypeQName = (QName)ruleAction.getParameterValue(PARAM_ASSOC_TYPE_QNAME);
QName destinationAssocQName = (QName)ruleAction.getParameterValue(PARAM_ASSOC_QNAME); QName destinationAssocQName = (QName)ruleAction.getParameterValue(PARAM_ASSOC_QNAME);
// TODO get this from a parameter value // Get the deep copy value
boolean deepCopy = false; boolean deepCopy = false;
Boolean deepCopyValue = (Boolean)ruleAction.getParameterValue(PARAM_DEEP_COPY);
if (deepCopyValue != null)
{
deepCopy = deepCopyValue.booleanValue();
}
// Get the overwirte value
boolean overwrite = true;
Boolean overwriteValue = (Boolean)ruleAction.getParameterValue(PARAM_OVERWRITE_COPY);
if (overwriteValue != null)
{
overwrite = overwriteValue.booleanValue();
}
// Since we are overwriting we need to figure out whether the destination node exists
NodeRef destinationNodeRef = null;
if (overwrite == true)
{
// Try and find copies of the actioned upon node reference
List<NodeRef> copies = this.copyService.getCopies(actionedUponNodeRef);
if (copies != null && copies.isEmpty() == false)
{
for (NodeRef copy : copies)
{
// Ignore if the copy is a working copy
if (this.nodeService.hasAspect(copy, ContentModel.ASPECT_WORKING_COPY) == false)
{
// We can assume that we are looking for a node created by this action so the primary parent will
// match the destination folder
NodeRef parent = this.nodeService.getPrimaryParent(copy).getParentRef();
if (parent.equals(destinationParent) == true)
{
if (destinationNodeRef == null)
{
destinationNodeRef = copy;
}
else
{
throw new RuleServiceException(ERR_OVERWRITE);
}
}
}
}
}
}
if (destinationNodeRef != null)
{
// Overwrite the state of the destination node ref with the actioned upon node state
this.copyService.copy(actionedUponNodeRef, destinationNodeRef);
}
else
{
// Create a new copy of the node
this.copyService.copy( this.copyService.copy(
actionedUponNodeRef, actionedUponNodeRef,
destinationParent, destinationParent,
@@ -97,4 +170,5 @@ public class CopyActionExecuter extends ActionExecuterAbstractBase
deepCopy); deepCopy);
} }
} }
}
} }

View File

@@ -33,6 +33,7 @@ import org.alfresco.service.cmr.repository.MimetypeService;
import org.alfresco.service.cmr.repository.NoTransformerException; import org.alfresco.service.cmr.repository.NoTransformerException;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.rule.RuleServiceException;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@@ -44,6 +45,9 @@ import org.apache.commons.logging.LogFactory;
*/ */
public class TransformActionExecuter extends ActionExecuterAbstractBase public class TransformActionExecuter extends ActionExecuterAbstractBase
{ {
/** Error messages */
public static final String ERR_OVERWRITE = "Unable to overwrite copy because more than one have been found.";
/** /**
* The logger * The logger
*/ */
@@ -57,6 +61,7 @@ public class TransformActionExecuter extends ActionExecuterAbstractBase
public static final String PARAM_DESTINATION_FOLDER = "destination-folder"; public static final String PARAM_DESTINATION_FOLDER = "destination-folder";
public static final String PARAM_ASSOC_TYPE_QNAME = "assoc-type"; public static final String PARAM_ASSOC_TYPE_QNAME = "assoc-type";
public static final String PARAM_ASSOC_QNAME = "assoc-name"; public static final String PARAM_ASSOC_QNAME = "assoc-name";
public static final String PARAM_OVERWRITE_COPY = "overwrite-copy";
private DictionaryService dictionaryService; private DictionaryService dictionaryService;
private NodeService nodeService; private NodeService nodeService;
@@ -124,6 +129,7 @@ public class TransformActionExecuter extends ActionExecuterAbstractBase
paramList.add(new ParameterDefinitionImpl(PARAM_DESTINATION_FOLDER, DataTypeDefinition.NODE_REF, true, getParamDisplayLabel(PARAM_DESTINATION_FOLDER))); paramList.add(new ParameterDefinitionImpl(PARAM_DESTINATION_FOLDER, DataTypeDefinition.NODE_REF, true, getParamDisplayLabel(PARAM_DESTINATION_FOLDER)));
paramList.add(new ParameterDefinitionImpl(PARAM_ASSOC_TYPE_QNAME, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_ASSOC_TYPE_QNAME))); paramList.add(new ParameterDefinitionImpl(PARAM_ASSOC_TYPE_QNAME, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_ASSOC_TYPE_QNAME)));
paramList.add(new ParameterDefinitionImpl(PARAM_ASSOC_QNAME, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_ASSOC_QNAME))); paramList.add(new ParameterDefinitionImpl(PARAM_ASSOC_QNAME, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_ASSOC_QNAME)));
paramList.add(new ParameterDefinitionImpl(PARAM_OVERWRITE_COPY, DataTypeDefinition.BOOLEAN, false, getParamDisplayLabel(PARAM_OVERWRITE_COPY)));
} }
/** /**
@@ -154,14 +160,59 @@ public class TransformActionExecuter extends ActionExecuterAbstractBase
QName destinationAssocTypeQName = (QName)ruleAction.getParameterValue(PARAM_ASSOC_TYPE_QNAME); QName destinationAssocTypeQName = (QName)ruleAction.getParameterValue(PARAM_ASSOC_TYPE_QNAME);
QName destinationAssocQName = (QName)ruleAction.getParameterValue(PARAM_ASSOC_QNAME); QName destinationAssocQName = (QName)ruleAction.getParameterValue(PARAM_ASSOC_QNAME);
// Get the overwirte value
boolean overwrite = true;
Boolean overwriteValue = (Boolean)ruleAction.getParameterValue(PARAM_OVERWRITE_COPY);
if (overwriteValue != null)
{
overwrite = overwriteValue.booleanValue();
}
// Since we are overwriting we need to figure out whether the destination node exists
NodeRef copyNodeRef = null;
if (overwrite == true)
{
// Try and find copies of the actioned upon node reference
List<NodeRef> copies = this.copyService.getCopies(actionedUponNodeRef);
if (copies != null && copies.isEmpty() == false)
{
for (NodeRef copy : copies)
{
// Ignore if the copy is a working copy
if (this.nodeService.hasAspect(copy, ContentModel.ASPECT_WORKING_COPY) == false)
{
// We can assume that we are looking for a node created by this action so the primary parent will
// match the destination folder
NodeRef parent = this.nodeService.getPrimaryParent(copy).getParentRef();
if (parent.equals(destinationParent) == true)
{
if (copyNodeRef == null)
{
copyNodeRef = copy;
}
else
{
throw new RuleServiceException(ERR_OVERWRITE);
}
}
}
}
}
}
boolean newCopy = false;
if (copyNodeRef == null)
{
// Copy the content node // Copy the content node
NodeRef copyNodeRef = this.copyService.copy( copyNodeRef = this.copyService.copy(
actionedUponNodeRef, actionedUponNodeRef,
destinationParent, destinationParent,
destinationAssocTypeQName, destinationAssocTypeQName,
destinationAssocQName, destinationAssocQName,
false); false);
newCopy = true;
}
// Get the content reader // Get the content reader
ContentReader contentReader = this.contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT); ContentReader contentReader = this.contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
@@ -179,6 +230,8 @@ public class TransformActionExecuter extends ActionExecuterAbstractBase
contentWriter.setMimetype(mimeType); // new mimetype contentWriter.setMimetype(mimeType); // new mimetype
contentWriter.setEncoding(contentReader.getEncoding()); // original encoding contentWriter.setEncoding(contentReader.getEncoding()); // original encoding
if (newCopy == true)
{
// Adjust the name of the copy // Adjust the name of the copy
String originalName = (String)nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME); String originalName = (String)nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME);
String newName = transformName(originalName, originalMimetype, mimeType); String newName = transformName(originalName, originalMimetype, mimeType);
@@ -188,6 +241,7 @@ public class TransformActionExecuter extends ActionExecuterAbstractBase
{ {
String newTitle = transformName(originalTitle, originalMimetype, mimeType); String newTitle = transformName(originalTitle, originalMimetype, mimeType);
nodeService.setProperty(copyNodeRef, ContentModel.PROP_TITLE, newTitle); nodeService.setProperty(copyNodeRef, ContentModel.PROP_TITLE, newTitle);
}
} }
// Try and transform the content // Try and transform the content
@@ -204,8 +258,11 @@ public class TransformActionExecuter extends ActionExecuterAbstractBase
" writer: " + contentWriter + "\n" + " writer: " + contentWriter + "\n" +
" action: " + this); " action: " + this);
} }
//if (newCopy == true)
//{
// TODO: Revisit this for alternative solutions // TODO: Revisit this for alternative solutions
nodeService.deleteNode(copyNodeRef); // nodeService.deleteNode(copyNodeRef);
// }
} }
} }

View File

@@ -17,6 +17,7 @@
package org.alfresco.repo.copy; package org.alfresco.repo.copy;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@@ -31,10 +32,10 @@ import org.alfresco.repo.policy.PolicyScope;
import org.alfresco.service.cmr.dictionary.AspectDefinition; import org.alfresco.service.cmr.dictionary.AspectDefinition;
import org.alfresco.service.cmr.dictionary.AssociationDefinition; import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.ClassDefinition; import org.alfresco.service.cmr.dictionary.ClassDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.dictionary.InvalidTypeException; import org.alfresco.service.cmr.dictionary.InvalidTypeException;
import org.alfresco.service.cmr.dictionary.PropertyDefinition; import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.TypeDefinition; import org.alfresco.service.cmr.dictionary.TypeDefinition;
import org.alfresco.service.cmr.repository.AssociationRef; import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.ChildAssociationRef;
@@ -44,6 +45,8 @@ import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.rule.RuleService; import org.alfresco.service.cmr.rule.RuleService;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern; import org.alfresco.service.namespace.RegexQNamePattern;
@@ -56,29 +59,22 @@ import org.alfresco.util.ParameterCheck;
*/ */
public class CopyServiceImpl implements CopyService public class CopyServiceImpl implements CopyService
{ {
/** /** The node service */
* The node service
*/
private NodeService nodeService; private NodeService nodeService;
/** /** The dictionary service*/
* The dictionary service
*/
private DictionaryService dictionaryService; private DictionaryService dictionaryService;
/** /** The search service */
* Policy component private SearchService searchService;
*/
/** Policy component */
private PolicyComponent policyComponent; private PolicyComponent policyComponent;
/** /** Rule service */
* Rule service
*/
private RuleService ruleService; private RuleService ruleService;
/** /** Policy delegates */
* Policy delegates
*/
private ClassPolicyDelegate<CopyServicePolicies.OnCopyNodePolicy> onCopyNodeDelegate; private ClassPolicyDelegate<CopyServicePolicies.OnCopyNodePolicy> onCopyNodeDelegate;
private ClassPolicyDelegate<CopyServicePolicies.OnCopyCompletePolicy> onCopyCompleteDelegate; private ClassPolicyDelegate<CopyServicePolicies.OnCopyCompletePolicy> onCopyCompleteDelegate;
@@ -112,6 +108,16 @@ public class CopyServiceImpl implements CopyService
this.policyComponent = policyComponent; this.policyComponent = policyComponent;
} }
/**
* Sets the search service
*
* @param searchService the search service
*/
public void setSearchService(SearchService searchService)
{
this.searchService = searchService;
}
/** /**
* Set the rule service * Set the rule service
* *
@@ -804,4 +810,33 @@ public class CopyServiceImpl implements CopyService
{ {
// Do nothing since we do not want the copy from aspect to be relative to the copied nodes // Do nothing since we do not want the copy from aspect to be relative to the copied nodes
} }
public List<NodeRef> getCopies(NodeRef nodeRef)
{
List<NodeRef> copies = new ArrayList<NodeRef>();
// Do a search to find the origional document
ResultSet resultSet = null;
try
{
resultSet = this.searchService.query(
nodeRef.getStoreRef(),
SearchService.LANGUAGE_LUCENE,
"+@\\{http\\://www.alfresco.org/model/content/1.0\\}" + ContentModel.PROP_COPY_REFERENCE.getLocalName() + ":\"" + nodeRef.toString() + "\"");
for (NodeRef copy : resultSet.getNodeRefs())
{
copies.add(copy);
}
}
finally
{
if (resultSet != null)
{
resultSet.close();
}
}
return copies;
}
} }

View File

@@ -315,6 +315,11 @@ public class CopyServiceImplTest extends BaseSpringTest
*/ */
public void testCopyToNewNode() public void testCopyToNewNode()
{ {
// Check that the node has no copies
List<NodeRef> copies = this.copyService.getCopies(this.sourceNodeRef);
assertNotNull(copies);
assertTrue(copies.isEmpty());
// Copy to new node without copying children // Copy to new node without copying children
NodeRef copy = this.copyService.copy( NodeRef copy = this.copyService.copy(
this.sourceNodeRef, this.sourceNodeRef,
@@ -322,15 +327,21 @@ public class CopyServiceImplTest extends BaseSpringTest
ContentModel.ASSOC_CHILDREN, ContentModel.ASSOC_CHILDREN,
QName.createQName("{test}copyAssoc")); QName.createQName("{test}copyAssoc"));
checkCopiedNode(this.sourceNodeRef, copy, true, true, false); checkCopiedNode(this.sourceNodeRef, copy, true, true, false);
List<NodeRef> copies2 = this.copyService.getCopies(this.sourceNodeRef);
assertNotNull(copies2);
assertEquals(1, copies2.size());
// Copy to new node, copying children // Copy to new node, copying children
NodeRef copy2 = this.copyService.copy( NodeRef copy2 = this.copyService.copy(
this.sourceNodeRef, this.sourceNodeRef,
this.rootNodeRef, this.rootNodeRef,
ContentModel.ASSOC_CHILDREN, ContentModel.ASSOC_CHILDREN,
QName.createQName("{test}copyAssoc"), QName.createQName("{test}copyAssoc2"),
true); true);
checkCopiedNode(this.sourceNodeRef, copy2, true, true, true); checkCopiedNode(this.sourceNodeRef, copy2, true, true, true);
List<NodeRef> copies3 = this.copyService.getCopies(this.sourceNodeRef);
assertNotNull(copies3);
assertEquals(2, copies3.size());
// Check that a copy of a copy works correctly // Check that a copy of a copy works correctly
NodeRef copyOfCopy = this.copyService.copy( NodeRef copyOfCopy = this.copyService.copy(

View File

@@ -16,6 +16,8 @@
*/ */
package org.alfresco.service.cmr.repository; package org.alfresco.service.cmr.repository;
import java.util.List;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
/** /**
@@ -116,4 +118,12 @@ public interface CopyService
* @param destinationNodeRef the destination node reference * @param destinationNodeRef the destination node reference
*/ */
public void copy(NodeRef sourceNodeRef, NodeRef destinationNodeRef); public void copy(NodeRef sourceNodeRef, NodeRef destinationNodeRef);
/**
* Gets all the copies of a given node that have been made using this service.
*
* @param nodeRef the origional node reference
* @return a list of copies, empty is none
*/
public List<NodeRef> getCopies(NodeRef nodeRef);
} }