Skip to content

Instantly share code, notes, and snippets.

@jverweijL
Created September 29, 2017 12:26
Show Gist options
  • Save jverweijL/09c20f69c763c1681dfb3a1084b4c94f to your computer and use it in GitHub Desktop.
Save jverweijL/09c20f69c763c1681dfb3a1084b4c94f to your computer and use it in GitHub Desktop.
This is a workflow xml example for Liferay DXP (7.0) to sent form data through a workflow to an external rest service.
<?xml version="1.0"?>
<!-- this workflow will sent data from a simple form (subject and description field) to an external rest service -->
<workflow-definition
xmlns="urn:liferay.com:liferay-workflow_7.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:liferay.com:liferay-workflow_7.0.0 http://www.liferay.com/dtd/liferay-workflow-definition_7_0_0.xsd">
<name>Logger</name>
<description></description>
<version>1</version>
<state>
<name>Start</name>
<metadata>
<![CDATA[{"xy":[100,40]}]]>
</metadata>
<actions>
<action>
<name>Log</name>
<description></description>
<script>
<![CDATA[
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.dynamic.data.lists.model.DDLRecord;
import com.liferay.dynamic.data.lists.service.DDLRecordLocalServiceUtil;
import com.liferay.dynamic.data.lists.service.DDLRecordVersionLocalServiceUtil
import com.liferay.dynamic.data.lists.model.DDLRecordVersion;
import com.liferay.dynamic.data.mapping.storage.DDMFormFieldValue;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.io.DataOutputStream;
import java.util.List;
import java.util.Locale;
long companyId = GetterUtil.getLong((String) workflowContext.get(WorkflowConstants.CONTEXT_COMPANY_ID));
System.out.println("companyID: " + companyId);
ServiceContext serviceContext = (ServiceContext) workflowContext.get(WorkflowConstants.CONTEXT_SERVICE_CONTEXT);
long classPK = GetterUtil.getLong((String)workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
System.out.println("original classPK: " + classPK);
DDLRecordVersion ddlRecordVersion = DDLRecordVersionLocalServiceUtil.getRecordVersion(classPK);
DDLRecord ddlRecord = DDLRecordLocalServiceUtil.getRecord(ddlRecordVersion.getRecordId());
Locale locale = new Locale("en", "US");
List<DDMFormFieldValue> values;
values = ddlRecord.getDDMFormFieldValues("Subject");
String subject = "";
for(DDMFormFieldValue value : values){
subject = value.getValue().getString(locale);
System.out.println("subject: " + subject);
}
values = ddlRecord.getDDMFormFieldValues("Description");
String description = "";
for(DDMFormFieldValue value : values){
description = value.getValue().getString(locale);
System.out.println("description: " + description);
}
// checkout http://mockbin.org/bin/14f67c2e-66d3-47dd-b715-083d7b7d6d57/log for actual request
URL obj = new URL("http://mockbin.org/bin/14f67c2e-66d3-47dd-b715-083d7b7d6d57");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "LiferayDXP");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "";
urlParameters += "subject=" + URLEncoder.encode(subject,"utf-8");
urlParameters += "&description=" + URLEncoder.encode(description,"utf-8");
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("Responsecode: " + responseCode);
]]>
</script>
<script-language>groovy</script-language>
<execution-type>onEntry</execution-type>
</action>
<notification>
<name>Notifier</name>
<description></description>
<template>
<![CDATA[Hi, you just received a message from the workflow]]>
</template>
<template-language>text</template-language>
<notification-type>user-notification</notification-type>
<recipients>
<user/>
</recipients>
<execution-type>onEntry</execution-type>
</notification>
</actions>
<initial>true</initial>
<transitions>
<transition>
<name>connector135430</name>
<target>End</target>
<default>true</default>
</transition>
</transitions>
</state>
<state>
<name>End</name>
<metadata>
<![CDATA[{"terminal":true,"xy":[100,500]}]]>
</metadata>
<actions>
<action>
<name>Approve</name>
<description>Approve</description>
<script>
<![CDATA[com.liferay.portal.kernel.workflow.WorkflowStatusManagerUtil.updateStatus(com.liferay.portal.kernel.workflow.WorkflowConstants.getLabelStatus("approved"), workflowContext);]]>
</script>
<script-language>groovy</script-language>
<execution-type>onEntry</execution-type>
</action>
</actions>
</state>
</workflow-definition>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment