[JAVA] How to call custom or standard services from Liferay 7 / DXP-Workflow / Workflow Script

Liferay7 / DXP --How to call custom services / standard services from Workflow / Workflow Script

One of Liferay's powerful features is workflow. Workflow can be set graphically using Kaleo Designer on the management screen, but in actual cases, not only standard functions but also scripts (Groovy, Javascript) are used to dynamically approve approvers. Often makes selections and notifications.

In doing so, I often use Liferay's built-in services or custom services I developed, but the standard service call on dev.liferay.com still uses the old 6.2 LocalServiceUtil, which is wrong. I tried to create a sample below.

The architecture has moved from Liferay7 / DXP to OSGi, and the service calling method has changed since 6.2. It takes the form of inquiring about the OSGi framework and fetching the necessary services.

When calling a service from within Java code normally, use the @Reference annotation as you can see by referring to the coding of the services in the Liferay core in https://github.com/liferay/liferay-portal. do it

How to get service in Java code (extracted from liferay-portal code)

WSRPAdminPortlet.java


@Reference(unbind = "-")
protected void setWSRPConsumerPortletLocalService(
    WSRPConsumerPortletLocalService wSRPConsumerPortletLocalService) {

    _wSRPConsumerPortletLocalService = wSRPConsumerPortletLocalService;
}

private static WSRPConsumerPortletLocalService
    _wSRPConsumerPortletLocalService;

It can be written as follows, but in tags such as scripted-assignment in Workflow, it is described as follows.

How to get and use services in Script page / Workflow

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker
import com.liferay.portal.scripting.groovy.internal.GroovyExecutor


def st = null
try {
    Bundle bundle = FrameworkUtil.getBundle(GroovyExecutor.class);

    st = new ServiceTracker(bundle.getBundleContext(), "com.liferay.journal.service.JournalArticleLocalService", null);

    st.open();
    if (!st.isEmpty()) {
        def service = st.getService();
        def articles = service.getArticles();
        for (def article: articles) {
            System.out.println("article Name:${article.getUrlTitle()}");
        }
    }
} catch(Exception e) {
    System.out.println("Handle error here appropriately")
    e.printStackTrace()
} finally {
    if (st != null) {
        st.close()
    }
}

This works even if it is described in the Workflow definition, but if you want to try it easily, after creating some web content, select Groovy from Control Panel-> Configuration-> Server Administration-> Script. If you ask and paste the above code in the Script field, the title of the created Web content will be displayed.

According to OSGi's method, specify the starting bundle with FrameworkUtil.getBundle (in this case, since Groovy Script engine is used, specify GroovyExecutor.class) Next, get the target service with ServiceTracker I'm doing it.

Recommended Posts

How to call custom or standard services from Liferay 7 / DXP-Workflow / Workflow Script
How to call Swift 5.3 code from Objective-C
[ruby] How to receive values from standard input?
How to output standard from an array with forEach
How to call AmazonSQSAsync
Investigated how to call services with Watson SDK for Java
How to migrate from JUnit4 to JUnit5
How to open a script file from Ubuntu with VS code