Drools rule session and node has empty language
Question
Drools rule session and node has empty language
When you use a drools rule which uses a multilingual field, it is possible, that the language is empty.
rule "Check title after node creatio1n"
when
Not in operation import
A new node is created
- the node has the type jnt:page
then
Log "**************************** Checking if title exists ****************************"
Log "hasProperty('jcr:title'): " + node.getNode().hasProperty("jcr:title") + " " + node.getNode().getPath() + " Lang: " + node.getLanguage()
end
In this case, the node.getNode().hasProperty("jcr:title") always returns false because the language is not set well.
Solution
In Jahia 8, the way that the contents are saved is not the same than in Jahia 7 which leads to use a session without locale in the rules.
For the moment, to solve the issue it's possible to use some Java code to access to the properties directly in the rules.
As an example, the following rule can be used to copy the jcr:title property to the description of the node when a page is created.
package org.jahia.qa.qamodule.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
import org.jahia.services.content.JCRTemplate
import java.util.Locale
import javax.jcr.RepositoryException
import org.jahia.services.content.JCRCallback
import org.jahia.services.content.JCRSessionWrapper
expander rules.dsl
#declare any global variables here
global User user
global Service service
global Logger logger
global JCRContentUtils jcrUtils
rule "Copy title to description after page creation"
when
Not in operation import
A new node is created
- the node has the type jnt:page
then
> final String currentNodePath = node.getNode().getPath();
> try{ JCRTemplate.getInstance().doExecuteWithSystemSessionAsUser(user.getJahiaUser(), null, Locale.ENGLISH, new JCRCallback<Object>() {
> @Override
> public Object doInJCR(JCRSessionWrapper session) throws RepositoryException {
> JCRNodeWrapper currentNode = session.getNode(currentNodePath);
> currentNode.setProperty("jcr:description", currentNode.getPropertyAsString("jcr:title"));
> session.save();
> return null; }});
> } catch(RepositoryException e){
> logger.error("error:", e);
> }
end