Thursday, July 4, 2013

#261 Short Digression on the Human Task API

Scenario:

I need to update the payload of an order via the API.
My order is very simple -














The User task is called Approve.
All I want to do is add my margin to the price ( * 1.5).
I also append " on special offer " to the product name

Here is the payload before -










 after -









Here is the code -

package hwjavaclient;


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:7001");
        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("weblogic", "welcome1".toCharArray(),
                                      null);
            System.out.println("CTX " + ctx.getIdentityContext());
            //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 all tasks
            List tasks =
                querySvc.queryTasks(ctx, queryColumns, optionalInfo, // Payload
                    ITaskQueryService.AssignmentFilter.ALL, 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...
            System.out.println("*** Nr of tasks " + tasks.size());
            for (int i = 0; i < tasks.size(); i++) {
                Task task = (Task)tasks.get(i);
                int taskNumber = task.getSystemAttributes().getTaskNumber();
                String title = task.getTitle();
                String outcome = task.getSystemAttributes().getOutcome();

                System.out.println("Task #" + taskNumber + " (" + title +
                                   ") has the outcome set to " + outcome);

                // Extract and update the payload for the approve order task

                if (title.equalsIgnoreCase("Approve")) {
                    // Get TaskDetailsByNumber so as to be able to update the payload
                    Task task2Update =
                        querySvc.getTaskDetailsByNumber(ctx, taskNumber);

                    System.out.println("Getting the Payload...");
                    Element el = task2Update.getPayloadAsElement();
                    Element newEl = getPayloadValues(el);
                    task2Update.setPayloadAsElement(newEl);
                    taskSvc.updateTask(ctx, task2Update);
                }
            }
        } catch (Exception e) {
            //Handle any exceptions raised here...
            System.out.println("Caught workflow exception: " + e.getMessage());
        }

        return "done";
    }

    public static Element getPayloadValues(Element pElement) {
        System.out.println("getPayloadValues() for Payload of type " +
                           pElement.getFirstChild().getNodeName());
        NodeList nl = pElement.getChildNodes();

        Node parentNode = nl.item(0);
        NodeList nlChildren = parentNode.getChildNodes();

        for (int i = 0; i < nlChildren.getLength(); i++) {
            Node n = nlChildren.item(i);
            String NodeName = n.getNodeName();
            if (!NodeName.equalsIgnoreCase("#text")) {
                Element myElement = (Element)nlChildren.item(i);
                NodeList nlElement = myElement.getChildNodes();
                String elName = n.getNodeName();
                String elValue = nlElement.item(0).getNodeValue();
                System.out.println("Element is " + elName +
                                   " with a value of " + elValue);
                if (elName.equalsIgnoreCase("ns2:product")) {
                    System.out.println("Updating product name to add special offer tag...");
                    nlElement.item(0).setNodeValue(elValue +
                                                   " on special offer");

                }
                if (elName.equalsIgnoreCase("ns2:price")) {
                    System.out.println("Updating price to add a decent margin...");
                    Double price = new Double(elValue);
                    price = price * 1.5;
                    String newPrice = price.toString();
                    nlElement.item(0).setNodeValue(newPrice);
                }

            }

        }
        return pElement;
    }
}






1 comment:

Celsa said...
This comment has been removed by a blog administrator.