Search in sources :

Example 41 with TypedValue

use of org.camunda.bpm.engine.variable.value.TypedValue in project camunda-bpm-platform by camunda.

the class ProcessVariableMapTest method testProcessVariableMap.

@Test
public void testProcessVariableMap() {
    BusinessProcess businessProcess = getBeanInstance(BusinessProcess.class);
    VariableMap variables = (VariableMap) getBeanInstance("processVariableMap");
    assertNotNull(variables);
    // /////////////////////////////////////////////////////////////////
    // Put a variable via BusinessProcess and get it via VariableMap //
    // /////////////////////////////////////////////////////////////////
    String aValue = "aValue";
    businessProcess.setVariable(VARNAME_1, Variables.stringValue(aValue));
    // Legacy API
    assertEquals(aValue, variables.get(VARNAME_1));
    // Typed variable API
    TypedValue aTypedValue = variables.getValueTyped(VARNAME_1);
    assertEquals(ValueType.STRING, aTypedValue.getType());
    assertEquals(aValue, aTypedValue.getValue());
    assertEquals(aValue, variables.getValue(VARNAME_1, String.class));
    // Type API with wrong type
    try {
        variables.getValue(VARNAME_1, Integer.class);
        fail("ClassCastException expected!");
    } catch (ClassCastException ex) {
        assertEquals("Cannot cast variable named 'aVariable' with value 'aValue' to type 'class java.lang.Integer'.", ex.getMessage());
    }
    // /////////////////////////////////////////////////////////////////
    // Put a variable via VariableMap and get it via BusinessProcess //
    // /////////////////////////////////////////////////////////////////
    String anotherValue = "anotherValue";
    variables.put(VARNAME_2, Variables.stringValue(anotherValue));
    // Legacy API
    assertEquals(anotherValue, businessProcess.getVariable(VARNAME_2));
    // Typed variable API
    TypedValue anotherTypedValue = businessProcess.getVariableTyped(VARNAME_2);
    assertEquals(ValueType.STRING, anotherTypedValue.getType());
    assertEquals(anotherValue, anotherTypedValue.getValue());
}
Also used : VariableMap(org.camunda.bpm.engine.variable.VariableMap) BusinessProcess(org.camunda.bpm.engine.cdi.BusinessProcess) TypedValue(org.camunda.bpm.engine.variable.value.TypedValue) Test(org.junit.Test)

Example 42 with TypedValue

use of org.camunda.bpm.engine.variable.value.TypedValue in project camunda-bpm-platform by camunda.

the class StartProcessTest method testStartProcessByKey.

@Test
@Deployment(resources = "org/camunda/bpm/engine/cdi/test/api/annotation/StartProcessTest.bpmn20.xml")
public void testStartProcessByKey() {
    assertNull(runtimeService.createProcessInstanceQuery().singleResult());
    getBeanInstance(DeclarativeProcessController.class).startProcessByKey();
    BusinessProcess businessProcess = getBeanInstance(BusinessProcess.class);
    assertNotNull(runtimeService.createProcessInstanceQuery().singleResult());
    assertEquals("camunda", businessProcess.getVariable("name"));
    TypedValue nameTypedValue = businessProcess.getVariableTyped("name");
    assertNotNull(nameTypedValue);
    assertTrue(nameTypedValue instanceof StringValue);
    assertEquals(ValueType.STRING, nameTypedValue.getType());
    assertEquals("camunda", nameTypedValue.getValue());
    assertEquals("untypedName", businessProcess.getVariable("untypedName"));
    TypedValue untypedNameTypedValue = businessProcess.getVariableTyped("untypedName");
    assertNotNull(untypedNameTypedValue);
    assertTrue(untypedNameTypedValue instanceof StringValue);
    assertEquals(ValueType.STRING, untypedNameTypedValue.getType());
    assertEquals("untypedName", untypedNameTypedValue.getValue());
    assertEquals("typedName", businessProcess.getVariable("typedName"));
    TypedValue typedNameTypedValue = businessProcess.getVariableTyped("typedName");
    assertNotNull(typedNameTypedValue);
    assertTrue(typedNameTypedValue instanceof StringValue);
    assertEquals(ValueType.STRING, typedNameTypedValue.getType());
    assertEquals("typedName", typedNameTypedValue.getValue());
    businessProcess.startTask(taskService.createTaskQuery().singleResult().getId());
    businessProcess.completeTask();
}
Also used : DeclarativeProcessController(org.camunda.bpm.engine.cdi.test.impl.beans.DeclarativeProcessController) StringValue(org.camunda.bpm.engine.variable.value.StringValue) BusinessProcess(org.camunda.bpm.engine.cdi.BusinessProcess) TypedValue(org.camunda.bpm.engine.variable.value.TypedValue) Test(org.junit.Test) Deployment(org.camunda.bpm.engine.test.Deployment)

Example 43 with TypedValue

use of org.camunda.bpm.engine.variable.value.TypedValue in project camunda-bpm-platform by camunda.

the class AbstractVariablesResource method putVariable.

