|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.adminshell.aas.v3.dataformat.aml.deserialization.mappers; |
| 17 | + |
| 18 | +import io.adminshell.aas.v3.dataformat.aml.deserialization.AmlParser; |
| 19 | +import io.adminshell.aas.v3.dataformat.aml.deserialization.DefaultMapper; |
| 20 | +import io.adminshell.aas.v3.dataformat.aml.deserialization.MappingContext; |
| 21 | +import io.adminshell.aas.v3.dataformat.aml.model.caex.AttributeType; |
| 22 | +import io.adminshell.aas.v3.dataformat.core.util.AasUtils; |
| 23 | +import io.adminshell.aas.v3.dataformat.mapping.MappingException; |
| 24 | +import io.adminshell.aas.v3.model.Range; |
| 25 | + |
| 26 | +import java.beans.PropertyDescriptor; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +/** |
| 30 | + * |
| 31 | + */ |
| 32 | +public class RangeMapper extends DefaultMapper<Range> { |
| 33 | + |
| 34 | + public static final String MIN_ATTRIBUTE_NAME = "min"; |
| 35 | + public static final String MAX_ATTRIBUTE_NAME = "max"; |
| 36 | + protected static PropertyDescriptor PROPERTY_VALUE_TYPE = AasUtils.getProperty(Range.class, "valueType"); |
| 37 | + |
| 38 | + public RangeMapper() { |
| 39 | + super(PROPERTY_VALUE_TYPE.getName()); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void mapProperties(Object parent, AmlParser parser, MappingContext context) throws MappingException { |
| 44 | + if (parent == null || context == null) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + AttributeType attributeTypeMin = findAttributes(parser.getCurrent(), |
| 49 | + x -> x.getName().equalsIgnoreCase(MIN_ATTRIBUTE_NAME)).stream().findFirst().orElse(null); |
| 50 | + |
| 51 | + AttributeType attributeTypeMax = findAttributes(parser.getCurrent(), |
| 52 | + x -> x.getName().equalsIgnoreCase(MAX_ATTRIBUTE_NAME)).stream().findFirst().orElse(null); |
| 53 | + |
| 54 | + if (attributeTypeMax == null || attributeTypeMin == null) |
| 55 | + throw new MappingException(String.format("no min/max attributes are found in range %s %s", parser.getCurrent().getID(), parser.getCurrent().getName())); |
| 56 | + |
| 57 | + String dataTypeMin = getDataTypeFromAttribute(attributeTypeMin); |
| 58 | + String dataTypeMax = getDataTypeFromAttribute(attributeTypeMax); |
| 59 | + |
| 60 | + //TODO: is that a constraint? |
| 61 | + if (!dataTypeMax.equalsIgnoreCase(dataTypeMin)) |
| 62 | + throw new MappingException(String.format("min/max attributes in range %s %s has different attribute datatypes", parser.getCurrent().getID(), parser.getCurrent().getName())); |
| 63 | + |
| 64 | + ((Range) parent).setValueType(dataTypeMax); |
| 65 | + |
| 66 | + super.mapProperties(parent, parser, context); |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments