Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.kie.remote.services.rest;
  
  import java.util.List;
  import java.util.Map;
  
  import  javax.enterprise.context.RequestScoped;
 import  javax.ws.rs.GET;
 import  javax.ws.rs.POST;
 import  javax.ws.rs.Path;
 import  javax.ws.rs.PathParam;
 import  javax.ws.rs.core.Context;
 import  javax.ws.rs.core.HttpHeaders;
 import  javax.ws.rs.core.Response;
 
 import  org.jbpm.process.audit.NodeInstanceLog;
 import  org.jbpm.process.audit.ProcessInstanceLog;
 import  org.jbpm.process.audit.VariableInstanceLog;
 import  org.kie.api.runtime.process.ProcessInstance;
 import  org.kie.services.client.serialization.jaxb.impl.audit.JaxbHistoryLogList;
 import  org.kie.services.client.serialization.jaxb.impl.audit.JaxbProcessInstanceLog;
 import  org.kie.services.client.serialization.jaxb.rest.JaxbGenericResponse;

This resource is responsible for direct, simple access to the history information. For complex queries, see the QueryResourceImpl
 
 @Path("/history")
 @RequestScoped
 @SuppressWarnings("unchecked")
 public class HistoryResourceImpl extends ResourceBase {
 
     /* REST information */
     
     @Context
     private HttpHeaders headers;
    
     // Rest methods --------------------------------------------------------------------------------------------------------------
 
     @GET
     @Path("/instances")
     public Response getProcessInstanceLogs() {
         String oper = getRelativePath();
         Map<StringString []> params = getRequestParams();
         String activeProcessesStr = getStringParam("activeProcesses"falseparamsoper);
         boolean activeProcesses = Boolean.parseBoolean(activeProcessesStr);
         
         List<ProcessInstanceLog> procInstLogResults;
         ifactiveProcesses ) { 
             procInstLogResults = getAuditLogService().findActiveProcessInstances();
         } else { 
             procInstLogResults = getAuditLogService().findProcessInstances();
         }
         sortProcessInstanceLogs(procInstLogResults);
         
         List<Objectresults = new ArrayList<Object>(procInstLogResults);
         JaxbHistoryLogList resultList =  paginateAndCreateResult(paramsoperresultsnew JaxbHistoryLogList());
         
         return createCorrectVariant(resultList);
     }
 
     @GET
     @Path("/instance/{procInstId: [0-9]+}")
     public Response getProcessInstanceLog(@PathParam("procInstId"long procInstId ) {
         ProcessInstanceLog procInstLog = getAuditLogService().findProcessInstance(procInstId);
         JaxbProcessInstanceLog jaxbProcLog = new JaxbProcessInstanceLog(procInstLog);
         
         return createCorrectVariant(jaxbProcLog);
     }
 
     @GET
     @Path("/instance/{procInstId: [0-9]+}/{type: [a-zA-Z]+}")
     public Response getInstanceLogsByProcInstId( @PathParam("procInstId"long instId, @PathParam("type"String logType)  {
         Map<StringString []> params = getRequestParams();
         String oper = getRelativePath();
         
         List<? extends ObjectvarInstLogList;
         if ("child".equalsIgnoreCase(logType)) {
             Object result = getAuditLogService().findSubProcessInstances(instId);
             varInstLogList = (List<Object>) result;
             sortProcessInstanceLogs((List<ProcessInstanceLog>) varInstLogList);
         } else if ("node".equalsIgnoreCase(logType)) {
             Object result = getAuditLogService().findNodeInstances(instId);
             varInstLogList = (List<Object>) result;
             sortNodeInstanceLogs((List<NodeInstanceLog>) varInstLogList);
         } else if ("variable".equalsIgnoreCase(logType)) {
             Object result = getAuditLogService().findVariableInstances(instId);
             varInstLogList = (List<Object>) result;
             sortVariableInstanceLogs((List<VariableInstanceLog>) varInstLogList);
         } else {
             throw KieRemoteRestOperationException.badRequest("Unsupported operation: " + oper );
         }
 
         JaxbHistoryLogList resultList =  paginateAndCreateResult(paramsoper, (List<Object>) varInstLogListnew JaxbHistoryLogList());
         
        return createCorrectVariant(resultList);
    }
    
    @GET
    @Path("/instance/{procInstId: [0-9]+}/{type: [a-zA-Z]+}/{logId: [a-zA-Z0-9-:\\._]+}")
    public Response getInstanceLogsByProcInstIdByLogId(@PathParam("procInstId"long procInstId, @PathParam("type"String operation, @PathParam("logId"String logId) {
        Map<StringString []> params = getRequestParams();
        String oper = getRelativePath();
        
        List<? extends ObjectvarInstLogList;
        if ("node".equalsIgnoreCase(operation)) {
            Object result = getAuditLogService().findNodeInstances(procInstIdlogId);
            varInstLogList = (List<Object>) result;
            sortNodeInstanceLogs((List<NodeInstanceLog>) varInstLogList);
        } else if ("variable".equalsIgnoreCase(operation)) {
            Object result = getAuditLogService().findVariableInstances(procInstIdlogId);
            varInstLogList = (List<Object>) result;
            sortVariableInstanceLogs((List<VariableInstanceLog>) varInstLogList);
        } else {
            throw KieRemoteRestOperationException.badRequest("Unsupported operation: " + oper );
        }
        
        JaxbHistoryLogList resultList = paginateAndCreateResult(paramsoper, (List<Object>) varInstLogListnew JaxbHistoryLogList());
        
        return createCorrectVariant(resultList);
    }
    @GET
    @Path("/process/{processDefId: [a-zA-Z0-9-:\\._]+}")
    public Response getProcessInstanceLogsByProcessId(@PathParam("processDefId"String processId) {
        Map<StringString []> params = getRequestParams();
        Number statusParam = getNumberParam("status"falseparamsgetRelativePath(), false);
        String oper = getRelativePath();
        int[] pageInfo = getPageNumAndPageSize(paramsoper);
        Object result;
        if (statusParam != null) {
            if (statusParam.intValue() == ProcessInstance.STATE_ACTIVE) {
                result = getAuditLogService().findActiveProcessInstances(processId);
            } else {
                result = getAuditLogService().findProcessInstances(processId);
            }
        } else {
            result = getAuditLogService().findProcessInstances(processId);
        }
        
        List<ProcessInstanceLog> procInstLogList = (List<ProcessInstanceLog>) result;
        
        List<ProcessInstanceLog> filteredProcLogList = procInstLogList;
        if (statusParam != null && !statusParam.equals(ProcessInstance.STATE_ACTIVE)) {
            filteredProcLogList = new ArrayList<ProcessInstanceLog>();
            for (int i = 0; 
                    i < procInstLogList.size() && filteredProcLogList.size() < getMaxNumResultsNeeded(pageInfo);
                    ++i) {
                ProcessInstanceLog procLog = procInstLogList.get(i);
                if (procLog.getStatus().equals(statusParam.intValue())) {
                    filteredProcLogList.add(procLog);
                }
            }
        }
        
        sortProcessInstanceLogs(filteredProcLogList);
        List<Objectresults = new ArrayList<Object>(filteredProcLogList);
        JaxbHistoryLogList resultList = paginateAndCreateResult(pageInforesultsnew JaxbHistoryLogList());
        return createCorrectVariant(resultList);
    }
    @GET
    @Path("/variable/{varId: [a-zA-Z0-9-:\\._]+}")
    public Response getVariableInstanceLogsByVariableId(@PathParam("varId"String variableId) {
        Map<StringString []> params = getRequestParams();
        String oper = getRelativePath();
        
        List<VariableInstanceLog> varInstLogList = internalGetVariableInstancesByVarAndValue(variableIdnullparamsoper);
        sortVariableInstanceLogs(varInstLogList);
        
        List<Objectresults = new ArrayList<Object>(varInstLogList);
        JaxbHistoryLogList resultList = paginateAndCreateResult(paramsoperresultsnew JaxbHistoryLogList());
        
        return createCorrectVariant(resultList);
    }
    
    @GET
    @Path("/variable/{varId: [a-zA-Z0-9-:\\._]+}/value/{value: [a-zA-Z0-9-:\\._]+}")
    public Response getVariableInstanceLogsByVariableIdByVariableValue(@PathParam("varId"String variableId, @PathParam("value"String value) {
        Map<StringString []> params = getRequestParams();
        String oper = getRelativePath();
        List<VariableInstanceLog> varInstLogList = internalGetVariableInstancesByVarAndValue(variableIdvalueparamsoper);
        sortVariableInstanceLogs(varInstLogList);
        
        List<Objectresults = new ArrayList<Object>(varInstLogList);
        JaxbHistoryLogList resultList = paginateAndCreateResult(paramsoperresultsnew JaxbHistoryLogList());
        
        return createCorrectVariant(resultList);
    } 
   
    @GET
    @Path("/variable/{varId: [a-zA-Z0-9-:\\._]+}/instances")
    public Response getProcessInstanceLogsByVariableId(@PathParam("varId"String variableId) {
        Map<StringString[]> params = getRequestParams();
        String oper = getRelativePath();
        // get variables
        List<VariableInstanceLog> varLogList = internalGetVariableInstancesByVarAndValue(variableIdnullparamsoper);
        
        // get process instance logs
        int [] pageInfo = getPageNumAndPageSize(paramsoper);
        int maxNumResults = getMaxNumResultsNeeded(pageInfo);
        List<ProcessInstanceLog> procInstLogList = getProcessInstanceLogsByVariable(varLogListmaxNumResults);
       
        // paginate
        List<Objectresults = new ArrayList<Object>(procInstLogList);
        JaxbHistoryLogList resultList = paginateAndCreateResult(pageInforesultsnew JaxbHistoryLogList());
        
        return createCorrectVariant(resultList);
    }
    
    @GET
    @Path("/variable/{varId: [a-zA-Z0-9-:\\.]+}/value/{value: [a-zA-Z0-9-:\\._]+}/instances")
    public Response getProcessInstanceLogsByVariableIdByVariableValue(@PathParam("varId"String variableId, @PathParam("value"String value) {
        Map<StringString[]> params = getRequestParams();
        String oper = getRelativePath();
        // get variables
        List<VariableInstanceLog> varLogList = internalGetVariableInstancesByVarAndValue(variableIdvalueparamsoper);
        
        // get process instance logs
        int [] pageInfo = getPageNumAndPageSize(paramsoper);
        int maxNumResults = getMaxNumResultsNeeded(pageInfo);
        List<ProcessInstanceLog> procInstLogList = getProcessInstanceLogsByVariable(varLogListmaxNumResults);
        
        List<Objectresults = new ArrayList<Object>(procInstLogList);
        JaxbHistoryLogList resultList = paginateAndCreateResult(pageInforesultsnew JaxbHistoryLogList());
        return createCorrectVariant(resultList);
    }
   
    @POST
    @Path("/clear")
    public Response clear() {
        getAuditLogService().clear();
        return createCorrectVariant(new JaxbGenericResponse(getRequestUri()), );
    }
    
    // Helper methods --------------------------------------------------------------------------------------------------------------
    private List<VariableInstanceLog> internalGetVariableInstancesByVarAndValue(String varIdString value
            Map<StringString[]> paramsString oper) { 
        // active processes parameter
        String activeProcsParam = getStringParam("activeProcesses"falseparamsoper); 
        boolean onlyActiveProcesses = false;
        ifactiveProcsParam != null ) { 
            onlyActiveProcesses = Boolean.parseBoolean(activeProcsParam);
        }
       
        Object result;
        ifvalue == null ) { 
            result = getAuditLogService().findVariableInstancesByName(varIdonlyActiveProcesses);
        } else { 
            result = getAuditLogService().findVariableInstancesByNameAndValue(varIdvalueonlyActiveProcesses);
        }
        
        return (List<VariableInstanceLog>) result;
    }
    private List<ProcessInstanceLog> getProcessInstanceLogsByVariable(List<VariableInstanceLog> varLogListint maxNumResults) {
        int numVarLogs = varLogList.size();
        int numProcInsts = 0;
       
        List<ProcessInstanceLog> resultList = new ArrayList<ProcessInstanceLog>();
        forint i = 0; i < numVarLogs && numProcInsts < maxNumResults; ++i ) { 
            long procInstId = varLogList.get(i).getProcessInstanceId();
            ProcessInstanceLog procInstlog = getAuditLogService().findProcessInstance(procInstId);
            ifprocInstlog != null ) { 
                resultList.add(procInstlog);
                ++numProcInsts;
            }
        }
        return resultList;
    }
    private void sortProcessInstanceLogs(List<ProcessInstanceLog> procInstLogList) { 
        Collections.sort(procInstLogListnew Comparator<ProcessInstanceLog>() {
    
            @Override
            public int compare( ProcessInstanceLog o1, ProcessInstanceLog o2 ) {
                if( ! o1.getExternalId().equals(o2.getExternalId()) ) { 
                   return o1.getExternalId().compareTo(o2.getExternalId());
                }
                if( ! o1.getProcessId().equals(o2.getProcessId()) ) { 
                   return o1.getProcessId().compareTo(o2.getProcessId());
                }
                return o1.getProcessInstanceId().compareTo(o2.getProcessInstanceId());
            }
        });
    }
    
    private void sortNodeInstanceLogs(List<NodeInstanceLog> procInstLogList) { 
        Collections.sort(procInstLogListnew Comparator<NodeInstanceLog>() {
    
            @Override
            public int compare( NodeInstanceLog o1, NodeInstanceLog o2 ) {
                if( ! o1.getExternalId().equals(o2.getExternalId()) ) { 
                   return o1.getExternalId().compareTo(o2.getExternalId());
                }
                if( ! o1.getProcessId().equals(o2.getProcessId()) ) { 
                   return o1.getProcessId().compareTo(o2.getProcessId());
                }
                if( ! o1.getProcessInstanceId().equals(o2.getProcessInstanceId()) ) { 
                   return o1.getProcessInstanceId().compareTo(o2.getProcessInstanceId());
                }
                if( ! o1.getNodeId().equals(o2.getNodeId()) ) { 
                   return o1.getNodeId().compareTo(o2.getNodeId());
                }
                return o1.getNodeInstanceId().compareTo(o2.getNodeInstanceId());
            }
        });
    }
    private void sortVariableInstanceLogs(List<VariableInstanceLog> varInstLogList ) { 
        Collections.sort(varInstLogListnew Comparator<VariableInstanceLog>() {
            @Override
            public int compare( VariableInstanceLog o1, VariableInstanceLog o2 ) {
                if( ! o1.getExternalId().equals(o2.getExternalId()) ) { 
                    return o1.getExternalId().compareTo(o2.getExternalId());
                }
                if( ! o1.getProcessId().equals(o2.getProcessId()) ) { 
                    return o1.getProcessId().compareTo(o2.getProcessId());
                }
                if( ! o1.getProcessInstanceId().equals(o2.getProcessInstanceId()) ) { 
                   return o1.getProcessInstanceId().compareTo(o2.getProcessInstanceId());
                }
                if( ! o1.getVariableId().equals(o2.getVariableId()) ) { 
                   return o1.getVariableId().compareTo(o2.getVariableId());
                }
                return o1.getVariableInstanceId().compareTo(o2.getVariableInstanceId());
            }
        });
    }
}
    
New to GrepCode? Check out our FAQ X