Rules

December 21, 2021

Jahia is designed to enhance the productivity of all content authors working on the platform. Some repetitive tasks can be easily automated by using the rule engine.

Jahia embeds Drools, a Java-based rule engine that executes actions(called consequences) when a set of conditions are met. .Rules can be triggered whenever content is modified in Jahia (content creation, update and deletion) and are designed to make the use of the conditions and consequences straightforward to use.

Before you begin

Jahia modules rely on Java and Maven. The key requirements are as follows:

  • Access to a local Jahia system or the free cloud trial from Jahia
  • Oracle JDK 11 or OpenJDK 11
  • Maven 3.3+
  • Completion of the following tutorials:
  • The IDE of your choice
  • Access to the Jahia administration console

What you will learn

This tutorial will guide you through creating your first Drools rule:

  • Apply taxonomy improvements when creating content
  • Automatically grant a role to a user on new content
  • Move new content somewhere else
  • Automatically populate fields

Deploy a new rule

Rules are deployed in a Jahia module, in a file named rules.drl. The path inside a module is always the same: src/main/resources/META-INF/rules.drl

By default, the file is named rules.drl.disabled and needs the disabled part removed.

The structure of this file is composed of three sections: the imports, the variable declaration, and the rules. The base file file (without rules) looks like this:

 

package org.jahia.modules.<moduleName>.rules

#list any import classes here.
import org.jahia.services.content.rules.*
import org.jahia.services.content.JCRContentUtils
import org.slf4j.Logger
import org.jahia.services.content.JCRNodeWrapper

expander rules.dsl

#declare any global variables here
global User user
global Service service
global Logger logger
global JCRContentUtils jcrUtils

Rules are deployed to Jahia once the module they are embedded in is deployed on the environment. The module needs to be activated on the site where its rules must apply.

Rule creation practice

Apply taxonomy tags when creating content

Taxonomies helps classify content and are becoming key to most digital strategies. Most people won't browse a site's navigation tree to fetch their content; Instead, they will rely on search, facets or content recommendations to access the digital information they are looking for.

A Taxonomy can be applied via multiple means: auto tagging based on some text occurrence, adding mixins, or even machine learning algorithms using image or text recognition to add categories to a content.

The following example applies a mixin to a content. Mixins are very useful to query contents of a specific type across the Jahia platform.

rule "New health-related article"
    salience 100
    when
        A new node is created
            - it has the type jnt:article
 The node has a parent
            - the parent has the type mix:healthPage

    then
        Add the type mix:healthArticle
end

Automatically grant a role to a user on a new content

Roles are inherited from parent contents in the content hierarchy. Sometimes, we want a specific group of users to have the rights to edit a specific type of content, no matter where this content was created. The following rule helps solve this challenge.

rule "Let all editorial authors edit the news"
    salience 100
    when
        A new node is created
            - it has the type jnt:news
    then
        Grant role editorial-author on the node to the group authors
end

Automatically publish nodes of a certain type

Content publication is the validation process to make sure information is reviewed before being pushed to the live site. In some cases, you might want to publish modifications right away. The following rule does just that.

rule "Automatically publish images once uploaded"
    salience 100
  when
    A new node is created
        - the node has the type jnt:file
        - the node has the type jmix:image
  then
      Publish the node
end

Going further

Jahia embeds hundreds of prepackaged conditions and consequences, and you can even create your own,  some conditions and consequences are stored in a .dsl file. The prepackaged rules of Jahia are available here. you can also use Drools to execute Java code in the 'when' and 'then' sections of a .drl file:

rule "Execute Java logic"
  when
    A property jcr:title has been set on a node
        - the node has the type jnt:article
  then
    > log.info ("The title of the article {} has been updated", node.getProperty("jcr:title"));
end

 

Congratulations!  You have created an automated rule!  To learn more  about how rules work in Jahia visit the academy.