Monday, January 20, 2014

# 304 Adaptive Case Management Java API Part 1

Here is the stakeholders definition of my demo order approvals case -















Here is an instance of that case -




















Test API Output


















API code to query the case -

package com.niall;


import java.io.StringWriter;

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

import javax.servlet.http.HttpServletRequest;

import javax.xml.bind.JAXB;
import javax.xml.bind.JAXBElement;

import oracle.bpel.services.bpm.common.IBPMContext;
import oracle.bpel.services.workflow.client.IWorkflowServiceClientConstants;
import oracle.bpel.services.workflow.query.ITaskQueryService;
import oracle.bpel.services.workflow.task.ITaskService;

import oracle.bpel.services.workflow.verification.IWorkflowContext;
import oracle.bpel.services.workflow.WorkflowException;

import oracle.bpm.casemgmt.CaseIdentifier;
import oracle.bpm.casemgmt.CaseList;
import oracle.bpm.casemgmt.CaseServiceException;
import oracle.bpm.casemgmt.ICaseService;
import oracle.bpm.casemgmt.persistence.model.Case;
import oracle.bpm.casemgmt.persistence.model.CaseData;
import oracle.bpm.casemgmt.persistence.model.CaseHeader;
import oracle.bpm.casemgmt.persistence.model.CaseMilestone;
import oracle.bpm.casemgmt.persistence.model.CaseStakeHolder;
import oracle.bpm.casemgmt.persistence.model.CaseStakeHolderMember;
import oracle.bpm.casemgmt.persistence.model.Comment;
import oracle.bpm.casemgmt.persistence.model.CompletedCaseActivity;
import oracle.bpm.casemgmt.persistence.model.CompletedCaseActivityList;
import oracle.bpm.casemgmt.persistence.model.ICaseConstants;
import oracle.bpm.client.BPMServiceClientFactory;
import oracle.bpm.services.authentication.IBPMUserAuthenticationService;
import oracle.bpm.services.common.exception.BPMException;
import oracle.bpm.training.acm.apilab.model.Complaint.ObjectFactory;
import oracle.bpm.training.acm.apilab.model.Complaint.TComplaint;
import oracle.bpm.training.acm.apilab.util.CaseHelper;


public class CaseMgtAPI {

    private BPMServiceClientFactory mServiceClientFactory = null;
    private ICaseService mCaseService = null;
    private IBPMContext mBPMUserContext = null;

    private static final String WLS_ADMIN_PASSWORD = "welcome1";
    private static final String WLS_ADMIN_USERID = "jcooper";

    public CaseMgtAPI() {
        super();
    }


