Added option to execute all rules on children

Added a boolean parameter to execute all rules action which traverses all child folders and executes all their rules too.  This should be used with caution as it it potentially expensive.



git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@19083 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Roy Wetherall
2010-03-05 01:34:47 +00:00
parent 1862567b34
commit 81111c11d8
3 changed files with 137 additions and 36 deletions

View File

@@ -554,9 +554,6 @@
<property name="nodeService"> <property name="nodeService">
<ref bean="NodeService" /> <ref bean="NodeService" />
</property> </property>
<property name="actionService">
<ref bean="ActionService" />
</property>
<property name="ruleService"> <property name="ruleService">
<ref bean="RuleService" /> <ref bean="RuleService" />
</property> </property>

View File

@@ -24,7 +24,6 @@ import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.ParameterDefinitionImpl; import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.rule.RuntimeRuleService; import org.alfresco.repo.rule.RuntimeRuleService;
import org.alfresco.service.cmr.action.Action; import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.action.ParameterDefinition; import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition; import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.dictionary.DictionaryService;
@@ -33,6 +32,7 @@ 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.Rule; import org.alfresco.service.cmr.rule.Rule;
import org.alfresco.service.cmr.rule.RuleService; import org.alfresco.service.cmr.rule.RuleService;
import org.alfresco.service.namespace.QName;
/** /**
* This action executes all rules present on the actioned upon node * This action executes all rules present on the actioned upon node
@@ -46,6 +46,7 @@ public class ExecuteAllRulesActionExecuter extends ActionExecuterAbstractBase
*/ */
public static final String NAME = "execute-all-rules"; public static final String NAME = "execute-all-rules";
public static final String PARAM_EXECUTE_INHERITED_RULES = "execute-inherited-rules"; public static final String PARAM_EXECUTE_INHERITED_RULES = "execute-inherited-rules";
public static final String PARAM_RUN_ALL_RULES_ON_CHILDREN = "run-all-rules-on-children";
/** /**
* The node service * The node service
@@ -62,11 +63,6 @@ public class ExecuteAllRulesActionExecuter extends ActionExecuterAbstractBase
*/ */
private RuntimeRuleService runtimeRuleService; private RuntimeRuleService runtimeRuleService;
/**
* The action service
*/
private ActionService actionService;
/** The dictionary Service */ /** The dictionary Service */
private DictionaryService dictionaryService; private DictionaryService dictionaryService;
@@ -99,16 +95,6 @@ public class ExecuteAllRulesActionExecuter extends ActionExecuterAbstractBase
{ {
this.runtimeRuleService = runtimeRuleService; this.runtimeRuleService = runtimeRuleService;
} }
/**
* Set the action service
*
* @param actionService the action service
*/
public void setActionService(ActionService actionService)
{
this.actionService = actionService;
}
/** /**
* Sets the dictionary service * Sets the dictionary service
@@ -135,34 +121,49 @@ public class ExecuteAllRulesActionExecuter extends ActionExecuterAbstractBase
includeInherited = includeInheritedValue.booleanValue(); includeInherited = includeInheritedValue.booleanValue();
} }
// Get the rules boolean runAllChildren = false;
List<Rule> rules = this.ruleService.getRules(actionedUponNodeRef, includeInherited); Boolean runAllChildrenValue = (Boolean)ruleAction.getParameterValue(PARAM_RUN_ALL_RULES_ON_CHILDREN);
if (runAllChildrenValue != null)
{
runAllChildren = runAllChildrenValue.booleanValue();
}
// Get the rules
List<Rule> rules = ruleService.getRules(actionedUponNodeRef, includeInherited);
if (rules != null && rules.isEmpty() == false) if (rules != null && rules.isEmpty() == false)
{ {
// Get the child nodes for the actioned upon node // Get the child nodes for the actioned upon node
List<ChildAssociationRef> children = this.nodeService.getChildAssocs(actionedUponNodeRef); List<ChildAssociationRef> children = nodeService.getChildAssocs(actionedUponNodeRef);
for (ChildAssociationRef childAssoc : children) for (ChildAssociationRef childAssoc : children)
{ {
// Get the child node reference // Get the child node reference
NodeRef child = childAssoc.getChildRef(); NodeRef child = childAssoc.getChildRef();
// Only execute rules on non-system folders // Only execute rules on non-system folders
if (this.dictionaryService.isSubClass(this.nodeService.getType(child), ContentModel.TYPE_SYSTEM_FOLDER) == false) QName childType = nodeService.getType(child);
if (dictionaryService.isSubClass(childType, ContentModel.TYPE_SYSTEM_FOLDER) == false)
{ {
for (Rule rule : rules) for (Rule rule : rules)
{ {
// Only reapply rules that are enabled // Only re-apply rules that are enabled
if (rule.getRuleDisabled() == false) if (rule.getRuleDisabled() == false)
{ {
Action action = rule.getAction(); Action action = rule.getAction();
if (action != null) if (action != null)
{ {
//this.actionService.executeAction(action, child); runtimeRuleService.addRulePendingExecution(actionedUponNodeRef, child, rule);
this.runtimeRuleService.addRulePendingExecution(actionedUponNodeRef, child, rule);
} }
} }
} }
}
// If the child is a folder and we have asked to run rules on children
if (runAllChildren == true &&
dictionaryService.isSubClass(childType, ContentModel.TYPE_FOLDER) == true)
{
// Recurse with the child folder
executeImpl(ruleAction, child);
}
}
} }
} }
} }
@@ -175,5 +176,6 @@ public class ExecuteAllRulesActionExecuter extends ActionExecuterAbstractBase
protected void addParameterDefinitions(List<ParameterDefinition> paramList) protected void addParameterDefinitions(List<ParameterDefinition> paramList)
{ {
paramList.add(new ParameterDefinitionImpl(PARAM_EXECUTE_INHERITED_RULES, DataTypeDefinition.BOOLEAN, false, getParamDisplayLabel(PARAM_EXECUTE_INHERITED_RULES))); paramList.add(new ParameterDefinitionImpl(PARAM_EXECUTE_INHERITED_RULES, DataTypeDefinition.BOOLEAN, false, getParamDisplayLabel(PARAM_EXECUTE_INHERITED_RULES)));
paramList.add(new ParameterDefinitionImpl(PARAM_RUN_ALL_RULES_ON_CHILDREN, DataTypeDefinition.BOOLEAN, false, getParamDisplayLabel(PARAM_RUN_ALL_RULES_ON_CHILDREN)));
} }
} }

View File

@@ -21,6 +21,10 @@ package org.alfresco.repo.action.executer;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.ActionImpl; import org.alfresco.repo.action.ActionImpl;
import org.alfresco.repo.security.authentication.AuthenticationComponent; import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.TransactionUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
import org.alfresco.service.cmr.action.Action; import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ActionService; import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
@@ -49,6 +53,8 @@ public class ExecuteAllRulesActionExecuterTest extends BaseSpringTest
/** The action service */ /** The action service */
private ActionService actionService; private ActionService actionService;
private RetryingTransactionHelper transactionHelper;
/** The store reference */ /** The store reference */
private StoreRef testStoreRef; private StoreRef testStoreRef;
@@ -70,6 +76,7 @@ public class ExecuteAllRulesActionExecuterTest extends BaseSpringTest
this.nodeService = (NodeService)this.applicationContext.getBean("nodeService"); this.nodeService = (NodeService)this.applicationContext.getBean("nodeService");
this.ruleService = (RuleService)this.applicationContext.getBean("ruleService"); this.ruleService = (RuleService)this.applicationContext.getBean("ruleService");
this.actionService = (ActionService)this.applicationContext.getBean("actionService"); this.actionService = (ActionService)this.applicationContext.getBean("actionService");
transactionHelper = (RetryingTransactionHelper)applicationContext.getBean("retryingTransactionHelper");
AuthenticationComponent authenticationComponent = (AuthenticationComponent)applicationContext.getBean("authenticationComponent"); AuthenticationComponent authenticationComponent = (AuthenticationComponent)applicationContext.getBean("authenticationComponent");
authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName()); authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName());
@@ -90,21 +97,32 @@ public class ExecuteAllRulesActionExecuterTest extends BaseSpringTest
public void testExecution() public void testExecution()
{ {
// Create a folder and put a couple of documents in it // Create a folder and put a couple of documents in it
NodeRef folder = this.nodeService.createNode( final NodeRef folder = this.nodeService.createNode(
this.rootNodeRef, this.rootNodeRef,
ContentModel.ASSOC_CHILDREN, ContentModel.ASSOC_CHILDREN,
QName.createQName("{test}folderOne"), QName.createQName("{test}folderOne"),
ContentModel.TYPE_FOLDER).getChildRef(); ContentModel.TYPE_FOLDER).getChildRef();
NodeRef doc1 = this.nodeService.createNode( final NodeRef doc1 = this.nodeService.createNode(
folder, folder,
ContentModel.ASSOC_CHILDREN, ContentModel.ASSOC_CONTAINS,
QName.createQName("{test}docOne"), QName.createQName("{test}docOne"),
ContentModel.TYPE_CONTENT).getChildRef(); ContentModel.TYPE_CONTENT).getChildRef();
NodeRef doc2 = this.nodeService.createNode( final NodeRef doc2 = this.nodeService.createNode(
folder, folder,
ContentModel.ASSOC_CHILDREN, ContentModel.ASSOC_CONTAINS,
QName.createQName("{test}docTwo"), QName.createQName("{test}docTwo"),
ContentModel.TYPE_CONTENT).getChildRef(); ContentModel.TYPE_CONTENT).getChildRef();
final NodeRef folder2 = this.nodeService.createNode(
folder,
ContentModel.ASSOC_CONTAINS,
QName.createQName("{test}folderTwo"),
ContentModel.TYPE_FOLDER).getChildRef();
final NodeRef doc3 = this.nodeService.createNode(
folder2,
ContentModel.ASSOC_CONTAINS,
QName.createQName("{test}docThree"),
ContentModel.TYPE_CONTENT).getChildRef();
// Add a couple of rules to the folder // Add a couple of rules to the folder
Rule rule1 = new Rule(); Rule rule1 = new Rule();
@@ -121,11 +139,23 @@ public class ExecuteAllRulesActionExecuterTest extends BaseSpringTest
rule2.setAction(action2); rule2.setAction(action2);
this.ruleService.saveRule(folder, rule2); this.ruleService.saveRule(folder, rule2);
Rule rule3 = new Rule();
rule3.setRuleType(RuleType.INBOUND);
Action action3 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
action3.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_TITLED);
rule3.setAction(action3);
this.ruleService.saveRule(folder2, rule3);
// Check the the docs don't have the aspects yet // Check the the docs don't have the aspects yet
assertFalse(this.nodeService.hasAspect(doc1, ContentModel.ASPECT_VERSIONABLE)); assertFalse(this.nodeService.hasAspect(doc1, ContentModel.ASPECT_VERSIONABLE));
assertFalse(this.nodeService.hasAspect(doc1, ContentModel.ASPECT_CLASSIFIABLE)); assertFalse(this.nodeService.hasAspect(doc1, ContentModel.ASPECT_CLASSIFIABLE));
assertFalse(this.nodeService.hasAspect(doc1, ContentModel.ASPECT_TITLED));
assertFalse(this.nodeService.hasAspect(doc2, ContentModel.ASPECT_VERSIONABLE)); assertFalse(this.nodeService.hasAspect(doc2, ContentModel.ASPECT_VERSIONABLE));
assertFalse(this.nodeService.hasAspect(doc2, ContentModel.ASPECT_CLASSIFIABLE)); assertFalse(this.nodeService.hasAspect(doc2, ContentModel.ASPECT_CLASSIFIABLE));
assertFalse(this.nodeService.hasAspect(doc2, ContentModel.ASPECT_TITLED));
assertFalse(nodeService.hasAspect(doc3, ContentModel.ASPECT_VERSIONABLE));
assertFalse(nodeService.hasAspect(doc3, ContentModel.ASPECT_CLASSIFIABLE));
assertFalse(nodeService.hasAspect(doc3, ContentModel.ASPECT_TITLED));
assertTrue(this.nodeService.exists(folder)); assertTrue(this.nodeService.exists(folder));
@@ -133,9 +163,81 @@ public class ExecuteAllRulesActionExecuterTest extends BaseSpringTest
ActionImpl action = new ActionImpl(null, ID, ExecuteAllRulesActionExecuter.NAME, null); ActionImpl action = new ActionImpl(null, ID, ExecuteAllRulesActionExecuter.NAME, null);
this.executer.execute(action, folder); this.executer.execute(action, folder);
assertTrue(this.nodeService.hasAspect(doc1, ContentModel.ASPECT_VERSIONABLE)); setComplete();
assertTrue(this.nodeService.hasAspect(doc1, ContentModel.ASPECT_CLASSIFIABLE)); endTransaction();
assertTrue(this.nodeService.hasAspect(doc2, ContentModel.ASPECT_VERSIONABLE));
assertTrue(this.nodeService.hasAspect(doc2, ContentModel.ASPECT_CLASSIFIABLE)); transactionHelper.doInTransaction(new RetryingTransactionCallback<Object>()
{
public Object execute() throws Throwable
{
assertTrue(nodeService.hasAspect(doc1, ContentModel.ASPECT_VERSIONABLE));
assertTrue(nodeService.hasAspect(doc1, ContentModel.ASPECT_CLASSIFIABLE));
assertFalse(nodeService.hasAspect(doc2, ContentModel.ASPECT_TITLED));
assertTrue(nodeService.hasAspect(doc2, ContentModel.ASPECT_VERSIONABLE));
assertTrue(nodeService.hasAspect(doc2, ContentModel.ASPECT_CLASSIFIABLE));
assertFalse(nodeService.hasAspect(doc2, ContentModel.ASPECT_TITLED));
assertFalse(nodeService.hasAspect(doc3, ContentModel.ASPECT_VERSIONABLE));
assertFalse(nodeService.hasAspect(doc3, ContentModel.ASPECT_CLASSIFIABLE));
assertFalse(nodeService.hasAspect(doc3, ContentModel.ASPECT_TITLED));
clearAspects(doc1);
clearAspects(doc2);
clearAspects(doc3);
return null;
}
});
transactionHelper.doInTransaction(new RetryingTransactionCallback<Object>()
{
public Object execute() throws Throwable
{
assertFalse(nodeService.hasAspect(doc1, ContentModel.ASPECT_VERSIONABLE));
assertFalse(nodeService.hasAspect(doc1, ContentModel.ASPECT_CLASSIFIABLE));
assertFalse(nodeService.hasAspect(doc1, ContentModel.ASPECT_TITLED));
assertFalse(nodeService.hasAspect(doc2, ContentModel.ASPECT_VERSIONABLE));
assertFalse(nodeService.hasAspect(doc2, ContentModel.ASPECT_CLASSIFIABLE));
assertFalse(nodeService.hasAspect(doc2, ContentModel.ASPECT_TITLED));
assertFalse(nodeService.hasAspect(doc3, ContentModel.ASPECT_VERSIONABLE));
assertFalse(nodeService.hasAspect(doc3, ContentModel.ASPECT_CLASSIFIABLE));
assertFalse(nodeService.hasAspect(doc3, ContentModel.ASPECT_TITLED));
// Execute the action
ActionImpl action = new ActionImpl(null, ID, ExecuteAllRulesActionExecuter.NAME, null);
action.setParameterValue(ExecuteAllRulesActionExecuter.PARAM_RUN_ALL_RULES_ON_CHILDREN, Boolean.TRUE);
executer.execute(action, folder);
return null;
}
});
transactionHelper.doInTransaction(new RetryingTransactionCallback<Object>()
{
public Object execute() throws Throwable
{
assertTrue(nodeService.hasAspect(doc1, ContentModel.ASPECT_VERSIONABLE));
assertTrue(nodeService.hasAspect(doc1, ContentModel.ASPECT_CLASSIFIABLE));
assertFalse(nodeService.hasAspect(doc2, ContentModel.ASPECT_TITLED));
assertTrue(nodeService.hasAspect(doc2, ContentModel.ASPECT_VERSIONABLE));
assertTrue(nodeService.hasAspect(doc2, ContentModel.ASPECT_CLASSIFIABLE));
assertFalse(nodeService.hasAspect(doc2, ContentModel.ASPECT_TITLED));
assertFalse(nodeService.hasAspect(doc3, ContentModel.ASPECT_VERSIONABLE));
assertFalse(nodeService.hasAspect(doc3, ContentModel.ASPECT_CLASSIFIABLE));
assertTrue(nodeService.hasAspect(doc3, ContentModel.ASPECT_TITLED));
clearAspects(doc1);
clearAspects(doc2);
clearAspects(doc3);
return null;
}
});
}
private void clearAspects(NodeRef nodeRef)
{
nodeService.removeAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE);
nodeService.removeAspect(nodeRef, ContentModel.ASPECT_CLASSIFIABLE);
nodeService.removeAspect(nodeRef, ContentModel.ASPECT_TITLED);
} }
} }