Blog Post

...

Alfresco custom rules and actions in java

Post is dedicated to the task of rules and actions development in Alfresco Java. As an example is taken some production case which demonstrates how to implement this task.

Production case: given some amount of sites in Alfresco, one of them concerned to be centralized. “Centralized” means that any folder or content (file) that is created within document library of Centralized site should be copied to all other sites preserving the same folder and content hierarchy. To resolve this issue first of all an Alfresco rule should be created. This rule will be bounded to the document library (of Centralized site) and will trigger the copy Alfresco action when new content created within this library.

So what is the Alfresco rule? Alfresco rule is a some definition that invokes an action upon determined node event (create, update, etc) when all conditions are met. For example, let’s create a Rule for document library of a site. In the beginning we need to get a node to bind the rule to. This node will be a document library which is a core node for content storage of every alfresco site:

import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.service.cmr.repository.NodeService;

private static final String _DOC_LIB = "documentLibrary";

private static NodeService nodeService;
private static SiteService siteService;	

public void go(NodeRef siteRef) {
	NodeRef docLibRef = createOrGetDocLib(siteRef);
	createRule(docLibRef);
}

private static NodeRef createOrGetDocLib(NodeRef siteRef) {
	String shortName = (String) nodeService.getProperty(siteRef, ContentModel.PROP_NAME);
	NodeRef docLibRef = null;
	if (!siteService.hasContainer(shortName, _DOC_LIB)) {
		docLibRef = siteService.createContainer(shortName, _DOC_LIB, null, null);
	} else {
		docLibRef = siteService.getContainer(shortName, _DOC_LIB);
	}
	return docLibRef;
}

 

In the code above should be mentioned that nodeService and siteService are Alfresco spring beans that should be defined as properties in the spring context file of our application (setters in this code are omitted).

Rule creation looks as following, method names speak for themselves:

import org.alfresco.service.cmr.rule.Rule;
import org.alfresco.service.cmr.rule.RuleService;
import org.alfresco.service.cmr.rule.RuleType;

private void createRule(NodeRef parent) {
	  Rule rule = new Rule();
	  // rule type RuleType.INBOUND to invoke rule on node create events
	  rule.setRuleType(RuleType.INBOUND);
	  rule.applyToChildren(true);
	  String title = "Copy content on creation";
	  rule.setTitle(title);
	  rule.setAction(getCompositeAction());
	  ruleService.saveRule(parent, rule); // Bind the rule to the docLib	
}

 

There is left to create the action. The Alfresco documentation says:

An action is a unit of work that can be performed against a node. It is configured through a content rule. For example, moving a node, copying a node, checking a node in, transfoming the contents of a node, etc.

Action’s code:

import org.alfresco.service.cmr.action.CompositeAction;
import org.alfresco.repo.action.evaluator.IsSubTypeEvaluator;
import org.alfresco.model.ContentModel;

private static ActionService actionService; 

private CompositeAction getCompositeAction() {
	  CompositeAction compositeAction = actionService.createCompositeAction();
	  ActionCondition actionCondition = actionService.createActionCondition(IsSubTypeEvaluator.NAME); 
	  Map<String, Serializable> conditionParameters = new HashMap<String, Serializable>(1);
	  conditionParameters.put(IsSubTypeEvaluator.PARAM_TYPE, ContentModel.TYPE_CMOBJECT);
	  actionCondition.setParameterValues(conditionParameters);
	  compositeAction.addActionCondition(actionCondition);		  
	  Action action = actionService.createAction("copycontent");	             
	  action.setExecuteAsynchronously(false);
	  String title = "Copy content on creation";
	  action.setTitle(title);	
         return action;
}

 

In the code above should be noticed:

  • The condition for the action is defined. Here involved Alfresco evaluator to check whether the type of the node is a subtype of cm_object. ContentModel.TYPE_FOLDER and ContentModel.TYPE_CONTENT are subtypes of this type, in this way the action is triggered for the nodes of these types only.
  • createAction(String name) method used to extract action spring bean by its id (“copycontent”). An example of action bean will be given in the following post of our blog.

 

That’s it. As a result we’ve created an Alfresco content rule that executed upon node creation (condition == subtype cm_object) within document library of a site.

Comments (2)

Tags: alfresco


Comments:

...

Andi Aug 17, 2015 at 13:03 #

Thanks, useful post!

...

Stanislav Mar 30, 2017 at 19:57 #

Thanks Andi!

Leave a Comment