    public static CaseMgtAPI getAllCases4User(String cUser,
                                              String cPwd) throws Exception {
        CaseMgtAPI caseMgtAPI = new CaseMgtAPI();

        Map properties =
            new HashMap();
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.CLIENT_TYPE,
                       BPMServiceClientFactory.REMOTE_CLIENT);
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_PROVIDER_URL,
                       "t3://localhost:7001");
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_SECURITY_CREDENTIALS,
                       cPwd);
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_SECURITY_PRINCIPAL,
                       cUser);
        caseMgtAPI.mServiceClientFactory =
                BPMServiceClientFactory.getInstance(properties, "default",
                                                    null);
        /* Get Task Query Service */
        ITaskQueryService service =
            caseMgtAPI.mServiceClientFactory.getWorkflowServiceClient().getTaskQueryService();

        /* Get Case Mgt Service */
        caseMgtAPI.mCaseService =
                caseMgtAPI.mServiceClientFactory.getCaseManagementServiceClient().getCaseService();

        caseMgtAPI.mBPMUserContext =
                (IBPMContext)service.authenticate(cUser, cPwd.toCharArray(),
                                                  null);


        if (caseMgtAPI.mBPMUserContext != null) {
            System.out.println("CaseMgtAPI.getAllCases4User() Returning context for:" +
                               caseMgtAPI.mBPMUserContext.getUser());
            // list Case instances
            listCases(caseMgtAPI.mCaseService, caseMgtAPI.mBPMUserContext);

        } else {
            System.out.println("CaseMgtAPI.getAllCases4User()  invalid context");
        }

        return caseMgtAPI;
    }

    public static void listCases(ICaseService caseService,
                                 IBPMContext bpmContext) throws CaseServiceException {
        System.out.println("CaseMgtAPI.listCases()");
        System.out.println("");

        Case myCase = new Case();
        List caseInfo =
            new ArrayList();

        CaseList caseList =
            caseService.queryCase(bpmContext, caseInfo, null, null, 10, 1);
        System.out.println("Total Nr of Case Instances = " +
                           caseList.getTotalNumberOfCases());
        List myCaseList = caseList.getCases();
        System.out.println("Case list size " + myCaseList.size());
        for (int i = 0; i < myCaseList.size(); i++) {
            myCase = myCaseList.get(i);
            System.out.println("");
            System.out.println("Info for Case Instance " + i);
            //
            // Case Header
            System.out.println("");

            System.out.println("### Case Header Info ...");
            System.out.println("Composite Name = " +
                               myCase.getCaseHeader().getCompositeDn());
            System.out.println("Case Def. = " +
                               myCase.getCaseHeader().getCaseDefinitionName());
            System.out.println("ECM Folder = " +
                               myCase.getCaseHeader().getEcmFolder());
            System.out.println("Id Key = " +
                               myCase.getCaseHeader().getIdentificationKey());
            System.out.println("Case Definition Name = " +
                               myCase.getCaseHeader().getCaseDefinitionName());
            System.out.println("Application Name = " +
                               myCase.getCaseHeader().getApplicationName());
            System.out.println("Component Name = " +
                               myCase.getCaseHeader().getComponentName());
            System.out.println("Case Number = " +
                               myCase.getCaseHeader().getCaseNumber());
            System.out.println("Case Definition Id = " +
                               myCase.getCaseHeader().getCaseDefinitionId());
            // Case Activities
            CompletedCaseActivityList ccal =
                myCase.getCaseHeader().getCompletedCaseActivityList();
            List cca_l =
                ccal.getCompletedCaseActivity();
            System.out.println("### Nr of Case Activities Completed for this instance =" +
                               cca_l.size());

            for (int a = 0; a < cca_l.size(); a++) {
                CompletedCaseActivity cca = cca_l.get(a);
                String name = cca.getName();
                System.out.println("Completed Case Activity name = " + name);
            }

            System.out.println("### End of Case Header Info ...");
            System.out.println("");


          
            // Case Actions
            List myActions = myCase.getActions();
            for (int c = 0; c < myActions.size(); c++) {
                System.out.println("+++ Actions : " +
                                   myActions.get(c).toString());
                

            }
            System.out.println("");
            
      
          
            
            // stakeholders
            List caseStakeholderList =
                myCase.getCaseStakeHolders();
            System.out.println("Case StakeHolder List size = " +
                               caseStakeholderList.size());

            for (int f = 0; f < caseStakeholderList.size(); f++) {
                CaseStakeHolder caseStakeholder = caseStakeholderList.get(f);
                System.out.println("Case StakeHolder Name :" +
                                   caseStakeholder.getObjectDisplayName());
                List l_csm =
                    caseStakeholder.getCaseStakeHolderMembers();
                for (int g = 0; g < l_csm.size(); g++) {
                    CaseStakeHolderMember csm = l_csm.get(g);
                    System.out.println("+++ Case StakeHolder Member Name :" +
                                       csm.getStakeHolderDisplayName());
                }


            }

            System.out.println("");
            
        }
    }

    public IBPMContext getBPMContext() {
        return this.mBPMUserContext;
    }


    public BPMServiceClientFactory getServiceClientFactory() {
        return mServiceClientFactory;
    }

    public ICaseService getCaseService() {
        return mCaseService;
    }

    public ITaskService getTaskService() {
        return this.mServiceClientFactory.getWorkflowServiceClient().getTaskService();
    }


}


JDeveloper Project Libraries




















JDev Project download













Available here 

Useful resources/docs:

https://blogs.oracle.com/VenugopalMangipudi/entry/obpm_case_management_api_s


http://docs.oracle.com/cd/E28280_01/apirefs.1111/e25378/oracle/bpm/casemgmt/ICaseInstanceService.html


Wednesday, January 15, 2014

#303 BPM 11g Adaptive Case Management without UCM

Maybe you are trying out ACM and do not have UCM installed.
You enter such document details when creating a case.













Usually this results in the creation of a sub-folder in the parent folder. In my case, a subfolder in NewOrders. This title of this subfolder is based on the incoming custNr.

Great stuff! But what if you don't have UCM installed or do but want to save on resources when developing?

Ensure there is no setting in the SOA config for UCM - Click on Workflow properties























Click on More...













WorkflowConfig...











Amend the UCMIcdUrl value - In this case I just delete it.
Note the port 4444 this is for the Inbound Refinery from UCM - see

http://docs.oracle.com/cd/E21764_01/doc.1111/e14495/configucm.htm
for more details.









Restart your server

Test -






































Here is my case -














I upload a document -









































I start an activity in the case, in my scenario - Approve Order.
I can also view the attachment here.

















File is stored in the following DB table.






Naturally, when you store in the DB you do not have the usual UCM functionality - Check In / Check Out etc.


Monday, January 13, 2014

#302 Using the Human Workflow Event mechanism

Simple scenario - I want to pick up any re-assignment of tasks made in BPM workspace.

Here is my sample process -




All that is really salient for this example is the human task.

So I need to pick up any reassignments - firstly, I activate workflow events.
Amend the task definition as follows -















In the above, I activate for OnAssigned, OnCompleted and OnUpdated.
For my particular use case I only really require OnAssigned.

Once the task has been re-assigned, an EDN event will be thrown. The event definitions are stored in MDS -























This .edl file contains the XSDs of the OnAssigned etc. messages.












I now add a Mediator to my composite to subscribe to this event - As you can see, I simply write the data to a file.



.








So let's now look at the mapping for OnTaskAssigned















You see I am filtering on reassigns only.




















Now to the mapping -

I have defined the file adapter to use the same message type -









Testing

Task is assigned to jcooper






I now reassign to jstein













Output file is generated






































All that remains to be done is to pare down this info into something somewhat simpler.

The above message does not contain the actual task payload. 

So what can I do, if, for example, I want to see the orderNr of the reassigned order?

A simple method is to use the workflow flex fields -























I map the incoming order  nr to 




I re-test -








Monday, January 6, 2014

#301 Invoking BPM processes programmatically - when to use what?

- A big thank you to my colleague David R. for his comments -

For invoking the process (initial BPM process instance creation) we could --

1. Create a web service proxy based on the BPM process WSDL.
http://niallcblogs.blogspot.de/2013/11/283-bpm-11g-web-service-clients.html

2. Create an EJB Service Client to front the BPM process and call this.
http://niallcblogs.blogspot.de/2013/11/284-283-bpm-11g-ejb-service-clients.html

3. Create a direct binding interface to the BPM process.
http://niallcblogs.blogspot.de/2014/01/299-bpm-apis-revisited-directbinding.html

4. Explicitly use the BPM API as per https://java.net/projects/oraclebpmsuite11g/downloads/directory/Samples/bpm-api-101-instance-query

Other variations -

5. Use Technology Adapters

6. Use EDN

Some heuristics

Direct binding enables Java clients to directly invoke composite services, bypassing the intermediate conversion to XML required with web service binding.
Direct binding for propagating identity/transaction context
Direct binding for native connectivity between OSB and SOA/BPM Suite.
Direct binding for HiAv / Clustering support

Web Services when this is the preferred canonical model for integration.

Web Service Proxy - loose coupling, simple to develop.

In each of the above,  the caller is responsible for detecting failures and preserving the original message, The caller is responsible for retrying if required.  

EJB Service Client - useful for those who have standardised on EJBs - allows propagating identity/transaction context

BPM API - tightly coupled - for those who want full control. Not that simple to develop.

Adapters/EDN

Use of the adapters can move error handling/recovery off the client.  For example using JMS adapter or Database adapter.  For example, an integration with Mainframes where WLS/SOA Suite were not running when the process is started on the mainframe. 

Another common pattern would be a local queue with store & forward to the SOA instance, or inserting row into the DB. Then leveraging the DB adapter to start the process.  

EDN (Signal) can also be used. has it’s benefits, but does not currently have durable subscription. EDN thus decreases coupling, but does not ensure the message/event will be handled.


