Search in sources :

Example 1 with ValueType

use of org.camunda.bpm.engine.variable.type.ValueType in project camunda-bpm-platform by camunda.

the class VariableValueDto method fromTypedValue.

public static void fromTypedValue(VariableValueDto dto, TypedValue typedValue, boolean preferSerializedValue) {
    ValueType type = typedValue.getType();
    if (type != null) {
        String typeName = type.getName();
        dto.setType(toRestApiTypeName(typeName));
        dto.setValueInfo(type.getValueInfo(typedValue));
    }
    if (typedValue instanceof SerializableValue) {
        SerializableValue serializableValue = (SerializableValue) typedValue;
        if (serializableValue.isDeserialized() && !preferSerializedValue) {
            dto.setValue(serializableValue.getValue());
        } else {
            dto.setValue(serializableValue.getValueSerialized());
        }
    } else if (typedValue instanceof FileValue) {
    // do not set the value for FileValues since we don't want to send megabytes over the network without explicit request
    } else {
        dto.setValue(typedValue.getValue());
    }
}
Also used : FileValue(org.camunda.bpm.engine.variable.value.FileValue) SerializableValue(org.camunda.bpm.engine.variable.value.SerializableValue) SerializableValueType(org.camunda.bpm.engine.variable.type.SerializableValueType) FileValueType(org.camunda.bpm.engine.variable.type.FileValueType) ValueType(org.camunda.bpm.engine.variable.type.ValueType) PrimitiveValueType(org.camunda.bpm.engine.variable.type.PrimitiveValueType)

Example 2 with ValueType

use of org.camunda.bpm.engine.variable.type.ValueType in project camunda-bpm-platform by camunda.

the class DefaultVariableSerializers method findSerializerForValue.

public TypedValueSerializer<?> findSerializerForValue(TypedValue value, VariableSerializerFactory fallBackSerializerFactory) {
    String defaultSerializationFormat = Context.getProcessEngineConfiguration().getDefaultSerializationFormat();
    List<TypedValueSerializer<?>> matchedSerializers = new ArrayList<TypedValueSerializer<?>>();
    ValueType type = value.getType();
    if (type != null && type.isAbstract()) {
        throw new ProcessEngineException("Cannot serialize value of abstract type " + type.getName());
    }
    for (TypedValueSerializer<?> serializer : serializerList) {
        if (type == null || serializer.getType().equals(type)) {
            if (serializer.canHandle(value)) {
                matchedSerializers.add(serializer);
                if (serializer.getType().isPrimitiveValueType()) {
                    break;
                }
            }
        }
    }
    if (matchedSerializers.size() == 0) {
        if (fallBackSerializerFactory != null) {
            TypedValueSerializer<?> serializer = fallBackSerializerFactory.getSerializer(value);
            if (serializer != null) {
                return serializer;
            }
        }
        throw new ProcessEngineException("Cannot find serializer for value '" + value + "'.");
    } else if (matchedSerializers.size() == 1) {
        return matchedSerializers.get(0);
    } else {
        // ambiguous match, use default serializer
        if (defaultSerializationFormat != null) {
            for (TypedValueSerializer<?> typedValueSerializer : matchedSerializers) {
                if (defaultSerializationFormat.equals(typedValueSerializer.getSerializationDataformat())) {
                    return typedValueSerializer;
                }
            }
        }
        // no default serialization dataformat defined or default dataformat cannot serialize this value => use first serializer
        return matchedSerializers.get(0);
    }
}
Also used : ValueType(org.camunda.bpm.engine.variable.type.ValueType) ArrayList(java.util.ArrayList) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException)

Example 3 with ValueType

use of org.camunda.bpm.engine.variable.type.ValueType in project camunda-bpm-platform by camunda.

the class VariableValueDto method toTypedValue.

