Monday, December 2, 2013

#289 Human Task API - getting payload values

My process is as follows -










Human task title = ApproveTheOrder for Commiskey

Test Instance -















Task is assisgned to user jcooper.

API to retrieve the above data -

    public String getTaskdetails(String user, String pwd) {
        System.out.println("getTaskdetails()");
        Map properties = new HashMap();
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_PROVIDER_URL,
                       "t3://localhost:7001");
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_SECURITY_CREDENTIALS,
                       pwd);
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_SECURITY_PRINCIPAL,
                       user);

        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(user, pwd.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,
                    ITaskQueryService.AssignmentFilter.MY_AND_GROUP, 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("getTaskdetails() nr of Tasks = " +
                               tasks.size());


            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();
                int priority = task.getPriority();
                int taskNr = task.getSystemAttributes().getTaskNumber();
                String taskDefId =
                    task.getSystemAttributes().getTaskDefinitionId();
                System.out.println("In task loop... title/taskDefId = " +
                                   title + " / " + taskDefId);
                // Change priority from 3 to 1 for SimpleApprove


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

                System.out.println("Task #" + taskNumber + " (" + title +
                                   ") is " + outcome + " has priority " +
                                   priority + " Id = " + taskId + " taskNr= " +
                                   taskNr);
                if (title.equalsIgnoreCase("ApproveTheOrder for Commiskey")) {
                    System.out.println("*** Getting payload for ApproveTheOrder Human Task...");

                    //
                    Task task2Update =
                        querySvc.getTaskDetailsByNumber(ctx, taskNumber);

                    System.out.println("Getting the Payload...");
                    Element el = task2Update.getPayloadAsElement();
                    System.out.println("el "+el.toString());
                    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);
         
            }

        }
        return pElement;
    }


Test output -

















No comments: