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




2 comments:

Digambar DAUND said...

Scenario – With arrival of new proposal, system starts 4 different bpmn processes; Can I query BPM engine to fetch the processIds started for the proposal with the process status? How?
Tip – I have my custom workspace application.

Digambar DAUND said...

Scenario – With arrival of new proposal, system starts 4 different bpmn processes; Can I query BPM engine to fetch the processIds started for the proposal with the process status? How?
Tip – I have my custom workspace application.