public TypedValue toTypedValue(ProcessEngine processEngine, ObjectMapper objectMapper) {
    ValueTypeResolver valueTypeResolver = processEngine.getProcessEngineConfiguration().getValueTypeResolver();
    if (type == null) {
        if (valueInfo != null && valueInfo.get(ValueType.VALUE_INFO_TRANSIENT) instanceof Boolean) {
            return Variables.untypedValue(value, (Boolean) valueInfo.get(ValueType.VALUE_INFO_TRANSIENT));
        }
        return Variables.untypedValue(value);
    }
    ValueType valueType = valueTypeResolver.typeForName(fromRestApiTypeName(type));
    if (valueType == null) {
        throw new RestException(Status.BAD_REQUEST, String.format("Unsupported value type '%s'", type));
    } else {
        if (valueType instanceof PrimitiveValueType) {
            PrimitiveValueType primitiveValueType = (PrimitiveValueType) valueType;
            Class<?> javaType = primitiveValueType.getJavaType();
            Object mappedValue = null;
            try {
                if (value != null) {
                    if (javaType.isAssignableFrom(value.getClass())) {
                        mappedValue = value;
                    } else {
                        // use jackson to map the value to the requested java type
                        mappedValue = objectMapper.readValue("\"" + value + "\"", javaType);
                    }
                }
                return valueType.createValue(mappedValue, valueInfo);
            } catch (Exception e) {
                throw new InvalidRequestException(Status.BAD_REQUEST, e, String.format("Cannot convert value '%s' of type '%s' to java type %s", value, type, javaType.getName()));
            }
        } else if (valueType instanceof SerializableValueType) {
            if (value != null && !(value instanceof String)) {
                throw new InvalidRequestException(Status.BAD_REQUEST, "Must provide 'null' or String value for value of SerializableValue type '" + type + "'.");
            }
            return ((SerializableValueType) valueType).createValueFromSerialized((String) value, valueInfo);
        } else if (valueType instanceof FileValueType) {
            if (value instanceof String) {
                value = Base64.decodeBase64((String) value);
            }
            return valueType.createValue(value, valueInfo);
        } else {
            return valueType.createValue(value, valueInfo);
        }
    }
}
Also used : SerializableValueType(org.camunda.bpm.engine.variable.type.SerializableValueType) SerializableValueType(org.camunda.bpm.engine.variable.type.SerializableValueType) FileValueType(org.camunda.bpm.engine.variable.type.FileValueType) ValueType(org.camunda.bpm.engine.variable.type.ValueType) PrimitiveValueType(org.camunda.bpm.engine.variable.type.PrimitiveValueType) PrimitiveValueType(org.camunda.bpm.engine.variable.type.PrimitiveValueType) RestException(org.camunda.bpm.engine.rest.exception.RestException) ValueTypeResolver(org.camunda.bpm.engine.variable.type.ValueTypeResolver) InvalidRequestException(org.camunda.bpm.engine.rest.exception.InvalidRequestException) FileValueType(org.camunda.bpm.engine.variable.type.FileValueType) InvalidRequestException(org.camunda.bpm.engine.rest.exception.InvalidRequestException) MimeTypeParseException(javax.activation.MimeTypeParseException) RestException(org.camunda.bpm.engine.rest.exception.RestException)

Example 4 with ValueType

use of org.camunda.bpm.engine.variable.type.ValueType in project camunda-bpm-platform by camunda.

the class CompositeQueryVariableValueCondition method initializeValue.

public void initializeValue(VariableSerializers serializers) {
    TypedValue typedValue = wrappedQueryValue.getTypedValue();
    ValueTypeResolver resolver = Context.getProcessEngineConfiguration().getValueTypeResolver();
    Collection<ValueType> concreteTypes = resolver.getSubTypes(typedValue.getType());
    for (ValueType type : concreteTypes) {
        if (type.canConvertFromTypedValue(typedValue)) {
            TypedValue convertedValue = type.convertFromTypedValue(typedValue);
            SingleQueryVariableValueCondition aggregatedValue = new SingleQueryVariableValueCondition(wrappedQueryValue);
            aggregatedValue.initializeValue(serializers, convertedValue);
            aggregatedValues.add(aggregatedValue);
        }
    }
}
Also used : ValueType(org.camunda.bpm.engine.variable.type.ValueType) ValueTypeResolver(org.camunda.bpm.engine.variable.type.ValueTypeResolver) TypedValue(org.camunda.bpm.engine.variable.value.TypedValue)

Example 5 with ValueType

use of org.camunda.bpm.engine.variable.type.ValueType in project camunda-bpm-platform by camunda.

the class ValueTypeResolverImpl method getSubTypes.

public Collection<ValueType> getSubTypes(ValueType type) {
    List<ValueType> types = new ArrayList<ValueType>();
    Set<ValueType> validParents = new HashSet<ValueType>();
    validParents.add(type);
    for (ValueType knownType : knownTypes.values()) {
        if (validParents.contains(knownType.getParent())) {
            validParents.add(knownType);
            if (!knownType.isAbstract()) {
                types.add(knownType);
            }
        }
    }
    return types;
}
Also used : ValueType(org.camunda.bpm.engine.variable.type.ValueType) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

ValueType (org.camunda.bpm.engine.variable.type.ValueType)5 ArrayList (java.util.ArrayList)2 FileValueType (org.camunda.bpm.engine.variable.type.FileValueType)2 PrimitiveValueType (org.camunda.bpm.engine.variable.type.PrimitiveValueType)2 SerializableValueType (org.camunda.bpm.engine.variable.type.SerializableValueType)2 ValueTypeResolver (org.camunda.bpm.engine.variable.type.ValueTypeResolver)2 HashSet (java.util.HashSet)1 MimeTypeParseException (javax.activation.MimeTypeParseException)1 ProcessEngineException (org.camunda.bpm.engine.ProcessEngineException)1 InvalidRequestException (org.camunda.bpm.engine.rest.exception.InvalidRequestException)1 RestException (org.camunda.bpm.engine.rest.exception.RestException)1 FileValue (org.camunda.bpm.engine.variable.value.FileValue)1 SerializableValue (org.camunda.bpm.engine.variable.value.SerializableValue)1 TypedValue (org.camunda.bpm.engine.variable.value.TypedValue)1