Created
August 18, 2015 01:55
-
-
Save bmvakili/28ac598594e6e1678fb7 to your computer and use it in GitHub Desktop.
Groovy script to get Assignments element of a Kaleo Workflow definition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.liferay.portal.util.*; | |
import com.liferay.portal.service.*; | |
import com.liferay.portal.model.*; | |
import com.liferay.portal.kernel.util.*; | |
import com.liferay.portal.kernel.workflow.*; | |
import javax.xml.parsers.*; | |
import javax.xml.xpath.*; | |
import java.io.*; | |
import org.xml.sax.*; | |
import org.w3c.dom.*; | |
// variables used in this groovy script | |
WORKFLOW_ASSIGNMENTS_XPATH = "//assignments/*"; | |
WORKFLOW_DEFINITION_NAME = "Single Approver"; | |
WORKFLOW_DEFINITION_VERSION = 1; | |
def getWorkflowDefinition() { | |
long companyId = PortalUtil.getDefaultCompanyId(); | |
String name = WORKFLOW_DEFINITION_NAME; | |
int version = WORKFLOW_DEFINITION_VERSION; | |
return WorkflowDefinitionManagerUtil.getWorkflowDefinition( companyId, name, version ); | |
} | |
def parseXMLToDOM(workflowDefinitionContent) { | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
return builder.parse(new InputSource(new StringReader(workflowDefinitionContent))); | |
} | |
def getWorkflowAssignmentsAsNodeList(doc) { | |
XPathFactory xPathfactory = XPathFactory.newInstance(); | |
XPath xpath = xPathfactory.newXPath(); | |
XPathExpression expr = xpath.compile(WORKFLOW_ASSIGNMENTS_XPATH); | |
return expr.evaluate(doc, XPathConstants.NODESET); | |
} | |
def getAssignments() { | |
WorkflowDefinition workflowDefinition = getWorkflowDefinition(); | |
String workflowDefinitionContent = workflowDefinition.getContent(); | |
Document doc = parseXMLToDOM(workflowDefinitionContent); | |
return getWorkflowAssignmentsAsNodeList(doc); | |
} | |
try { | |
NodeList nodeList = getAssignments(); | |
for (int i = 0; i < nodeList.getLength(); i++) { | |
Node node = nodeList.item(i); | |
//@TODO: Do something with the item | |
out.println("Assignment: " + node.getNodeName()); | |
} | |
} catch (Exception e) { | |
out.println(e.getMessage()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment