diff --git a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java index d2ce12523..c068ad20b 100644 --- a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java +++ b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java @@ -39,6 +39,8 @@ import org.eclipse.digitaltwin.aas4j.v3.model.Submodel; import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement; import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection; +import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; +import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation; /** * The AASX package converter converts a aasx package into a list of aas, a list @@ -148,9 +150,11 @@ private List parseReferencedFilePathsFromAASX() throws IOException, Inva read(); List paths = new ArrayList<>(); - for (Submodel sm : environment.getSubmodels()) { - paths.addAll(parseElements(sm.getSubmodelElements())); - } + environment.getAssetAdministrationShells().stream().filter(aas -> aas.getAssetInformation() != null + && aas.getAssetInformation().getDefaultThumbnail() != null + && aas.getAssetInformation().getDefaultThumbnail().getPath() != null) + .forEach(aas -> paths.add(aas.getAssetInformation().getDefaultThumbnail().getPath())); + environment.getSubmodels().forEach(sm -> paths.addAll(parseElements(sm.getSubmodelElements()))); return paths; } diff --git a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java index 095c964e2..71e61ccb8 100644 --- a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java +++ b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java @@ -41,6 +41,8 @@ import org.eclipse.digitaltwin.aas4j.v3.model.Submodel; import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement; import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection; +import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; +import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation; import org.eclipse.digitaltwin.aas4j.v3.dataformat.SerializationException; import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.internal.AASXUtils; @@ -110,7 +112,7 @@ public void write(Environment environment, Collection files, Outpu // Save the XML to aasx/xml/content.xml PackagePart xmlPart = createAASXPart(rootPackage, origin, XML_PATH, MIME_XML, AASSPEC_RELTYPE, xml.getBytes(DEFAULT_CHARSET)); - storeFilesInAASX(environment.getSubmodels(), files, rootPackage, xmlPart); + storeFilesInAASX(environment, files, rootPackage, xmlPart); saveAASX(os, rootPackage); } @@ -118,26 +120,42 @@ public void write(Environment environment, Collection files, Outpu /** * Stores the files from the Submodels in the .aasx file * - * @param submodelList the Submodels + * @param environment the Environment * @param files the content of the files * @param rootPackage the OPCPackage * @param xmlPart the Part the files should be related to */ - private void storeFilesInAASX(List submodelList, Collection files, OPCPackage rootPackage, + private void storeFilesInAASX(Environment environment, Collection files, OPCPackage rootPackage, PackagePart xmlPart) { + environment.getAssetAdministrationShells().stream().filter(aas -> aas.getAssetInformation() != null + && aas.getAssetInformation().getDefaultThumbnail() != null + && aas.getAssetInformation().getDefaultThumbnail().getPath() != null) + .forEach(aas -> createParts(files, + AASXUtils.getPathFromURL(aas.getAssetInformation().getDefaultThumbnail().getPath()), + rootPackage, xmlPart, aas.getAssetInformation().getDefaultThumbnail().getContentType())); + environment.getSubmodels().forEach(sm -> + findFileElements(sm.getSubmodelElements()).forEach(file -> createParts(files, + AASXUtils.getPathFromURL(file.getValue()), rootPackage, xmlPart, file.getContentType()))); + } - for (Submodel sm : submodelList) { - for (File file : findFileElements(sm.getSubmodelElements())) { - String filePath = AASXUtils.getPathFromURL(file.getValue()); - try { - InMemoryFile content = findFileByPath(files, filePath); - logger.trace("Writing file '" + filePath + "' to .aasx."); - createAASXPart(rootPackage, xmlPart, filePath, file.getContentType(), AASSUPPL_RELTYPE, content.getFileContent()); - } catch (RuntimeException e) { - // Log that a file is missing and continue building the .aasx - logger.warn("Could not add File '" + filePath + "'. It was not contained in given InMemoryFiles."); - } - } + /** + * Adds a part to the .aasx file with the given file, filePath and contentType + * + * @param files the content of the files + * @param filePath the path of the file + * @param rootPackage the OPCPackage + * @param xmlPart the Part the files should be related to + * @param contentType the contentType of the file + */ + private void createParts(Collection files, String filePath, OPCPackage rootPackage, + PackagePart xmlPart, String contentType) { + try { + InMemoryFile content = findFileByPath(files, filePath); + logger.trace("Writing file '" + filePath + "' to .aasx."); + createAASXPart(rootPackage, xmlPart, filePath, contentType, AASSUPPL_RELTYPE, content.getFileContent()); + } catch (RuntimeException e) { + // Log that a file is missing and continue building the .aasx + logger.warn("Could not add File '" + filePath + "'. It was not contained in given InMemoryFiles."); } } diff --git a/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/deserialization/AASXDeserializerTest.java b/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/deserialization/AASXDeserializerTest.java index 77425197e..0c274625e 100644 --- a/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/deserialization/AASXDeserializerTest.java +++ b/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/deserialization/AASXDeserializerTest.java @@ -16,6 +16,7 @@ package org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.deserialization; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.io.File; import java.io.FileInputStream; @@ -27,6 +28,7 @@ import javax.xml.parsers.ParserConfigurationException; +import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.AASXDeserializer; import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.AASXSerializer; @@ -50,8 +52,11 @@ public void testRoundTrip() throws SerializationException, IOException, InvalidF List fileList = new ArrayList<>(); byte[] operationManualContent = { 0, 1, 2, 3, 4 }; + byte[] thumbnail = { 0, 1, 2, 3, 4 }; InMemoryFile inMemoryFile = new InMemoryFile(operationManualContent, "file:///aasx/OperatingManual.pdf"); + InMemoryFile inMemoryFileThumbnail = new InMemoryFile(thumbnail, "file:///master/verwaltungsschale-detail-part1.png"); fileList.add(inMemoryFile); + fileList.add(inMemoryFileThumbnail); File file = tempFolder.newFile("output.aasx"); @@ -61,6 +66,6 @@ public void testRoundTrip() throws SerializationException, IOException, InvalidF AASXDeserializer deserializer = new AASXDeserializer(in); assertEquals(AASSimple.createEnvironment(), deserializer.read()); - assertEquals(fileList, deserializer.getRelatedFiles()); + assertTrue(CollectionUtils.isEqualCollection(fileList, deserializer.getRelatedFiles())); } } diff --git a/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/serialization/AASXSerializerTest.java b/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/serialization/AASXSerializerTest.java index cfce0d5c1..e8388f3a9 100644 --- a/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/serialization/AASXSerializerTest.java +++ b/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/serialization/AASXSerializerTest.java @@ -48,8 +48,11 @@ public class AASXSerializerTest { @Before public void setup() throws IOException { byte[] operationManualContent = { 0, 1, 2, 3, 4 }; + byte[] thumbnail = { 0, 1, 2, 3, 4 }; InMemoryFile file = new InMemoryFile(operationManualContent, "file:///aasx/OperatingManual.pdf"); + InMemoryFile inMemoryFileThumbnail = new InMemoryFile(thumbnail, "file:///master/verwaltungsschale-detail-part1.png"); fileList.add(file); + fileList.add(inMemoryFileThumbnail); } @Test