Search in sources :

Example 1 with VariableInstance

use of org.activiti.engine.impl.persistence.entity.VariableInstance in project Activiti by Activiti.

the class GetExecutionVariableInstancesCmd method execute.

public Map<String, VariableInstance> execute(CommandContext commandContext) {
    // Verify existance of execution
    if (executionId == null) {
        throw new ActivitiIllegalArgumentException("executionId is null");
    }
    ExecutionEntity execution = commandContext.getExecutionEntityManager().findExecutionById(executionId);
    if (execution == null) {
        throw new ActivitiObjectNotFoundException("execution " + executionId + " doesn't exist", Execution.class);
    }
    Map<String, VariableInstance> variables = null;
    if (variableNames == null || variableNames.isEmpty()) {
        // Fetch all
        if (isLocal) {
            variables = execution.getVariableInstancesLocal();
        } else {
            variables = execution.getVariableInstances();
        }
    } else {
        // Fetch specific collection of variables
        if (isLocal) {
            variables = execution.getVariableInstancesLocal(variableNames, false);
        } else {
            variables = execution.getVariableInstances(variableNames, false);
        }
    }
    if (variables != null && locale != null) {
        for (Entry<String, VariableInstance> entry : variables.entrySet()) {
            String variableName = entry.getKey();
            VariableInstance variableEntity = entry.getValue();
            String localizedName = null;
            String localizedDescription = null;
            ObjectNode languageNode = Context.getLocalizationElementProperties(locale, variableName, execution.getProcessDefinitionId(), withLocalizationFallback);
            if (languageNode != null) {
                JsonNode nameNode = languageNode.get(DynamicBpmnConstants.LOCALIZATION_NAME);
                if (nameNode != null) {
                    localizedName = nameNode.asText();
                }
                JsonNode descriptionNode = languageNode.get(DynamicBpmnConstants.LOCALIZATION_DESCRIPTION);
                if (descriptionNode != null) {
                    localizedDescription = descriptionNode.asText();
                }
            }
            variableEntity.setLocalizedName(localizedName);
            variableEntity.setLocalizedDescription(localizedDescription);
        }
    }
    return variables;
}
Also used : ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) VariableInstance(org.activiti.engine.impl.persistence.entity.VariableInstance)

Example 2 with VariableInstance

use of org.activiti.engine.impl.persistence.entity.VariableInstance in project Activiti by Activiti.

the class CustomVariableType method testLocalizeVariables.

