Search in sources :

Example 1 with WorkflowError

use of org.finra.herd.model.api.xml.WorkflowError in project herd by FINRAOS.

the class JobServiceImpl method getJob.

/**
 * Gets a job by the given jobId, and displays verbose information if verbose flag is true. Does namespace permission check if namespace permissions flag is
 * true.
 *
 * @param jobId the job ID
 * @param verbose specifies if verbose mode is enabled or not
 * @param checkNamespacePermissions specifies whether to check namespace permissions or not
 *
 * @return the job information
 */
private Job getJob(String jobId, boolean verbose, boolean checkNamespacePermissions) {
    String jobIdLocal = jobId;
    // Validate and trim required elements
    Assert.hasText(jobIdLocal, "A job Id must be specified.");
    jobIdLocal = jobIdLocal.trim();
    // Get process instance from Activiti runtime.
    ProcessInstance processInstance = activitiService.getProcessInstanceById(jobIdLocal);
    // Get historic process instance.
    HistoricProcessInstance historicProcessInstance = activitiService.getHistoricProcessInstanceByProcessInstanceId(jobIdLocal);
    /*
         * Check permissions against namespace of the job.
         * Cannot be done through the annotation because the namespace is not part of the request.
         * TODO refactor this so it gets namespace from JobDefinitionEntity instead of parsing process definition key.
         */
    if (checkNamespacePermissions) {
        String processDefinitionKey = null;
        if (processInstance != null) {
            processDefinitionKey = processInstance.getProcessDefinitionKey();
        } else if (historicProcessInstance != null) {
            processDefinitionKey = activitiService.getProcessDefinitionById(historicProcessInstance.getProcessDefinitionId()).getKey();
        }
        if (processDefinitionKey != null) {
            checkPermissions(processDefinitionKey, new NamespacePermissionEnum[] { NamespacePermissionEnum.READ });
        }
    }
    Job job = new Job();
    job.setId(jobIdLocal);
    if (processInstance == null && historicProcessInstance == null) {
        // Process not found
        throw new ObjectNotFoundException(String.format("Job with Id: \"%s\" doesn't exist.", jobIdLocal));
    } else if (processInstance != null) {
        // Process is still running or suspended.
        job.setStatus(processInstance.isSuspended() ? JobStatusEnum.SUSPENDED : JobStatusEnum.RUNNING);
        // Check if there are errors.
        List<org.activiti.engine.runtime.Job> erroredJobs = activitiService.getJobsWithExceptionByProcessInstanceId(processInstance.getProcessInstanceId());
        if (!CollectionUtils.isEmpty(erroredJobs)) {
            // Errors found.
            List<WorkflowError> workflowErrors = new ArrayList<>();
            for (org.activiti.engine.runtime.Job erroredJob : erroredJobs) {
                WorkflowError workflowError = new WorkflowError();
                workflowError.setErrorMessage(erroredJob.getExceptionMessage());
                workflowError.setRetriesLeft(erroredJob.getRetries());
                workflowError.setErrorStackTrace(activitiService.getJobExceptionStacktrace(erroredJob.getId()));
                workflowErrors.add(workflowError);
            }
            job.setWorkflowErrors(workflowErrors);
        }
        if (processInstance.getActivityId() != null) {
            // Set current workflow step.
            WorkflowStep currentStep = new WorkflowStep();
            currentStep.setId(processInstance.getActivityId());
            job.setCurrentWorkflowStep(currentStep);
        }
        // Set the workflow variables.
        populateWorkflowParameters(job, processInstance.getProcessVariables());
        // If verbose, set activiti workflow xml.
        if (verbose) {
            populateActivitiXml(job, processInstance.getProcessDefinitionId());
        }
    } else {
        // Process completed
        job.setStatus(JobStatusEnum.COMPLETED);
        // Set the workflow variables.
        populateWorkflowParameters(job, historicProcessInstance.getProcessVariables());
        job.setDeleteReason(historicProcessInstance.getDeleteReason());
        // If verbose, set activiti workflow xml.
        if (verbose) {
            populateActivitiXml(job, historicProcessInstance.getProcessDefinitionId());
        }
    }
    if (historicProcessInstance != null) {
        // If verbose, set completed steps.
        if (verbose) {
            populateCompletedActivitiSteps(job, activitiService.getHistoricActivityInstancesByProcessInstanceId(jobIdLocal));
        }
        // Set the start time always since all jobs will have a start time.
        job.setStartTime(HerdDateUtils.getXMLGregorianCalendarValue(historicProcessInstance.getStartTime()));
        // Set the end time if the historic process instance has it set (the job is completed).
        if (historicProcessInstance.getEndTime() != null) {
            job.setEndTime(HerdDateUtils.getXMLGregorianCalendarValue(historicProcessInstance.getEndTime()));
        }
    }
    return job;
}
Also used : WorkflowStep(org.finra.herd.model.api.xml.WorkflowStep) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) WorkflowError(org.finra.herd.model.api.xml.WorkflowError) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) List(java.util.List) ArrayList(java.util.ArrayList) Job(org.finra.herd.model.api.xml.Job)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)1 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)1 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)1 Job (org.finra.herd.model.api.xml.Job)1 WorkflowError (org.finra.herd.model.api.xml.WorkflowError)1 WorkflowStep (org.finra.herd.model.api.xml.WorkflowStep)1