Search in sources :

Example 16 with ElementValue

use of de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue in project FAAAST-Service by FraunhoferIOSB.

the class SetSubmodelElementValueByPathRequestMapper method parse.

@Override
public Request parse(HttpRequest httpRequest) {
    final List<Key> path = ElementPathHelper.toKeys(EncodingHelper.urlDecode(httpRequest.getPathElements().get(4)));
    final Identifier identifier = IdentifierHelper.parseIdentifier(EncodingHelper.base64Decode(httpRequest.getPathElements().get(1)));
    return SetSubmodelElementValueByPathRequest.builder().id(identifier).path(path).value(httpRequest.getBody()).valueParser(new ElementValueParser<Object>() {

        @Override
        public <U extends ElementValue> U parse(Object raw, Class<U> type) throws DeserializationException {
            if (ElementValue.class.isAssignableFrom(type)) {
                return deserializer.readValue(raw.toString(), serviceContext.getTypeInfo(ReferenceHelper.toReference(path, identifier, Submodel.class)));
            } else if (SubmodelElement.class.isAssignableFrom(type)) {
                SubmodelElement submodelElement = (SubmodelElement) deserializer.read(raw.toString(), type);
                return ElementValueMapper.toValue(submodelElement);
            }
            throw new DeserializationException(String.format("error deserializing payload - invalid type '%s' (must be either instance of ElementValue or SubmodelElement", type.getSimpleName()));
        }
    }).build();
}
Also used : ElementValueParser(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValueParser) Identifier(io.adminshell.aas.v3.model.Identifier) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) Key(io.adminshell.aas.v3.model.Key) DeserializationException(de.fraunhofer.iosb.ilt.faaast.service.dataformat.DeserializationException)

Example 17 with ElementValue

use of de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue in project FAAAST-Service by FraunhoferIOSB.

the class SetSubmodelElementValueByPathRequestHandler method process.

@Override
public SetSubmodelElementValueByPathResponse process(SetSubmodelElementValueByPathRequest request) {
    SetSubmodelElementValueByPathResponse response = new SetSubmodelElementValueByPathResponse();
    try {
        Reference reference = ReferenceHelper.toReference(request.getPath(), request.getId(), Submodel.class);
        SubmodelElement submodelElement = persistence.get(reference, new OutputModifier.Builder().extend(Extend.WithBLOBValue).build());
        ElementValue oldValue = ElementValueMapper.toValue(submodelElement);
        if (request.getValueParser() != null) {
            ElementValue newValue = request.getValueParser().parse(request.getRawValue(), oldValue.getClass());
            ElementValueMapper.setValue(submodelElement, newValue);
            writeValueToAssetConnection(reference, newValue);
            persistence.put(null, reference, submodelElement);
            response.setStatusCode(StatusCode.Success);
            publishValueChangeEventMessage(reference, oldValue, newValue);
        } else {
            throw new RuntimeException("Value parser of request must be non-null");
        }
    } catch (ResourceNotFoundException ex) {
        response.setStatusCode(StatusCode.ClientErrorResourceNotFound);
    } catch (Exception ex) {
        response.setStatusCode(StatusCode.ServerInternalError);
    }
    return response;
}
Also used : SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) OutputModifier(de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.OutputModifier) Reference(io.adminshell.aas.v3.model.Reference) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) SetSubmodelElementValueByPathResponse(de.fraunhofer.iosb.ilt.faaast.service.model.api.response.SetSubmodelElementValueByPathResponse)

Example 18 with ElementValue

use of de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue in project FAAAST-Service by FraunhoferIOSB.

the class JsonDeserializerTest method compareValueMap.

private void compareValueMap(Map<SubmodelElement, File> input) throws DeserializationException, IOException {
    Map<Object, ElementValue> expected = input.keySet().stream().collect(Collectors.toMap(x -> x.getIdShort(), x -> ElementValueMapper.toValue(x)));
    TypeInfo typeInfo = TypeExtractor.extractTypeInfo(input.keySet().stream().collect(Collectors.toMap(x -> x.getIdShort(), x -> x)));
    Map<Object, ElementValue> actual = deserializer.readValueMap(filesAsJsonObject(input), typeInfo);
    Assert.assertEquals(expected, actual);
}
Also used : SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) TypeExtractor(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeExtractor) ElementValueMapper(de.fraunhofer.iosb.ilt.faaast.service.model.value.mapper.ElementValueMapper) Datatype(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype) IOException(java.io.IOException) Test(org.junit.Test) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Level(java.util.logging.Level) List(java.util.List) DeserializationException(de.fraunhofer.iosb.ilt.faaast.service.dataformat.DeserializationException) RangeValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.RangeValue) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) PropertyValues(de.fraunhofer.iosb.ilt.faaast.service.serialization.json.fixture.PropertyValues) Map(java.util.Map) JsonDeserializer(de.fraunhofer.iosb.ilt.faaast.service.dataformat.json.JsonDeserializer) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) Assert(org.junit.Assert) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo)

Example 19 with ElementValue

use of de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue in project FAAAST-Service by FraunhoferIOSB.

the class JsonDeserializerTest method compareValueArray.

private void compareValueArray(Map<SubmodelElement, File> input) throws DeserializationException, IOException {
    Object[] expected = input.keySet().stream().map(x -> ElementValueMapper.toValue(x)).toArray();
    TypeInfo typeInfo = TypeExtractor.extractTypeInfo(input.keySet().toArray());
    ElementValue[] actual = deserializer.readValueArray(filesAsJsonArray(input), typeInfo);
    Assert.assertEquals(expected, actual);
}
Also used : SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) TypeExtractor(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeExtractor) ElementValueMapper(de.fraunhofer.iosb.ilt.faaast.service.model.value.mapper.ElementValueMapper) Datatype(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype) IOException(java.io.IOException) Test(org.junit.Test) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Level(java.util.logging.Level) List(java.util.List) DeserializationException(de.fraunhofer.iosb.ilt.faaast.service.dataformat.DeserializationException) RangeValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.RangeValue) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) PropertyValues(de.fraunhofer.iosb.ilt.faaast.service.serialization.json.fixture.PropertyValues) Map(java.util.Map) JsonDeserializer(de.fraunhofer.iosb.ilt.faaast.service.dataformat.json.JsonDeserializer) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) Assert(org.junit.Assert) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo)

Example 20 with ElementValue

use of de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue in project FAAAST-Service by FraunhoferIOSB.

the class JsonDeserializerTest method compareValue.

private void compareValue(SubmodelElement element, File file) throws DeserializationException, IOException {
    ElementValue expected = ElementValueMapper.toValue(element);
    TypeInfo typeInfo = TypeExtractor.extractTypeInfo(element);
    ElementValue actual = deserializer.readValue(TestUtils.extractValueJson(file, element), typeInfo);
    Assert.assertEquals(expected, actual);
}
Also used : ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo)

Aggregations

ElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue)25 SubmodelElement (io.adminshell.aas.v3.model.SubmodelElement)20 Test (org.junit.Test)15 AnnotatedRelationshipElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.AnnotatedRelationshipElementValue)11 ReferenceElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.ReferenceElementValue)11 RelationshipElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.RelationshipElementValue)11 PropertyValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue)7 TypeInfo (de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo)6 ElementValueMapper (de.fraunhofer.iosb.ilt.faaast.service.model.value.mapper.ElementValueMapper)5 Reference (io.adminshell.aas.v3.model.Reference)5 DefaultProperty (io.adminshell.aas.v3.model.impl.DefaultProperty)5 List (java.util.List)5 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5 DeserializationException (de.fraunhofer.iosb.ilt.faaast.service.dataformat.DeserializationException)4 ResourceNotFoundException (de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException)4 DataElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)4 RangeValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.RangeValue)4 Datatype (de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype)4 TypeExtractor (de.fraunhofer.iosb.ilt.faaast.service.typing.TypeExtractor)4