rules Jahia 7.3 Jahia 8

Execute an action when visibility changes

Question

In Jahia, a Visibility Condition can be set to schedule when content will be visible. How can we execute a custom action when the visibility starts/ends?

Answer

Jahia uses Drools to check for the visibility conditions. To execute a custom code when the visibility changes, a custom module can be created with the following:

1) The Drools rule

Below is an example rule to check for the End Date property


rule "Execute an action at end date visibility condition"
    salience 50
    when
        A property end has been set on a node
             - the node has the type jnt:startEndDateCondition
    then
        Log "Action for End visibility set"
        Execute the action "visibilityEndAction" at end on the node
end


// Handle the case if the End Date property is removed

rule "Cancel customAction on end date visibility" salience 25 when A property end has been removed from a node - the node has the type jnt:startEndDateCondition then Log "Action for End visibility canceld" Cancel execution of action "visibilityEndAction" on the node end  

2) The custom action code

You can then create a custom action that will execute on the rule above:


public class VisibilityEndAction extends BaseBackgroundAction {
 private static Logger logger = LoggerFactory.getLogger(VisibilityEndAction.class);
 @Override
 public void executeBackgroundAction(JCRNodeWrapper node) {
          
  // Executing a customer action
             
    }  
}

Related links