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();
}
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;
}
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);
}
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);
}
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);
}
Aggregations