This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from admin-shell-io/bugfix/serialization
Bugfix/serialization
- Loading branch information
Showing
13 changed files
with
2,325 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...aas/v3/dataformat/aml/deserialization/mappers/IdentifierKeyValuePairCollectionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.adminshell.aas.v3.dataformat.aml.deserialization.mappers; | ||
|
||
import io.adminshell.aas.v3.dataformat.aml.deserialization.AmlParser; | ||
import io.adminshell.aas.v3.dataformat.aml.deserialization.DefaultMapper; | ||
import io.adminshell.aas.v3.dataformat.aml.deserialization.MappingContext; | ||
import io.adminshell.aas.v3.dataformat.aml.model.caex.AttributeType; | ||
import io.adminshell.aas.v3.dataformat.aml.model.caex.CAEXObject; | ||
import io.adminshell.aas.v3.dataformat.core.util.AasUtils; | ||
import io.adminshell.aas.v3.dataformat.mapping.MappingException; | ||
import io.adminshell.aas.v3.model.IdentifierKeyValuePair; | ||
import java.beans.PropertyDescriptor; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class IdentifierKeyValuePairCollectionMapper extends DefaultMapper<Collection<IdentifierKeyValuePair>> { | ||
|
||
@Override | ||
protected Collection mapCollectionValueProperty(AmlParser parser, MappingContext context) throws MappingException { | ||
Collection result = new ArrayList<>(); | ||
CAEXObject current = parser.getCurrent(); | ||
AttributeType parent = findAttribute(current, context.getProperty(), context); | ||
if (parent == null) { | ||
return result; | ||
} | ||
List<AttributeType> attributes = findAttributes(parent, x -> x.getName().startsWith("specificAssetId")); | ||
for (AttributeType attribute : attributes) { | ||
parser.setCurrent(attribute); | ||
try { | ||
IdentifierKeyValuePair element = context.getTypeFactory().newInstance(IdentifierKeyValuePair.class); | ||
for (PropertyDescriptor property : AasUtils.getAasProperties(IdentifierKeyValuePair.class)) { | ||
Object propertyValue = context | ||
.with(element) | ||
.with(property) | ||
.withoutType() | ||
.map(property.getReadMethod().getGenericReturnType(), parser); | ||
if (propertyValue != null) { | ||
try { | ||
property.getWriteMethod().invoke(element, propertyValue); | ||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
throw new MappingException(String.format("error setting property value for property %s", property.getName()), ex); | ||
} | ||
} | ||
} | ||
parser.setCurrent(attribute); | ||
result.add(element); | ||
} catch (NoSuchMethodException ex) { | ||
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex); | ||
} catch (InstantiationException ex) { | ||
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex); | ||
} catch (IllegalAccessException ex) { | ||
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex); | ||
} catch (IllegalArgumentException ex) { | ||
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex); | ||
} catch (InvocationTargetException ex) { | ||
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex); | ||
} | ||
} | ||
parser.setCurrent(parent); | ||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...l/aas/v3/dataformat/aml/serialization/mappers/IdentifierKeyValuePairCollectionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.adminshell.aas.v3.dataformat.aml.serialization.mappers; | ||
|
||
import io.adminshell.aas.v3.dataformat.aml.serialization.DefaultMapper; | ||
import io.adminshell.aas.v3.dataformat.aml.serialization.AmlGenerator; | ||
import io.adminshell.aas.v3.dataformat.aml.serialization.MappingContext; | ||
import io.adminshell.aas.v3.dataformat.aml.model.caex.AttributeType; | ||
import io.adminshell.aas.v3.dataformat.core.util.AasUtils; | ||
import io.adminshell.aas.v3.dataformat.mapping.MappingException; | ||
import io.adminshell.aas.v3.model.AssetInformation; | ||
import io.adminshell.aas.v3.model.IdentifierKeyValuePair; | ||
import java.beans.PropertyDescriptor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public class IdentifierKeyValuePairCollectionMapper extends DefaultMapper<Collection<IdentifierKeyValuePair>> { | ||
|
||
private static final String ATTRIBUTE_NAME = "specificAssetId"; | ||
|
||
@Override | ||
public void map(Collection<IdentifierKeyValuePair> value, AmlGenerator generator, MappingContext context) throws MappingException { | ||
if (value == null || context == null || value.isEmpty()) { | ||
return; | ||
} | ||
AttributeType.Builder wrapperBuilder = AttributeType.builder() | ||
.withName(ATTRIBUTE_NAME) | ||
.withRefSemantic(generator.refSemantic(AssetInformation.class, ATTRIBUTE_NAME)); | ||
List<AttributeType> attributes = new ArrayList<>(); | ||
for (IdentifierKeyValuePair element : value) { | ||
AttributeType.Builder builder = AttributeType.builder() | ||
.withName(ATTRIBUTE_NAME + (value.size() > 1 ? "_" + (attributes.size() + 1) : "")); | ||
for (PropertyDescriptor property : AasUtils.getAasProperties(element.getClass())) { | ||
if (!skipProperty(property)) { | ||
context.with(property) | ||
.map(property.getReadMethod().getGenericReturnType(), | ||
getElemenetPropertyValue(element, property, context), | ||
generator.with(builder)); | ||
} | ||
} | ||
attributes.add(builder.build()); | ||
} | ||
attributes.forEach(x -> wrapperBuilder.addAttribute(x)); | ||
generator.add(wrapperBuilder.build()); | ||
} | ||
|
||
protected Object getElemenetPropertyValue(IdentifierKeyValuePair elemenet, PropertyDescriptor property, MappingContext context) throws MappingException { | ||
try { | ||
return property.getReadMethod().invoke(elemenet); | ||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
throw new MappingException("failed to get property value for property " + property.getName(), ex); | ||
} | ||
} | ||
} |
Oops, something went wrong.