@Deployment
public void testLocalizeVariables() {
    // Start process instance with different types of variables
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("stringVar", "coca-cola");
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("localizeVariables", variables);
    ObjectNode infoNode = dynamicBpmnService.getProcessDefinitionInfo(processInstance.getProcessDefinitionId());
    dynamicBpmnService.changeLocalizationName("en-US", "stringVar", "stringVar 'en-US' Name", infoNode);
    dynamicBpmnService.changeLocalizationDescription("en-US", "stringVar", "stringVar 'en-US' Description", infoNode);
    dynamicBpmnService.changeLocalizationName("en-AU", "stringVar", "stringVar 'en-AU' Name", infoNode);
    dynamicBpmnService.changeLocalizationDescription("en-AU", "stringVar", "stringVar 'en-AU' Description", infoNode);
    dynamicBpmnService.changeLocalizationName("en", "stringVar", "stringVar 'en' Name", infoNode);
    dynamicBpmnService.changeLocalizationDescription("en", "stringVar", "stringVar 'en' Description", infoNode);
    dynamicBpmnService.saveProcessDefinitionInfo(processInstance.getProcessDefinitionId(), infoNode);
    Map<String, VariableInstance> variableInstances = runtimeService.getVariableInstances(processInstance.getId(), "es", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'es' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'es' Description", variableInstances.get("stringVar").getLocalizedDescription());
    // getVariableInstances
    variableInstances = runtimeService.getVariableInstances(processInstance.getId(), "en-US", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'en-US' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'en-US' Description", variableInstances.get("stringVar").getLocalizedDescription());
    variableInstances = runtimeService.getVariableInstances(processInstance.getId(), "en-AU", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'en-AU' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'en-AU' Description", variableInstances.get("stringVar").getLocalizedDescription());
    variableInstances = runtimeService.getVariableInstances(processInstance.getId(), "en-GB", true);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'en' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'en' Description", variableInstances.get("stringVar").getLocalizedDescription());
    variableInstances = runtimeService.getVariableInstances(processInstance.getId(), "en-GB", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals(null, variableInstances.get("stringVar").getLocalizedName());
    assertEquals(null, variableInstances.get("stringVar").getLocalizedDescription());
    List<String> variableNames = new ArrayList<String>();
    variableNames.add("stringVar");
    // getVariablesInstances via names
    variableInstances = runtimeService.getVariableInstances(processInstance.getId(), variableNames, "en-US", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'en-US' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'en-US' Description", variableInstances.get("stringVar").getLocalizedDescription());
    variableInstances = runtimeService.getVariableInstances(processInstance.getId(), variableNames, "en-AU", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'en-AU' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'en-AU' Description", variableInstances.get("stringVar").getLocalizedDescription());
    variableInstances = runtimeService.getVariableInstances(processInstance.getId(), variableNames, "en-GB", true);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'en' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'en' Description", variableInstances.get("stringVar").getLocalizedDescription());
    variableInstances = runtimeService.getVariableInstances(processInstance.getId(), variableNames, "en-GB", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals(null, variableInstances.get("stringVar").getLocalizedName());
    assertEquals(null, variableInstances.get("stringVar").getLocalizedDescription());
    // getVariableInstancesLocal
    variableInstances = runtimeService.getVariableInstancesLocal(processInstance.getId(), "en-US", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'en-US' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'en-US' Description", variableInstances.get("stringVar").getLocalizedDescription());
    variableInstances = runtimeService.getVariableInstancesLocal(processInstance.getId(), "en-AU", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'en-AU' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'en-AU' Description", variableInstances.get("stringVar").getLocalizedDescription());
    variableInstances = runtimeService.getVariableInstancesLocal(processInstance.getId(), "en-GB", true);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'en' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'en' Description", variableInstances.get("stringVar").getLocalizedDescription());
    variableInstances = runtimeService.getVariableInstancesLocal(processInstance.getId(), "en-GB", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals(null, variableInstances.get("stringVar").getLocalizedName());
    assertEquals(null, variableInstances.get("stringVar").getLocalizedDescription());
    // getVariableInstancesLocal via names
    variableInstances = runtimeService.getVariableInstancesLocal(processInstance.getId(), variableNames, "en-US", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'en-US' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'en-US' Description", variableInstances.get("stringVar").getLocalizedDescription());
    variableInstances = runtimeService.getVariableInstancesLocal(processInstance.getId(), variableNames, "en-AU", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'en-AU' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'en-AU' Description", variableInstances.get("stringVar").getLocalizedDescription());
    variableInstances = runtimeService.getVariableInstancesLocal(processInstance.getId(), variableNames, "en-GB", true);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals("stringVar 'en' Name", variableInstances.get("stringVar").getLocalizedName());
    assertEquals("stringVar 'en' Description", variableInstances.get("stringVar").getLocalizedDescription());
    variableInstances = runtimeService.getVariableInstancesLocal(processInstance.getId(), variableNames, "en-GB", false);
    assertEquals(1, variableInstances.size());
    assertEquals("stringVar", variableInstances.get("stringVar").getName());
    assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
    assertEquals(null, variableInstances.get("stringVar").getLocalizedName());
    assertEquals(null, variableInstances.get("stringVar").getLocalizedDescription());
    // getVariableInstance
    VariableInstance variableInstance = runtimeService.getVariableInstance(processInstance.getId(), "stringVar", "en-GB", false);
    assertNotNull(variableInstance);
    assertEquals("stringVar", variableInstance.getName());
    assertEquals("coca-cola", variableInstance.getValue());
    assertEquals(null, variableInstance.getLocalizedName());
    assertEquals(null, variableInstance.getLocalizedDescription());
    variableInstance = runtimeService.getVariableInstance(processInstance.getId(), "stringVar", "en-US", false);
    assertNotNull(variableInstance);
    assertEquals("stringVar", variableInstance.getName());
    assertEquals("coca-cola", variableInstance.getValue());
    assertEquals("stringVar 'en-US' Name", variableInstance.getLocalizedName());
    assertEquals("stringVar 'en-US' Description", variableInstance.getLocalizedDescription());
    variableInstance = runtimeService.getVariableInstance(processInstance.getId(), "stringVar", "en-AU", false);
    assertNotNull(variableInstance);
    assertEquals("stringVar", variableInstance.getName());
    assertEquals("coca-cola", variableInstance.getValue());
    assertEquals("stringVar 'en-AU' Name", variableInstance.getLocalizedName());
    assertEquals("stringVar 'en-AU' Description", variableInstance.getLocalizedDescription());
    variableInstance = runtimeService.getVariableInstance(processInstance.getId(), "stringVar", "en-GB", true);
    assertNotNull(variableInstance);
    assertEquals("stringVar", variableInstance.getName());
    assertEquals("coca-cola", variableInstance.getValue());
    assertEquals("stringVar 'en' Name", variableInstance.getLocalizedName());
    assertEquals("stringVar 'en' Description", variableInstance.getLocalizedDescription());
    variableInstance = runtimeService.getVariableInstance(processInstance.getId(), "stringVar", "en-GB", false);
    assertNotNull(variableInstance);
    assertEquals("stringVar", variableInstance.getName());
    assertEquals("coca-cola", variableInstance.getValue());
    assertEquals(null, variableInstance.getLocalizedName());
    assertEquals(null, variableInstance.getLocalizedDescription());
    // getVariableInstanceLocal
    variableInstance = runtimeService.getVariableInstanceLocal(processInstance.getId(), "stringVar", "en-US", false);
    assertNotNull(variableInstance);
    assertEquals("stringVar", variableInstance.getName());
    assertEquals("coca-cola", variableInstance.getValue());
    assertEquals("stringVar 'en-US' Name", variableInstance.getLocalizedName());
    assertEquals("stringVar 'en-US' Description", variableInstance.getLocalizedDescription());
    variableInstance = runtimeService.getVariableInstanceLocal(processInstance.getId(), "stringVar", "en-AU", false);
    assertNotNull(variableInstance);
    assertEquals("stringVar", variableInstance.getName());
    assertEquals("coca-cola", variableInstance.getValue());
    assertEquals("stringVar 'en-AU' Name", variableInstance.getLocalizedName());
    assertEquals("stringVar 'en-AU' Description", variableInstance.getLocalizedDescription());
    variableInstance = runtimeService.getVariableInstanceLocal(processInstance.getId(), "stringVar", "en-GB", true);
    assertNotNull(variableInstance);
    assertEquals("stringVar", variableInstance.getName());
    assertEquals("coca-cola", variableInstance.getValue());
    assertEquals("stringVar 'en' Name", variableInstance.getLocalizedName());
    assertEquals("stringVar 'en' Description", variableInstance.getLocalizedDescription());
    variableInstance = runtimeService.getVariableInstanceLocal(processInstance.getId(), "stringVar", "en-GB", false);
    assertNotNull(variableInstance);
    assertEquals("stringVar", variableInstance.getName());
    assertEquals("coca-cola", variableInstance.getValue());
    assertEquals(null, variableInstance.getLocalizedName());
    assertEquals(null, variableInstance.getLocalizedDescription());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) VariableInstance(org.activiti.engine.impl.persistence.entity.VariableInstance) Deployment(org.activiti.engine.test.Deployment)

Example 3 with VariableInstance

use of org.activiti.engine.impl.persistence.entity.VariableInstance in project Activiti by Activiti.

the class TaskVariablesTest method testGetVariablesLocalByTaskIdsForScope.

@Deployment(resources = { "org/activiti/engine/test/api/runtime/variableScope.bpmn20.xml" })
public void testGetVariablesLocalByTaskIdsForScope() {
    Map<String, Object> processVars = new HashMap<String, Object>();
    processVars.put("processVar", "processVar");
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("variableScopeProcess", processVars);
    Set<String> executionIds = new HashSet<String>();
    List<Execution> executions = runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).list();
    for (Execution execution : executions) {
        if (!processInstance.getId().equals(execution.getId())) {
            executionIds.add(execution.getId());
            runtimeService.setVariableLocal(execution.getId(), "executionVar", "executionVar");
        }
    }
    List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();
    Set<String> taskIds = new HashSet<String>();
    for (Task task : tasks) {
        taskService.setVariableLocal(task.getId(), "taskVar", "taskVar");
        taskIds.add(task.getId());
    }
    List<VariableInstance> variableInstances = taskService.getVariableInstancesLocalByTaskIds(taskIds);
    assertEquals(variableInstances.size(), 2);
    assertEquals(variableInstances.get(0).getName(), "taskVar");
    assertEquals(variableInstances.get(0).getValue(), "taskVar");
    assertEquals(variableInstances.get(1).getName(), "taskVar");
    assertEquals(variableInstances.get(1).getValue(), "taskVar");
}
Also used : Task(org.activiti.engine.task.Task) HashMap(java.util.HashMap) Execution(org.activiti.engine.runtime.Execution) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) VariableInstance(org.activiti.engine.impl.persistence.entity.VariableInstance) HashSet(java.util.HashSet) Deployment(org.activiti.engine.test.Deployment)

Example 4 with VariableInstance

use of org.activiti.engine.impl.persistence.entity.VariableInstance in project Activiti by Activiti.

the class TaskVariablesTest method testGetVariablesLocalByTaskIds.

@Deployment
public void testGetVariablesLocalByTaskIds() {
    ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey("twoTaskProcess");
    ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey("twoTaskProcess");
    List<Task> taskList1 = taskService.createTaskQuery().processInstanceId(processInstance1.getId()).list();
    List<Task> taskList2 = taskService.createTaskQuery().processInstanceId(processInstance2.getId()).list();
    // Task local variables
    for (Task task : taskList1) {
        if ("usertask1".equals(task.getTaskDefinitionKey())) {
            taskService.setVariableLocal(task.getId(), "taskVar1", "sayHello1");
        } else {
            taskService.setVariableLocal(task.getId(), "taskVar2", "sayHello2");
        }
        // Execution variables
        taskService.setVariable(task.getId(), "executionVar1", "helloWorld1");
    }
    // Task local variables
    for (Task task : taskList2) {
        if ("usertask1".equals(task.getTaskDefinitionKey())) {
            taskService.setVariableLocal(task.getId(), "taskVar3", "sayHello3");
        } else {
            taskService.setVariableLocal(task.getId(), "taskVar4", "sayHello4");
        }
        // Execution variables
        taskService.setVariable(task.getId(), "executionVar2", "helloWorld2");
    }
    // only 1 process
    Set<String> taskIds = new HashSet<String>();
    taskIds.add(taskList1.get(0).getId());
    taskIds.add(taskList1.get(1).getId());
    List<VariableInstance> variables = taskService.getVariableInstancesLocalByTaskIds(taskIds);
    assertEquals(2, variables.size());
    checkVariable(taskList1.get(0).getId(), "taskVar1", "sayHello1", variables);
    checkVariable(taskList1.get(1).getId(), "taskVar2", "sayHello2", variables);
    // 2 process
    taskIds = new HashSet<String>();
    taskIds.add(taskList1.get(0).getId());
    taskIds.add(taskList1.get(1).getId());
    taskIds.add(taskList2.get(0).getId());
    taskIds.add(taskList2.get(1).getId());
    variables = taskService.getVariableInstancesLocalByTaskIds(taskIds);
    assertEquals(4, variables.size());
    checkVariable(taskList1.get(0).getId(), "taskVar1", "sayHello1", variables);
    checkVariable(taskList1.get(1).getId(), "taskVar2", "sayHello2", variables);
    checkVariable(taskList2.get(0).getId(), "taskVar3", "sayHello3", variables);
    checkVariable(taskList2.get(1).getId(), "taskVar4", "sayHello4", variables);
    // mixture 2 process
    taskIds = new HashSet<String>();
    taskIds.add(taskList1.get(0).getId());
    taskIds.add(taskList2.get(1).getId());
    variables = taskService.getVariableInstancesLocalByTaskIds(taskIds);
    assertEquals(2, variables.size());
    checkVariable(taskList1.get(0).getId(), "taskVar1", "sayHello1", variables);
    checkVariable(taskList2.get(1).getId(), "taskVar4", "sayHello4", variables);
}
Also used : Task(org.activiti.engine.task.Task) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) VariableInstance(org.activiti.engine.impl.persistence.entity.VariableInstance) HashSet(java.util.HashSet) Deployment(org.activiti.engine.test.Deployment)

Example 5 with VariableInstance

use of org.activiti.engine.impl.persistence.entity.VariableInstance in project Activiti by Activiti.

the class TaskVariablesTest method checkVariable.

private void checkVariable(String taskId, String name, String value, List<VariableInstance> variables) {
    for (VariableInstance variable : variables) {
        if (taskId.equals(variable.getTaskId())) {
            assertEquals(name, variable.getName());
            assertEquals(value, variable.getValue());
            return;
        }
    }
    fail();
}
Also used : VariableInstance(org.activiti.engine.impl.persistence.entity.VariableInstance)

Aggregations

VariableInstance (org.activiti.engine.impl.persistence.entity.VariableInstance)12 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)6 Deployment (org.activiti.engine.test.Deployment)6 HashSet (java.util.HashSet)5 Task (org.activiti.engine.task.Task)5 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)2 ExecutionEntity (org.activiti.engine.impl.persistence.entity.ExecutionEntity)2 VariableInstanceEntity (org.activiti.engine.impl.persistence.entity.VariableInstanceEntity)2 Execution (org.activiti.engine.runtime.Execution)2 HistoricVariableInstance (org.activiti.engine.history.HistoricVariableInstance)1