- Rule and action tasks for 1.4

- Updated W/S include paths

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@3526 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Roy Wetherall
2006-08-16 13:27:35 +00:00
parent 895935e8d6
commit 29beac8efb
16 changed files with 501 additions and 193 deletions

View File

@@ -156,6 +156,11 @@ public class BaseRuleTest extends BaseSpringTest
}
protected Rule createTestRule(boolean isAppliedToChildren)
{
return createTestRule(isAppliedToChildren, TITLE);
}
protected Rule createTestRule(boolean isAppliedToChildren, String title)
{
// Rule properties
Map<String, Serializable> conditionProps = new HashMap<String, Serializable>();
@@ -178,7 +183,7 @@ public class BaseRuleTest extends BaseSpringTest
// Create the rule
Rule rule = new Rule();
rule.setRuleTypes(ruleTypes);
rule.setTitle(TITLE);
rule.setTitle(title);
rule.setDescription(DESCRIPTION);
rule.applyToChildren(isAppliedToChildren);
rule.setAction(action);

View File

@@ -18,7 +18,10 @@ public interface RuleModel
static final QName PROP_APPLY_TO_CHILDREN = QName.createQName(RULE_MODEL_URI, "applyToChildren");
static final QName PROP_EXECUTE_ASYNC = QName.createQName(RULE_MODEL_URI, "executeAsynchronously");
static final QName ASSOC_ACTION = QName.createQName(RULE_MODEL_URI, "action");
static final QName PROP_DISABLED = QName.createQName(RULE_MODEL_URI, "disabled");
static final QName ASPECT_RULES = QName.createQName(RULE_MODEL_URI, "rules");
static final QName ASSOC_RULE_FOLDER = QName.createQName(RULE_MODEL_URI, "ruleFolder");
static final QName ASPECT_IGNORE_INHERITED_RULES = QName.createQName(RULE_MODEL_URI, "ignoreInheritedRules");
}

View File

@@ -85,9 +85,7 @@ import org.springframework.util.StopWatch;
*/
public class RuleServiceCoverageTest extends TestCase
{
//private static final ContentData CONTENT_DATA_TEXT = new ContentData(null, MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, "UTF-8");
/**
/**
* Application context used during the test
*/
static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
@@ -343,6 +341,46 @@ public class RuleServiceCoverageTest extends TestCase
// System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
}
public void testDisableIndividualRules()
{
Map<String, Serializable> params = new HashMap<String, Serializable>(1);
params.put("aspect-name", ContentModel.ASPECT_CONFIGURABLE);
Rule rule = createRule(
RuleType.INBOUND,
AddFeaturesActionExecuter.NAME,
params,
NoConditionEvaluator.NAME,
null);
rule.setRuleDisabled(true);
this.ruleService.saveRule(this.nodeRef, rule);
NodeRef newNodeRef = this.nodeService.createNode(
this.nodeRef,
ContentModel.ASSOC_CHILDREN,
QName.createQName(TEST_NAMESPACE, "children"),
ContentModel.TYPE_CONTENT,
getContentProperties()).getChildRef();
addContentToNode(newNodeRef);
assertFalse(this.nodeService.hasAspect(newNodeRef, ContentModel.ASPECT_CONFIGURABLE));
Rule rule2 = this.ruleService.getRule(rule.getNodeRef());
rule2.setRuleDisabled(false);
this.ruleService.saveRule(this.nodeRef, rule2);
// Re-try the test now the rule has been re-enabled
NodeRef newNodeRef2 = this.nodeService.createNode(
this.nodeRef,
ContentModel.ASSOC_CHILDREN,
QName.createQName(TEST_NAMESPACE, "children"),
ContentModel.TYPE_CONTENT,
getContentProperties()).getChildRef();
addContentToNode(newNodeRef2);
assertTrue(this.nodeService.hasAspect(newNodeRef2, ContentModel.ASPECT_CONFIGURABLE));
}
public void testDisableRule()
{
this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_LOCKABLE, null);
@@ -1597,36 +1635,8 @@ public class RuleServiceCoverageTest extends TestCase
tx.commit();
Thread.sleep(10000);
//System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
//AuthenticationComponent authenticationComponent = (AuthenticationComponent)applicationContext.getBean("authenticationComponent");
//authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName());
// Check that the created node is still there
//List<ChildAssociationRef> origRefs = this.nodeService.getChildAssocs(
// this.nodeRef,
// RegexQNamePattern.MATCH_ALL, QName.createQName(TEST_NAMESPACE, "origional"));
//assertNotNull(origRefs);
//assertEquals(1, origRefs.size());
//NodeRef origNodeRef = origRefs.get(0).getChildRef();
//assertEquals(newNodeRef, origNodeRef);
// Check that the created node has been copied
//List<ChildAssociationRef> copyChildAssocRefs = this.nodeService.getChildAssocs(
// this.rootNodeRef,
// RegexQNamePattern.MATCH_ALL, QName.createQName(TEST_NAMESPACE, "transformed"));
//assertNotNull(copyChildAssocRefs);
//assertEquals(1, copyChildAssocRefs.size());
//NodeRef copyNodeRef = copyChildAssocRefs.get(0).getChildRef();
//assertTrue(this.nodeService.hasAspect(copyNodeRef, ContentModel.ASPECT_COPIEDFROM));
//NodeRef source = (NodeRef)this.nodeService.getProperty(copyNodeRef, ContentModel.PROP_COPY_REFERENCE);
//assertEquals(newNodeRef, source);
// Check the transformed content
//ContentData contentData = (ContentData) nodeService.getProperty(copyNodeRef, ContentModel.PROP_CONTENT);
//assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentData.getMimetype());
// Sleep to ensure work is done b4 execution is canceled
Thread.sleep(10000);
}
catch (Exception exception)
{

View File

@@ -40,7 +40,6 @@ import org.alfresco.service.cmr.rule.Rule;
import org.alfresco.service.cmr.rule.RuleService;
import org.alfresco.service.cmr.rule.RuleServiceException;
import org.alfresco.service.cmr.rule.RuleType;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.util.GUID;
@@ -88,11 +87,6 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
*/
private ActionService actionService;
/**
* The search service
*/
private SearchService searchService;
/**
* The dictionary service
*/
@@ -126,10 +120,15 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
private TransactionListener ruleTransactionListener = new RuleTransactionListener(this);
/**
* Indicates whether the rules are disabled for the curren thread
* Indicates whether the rules are disabled for the current thread
*/
private ThreadLocal<Boolean> rulesDisabled = new ThreadLocal<Boolean>();
/**
* Global flag that indicates whether the
*/
private boolean globalRulesDisabled = false;
/**
* Set the permission-safe node service
*
@@ -170,16 +169,6 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
this.runtimeActionService = runtimeActionService;
}
/**
* Set the search service
*
* @param searchService the search service
*/
public void setSearchService(SearchService searchService)
{
this.searchService = searchService;
}
/**
* Set the dictionary service
*
@@ -189,6 +178,16 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
{
this.dictionaryService = dictionaryService;
}
/**
* Set the global rules disabled flag
*
* @param rulesDisabled true to disable allr ules, false otherwise
*/
public void setRulesDisabled(boolean rulesDisabled)
{
this.globalRulesDisabled = rulesDisabled;
}
/**
* Gets the saved rule folder reference
@@ -253,7 +252,7 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
*/
public boolean isEnabled()
{
return (this.rulesDisabled.get() == null);
return (this.globalRulesDisabled == false && this.rulesDisabled.get() == null);
}
/**
@@ -331,7 +330,7 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
if (this.runtimeNodeService.exists(nodeRef) == true && checkNodeType(nodeRef) == true)
{
if (includeInherited == true)
if (includeInherited == true && this.nodeService.hasAspect(nodeRef, RuleModel.ASPECT_IGNORE_INHERITED_RULES) == false)
{
// Get any inherited rules
for (Rule rule : getInheritedRules(nodeRef, ruleTypeName, null))
@@ -441,58 +440,62 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
private List<Rule> getInheritedRules(NodeRef nodeRef, String ruleTypeName, Set<NodeRef> visitedNodeRefs)
{
List<Rule> inheritedRules = new ArrayList<Rule>();
// Create the visited nodes set if it has not already been created
if (visitedNodeRefs == null)
{
visitedNodeRefs = new HashSet<NodeRef>();
}
// This check prevents stack over flow when we have a cyclic node graph
if (visitedNodeRefs.contains(nodeRef) == false)
{
visitedNodeRefs.add(nodeRef);
List<Rule> allInheritedRules = new ArrayList<Rule>();
List<ChildAssociationRef> parents = this.runtimeNodeService.getParentAssocs(nodeRef);
for (ChildAssociationRef parent : parents)
{
List<Rule> rules = getRules(parent.getParentRef(), false);
for (Rule rule : rules)
{
// Add is we hanvn't already added and it should be applied to the children
if (rule.isAppliedToChildren() == true && allInheritedRules.contains(rule) == false)
{
allInheritedRules.add(rule);
}
}
for (Rule rule : getInheritedRules(parent.getParentRef(), ruleTypeName, visitedNodeRefs))
{
// Ensure that we don't get any rule duplication (don't use a set cos we want to preserve order)
if (allInheritedRules.contains(rule) == false)
{
allInheritedRules.add(rule);
}
}
}
if (ruleTypeName == null)
{
inheritedRules = allInheritedRules;
}
else
{
// Filter the rule list by rule type
for (Rule rule : allInheritedRules)
{
if (rule.getRuleTypes().contains(ruleTypeName) == true)
{
inheritedRules.add(rule);
}
}
}
}
if (this.nodeService.hasAspect(nodeRef, RuleModel.ASPECT_IGNORE_INHERITED_RULES) == false)
{
// Create the visited nodes set if it has not already been created
if (visitedNodeRefs == null)
{
visitedNodeRefs = new HashSet<NodeRef>();
}
// This check prevents stack over flow when we have a cyclic node graph
if (visitedNodeRefs.contains(nodeRef) == false)
{
visitedNodeRefs.add(nodeRef);
List<Rule> allInheritedRules = new ArrayList<Rule>();
List<ChildAssociationRef> parents = this.runtimeNodeService.getParentAssocs(nodeRef);
for (ChildAssociationRef parent : parents)
{
// Add the inherited rule first
for (Rule rule : getInheritedRules(parent.getParentRef(), ruleTypeName, visitedNodeRefs))
{
// Ensure that we don't get any rule duplication (don't use a set cos we want to preserve order)
if (allInheritedRules.contains(rule) == false)
{
allInheritedRules.add(rule);
}
}
List<Rule> rules = getRules(parent.getParentRef(), false);
for (Rule rule : rules)
{
// Add is we hanvn't already added and it should be applied to the children
if (rule.isAppliedToChildren() == true && allInheritedRules.contains(rule) == false)
{
allInheritedRules.add(rule);
}
}
}
if (ruleTypeName == null)
{
inheritedRules = allInheritedRules;
}
else
{
// Filter the rule list by rule type
for (Rule rule : allInheritedRules)
{
if (rule.getRuleTypes().contains(ruleTypeName) == true)
{
inheritedRules.add(rule);
}
}
}
}
}
return inheritedRules;
}
@@ -511,9 +514,6 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
// Create the rule
Rule rule = new Rule(ruleNodeRef);
// Set the owning node ref
//rule.setOwningNodeRef((NodeRef)props.get(RuleModel.PROP_OWNING_NODEREF));
// Set the title and description
rule.setTitle((String)props.get(ContentModel.PROP_TITLE));
rule.setDescription((String)props.get(ContentModel.PROP_DESCRIPTION));
@@ -538,6 +538,15 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
executeAsync = value2.booleanValue();
}
rule.setExecuteAsynchronously(executeAsync);
// Set the disabled value
boolean ruleDisabled = false;
Boolean value3 = (Boolean)props.get(RuleModel.PROP_DISABLED);
if (value3 != null)
{
ruleDisabled = value3.booleanValue();
}
rule.setRuleDisabled(ruleDisabled);
// Get the action node reference
List<ChildAssociationRef> actions = this.nodeService.getChildAssocs(ruleNodeRef, RuleModel.ASSOC_ACTION, RuleModel.ASSOC_ACTION);
@@ -597,6 +606,7 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
this.nodeService.setProperty(ruleNodeRef, RuleModel.PROP_RULE_TYPE, (Serializable)rule.getRuleTypes());
this.nodeService.setProperty(ruleNodeRef, RuleModel.PROP_APPLY_TO_CHILDREN, rule.isAppliedToChildren());
this.nodeService.setProperty(ruleNodeRef, RuleModel.PROP_EXECUTE_ASYNC, rule.getExecuteAsynchronously());
this.nodeService.setProperty(ruleNodeRef, RuleModel.PROP_DISABLED, rule.getRuleDisabled());
// Save the rule's action
saveAction(ruleNodeRef, rule);
@@ -607,6 +617,12 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
}
}
/**
* Save the action related to the rule.
*
* @param ruleNodeRef the node reference representing the rule
* @param rule the rule
*/
private void saveAction(NodeRef ruleNodeRef, Rule rule)
{
// Get the action definition from the rule
@@ -708,18 +724,18 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
public void addRulePendingExecution(NodeRef actionableNodeRef, NodeRef actionedUponNodeRef, Rule rule, boolean executeAtEnd)
{
// First check to see if the node has been disabled
if (this.rulesDisabled.get() == null &&
if (this.isEnabled() == true &&
this.disabledNodeRefs.contains(this.getOwningNodeRef(rule)) == false &&
this.disabledRules.contains(rule) == false)
{
PendingRuleData pendingRuleData = new PendingRuleData(actionableNodeRef, actionedUponNodeRef, rule, executeAtEnd);
Set<PendingRuleData> pendingRules =
(Set<PendingRuleData>) AlfrescoTransactionSupport.getResource(KEY_RULES_PENDING);
List<PendingRuleData> pendingRules =
(List<PendingRuleData>) AlfrescoTransactionSupport.getResource(KEY_RULES_PENDING);
if (pendingRules == null)
{
// bind pending rules to the current transaction
pendingRules = new HashSet<PendingRuleData>();
pendingRules = new ArrayList<PendingRuleData>();
AlfrescoTransactionSupport.bindResource(KEY_RULES_PENDING, pendingRules);
// bind the rule transaction listener
AlfrescoTransactionSupport.bindListener(this.ruleTransactionListener);
@@ -730,8 +746,11 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
}
}
// Prevent the same rule being executed more than once in the same transaction
pendingRules.add(pendingRuleData);
// Prevent the same rule being executed more than once in the same transaction
if (pendingRules.contains(pendingRuleData) == false)
{
pendingRules.add(pendingRuleData);
}
}
else
{
@@ -773,8 +792,8 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
private void executePendingRulesImpl(List<PendingRuleData> executeAtEndRules)
{
// get the transaction-local rules to execute
Set<PendingRuleData> pendingRules =
(Set<PendingRuleData>) AlfrescoTransactionSupport.getResource(KEY_RULES_PENDING);
List<PendingRuleData> pendingRules =
(List<PendingRuleData>) AlfrescoTransactionSupport.getResource(KEY_RULES_PENDING);
// only execute if there are rules present
if (pendingRules != null && !pendingRules.isEmpty())
{
@@ -815,29 +834,41 @@ public class RuleServiceImpl implements RuleService, RuntimeRuleService
if (executedRules == null || canExecuteRule(executedRules, actionedUponNodeRef, rule) == true)
{
Action action = rule.getAction();
if (action == null)
{
throw new RuleServiceException("Attempting to execute a rule that does not have a rule specified.");
}
// Evaluate the condition
if (this.actionService.evaluateAction(action, actionedUponNodeRef) == true)
{
// Add the rule to the executed rule list
// (do this before this is executed to prevent rules being added to the pending list)
executedRules.add(new ExecutedRuleData(actionedUponNodeRef, rule));
if (logger.isDebugEnabled() == true)
{
logger.debug(" ... Adding rule (" + rule.getTitle() + ") and nodeRef (" + actionedUponNodeRef.getId() + ") to executed list");
}
// Execute the rule
boolean executeAsync = rule.getExecuteAsynchronously();
this.actionService.executeAction(action, actionedUponNodeRef, true, executeAsync);
}
executeRule(rule, actionedUponNodeRef, executedRules);
}
}
/**
* @see org.alfresco.repo.rule.RuntimeRuleService#executeRule(org.alfresco.service.cmr.rule.Rule, org.alfresco.service.cmr.repository.NodeRef, java.util.Set)
*/
public void executeRule(Rule rule, NodeRef actionedUponNodeRef, Set<ExecutedRuleData> executedRules)
{
// Get the action associated with the rule
Action action = rule.getAction();
if (action == null)
{
throw new RuleServiceException("Attempting to execute a rule that does not have a rule specified.");
}
// Evaluate the condition
if (this.actionService.evaluateAction(action, actionedUponNodeRef) == true)
{
if (executedRules != null)
{
// Add the rule to the executed rule list
// (do this before this is executed to prevent rules being added to the pending list)
executedRules.add(new ExecutedRuleData(actionedUponNodeRef, rule));
if (logger.isDebugEnabled() == true)
{
logger.debug(" ... Adding rule (" + rule.getTitle() + ") and nodeRef (" + actionedUponNodeRef.getId() + ") to executed list");
}
}
// Execute the rule
boolean executeAsync = rule.getExecuteAsynchronously();
this.actionService.executeAction(action, actionedUponNodeRef, true, executeAsync);
}
}
/**
* Determines whether the rule can be executed

View File

@@ -148,6 +148,127 @@ public class RuleServiceImplTest extends BaseRuleTest
assertEquals(1, conditions.size());
}
/** Ensure the rules are retrieved in the correct order **/
public void testGetRulesOrder()
{
for (int index = 0; index < 10; index++)
{
Rule newRule = createTestRule(true, Integer.toString(index));
this.ruleService.saveRule(this.nodeRef, newRule);
}
// Check that they are all returned in the correct order
List<Rule> rules = this.ruleService.getRules(this.nodeRef);
int index = 0;
for (Rule rule : rules)
{
assertEquals(Integer.toString(index), rule.getTitle());
index++;
}
// Create a child node
NodeRef level1 = createNewNode(this.nodeRef);
for (int index2 = 10; index2 < 20; index2++)
{
Rule newRule = createTestRule(true, Integer.toString(index2));
this.ruleService.saveRule(level1, newRule);
}
// Check that they are all returned in the correct order
List<Rule> rules2 = this.ruleService.getRules(level1);
int index2 = 0;
for (Rule rule : rules2)
{
assertEquals(Integer.toString(index2), rule.getTitle());
index2++;
}
// Create a child node
NodeRef level2 = createNewNode(level1);
for (int index3 = 20; index3 < 30; index3++)
{
Rule newRule = createTestRule(true, Integer.toString(index3));
this.ruleService.saveRule(level2, newRule);
}
// Check that they are all returned in the correct order
List<Rule> rules3 = this.ruleService.getRules(level2);
int index3 = 0;
for (Rule rule : rules3)
{
//System.out.println(rule.getTitle());
assertEquals(Integer.toString(index3), rule.getTitle());
index3++;
}
// Update a couple of the rules
Rule rule1 = rules3.get(2);
rule1.setDescription("This has been changed");
this.ruleService.saveRule(this.nodeRef, rule1);
Rule rule2 = rules3.get(12);
rule2.setDescription("This has been changed");
this.ruleService.saveRule(level1, rule2);
Rule rule3 = rules3.get(22);
rule3.setDescription("This has been changed");
this.ruleService.saveRule(level2, rule3);
// Check that they are all returned in the correct order
List<Rule> rules4 = this.ruleService.getRules(level2);
int index4 = 0;
for (Rule rule : rules4)
{
assertEquals(Integer.toString(index4), rule.getTitle());
index4++;
}
}
public void testIgnoreInheritedRules()
{
// Create the nodes and rules
this.ruleService.saveRule(this.nodeRef, createTestRule(true, "rule1"));
this.ruleService.saveRule(this.nodeRef, createTestRule(false, "rule2"));
NodeRef nodeRef1 = createNewNode(this.nodeRef);
this.ruleService.saveRule(nodeRef1, createTestRule(true, "rule3"));
this.ruleService.saveRule(nodeRef1, createTestRule(false, "rule4"));
NodeRef nodeRef2 = createNewNode(nodeRef1);
this.ruleService.saveRule(nodeRef2, createTestRule(true, "rule5"));
this.ruleService.saveRule(nodeRef2, createTestRule(false, "rule6"));
// Apply the ignore aspect
this.nodeService.addAspect(nodeRef1, RuleModel.ASPECT_IGNORE_INHERITED_RULES, null);
// Get the rules
List<Rule> rules1 = this.ruleService.getRules(nodeRef2);
assertNotNull(rules1);
assertEquals(3, rules1.size());
assertEquals("rule3", rules1.get(0).getTitle());
assertEquals("rule5", rules1.get(1).getTitle());
assertEquals("rule6", rules1.get(2).getTitle());
// Apply the ignore aspect
this.nodeService.addAspect(nodeRef2, RuleModel.ASPECT_IGNORE_INHERITED_RULES, null);
// Get the rules
List<Rule> rules2 = this.ruleService.getRules(nodeRef2);
assertNotNull(rules2);
assertEquals(2, rules2.size());
assertEquals("rule5", rules2.get(0).getTitle());
assertEquals("rule6", rules2.get(1).getTitle());
// Remove the ignore aspect
this.nodeService.removeAspect(nodeRef1, RuleModel.ASPECT_IGNORE_INHERITED_RULES);
this.nodeService.removeAspect(nodeRef2, RuleModel.ASPECT_IGNORE_INHERITED_RULES);
// Get the rules
List<Rule> rules3 = this.ruleService.getRules(nodeRef2);
assertNotNull(rules3);
assertEquals(4, rules3.size());
assertEquals("rule1", rules3.get(0).getTitle());
assertEquals("rule3", rules3.get(1).getTitle());
assertEquals("rule5", rules3.get(2).getTitle());
assertEquals("rule6", rules3.get(3).getTitle());
}
/**
* Test disabling the rules
*/
@@ -167,13 +288,12 @@ public class RuleServiceImplTest extends BaseRuleTest
* @param parent the parent node
* @param isActionable indicates whether the node is actionable or not
*/
private NodeRef createNewNode(NodeRef parent, boolean isActionable)
private NodeRef createNewNode(NodeRef parent)
{
NodeRef newNodeRef = this.nodeService.createNode(parent,
return this.nodeService.createNode(parent,
ContentModel.ASSOC_CHILDREN,
QName.createQName("{test}testnode"),
ContentModel.TYPE_CONTAINER).getChildRef();
return newNodeRef;
ContentModel.TYPE_CONTAINER).getChildRef();
}
/**
@@ -184,21 +304,21 @@ public class RuleServiceImplTest extends BaseRuleTest
{
// Create the nodes and rules
NodeRef rootWithRules = createNewNode(this.rootNodeRef, true);
NodeRef rootWithRules = createNewNode(this.rootNodeRef);
Rule rule1 = createTestRule();
this.ruleService.saveRule(rootWithRules, rule1);
Rule rule2 = createTestRule(true);
this.ruleService.saveRule(rootWithRules, rule2);
NodeRef nonActionableChild = createNewNode(rootWithRules, false);
NodeRef nonActionableChild = createNewNode(rootWithRules);
NodeRef childWithRules = createNewNode(nonActionableChild, true);
NodeRef childWithRules = createNewNode(nonActionableChild);
Rule rule3 = createTestRule();
this.ruleService.saveRule(childWithRules, rule3);
Rule rule4 = createTestRule(true);
this.ruleService.saveRule(childWithRules, rule4);
NodeRef rootWithRules2 = createNewNode(this.rootNodeRef, true);
NodeRef rootWithRules2 = createNewNode(this.rootNodeRef);
this.nodeService.addChild(
rootWithRules2,
childWithRules,
@@ -211,7 +331,7 @@ public class RuleServiceImplTest extends BaseRuleTest
// Check that the rules are inherited in the correct way
List<? extends Rule> allRules = this.ruleService.getRules(childWithRules, true);
List<? extends Rule> allRules = this.ruleService.getRules(childWithRules);
assertNotNull(allRules);
assertEquals(4, allRules.size());
assertTrue(allRules.contains(rule2));
@@ -432,9 +552,9 @@ public class RuleServiceImplTest extends BaseRuleTest
public void testCyclicGraphWithInheritedRules()
throws Exception
{
NodeRef nodeRef1 = createNewNode(this.rootNodeRef, true);
NodeRef nodeRef2 = createNewNode(nodeRef1, true);
NodeRef nodeRef3 = createNewNode(nodeRef2, true);
NodeRef nodeRef1 = createNewNode(this.rootNodeRef);
NodeRef nodeRef2 = createNewNode(nodeRef1);
NodeRef nodeRef3 = createNewNode(nodeRef2);
try
{
this.nodeService.addChild(nodeRef3, nodeRef1, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}loop"));
@@ -480,10 +600,10 @@ public class RuleServiceImplTest extends BaseRuleTest
*/
public void testRuleDuplication()
{
NodeRef nodeRef1 = createNewNode(this.rootNodeRef, true);
NodeRef nodeRef2 = createNewNode(nodeRef1, true);
NodeRef nodeRef3 = createNewNode(nodeRef2, true);
NodeRef nodeRef4 = createNewNode(nodeRef1, true);
NodeRef nodeRef1 = createNewNode(this.rootNodeRef);
NodeRef nodeRef2 = createNewNode(nodeRef1);
NodeRef nodeRef3 = createNewNode(nodeRef2);
NodeRef nodeRef4 = createNewNode(nodeRef1);
this.nodeService.addChild(nodeRef4, nodeRef3, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}test"));
Rule rule1 = createTestRule(true);
@@ -527,7 +647,7 @@ public class RuleServiceImplTest extends BaseRuleTest
public void testCyclicAsyncRules() throws Exception
{
NodeRef nodeRef = createNewNode(this.rootNodeRef, true);
NodeRef nodeRef = createNewNode(this.rootNodeRef);
// Create the first rule

View File

@@ -21,7 +21,6 @@ import java.util.List;
import org.alfresco.i18n.I18NUtil;
import org.alfresco.repo.action.CommonResourceAbstractBase;
import org.alfresco.repo.rule.ruletrigger.RuleTrigger;
import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.rule.Rule;
import org.alfresco.service.cmr.rule.RuleService;
@@ -99,7 +98,7 @@ public class RuleTypeImpl extends CommonResourceAbstractBase implements RuleType
/**
* @see org.alfresco.service.cmr.rule.RuleType#triggerRuleType(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef)
*/
public void triggerRuleType(NodeRef nodeRef, NodeRef actionedUponNodeRef)
public void triggerRuleType(NodeRef nodeRef, NodeRef actionedUponNodeRef, boolean executeRuleImmediately)
{
if (this.ruleService.isEnabled() == true)
{
@@ -121,8 +120,20 @@ public class RuleTypeImpl extends CommonResourceAbstractBase implements RuleType
}
}
// Queue the rule to be executed at the end of the transaction (but still in the transaction)
((RuntimeRuleService)this.ruleService).addRulePendingExecution(nodeRef, actionedUponNodeRef, rule);
// Only queue if the rule is not disabled
if (rule.getRuleDisabled() == false)
{
if (executeRuleImmediately == false)
{
// Queue the rule to be executed at the end of the transaction (but still in the transaction)
((RuntimeRuleService)this.ruleService).addRulePendingExecution(nodeRef, actionedUponNodeRef, rule);
}
else
{
// Execute the rule now
((RuntimeRuleService)this.ruleService).executeRule(rule, actionedUponNodeRef, null);
}
}
}
}
else

View File

@@ -129,7 +129,7 @@ public class RuleTypeImplTest extends BaseSpringTest
}
@Override
public void triggerRuleType(NodeRef nodeRef, NodeRef actionedUponNodeRef)
public void triggerRuleType(NodeRef nodeRef, NodeRef actionedUponNodeRef, boolean executeRuleImmediately)
{
this.rulesTriggered = true;
}

View File

@@ -16,6 +16,9 @@
*/
package org.alfresco.repo.rule;
import java.util.Set;
import org.alfresco.repo.rule.RuleServiceImpl.ExecutedRuleData;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.rule.Rule;
import org.alfresco.service.cmr.rule.RuleType;
@@ -25,6 +28,8 @@ import org.alfresco.service.cmr.rule.RuleType;
*/
public interface RuntimeRuleService
{
void executeRule(Rule rule, NodeRef actionedUponNodeRef, Set<ExecutedRuleData> executedRules);
void addRulePendingExecution(NodeRef actionableNodeRef, NodeRef actionedUponNodeRef, Rule rule);
void addRulePendingExecution(NodeRef actionableNodeRef, NodeRef actionedUponNodeRef, Rule rule, boolean executeAtEnd);

View File

@@ -16,7 +16,6 @@
<namespace uri="http://www.alfresco.org/model/rule/1.0" prefix="rule"/>
</namespaces>
<types>
<type name="rule:rule">
@@ -36,6 +35,11 @@
<type>d:boolean</type>
<mandatory>true</mandatory>
</property>
<property name="rule:disabled">
<type>d:boolean</type>
<mandatory>true</mandatory>
<default>false</default>
</property>
</properties>
<associations>
<child-association name="rule:action">
@@ -68,6 +72,10 @@
</child-association>
</associations>
</aspect>
<aspect name="rule:ignoreInheritedRules">
<title>Ignore Inherited Rules</title>
</aspect>
</aspects>

View File

@@ -56,6 +56,12 @@ public abstract class RuleTriggerAbstractBase implements RuleTrigger
/** The dictionary service */
protected DictionaryService dictionaryService;
/**
* Indicates whether the rule should be executed immediately or at the end of the transaction.
* By default this is false as all rules are executed at the end of the transaction.
*/
protected boolean executeRuleImmediately = false;
/**
* Set the policy component
*
@@ -96,6 +102,17 @@ public abstract class RuleTriggerAbstractBase implements RuleTrigger
this.dictionaryService = dictionaryService;
}
/**
* Sets the values that indicates whether the rule should be executed immediately
* or not.
*
* @param executeRuleImmediately true execute the rule immediaely, false otherwise
*/
public void setExecuteRuleImmediately(boolean executeRuleImmediately)
{
this.executeRuleImmediately = executeRuleImmediately;
}
/**
* Registration of an interested rule type
*/
@@ -117,7 +134,7 @@ public abstract class RuleTriggerAbstractBase implements RuleTrigger
{
for (RuleType ruleType : this.ruleTypes)
{
ruleType.triggerRuleType(nodeRef, actionedUponNodeRef);
ruleType.triggerRuleType(nodeRef, actionedUponNodeRef, this.executeRuleImmediately);
}
}
}

View File

@@ -35,7 +35,6 @@ public class RuleTriggerTest extends BaseSpringTest
{
private static final String ON_CREATE_NODE_TRIGGER = "on-create-node-trigger";
private static final String ON_UPDATE_NODE_TRIGGER = "on-update-node-trigger";
private static final String ON_DELETE_NODE_TRIGGER = "on-delete-node-trigger";
private static final String ON_CREATE_CHILD_ASSOCIATION_TRIGGER = "on-create-child-association-trigger";
private static final String ON_DELETE_CHILD_ASSOCIATION_TRIGGER = "on-delete-child-association-trigger";
private static final String ON_CREATE_ASSOCIATION_TRIGGER = "on-create-association-trigger";
@@ -287,7 +286,7 @@ public class RuleTriggerTest extends BaseSpringTest
return "displayLabel";
}
public void triggerRuleType(NodeRef nodeRef, NodeRef actionedUponNodeRef)
public void triggerRuleType(NodeRef nodeRef, NodeRef actionedUponNodeRef, boolean executeRuleImmediately)
{
// Indicate that the rules have been triggered
this.rulesTriggered = true;