Wednesday, 28 February 2018

Generate BPM process audit through API




package com.poc.bpm.task;

import com.poc.WfAudit;
import com.poc.ProcessUtils;
import java.io.IOException;
import oracle.bpm.services.instancequery.IAuditConstants;
import oracle.bpm.collections.Sequence;
import java.util.ArrayList;
import java.util.List;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.bpel.services.bpm.common.IBPMContext;
import oracle.bpel.services.workflow.IWorkflowConstants;
import oracle.bpel.services.workflow.WorkflowException;
import oracle.bpel.services.workflow.query.ITaskQueryService;
import oracle.bpel.services.workflow.repos.Ordering;
import oracle.bpel.services.workflow.repos.Predicate;
import oracle.bpel.services.workflow.repos.TableConstants;
import oracle.bpel.services.workflow.task.ITaskAssignee;
import oracle.bpel.services.workflow.task.ITaskService;
import oracle.bpel.services.workflow.task.impl.TaskAssignee;
import oracle.bpel.services.workflow.task.model.Task;
import oracle.bpm.project.model.processes.Activity;
import oracle.bpm.project.model.processes.SendTask;
import oracle.bpm.project.model.processes.conversation.Conversation;
import oracle.bpm.project.model.processes.conversation.ProcessCallConversationDefinition;
import oracle.bpm.services.instancemanagement.model.IProcessInstance;
import oracle.bpm.services.instancequery.IAuditInstance;
import oracle.bpm.services.instancequery.IColumnConstants;
import oracle.bpm.services.instancequery.IInstanceQueryInput;
import oracle.bpm.services.instancequery.IInstanceQueryService;
import oracle.bpm.services.instancequery.impl.InstanceQueryInput;
import oracle.bpm.services.internal.processmodel.model.IProcessModelPackage;
import oracle.bpm.services.processmetadata.ProcessMetadataSummary;
import oracle.bpel.services.workflow.repos.Column;
import oracle.bpm.services.common.exception.BPMException;
public class AuditServiceClient {
   
    private static final Logger LOGGER = Logger.getLogger("oracle.soa.Logger");
    private ProcessUtils _processUtils;
  
    private static final String AUDIT = "AUDIT";
    private static final String COMPLETE = "COMPLETE";
    private static final String TASKNUMBER = "TASKNUMBER";
    private static final String TITLE = "TITLE";
    private static final String PRIORITY = "PRIORITY";
    private static final String STATE = "STATE";
    private static final String ENDDATE = "ENDDATE";
   
    IBPMContext ctx = null;
    public AuditServiceClient(String adminUser, String adminPassword, String host, String port) { //
        try {
            _processUtils = new ProcessUtils(ProcessUtils.getServiceClientFactory(host, port));
            ctx =
                (IBPMContext) _processUtils.getWorkflowServiceClient().getTaskQueryService().authenticate(adminUser,
                                                                                      adminPassword.toCharArray(),
                                                                                      null);
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "\r\ne: " + e.getMessage());
            e.printStackTrace();
        }
    }
   
    public static void main(String[] argv) {
        AuditServiceClient c = new AuditServiceClient("weblogic", "password1$", "localhost", "7001");
        long startTm = System.currentTimeMillis();
        List<WfAudit> auditTrail = c.getAuditEntries("2d520f16-0665-4d8a-b9c3-4fe0f71595b6-0001930e");
        long endTm = System.currentTimeMillis();
        System.out.println(endTm-startTm);
       
        for(WfAudit a : auditTrail){
            System.out.println(a.getActivity()+"::"+a.getAction()+"::"+a.getParticipant()+"::"+a.getDt());
        }
        //List<WfAudit> auditTrail =  c.prepareInstanceAudit("140010");//30002, ,140010
    }
   
    public  List<WfAudit> getAuditEntries(String workItemId) {
        List<WfAudit> auditTrail = null;
        try{
            String instanceId = this.getInstanceId(workItemId);
            auditTrail =  this.prepareInstanceAudit(instanceId);
            auditTrail.sort(Comparator.comparing(o -> o.getDt()));
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return auditTrail;
    }
   
   
    private String getInstanceId(String ecid) throws Exception {
       
        List<Column> columns = new ArrayList<Column>(); 
        columns.add(IColumnConstants.PROCESS_ID_COLUMN); 
        columns.add(IColumnConstants.PROCESS_ECID_COLUMN);
        columns.add(IColumnConstants.PROCESS_INSTANCEID_COLUMN);
   
        Ordering ordering = new Ordering(IColumnConstants.PROCESS_INSTANCEID_COLUMN, true, true); 
        Predicate pred = new Predicate(IColumnConstants.PROCESS_ECID_COLUMN, Predicate.OP_EQ, ecid);
       
        IInstanceQueryInput input = new InstanceQueryInput(); 
        input.setAssignmentFilter(IInstanceQueryInput.AssignmentFilter.ALL); 
       
        List<IProcessInstance> instances =
            _processUtils.getBPMServiceClient().getInstanceQueryService()
                .queryProcessInstances(ctx, columns, pred,ordering, input);
       
        if(!instances.isEmpty())
            return instances.get(0).getSystemAttributes().getProcessInstanceId();
        return null;
    }
    private List<WfAudit> prepareInstanceAudit(String instanceId) {
        List<WfAudit> auditTrail = new ArrayList<>();
        try {
            List<IAuditInstance> auditInstances =
                _processUtils.getBPMServiceClient().getInstanceQueryService().queryAuditInstanceByProcessId(ctx,
                                                                                                            instanceId);
            for (IAuditInstance audit : auditInstances) {

                if("USER_TASK".equals(audit.getActivityName())){
                    WfAudit a = new WfAudit();
                    a.setActivity(audit.getLabel());
                    a.setDt(audit.getCreateTime().getTime());
                    a.setAction("START".equals(audit.getAuditInstanceType())?"ASSIGNED":"COMPLETED");
                    a.setParticipant(audit.getParticipant());
                    auditTrail.add(a);
                }
                if (audit.getActivityName().equals("SEND_TASK") && audit.getAuditInstanceType().equals("START")) {
                    String invokedProcessName = getInvokedProcess(audit.getActivityId(),audit.getProcessName());
                    searchInstance(invokedProcessName,audit.getFlowId(), auditTrail);
                }
            }
        } catch (Exception we) {
            we.printStackTrace();
        }
       
        return auditTrail;
    }
   
    private String getInvokedProcess(String activityId, String processName) throws Exception {
        ProcessMetadataSummary metadata =
            _processUtils.getMetadataForProcess(ctx, processName, null); //TODO: "1.2"
        IProcessModelPackage p = _processUtils.getBPMServiceClient().getProcessModelService().getProcessModel(ctx, metadata.getCompositeDN(), processName);
           
        Iterator<Activity> it = p.getProcessModel().getActivities().iterator();
        Sequence<Conversation> conversations = p.getProcessModel().getConversations();
       
        while(it.hasNext()){
            Activity act = it.next();
            if(act.getId().equals(activityId)){
                SendTask sendTask = act.as(SendTask.class);
                for(Conversation conv : conversations){
                    if(conv.getId().equals(sendTask.getConversation().getId())){
                        ProcessCallConversationDefinition pcallConvDef  = (ProcessCallConversationDefinition)conv.getConversationDefinition();
                        return pcallConvDef.getProcessRef().getName();
                    }
                }
            }
        }
        return null;
    }
    private void searchInstance(String processName,long flowId, List<WfAudit> auditTrail) throws Exception {
        String expression = IAuditConstants.COLUMN_COMPONENTNAME + " like '%"+processName +"%'";// and "+IAuditConstants.COLUMN_QUERYID +" = "+flowId;
           
        List<IAuditInstance> auditInstances =
            _processUtils.getBPMServiceClient().getInstanceQueryService().queryAuditInstances(ctx, expression);
        for (IAuditInstance audit : auditInstances) {
            if (audit.getFlowId()==flowId){
                if("USER_TASK".equals(audit.getActivityName())){
                  
                    WfAudit a = new WfAudit();
                    a.setActivity(audit.getLabel());
                    a.setDt(audit.getCreateTime().getTime());
                    a.setAction("START".equals(audit.getAuditInstanceType())?"ASSIGNED":"COMPLETED");
                    a.setParticipant(audit.getParticipant());
                    auditTrail.add(a);
                }
                else if ("SEND_TASK".equals(audit.getActivityName()) && "START".equals(audit.getAuditInstanceType())){
                    String invokedProcessName = getInvokedProcess(audit.getActivityId(),audit.getProcessName());
                    searchInstance(invokedProcessName,audit.getFlowId(), auditTrail);
                }
            }
        }
    }
}

2 comments:

  1. Hi, thanks for the complete code. Can you please also share the implementation of ProcessUtils.

    ReplyDelete
  2. what are the libraries you are using ?

    ReplyDelete