@Override
public void putVariable(String variableName, VariableValueDto variable) {
    try {
        TypedValue typedValue = variable.toTypedValue(engine, objectMapper);
        setVariableEntity(variableName, typedValue);
    } catch (RestException e) {
        throw new InvalidRequestException(e.getStatus(), e, String.format("Cannot put %s variable %s: %s", getResourceTypeName(), variableName, e.getMessage()));
    } catch (BadUserRequestException e) {
        throw new RestException(Status.BAD_REQUEST, e, String.format("Cannot put %s variable %s: %s", getResourceTypeName(), variableName, e.getMessage()));
    } catch (AuthorizationException e) {
        throw e;
    } catch (ProcessEngineException e) {
        throw new RestException(Status.INTERNAL_SERVER_ERROR, e, String.format("Cannot put %s variable %s: %s", getResourceTypeName(), variableName, e.getMessage()));
    }
}
Also used : AuthorizationException(org.camunda.bpm.engine.AuthorizationException) RestException(org.camunda.bpm.engine.rest.exception.RestException) InvalidRequestException(org.camunda.bpm.engine.rest.exception.InvalidRequestException) BadUserRequestException(org.camunda.bpm.engine.BadUserRequestException) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException) TypedValue(org.camunda.bpm.engine.variable.value.TypedValue)

Example 44 with TypedValue

use of org.camunda.bpm.engine.variable.value.TypedValue in project camunda-bpm-platform by camunda.

the class AbstractVariablesResource method setBinaryVariable.

public void setBinaryVariable(String variableKey, MultipartFormData payload) {
    FormPart dataPart = payload.getNamedPart("data");
    FormPart objectTypePart = payload.getNamedPart("type");
    FormPart valueTypePart = payload.getNamedPart("valueType");
    if (objectTypePart != null) {
        Object object = null;
        if (dataPart.getContentType() != null && dataPart.getContentType().toLowerCase().contains(MediaType.APPLICATION_JSON)) {
            object = deserializeJsonObject(objectTypePart.getTextContent(), dataPart.getBinaryContent());
        } else {
            throw new InvalidRequestException(Status.BAD_REQUEST, "Unrecognized content type for serialized java type: " + dataPart.getContentType());
        }
        if (object != null) {
            setVariableEntity(variableKey, Variables.objectValue(object).create());
        }
    } else {
        String valueTypeName = DEFAULT_BINARY_VALUE_TYPE;
        if (valueTypePart != null) {
            if (valueTypePart.getTextContent() == null) {
                throw new InvalidRequestException(Status.BAD_REQUEST, "Form part with name 'valueType' must have a text/plain value");
            }
            valueTypeName = valueTypePart.getTextContent();
        }
        VariableValueDto valueDto = VariableValueDto.fromFormPart(valueTypeName, dataPart);
        try {
            TypedValue typedValue = valueDto.toTypedValue(engine, objectMapper);
            setVariableEntity(variableKey, typedValue);
        } catch (AuthorizationException e) {
            throw e;
        } catch (ProcessEngineException e) {
            String errorMessage = String.format("Cannot put %s variable %s: %s", getResourceTypeName(), variableKey, e.getMessage());
            throw new RestException(Status.INTERNAL_SERVER_ERROR, e, errorMessage);
        }
    }
}
Also used : FormPart(org.camunda.bpm.engine.rest.mapper.MultipartFormData.FormPart) VariableValueDto(org.camunda.bpm.engine.rest.dto.VariableValueDto) AuthorizationException(org.camunda.bpm.engine.AuthorizationException) RestException(org.camunda.bpm.engine.rest.exception.RestException) InvalidRequestException(org.camunda.bpm.engine.rest.exception.InvalidRequestException) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException) TypedValue(org.camunda.bpm.engine.variable.value.TypedValue)

Example 45 with TypedValue

use of org.camunda.bpm.engine.variable.value.TypedValue in project camunda-bpm-platform by camunda.

the class DecisionDefinitionResourceImpl method createResultEntriesDto.

protected Map<String, VariableValueDto> createResultEntriesDto(DmnDecisionResultEntries entries) {
    VariableMap variableMap = Variables.createVariables();
    for (String key : entries.keySet()) {
        TypedValue typedValue = entries.getEntryTyped(key);
        variableMap.putValueTyped(key, typedValue);
    }
    return VariableValueDto.fromVariableMap(variableMap);
}
Also used : VariableMap(org.camunda.bpm.engine.variable.VariableMap) TypedValue(org.camunda.bpm.engine.variable.value.TypedValue)

Aggregations

TypedValue (org.camunda.bpm.engine.variable.value.TypedValue)81 Test (org.junit.Test)25 Deployment (org.camunda.bpm.engine.test.Deployment)15 DmnEngineTest (org.camunda.bpm.dmn.engine.test.DmnEngineTest)14 ProcessInstance (org.camunda.bpm.engine.runtime.ProcessInstance)11 DecisionResource (org.camunda.bpm.dmn.engine.test.DecisionResource)8 VariableMap (org.camunda.bpm.engine.variable.VariableMap)7 DmnDataTypeTransformer (org.camunda.bpm.dmn.engine.impl.spi.type.DmnDataTypeTransformer)6 BusinessProcess (org.camunda.bpm.engine.cdi.BusinessProcess)6 DmnDecisionResult (org.camunda.bpm.dmn.engine.DmnDecisionResult)4 DmnDecisionTableResult (org.camunda.bpm.dmn.engine.DmnDecisionTableResult)4 DmnExpressionImpl (org.camunda.bpm.dmn.engine.impl.DmnExpressionImpl)3 DeclarativeProcessController (org.camunda.bpm.engine.cdi.test.impl.beans.DeclarativeProcessController)3 RestException (org.camunda.bpm.engine.rest.exception.RestException)3 Task (org.camunda.bpm.engine.task.Task)3 ArrayList (java.util.ArrayList)2 DmnDecisionResultEntries (org.camunda.bpm.dmn.engine.DmnDecisionResultEntries)2 DmnDecisionRuleResult (org.camunda.bpm.dmn.engine.DmnDecisionRuleResult)2 AuthorizationException (org.camunda.bpm.engine.AuthorizationException)2 ProcessEngineException (org.camunda.bpm.engine.ProcessEngineException)2