cancel
Showing results for 
Search instead for 
Did you mean: 

Creating custom rule with Java code

amitev
Champ in-the-making
Champ in-the-making
Hi all! I tried the following in my environment   


Action action = actionService.createAction(AddAspectActionExecuter.NAME);

Rule rule = new Rule();
rule.setTitle("my Rule");
rule.setAction(action);
ruleService.saveRule(nodeRef, rule);

and later i try to get the rule to assert that it exists with the following code:


Rule rule = ruleService.getRule(nodeRef); //the nodeRef instance is the same used for rule save

When getRule() is called i get the following exception:


org.alfresco.service.cmr.rule.RuleServiceException: 02100001 Rule exists without a specified action
   at org.alfresco.repo.rule.RuleServiceImpl.getRule(RuleServiceImpl.java:702)
   at …..

Idea what I am doing wrong?
4 REPLIES 4

vincy
Champ in-the-making
Champ in-the-making
hi,
  I am trying the same…Can u explain me clearly what are steps u have done

mikemars
Champ in-the-making
Champ in-the-making
Hi,

Here is some sample code to add a rule to a node. In this case the rule will add the Verisonable aspect to all nodes of type Content for a particular space.


  Rule rule = new Rule();
  rule.setRuleType(RuleType.INBOUND);
  rule.setTitle("Your Rule Title");
  rule.applyToChildren(false); // set this to true if you want to cascade to sub folders
      
  CompositeAction compositeAction = actionService.createCompositeAction();
  rule.setAction(compositeAction);
      
  ActionCondition actionCondition = actionService.createActionCondition(IsSubTypeEvaluator.NAME);
      
  Map<String, Serializable> conditionParameters = new HashMap<String, Serializable>(1);
  conditionParameters.put(IsSubTypeEvaluator.PARAM_TYPE, ContentModel.TYPE_CONTENT); // setting subtypes to CONTENT
  actionCondition.setParameterValues(conditionParameters);
            
  compositeAction.addActionCondition(actionCondition);
      
  Action action = actionService.createAction(AddFeaturesActionExecuter.NAME);        // The Add Aspect Action           
  action.setTitle("Your action title");
  action.setExecuteAsynchronously(false);
      
  Map<String, Serializable> ruleParameters = new HashMap<String, Serializable>(1);
  ruleParameters.put(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE); // The Aspect to add      
  action.setParameterValues(ruleParameters);
      
  compositeAction.addAction(action);
            
  ruleService.saveRule(nodeRef, rule); // Save the rule to your nodeRef

Hope that helps.

sjeandroz
Champ in-the-making
Champ in-the-making
Hi,

I want to create a rule on a node,  from a java backed web script…

I tried your code but the objects "actionService" and "ruleService" are not defined

Could you help me please, I am a noob on alfresco's java developpment..

Thanks and sorry for my bad level in english…

tfrith
Champ on-the-rise
Champ on-the-rise
sjeandroz,

I wrote some code to create rules based on json descriptors.

Take a look at my blog post and see if it helps:
http://abstractive.ca/home/blog/~/blog/tim.frith/alfresco_dynamic_content_rules

Tim