|
| 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.aml.model.caex.CAEXObject; |
| 23 | +import io.adminshell.aas.v3.dataformat.core.util.AasUtils; |
| 24 | +import io.adminshell.aas.v3.dataformat.mapping.MappingException; |
| 25 | +import io.adminshell.aas.v3.model.IdentifierKeyValuePair; |
| 26 | +import java.beans.PropertyDescriptor; |
| 27 | + |
| 28 | +import java.lang.reflect.InvocationTargetException; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Collection; |
| 31 | +import java.util.List; |
| 32 | +import java.util.logging.Level; |
| 33 | +import java.util.logging.Logger; |
| 34 | + |
| 35 | +public class IdentifierKeyValuePairCollectionMapper extends DefaultMapper<Collection<IdentifierKeyValuePair>> { |
| 36 | + |
| 37 | + @Override |
| 38 | + protected Collection mapCollectionValueProperty(AmlParser parser, MappingContext context) throws MappingException { |
| 39 | + Collection result = new ArrayList<>(); |
| 40 | + CAEXObject current = parser.getCurrent(); |
| 41 | + AttributeType parent = findAttribute(current, context.getProperty(), context); |
| 42 | + if (parent == null) { |
| 43 | + return result; |
| 44 | + } |
| 45 | + List<AttributeType> attributes = findAttributes(parent, x -> x.getName().startsWith("specificAssetId")); |
| 46 | + for (AttributeType attribute : attributes) { |
| 47 | + parser.setCurrent(attribute); |
| 48 | + try { |
| 49 | + IdentifierKeyValuePair element = context.getTypeFactory().newInstance(IdentifierKeyValuePair.class); |
| 50 | + for (PropertyDescriptor property : AasUtils.getAasProperties(IdentifierKeyValuePair.class)) { |
| 51 | + Object propertyValue = context |
| 52 | + .with(element) |
| 53 | + .with(property) |
| 54 | + .withoutType() |
| 55 | + .map(property.getReadMethod().getGenericReturnType(), parser); |
| 56 | + if (propertyValue != null) { |
| 57 | + try { |
| 58 | + property.getWriteMethod().invoke(element, propertyValue); |
| 59 | + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { |
| 60 | + throw new MappingException(String.format("error setting property value for property %s", property.getName()), ex); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + parser.setCurrent(attribute); |
| 65 | + result.add(element); |
| 66 | + } catch (NoSuchMethodException ex) { |
| 67 | + Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex); |
| 68 | + } catch (InstantiationException ex) { |
| 69 | + Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex); |
| 70 | + } catch (IllegalAccessException ex) { |
| 71 | + Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex); |
| 72 | + } catch (IllegalArgumentException ex) { |
| 73 | + Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex); |
| 74 | + } catch (InvocationTargetException ex) { |
| 75 | + Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex); |
| 76 | + } |
| 77 | + } |
| 78 | + parser.setCurrent(parent); |
| 79 | + return result; |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments