Creating rules programaticaly


Nowadays I’ve been working on policies and triggering a behavior  based on user action, so I wanted to write about it, but I found more interesting stuff I did with it than triggering itself (which is by the way well described by Jeff Potts). I wanted a routine, which runs on every node created and adds a rule, if that node is a folder.

At first I did some research on rules. How are rules bound to nodes? We’re working on repository, so it should be a node. When you open a node browser and look on node with rule bound, you’ll see ‚ruleFolder‘ child of that node of ruleFolder type. Children of this node are rules itself – one child, one rule. Inside each are actions defined with conditions and actions with their parameters, like on following structure:

RuleFolder structure
RuleFolder structure

Conditions may be structured too – if condition is not „no-condition“ – there may be much complicated structure: either condition itself (actioncondition node type) with parameters as children, or composite-condition (compositeactioncondition node type) with conditions nested inside. Each condition has „invert“ boolean variable, which inverts condition meaning. Properties of simpliest condition (no-condition) are on following image:

Condition properties
Condition properties

Parameters are another types of node – actionparameter. Each has two important properties: parameterName and parameterValue and those values depends on action (or condition) on which is this parameter bind to. Example of parameter for copy-to-webproject action is on following image:

Parameter properties
Parameter properties

And of course third node type – action. With or without parameters. In both cases with properties like definitionName, executeAsynchronously, etc. see following image:

Action properties
Action properties

Examples on how to set rule on node are in nodeRef cookbook. I worked with CopyToWebProject action, which can be with parameters defined like this:

[code lang=“java“]
Action myAction = actionService.createAction(CopyToWebProjectActionExecuter.NAME);
Map<String, Serializable> actionProps = myAction.getParameterValues();
actionProps.put(CopyToWebProjectActionExecuter.PARAM_DESTINATION_FOLDER, avmRef);
myAction.setParameterValues(actionProps);
[/code]

Note static variables with parameter names (prefix PARAM_). Executers are in org.alfresco.repo.action.executer package, or it’s possible to use own action executer (extending ActionExecuterAbstractBase).

Next step is to add a condition, in my case HasAspectEvaluator like this:

[code lang=“java“]
ActionCondition condition = actionService.createActionCondition(HasAspectEvaluator.NAME);
Map<String, Serializable> conditionProps = new HashMap<String, Serializable>();
conditionProps.put(HasAspectEvaluator.PARAM_ASPECT, ContentModel.ASPECT_WORKING_COPY);
condition.setParameterValues(conditionProps);
condition.setInvertCondition(true);
[/code]

Evaluators (package org.alfresco.repo.action.evaluator) also have parameter names defined with prefix PARAM_. Previous code creates condition testing aspect is NOT set on node (note setInvertCondition() method) – this condition is for testing copied content is not working copy.

Created condition and action should be placed in CompositeAction:

[code lang=“java“]
CompositeAction compositeAction = actionService.createCompositeAction();
compositeAction.addActionCondition(condition);
compositeAction.addAction(myAction);
[/code]

And the final step is to add this whole composition inside created rule and use ruleService to bind created rule to node – through its nodeRef:

[code lang=“java“]
rule.setAction(compositeAction);
ruleService.saveRule(sourceRef, rule);
[/code]

This is enough and it should work. What am I using it for? I need CopyToWebProject rule to foolow tree structure in DM, not just copy everything in subdirs to given folder. So I have my own implementation of OnCreateNodePolicy, which binds this rule to every created folder (under some conditions of course).


Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *