Search in sources :

Example 11 with HistoricVariableInstance

use of org.activiti.engine.history.HistoricVariableInstance in project Activiti by Activiti.

the class ReportDetailPanel method generateReport.

protected void generateReport(ProcessInstance processInstance) {
    // Report dataset is stored as historical variable as json
    HistoricVariableInstance historicVariableInstance = ProcessEngines.getDefaultProcessEngine().getHistoryService().createHistoricVariableInstanceQuery().processInstanceId(processInstance.getId()).variableName("reportData").singleResult();
    // Generate chart
    byte[] reportData = (byte[]) historicVariableInstance.getValue();
    ChartComponent chart = ChartGenerator.generateChart(reportData);
    chart.setWidth(100, UNITS_PERCENTAGE);
    chart.setHeight(100, UNITS_PERCENTAGE);
    // Put chart on screen
    if (processDefinitionStartForm != null) {
        detailContainer.removeComponent(processDefinitionStartForm);
        processDefinitionStartForm = null;
    }
    detailContainer.addComponent(chart);
    // The historic process instance can now be removed from the system
    // Only when save is clicked, the report will be regenerated
    ProcessEngines.getDefaultProcessEngine().getHistoryService().deleteHistoricProcessInstance(processInstance.getId());
}
Also used : HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance)

Example 12 with HistoricVariableInstance

use of org.activiti.engine.history.HistoricVariableInstance in project Activiti by Activiti.

the class TaskResourceTest method testCompleteTask.

/**
   * Test completing a single task.
   * POST runtime/tasks/{taskId}
   */
@Deployment
public void testCompleteTask() throws Exception {
    try {
        Task task = taskService.newTask();
        taskService.saveTask(task);
        String taskId = task.getId();
        ObjectNode requestNode = objectMapper.createObjectNode();
        requestNode.put("action", "complete");
        HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK, task.getId()));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        closeResponse(executeRequest(httpPost, HttpStatus.SC_OK));
        // Task shouldn't exist anymore
        task = taskService.createTaskQuery().taskId(taskId).singleResult();
        assertNull(task);
        // Test completing with variables
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
        task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        taskId = task.getId();
        requestNode = objectMapper.createObjectNode();
        ArrayNode variablesNode = objectMapper.createArrayNode();
        requestNode.put("action", "complete");
        requestNode.put("variables", variablesNode);
        ObjectNode var1 = objectMapper.createObjectNode();
        variablesNode.add(var1);
        var1.put("name", "var1");
        var1.put("value", "First value");
        ObjectNode var2 = objectMapper.createObjectNode();
        variablesNode.add(var2);
        var2.put("name", "var2");
        var2.put("value", "Second value");
        httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK, taskId));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        closeResponse(executeRequest(httpPost, HttpStatus.SC_OK));
        task = taskService.createTaskQuery().taskId(taskId).singleResult();
        assertNull(task);
        if (processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.AUDIT)) {
            HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult();
            assertNotNull(historicTaskInstance);
            List<HistoricVariableInstance> updates = historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstance.getId()).list();
            assertNotNull(updates);
            assertEquals(2, updates.size());
            boolean foundFirst = false;
            boolean foundSecond = false;
            for (HistoricVariableInstance var : updates) {
                if (var.getVariableName().equals("var1")) {
                    assertEquals("First value", var.getValue());
                    foundFirst = true;
                } else if (var.getVariableName().equals("var2")) {
                    assertEquals("Second value", var.getValue());
                    foundSecond = true;
                }
            }
            assertTrue(foundFirst);
            assertTrue(foundSecond);
        }
    } finally {
        // Clean adhoc-tasks even if test fails
        List<Task> tasks = taskService.createTaskQuery().list();
        for (Task task : tasks) {
            taskService.deleteTask(task.getId(), true);
        }
        // Clean historic tasks with no runtime-counterpart
        List<HistoricTaskInstance> historicTasks = historyService.createHistoricTaskInstanceQuery().list();
        for (HistoricTaskInstance task : historicTasks) {
            historyService.deleteHistoricTaskInstance(task.getId());
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Task(org.activiti.engine.task.Task) HistoricTaskInstance(org.activiti.engine.history.HistoricTaskInstance) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) StringEntity(org.apache.http.entity.StringEntity) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Deployment(org.activiti.engine.test.Deployment)

Example 13 with HistoricVariableInstance

use of org.activiti.engine.history.HistoricVariableInstance in project Activiti by Activiti.

the class ProcessInstanceCollectionResource method createProcessInstance.

@RequestMapping(value = "/runtime/process-instances", method = RequestMethod.POST, produces = "application/json")
public ProcessInstanceResponse createProcessInstance(@RequestBody ProcessInstanceCreateRequest request, HttpServletRequest httpRequest, HttpServletResponse response) {
    if (request.getProcessDefinitionId() == null && request.getProcessDefinitionKey() == null && request.getMessage() == null) {
        throw new ActivitiIllegalArgumentException("Either processDefinitionId, processDefinitionKey or message is required.");
    }
    int paramsSet = ((request.getProcessDefinitionId() != null) ? 1 : 0) + ((request.getProcessDefinitionKey() != null) ? 1 : 0) + ((request.getMessage() != null) ? 1 : 0);
    if (paramsSet > 1) {
        throw new ActivitiIllegalArgumentException("Only one of processDefinitionId, processDefinitionKey or message should be set.");
    }
    if (request.isCustomTenantSet()) {
        // Tenant-id can only be used with either key or message
        if (request.getProcessDefinitionId() != null) {
            throw new ActivitiIllegalArgumentException("TenantId can only be used with either processDefinitionKey or message.");
        }
    }
    Map<String, Object> startVariables = null;
    if (request.getVariables() != null) {
        startVariables = new HashMap<String, Object>();
        for (RestVariable variable : request.getVariables()) {
            if (variable.getName() == null) {
                throw new ActivitiIllegalArgumentException("Variable name is required.");
            }
            startVariables.put(variable.getName(), restResponseFactory.getVariableValue(variable));
        }
    }
    // Actually start the instance based on key or id
    try {
        ProcessInstance instance = null;
        if (request.getProcessDefinitionId() != null) {
            instance = runtimeService.startProcessInstanceById(request.getProcessDefinitionId(), request.getBusinessKey(), startVariables);
        } else if (request.getProcessDefinitionKey() != null) {
            if (request.isCustomTenantSet()) {
                instance = runtimeService.startProcessInstanceByKeyAndTenantId(request.getProcessDefinitionKey(), request.getBusinessKey(), startVariables, request.getTenantId());
            } else {
                instance = runtimeService.startProcessInstanceByKey(request.getProcessDefinitionKey(), request.getBusinessKey(), startVariables);
            }
        } else {
            if (request.isCustomTenantSet()) {
                instance = runtimeService.startProcessInstanceByMessageAndTenantId(request.getMessage(), request.getBusinessKey(), startVariables, request.getTenantId());
            } else {
                instance = runtimeService.startProcessInstanceByMessage(request.getMessage(), request.getBusinessKey(), startVariables);
            }
        }
        response.setStatus(HttpStatus.CREATED.value());
        //Added by Ryan Johnston
        if (request.getReturnVariables()) {
            Map<String, Object> runtimeVariableMap = null;
            List<HistoricVariableInstance> historicVariableList = null;
            if (instance.isEnded()) {
                historicVariableList = historyService.createHistoricVariableInstanceQuery().processInstanceId(instance.getId()).list();
            } else {
                runtimeVariableMap = runtimeService.getVariables(instance.getId());
            }
            return restResponseFactory.createProcessInstanceResponse(instance, true, runtimeVariableMap, historicVariableList);
        } else {
            return restResponseFactory.createProcessInstanceResponse(instance);
        }
    //End Added by Ryan Johnston
    //Removed by Ryan Johnston (obsolete given the above).
    //return factory.createProcessInstanceResponse(this, instance);
    } catch (ActivitiObjectNotFoundException aonfe) {
        throw new ActivitiIllegalArgumentException(aonfe.getMessage(), aonfe);
    }
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with HistoricVariableInstance

use of org.activiti.engine.history.HistoricVariableInstance in project CzechIdMng by bcvsolutions.

the class DefaultWorkflowHistoricProcessInstanceService method toResource.

private WorkflowHistoricProcessInstanceDto toResource(HistoricProcessInstance instance, boolean trimmed) {
    if (instance == null) {
        return null;
    }
    String instanceName = instance.getName();
    // processInstanceName
    if (instanceName == null && instance.getProcessVariables() != null && instance.getProcessVariables().containsKey(WorkflowHistoricProcessInstanceService.PROCESS_INSTANCE_NAME)) {
        instanceName = (String) instance.getProcessVariables().get(WorkflowHistoricProcessInstanceService.PROCESS_INSTANCE_NAME);
    }
    // historic variables
    if (instanceName == null || instanceName.isEmpty()) {
        HistoricVariableInstance variableInstance = historyService.createHistoricVariableInstanceQuery().processInstanceId(instance.getId()).variableName(WorkflowHistoricProcessInstanceService.PROCESS_INSTANCE_NAME).singleResult();
        instanceName = variableInstance != null ? (String) variableInstance.getValue() : null;
    }
    if (instanceName == null || instanceName.isEmpty()) {
        instanceName = definitionService.get(instance.getProcessDefinitionId()).getName();
    }
    WorkflowHistoricProcessInstanceDto dto = new WorkflowHistoricProcessInstanceDto();
    dto.setTrimmed(trimmed);
    dto.setId(instance.getId());
    dto.setName(instanceName);
    dto.setProcessDefinitionId(instance.getProcessDefinitionId());
    dto.setProcessDefinitionKey(instance.getProcessDefinitionKey());
    dto.setProcessVariables(instance.getProcessVariables());
    dto.setDeleteReason(instance.getDeleteReason());
    dto.setDurationInMillis(instance.getDurationInMillis());
    dto.setEndTime(instance.getEndTime());
    dto.setStartActivityId(instance.getStartActivityId());
    dto.setStartTime(instance.getStartTime());
    dto.setStartUserId(instance.getStartUserId());
    dto.setSuperProcessInstanceId(instance.getSuperProcessInstanceId());
    return dto;
}
Also used : HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) WorkflowHistoricProcessInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowHistoricProcessInstanceDto)

