|
| 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.CAEXObject; |
| 22 | +import io.adminshell.aas.v3.dataformat.aml.model.caex.InternalElementType; |
| 23 | +import io.adminshell.aas.v3.dataformat.mapping.MappingException; |
| 24 | +import io.adminshell.aas.v3.model.OperationVariable; |
| 25 | +import io.adminshell.aas.v3.model.SubmodelElement; |
| 26 | + |
| 27 | +import java.lang.reflect.InvocationTargetException; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Collection; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +public class OperationCollectionMapper extends DefaultMapper<Collection<OperationVariable>> { |
| 33 | + |
| 34 | + |
| 35 | + @Override |
| 36 | + protected Collection mapCollectionValueProperty(AmlParser parser, MappingContext context) throws MappingException { |
| 37 | + |
| 38 | + if (parser == null || context == null || context.getProperty() == null) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + List<InternalElementType> variables = findInternalElements(parser.getCurrent(), x -> x.getName().equalsIgnoreCase(context.getProperty().getName())); |
| 43 | + if (variables == null || variables.size() == 0) return null; |
| 44 | + if (variables.size() > 1) |
| 45 | + throw new MappingException(String.format("multiple %s elements are found in %s %s", context.getProperty().getName(), parser.getCurrent().getID(), parser.getCurrent().getName())); |
| 46 | + |
| 47 | + List<InternalElementType> internalElements = findInternalElements(variables.get(0), SubmodelElement.class, true, context); |
| 48 | + |
| 49 | + CAEXObject current = parser.getCurrent(); |
| 50 | + |
| 51 | + if (!internalElements.isEmpty()) { |
| 52 | + Collection result = new ArrayList<>(); |
| 53 | + for (InternalElementType internalElement : internalElements) { |
| 54 | + try { |
| 55 | + parser.setCurrent(internalElement); |
| 56 | + OperationVariable operationVariable = context.getTypeFactory().newInstance(OperationVariable.class); |
| 57 | + |
| 58 | + SubmodelElement submodelElement = (SubmodelElement) context.withoutProperty().map(typeFromInternalElement(internalElement), parser); |
| 59 | + operationVariable.setValue(submodelElement); |
| 60 | + result.add(operationVariable); |
| 61 | + } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { |
| 62 | + throw new MappingException("error mapping Collection<OperationVariable> - could not create new instance for interface OperationVariable"); |
| 63 | + } |
| 64 | + } |
| 65 | + parser.setCurrent(current); |
| 66 | + return result; |
| 67 | + } |
| 68 | + return null; |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | +} |
0 commit comments