Friday, November 26, 2010

Human Workflow Java API Part 2

Continuing on from the previous post...

The payload I tested with is as follows -



Now I want to access the custName value in the payload.

Here's the code snippet(thanks to my colleaue Mireille) -

Element el= task.getPayloadAsElement();
String custName = null;
custName = getElementValue(el, "custName");
if(el != null){
System.out.println("Payload customer name = " + custName);


}
else{
System.out.println("Payload is null");
}

public static String getElementValue(Element pElement, String pElementName){
String value=null;
NodeList myNodeList = pElement.getElementsByTagName(pElementName);
Element myElement = (Element)myNodeList.item(0);
NodeList myChildNodeList = myElement.getChildNodes();
for (int i=0; i {
value = ((Node)myChildNodeList.item( i )).getNodeValue().trim();
if( value.equals("") value.equals("\r") ){
continue;
}
else{
break;
}
}
return value;
}

Testing...




The complete Java class is as follows -

package com.niall.hwclient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import java.util.Map;

import oracle.bpel.services.workflow.client.IWorkflowServiceClient;
import oracle.bpel.services.workflow.client.IWorkflowServiceClientConstants;
import oracle.bpel.services.workflow.client.WorkflowServiceClientFactory;
import oracle.bpel.services.workflow.query.ITaskQueryService;
import oracle.bpel.services.workflow.task.ITaskService;
import oracle.bpel.services.workflow.task.model.Task;
import oracle.bpel.services.workflow.verification.IWorkflowContext;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class HwTask {
public HwTask() {
super();
}

public String getTaskdetails(String user){

Map properties = new HashMap();
properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_PROVIDER_URL, "t3://localhost:8001");
properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_SECURITY_CREDENTIALS, "welcome1");
properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_SECURITY_PRINCIPAL, "weblogic");

try
{
//Create JAVA WorflowServiceClient
IWorkflowServiceClient wfSvcClient =
WorkflowServiceClientFactory.getWorkflowServiceClient(WorkflowServiceClientFactory.REMOTE_CLIENT, properties, null);

//Get the task query service
ITaskQueryService querySvc = wfSvcClient.getTaskQueryService();
//Login as jcooper
IWorkflowContext ctx = querySvc.authenticate("cdoyle","welcome1".toCharArray(),null);
//Set up list of columns to query
List queryColumns = new ArrayList();
queryColumns.add("TASKID");
queryColumns.add("TASKNUMBER");
queryColumns.add("TITLE");
queryColumns.add("OUTCOME");

// also get the payload
List optionalInfo = new ArrayList();
optionalInfo.add(ITaskQueryService.OptionalInfo.PAYLOAD);
//
//Query a list of tasks assigned to jcooper
List tasks = querySvc.queryTasks(ctx,
queryColumns,
optionalInfo, // Payload
ITaskQueryService.AssignmentFilter.MY,
null, //No keywords
null, //No custom predicate
null, //No special ordering
0, //Do not page the query result
0);
//Get the task service
ITaskService taskSvc = wfSvcClient.getTaskService();
//Loop over the tasks, outputting task information, and approving any
//tasks whose outcome has not been set...

for(int i = 0 ; i < tasks.size() ; i ++)
{
System.out.println("In task loop...");
Task task = (Task)tasks.get(i);
int taskNumber = task.getSystemAttributes().getTaskNumber();
String title = task.getTitle();
String taskId = task.getSystemAttributes().getTaskId();
String outcome = task.getSystemAttributes().getOutcome();

// Get the payload
Element el= task.getPayloadAsElement();
String custName = null;
custName = getElementValue(el, "custName");
if(el != null){
System.out.println("Payload customer name = " + custName);


}
else{
System.out.println("Payload is null");
}



/* if(outcome == null)
{
outcome = "APPROVED";
taskSvc.updateTaskOutcome(ctx,taskId,outcome);
}*/

System.out.println("Task #"+taskNumber+" ("+title+") is "+outcome);
}
}
catch (Exception e)
{
//Handle any exceptions raised here...
System.out.println("Caught workflow exception: "+e.getMessage());
}

return "done";
}
public static String getElementValue(Element pElement, String pElementName){
String value=null;
NodeList myNodeList = pElement.getElementsByTagName(pElementName);
Element myElement = (Element)myNodeList.item(0);
NodeList myChildNodeList = myElement.getChildNodes();
for (int i=0; i {
value = ((Node)myChildNodeList.item( i )).getNodeValue().trim();
if( value.equals("") value.equals("\r") ){
continue;
}
else{
break;
}
}
return value;
}
}

2 comments:

eyemen said...

Hello

Thank you for this good article.

In my case, the getpayload and the getPayloadAsElement methods work only with a task returned by the getTaskDetailsById() method.

Now, I have created a human task assigned to a Group. Before taking a decision, the user must request the task.

I couldn't find the method in the Task Service which do this action

Any suggestion plz?

Regards

Niall Commiskey said...

Hi Eyemen,

I assume you want to do a "claim task" via the Java API, correct?

Niall C.