Friday, January 3, 2014

#300 BPM APIs revisited - IInstanceQueryService

The IInstanceQueryService can be used to query instances at runtime -
here is an example output from my Java client.













The code is pretty straightforward -

    public String getAllInstances() {
        System.out.println("getAllInstances() ");
        try {
            List displayColumns = new ArrayList();
            displayColumns.add(IColumnConstants.PROCESS_ID_COLUMN);
            displayColumns.add(IColumnConstants.PROCESS_COMPOSITEDN_COLUMN);

            displayColumns.add(IColumnConstants.PROCESS_PROCESSNAME_COLUMN);
            displayColumns.add(IColumnConstants.PROCESS_STATE_COLUMN);
            displayColumns.add(IColumnConstants.PROCESS_TITLE_COLUMN);
            displayColumns.add(IColumnConstants.PROCESS_CREATOR_COLUMN);
            displayColumns.add(IColumnConstants.PROCESS_CREATEDDATE_COLUMN);

            IInstanceQueryInput input = new InstanceQueryInput();

            input.setAssignmentFilter(IInstanceQueryInput.AssignmentFilter.ALL);
            Ordering ordering =
                new Ordering(IColumnConstants.PROCESS_NUMBER_COLUMN, true,
                             true);
            IInstanceQueryService queryService =
                getBPMServiceClient().getInstanceQueryService();

            IBPMContext bpmContext = getIBPMContext("jcooper", "welcome1");
            System.out.println("getInstances() queryInstances");

            List processInstances =
                queryService.queryInstances(bpmContext, displayColumns, null,
                                            ordering, input);
            for (IProcessInstance instance : processInstances) {
                System.out.println(instance.getSystemAttributes().getProcessInstanceId() +
                                   "\t" +
                                   instance.getSystemAttributes().getProcessNumber() +
                                   "\t" +
                                   instance.getSystemAttributes().getState() +
                                   "\t" + instance.getTitle() + "\t" +
                                   instance.getCreator() + "\t" +
                                   instance.getSystemAttributes().getCreatedDate().getTime());

            }
        }

        catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

This can be further refined by adding a predicate to the query - In this case, I only want to check for "OPEN" instances -

    public String getAllOpenInstances() {
        System.out.println("getAllOpenInstances() ");
        try {
            List displayColumns = new ArrayList();
            displayColumns.add(IColumnConstants.PROCESS_ID_COLUMN);
            displayColumns.add(IColumnConstants.PROCESS_COMPOSITEDN_COLUMN);

            displayColumns.add(IColumnConstants.PROCESS_PROCESSNAME_COLUMN);
            displayColumns.add(IColumnConstants.PROCESS_STATE_COLUMN);
            displayColumns.add(IColumnConstants.PROCESS_TITLE_COLUMN);
            displayColumns.add(IColumnConstants.PROCESS_CREATOR_COLUMN);
            displayColumns.add(IColumnConstants.PROCESS_CREATEDDATE_COLUMN);

            IInstanceQueryInput input = new InstanceQueryInput();

            input.setAssignmentFilter(IInstanceQueryInput.AssignmentFilter.ALL);
            Ordering ordering =
                new Ordering(IColumnConstants.PROCESS_NUMBER_COLUMN, true,
                             true);

            Predicate pred =
                new Predicate(IColumnConstants.PROCESS_STATE_COLUMN,
                              Predicate.OP_EQ, "OPEN");


            IInstanceQueryService queryService =
                getBPMServiceClient().getInstanceQueryService();

            IBPMContext bpmContext = getIBPMContext("jcooper", "welcome1");
            System.out.println("getInstances() queryInstances");

            List processInstances =
                queryService.queryInstances(bpmContext, displayColumns, pred,
                                            ordering, input);
            for (IProcessInstance instance : processInstances) {
                System.out.println(instance.getSystemAttributes().getProcessInstanceId() +
                                   "\t" +
                                   instance.getSystemAttributes().getProcessNumber() +
                                   "\t" +
                                   instance.getSystemAttributes().getState() +
                                   "\t" + instance.getTitle() + "\t" +
                                   instance.getCreator() + "\t" +
                                   instance.getSystemAttributes().getCreatedDate().getTime());

            }
        }

        catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

Further reading
http://docs.oracle.com/cd/E28389_01/apirefs.1111/e25378/oracle/bpm/services/instancequery/IInstanceQueryService.html

https://java.net/projects/oraclebpmsuite11g/downloads/directory/Samples/bpm-api-101-instance-query




Thursday, January 2, 2014

#299 BPM APIs revisited - DirectBinding and Facade APIs

Direct Binding Invocation -

A common way to invoke a composite is to use SOAP over HTTP.
This is enabled by creating a SOAP service for your composite using web service binding. 
You can also use direct binding, which provides a tighter integration alternative. 

Direct binding enables Java clients to directly invoke composite services, 
bypassing the intermediate conversion to XML required with web service binding. 

Direct Binding Invocation API -

Here we leverage oracle.soa.management.facade.Locator

The following composite contains a BPM process that can be invoked via direct binding.




















The client code is relatively simple.
I tested the composite in EM to see what the payload should look like.












I then created the Java client -




















The direct binding connection supports

- post
- request

post can be used for invoking asynchronous processes such as the following -









We then need to define a conversation Id -







Here is the output from a test run -













Validate in EM -













Here is an example for request - 












Example test output -













Validate in EM -
























Facade API

Here is the demo composite I am using here



















The Facade API exposes operations and attributes such as composites, components, services, and references for programmatically managing SOA composite applications during runtime. The Facade API is part of Oracle SOA Suite's Infrastructure Management Java API.

The API can be used to query the composite and get info such as -



Composite DN = default/FacadeAPIDemoComposite!1.0

Composite State = on

Composite Instance count = 0

*** Component DN = default/FacadeAPIDemoComposite!1.0/ApproveOrder

*** Component Name = ApproveOrder

*** Component Type = workflow
*** Component Total Instances = 0
*** Component Active Instances = 0
*** Component DN = default/FacadeAPIDemoComposite!1.0/DemoProcess
*** Component Name = DemoProcess
*** Component Type = bpmn
*** Component Total Instances = 0
*** Component Active Instances = 0

I now create a couple of composite instances and run the API client again -

init() start ...
init() complete
getInstances() for composite default/FacadeAPIDemoComposite!1.0
*** Found 2 Composite Instances.
Composite Instance DN = default/FacadeAPIDemoComposite!1.0
Composite Instance State = 0
Looking for Composite Components...
Component Name = DemoProcess
*** Type bpmn
*** Component State = 1
*** Component Status = initiated
*** Component normalized State = running
Component Name = ApproveOrder
*** Type workflow
*** Component State = 1
*** Component Status = ASSIGNED
*** Component normalized State = running
Composite Instance DN = default/FacadeAPIDemoComposite!1.0
Composite Instance State = 0
Looking for Composite Components...
Component Name = DemoProcess
*** Type bpmn
*** Component State = 1
*** Component Status = initiated
*** Component normalized State = running
Component Name = ApproveOrder
*** Type workflow
*** Component State = 1
*** Component Status = ASSIGNED
*** Component normalized State = running
Process exited with exit code 0.

The Facade API Client Code can be found here

I now add a composite sensor for the OrderNr on DemoProcess.service







I can now leverage the api to get this information as well as the ECID etc.
I can also select composite instances based on the ECID, see the method - getInstances4ECID()

The API can also be used to delete composite instances. For example - stale instances -













I ran getInstances4ECID() again and see that the state = 6












Now I can delete stale instances using the method - deleteCompositeInstances4State()






















Again, Java Code etc - here

Related Blog Posts



Suggested Reading -

Workflow Services API -
http://docs.oracle.com/cd/E23943_01/apirefs.1111/e10660/toc.htm

Facade API

http://docs.oracle.com/cd/E28280_01/admin.1111/e10226/soaadmin_apimanage.htm

http://docs.oracle.com/cd/E17904_01/apirefs.1111/e10659/oracle/soa/management/facade/CompositeInstance.html

BPM APIs
http://docs.oracle.com/cd/E28389_01/apirefs.1111/e25378/overview-summary.html

FMW Dev Guide for SOA Suite
http://docs.oracle.com/cd/E23943_01/dev.1111/e10224/toc.htm