Example 15 with HistoricVariableInstance

use of org.activiti.engine.history.HistoricVariableInstance in project carbon-business-process by wso2.

the class HistoricVariableInstanceService method getVariableFromRequest.

protected RestVariable getVariableFromRequest(boolean includeBinary, String varInstanceId) {
    HistoryService historyService = BPMNOSGIService.getHistoryService();
    HistoricVariableInstance varObject = historyService.createHistoricVariableInstanceQuery().id(varInstanceId).singleResult();
    if (varObject == null) {
        throw new ActivitiObjectNotFoundException("Historic variable instance '" + varInstanceId + "' couldn't be found.", VariableInstanceEntity.class);
    } else {
        return new RestResponseFactory().createRestVariable(varObject.getVariableName(), varObject.getValue(), null, varInstanceId, RestResponseFactory.VARIABLE_HISTORY_VARINSTANCE, includeBinary, uriInfo.getBaseUri().toString());
    }
}
Also used : RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory) HistoryService(org.activiti.engine.HistoryService) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException)

Aggregations

HistoricVariableInstance (org.activiti.engine.history.HistoricVariableInstance)56 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)28 Deployment (org.activiti.engine.test.Deployment)25 HashMap (java.util.HashMap)20 Task (org.activiti.engine.task.Task)18 HistoricVariableInstanceEntity (org.activiti.engine.impl.persistence.entity.HistoricVariableInstanceEntity)8 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)5 Test (org.junit.Test)5 HistoricData (org.activiti.engine.history.HistoricData)4 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)4 HistoricVariableUpdate (org.activiti.engine.history.HistoricVariableUpdate)4 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 BpmnModel (org.activiti.bpmn.model.BpmnModel)3 HistoryService (org.activiti.engine.HistoryService)3 HistoricActivityInstance (org.activiti.engine.history.HistoricActivityInstance)3 HistoricTaskInstance (org.activiti.engine.history.HistoricTaskInstance)3 HistoricVariableInstanceQuery (org.activiti.engine.history.HistoricVariableInstanceQuery)3 ProcessInstanceHistoryLog (org.activiti.engine.history.ProcessInstanceHistoryLog)3