Skip to content

Commit

Permalink
AAS Thumbnail operations (#150)
Browse files Browse the repository at this point in the history
* merge conflict
AAS thumbnail operation

Signed-off-by: Zai Müller-Zhang <[email protected]>

* Revocer and update authors of CrudAasRepository

Signed-off-by: Zai Müller-Zhang <[email protected]>

* Refactor test logic

Signed-off-by: Zai Müller-Zhang <[email protected]>

* Optimize exception handling.

Signed-off-by: Zai Müller-Zhang <[email protected]>

* Adjust thumbnail operations to actual state

Signed-off-by: Zai Müller-Zhang <[email protected]>

* Minor changes for code refactoring

Signed-off-by: Zai Müller-Zhang <[email protected]>

* Move private method to the end.

Signed-off-by: Zai Müller-Zhang <[email protected]>

* Resolve conflict in BaSyxExceptionHandler.java

Signed-off-by: Zai Müller-Zhang <[email protected]>

* Fix ambiguous exception handling

Signed-off-by: Zai Müller-Zhang <[email protected]>

---------

Signed-off-by: Zai Müller-Zhang <[email protected]>
  • Loading branch information
zhangzai123 authored Dec 1, 2023
1 parent 745b31f commit bbfdf39
Show file tree
Hide file tree
Showing 21 changed files with 595 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,7 @@ public void deleteAll(Iterable<? extends AssetAdministrationShell> entities) {
public void deleteAll() {
inMemoryStore.clear();
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.eclipse.digitaltwin.basyx.aasrepository.backend.AasBackendProvider;
import org.eclipse.digitaltwin.basyx.aasrepository.backend.CrudAasRepository;
import org.eclipse.digitaltwin.basyx.aasrepository.backend.SimpleAasRepositoryFactory;
import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendProvider;
import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory;
import org.junit.Test;

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions basyx.aasrepository/basyx.aasrepository-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*******************************************************************************
* Copyright (C) 2023 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
******************************************************************************/

package org.eclipse.digitaltwin.basyx.aasrepository.backend;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;

import org.apache.commons.io.IOUtils;
import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation;
import org.eclipse.digitaltwin.aas4j.v3.model.Resource;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultResource;
import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository;
import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException;
import org.eclipse.digitaltwin.basyx.core.exceptions.FileHandlingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AASThumbnailHandler {

private static Logger logger = LoggerFactory.getLogger(AASThumbnailHandler.class);

public static void updateThumbnail(AasRepository aasRepo, String aasId, String contentType, String filePath) {
AssetInformation assetInfor = aasRepo.getAssetInformation(aasId);
assetInfor.getDefaultThumbnail().setContentType(contentType);
assetInfor.getDefaultThumbnail().setPath(filePath);
aasRepo.setAssetInformation(aasId, assetInfor);
}

public static void setNewThumbnail(AasRepository aasRepo, String aasId, String contentType, String filePath) {
Resource resource = new DefaultResource();
resource.setContentType(contentType);
resource.setPath(filePath);
AssetInformation assetInfor = aasRepo.getAssetInformation(aasId);
assetInfor.setDefaultThumbnail(resource);
aasRepo.setAssetInformation(aasId, assetInfor);
}

public static void throwIfFileDoesNotExist(String aasId, Resource resource) {
if (resource == null)
throw new FileDoesNotExistException(aasId);

String filePath = resource.getPath();
throwIfFilePathIsNotValid(aasId, filePath);
}

public static String createFilePath(String tmpDirectory, String aasId, String fileName) {
return tmpDirectory + "/" + aasId + "-" + "Thumbnail" + "-" + fileName;
}

public static void createFileAtSpecifiedPath(String fileName, InputStream inputStream, String filePath) {
java.io.File targetFile = new java.io.File(filePath);

try (FileOutputStream outStream = new FileOutputStream(targetFile)) {
IOUtils.copy(inputStream, outStream);
} catch (IOException e) {
throw new FileHandlingException(fileName);
}
}

public static void deleteExistingFile(String path) {
if (path == null || path.isEmpty())
return;

try {
Files.deleteIfExists(Paths.get(path, ""));
} catch (IOException e) {
logger.error("Unable to delete the file having path '{}'", path);
}
}

public static String getTemporaryDirectoryPath() {
String tempDirectoryPath = "";
try {
tempDirectoryPath = Files.createTempDirectory("basyx-temp-thumbnail").toAbsolutePath().toString();
} catch (IOException e) {
logger.error("Unable to create file in the temporary path.");
}
return tempDirectoryPath;
}

private static void throwIfFilePathIsNotValid(String aasId, String filePath) {
if (filePath.isEmpty())
throw new FileDoesNotExistException(aasId);
try {
Paths.get(filePath);
} catch (InvalidPathException | NullPointerException ex) {
throw new FileDoesNotExistException(aasId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
******************************************************************************/
package org.eclipse.digitaltwin.basyx.aasrepository.backend;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Collectors;
Expand All @@ -32,6 +34,7 @@
import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation;
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
import org.eclipse.digitaltwin.aas4j.v3.model.Resource;
import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository;
import org.eclipse.digitaltwin.basyx.aasservice.AasService;
import org.eclipse.digitaltwin.basyx.aasservice.AasServiceFactory;
Expand All @@ -45,9 +48,10 @@
import org.springframework.data.repository.CrudRepository;

/**
* Default Implementation for the {@link AasRepository} based on Spring {@link CrudRepository}
* Default Implementation for the {@link AasRepository} based on Spring
* {@link CrudRepository}
*
* @author mateusmolina, despen
* @author mateusmolina, despen, zhangzai
*
*/
public class CrudAasRepository implements AasRepository {
Expand All @@ -57,15 +61,15 @@ public class CrudAasRepository implements AasRepository {
private AasServiceFactory aasServiceFactory;

private String aasRepositoryName = null;

public CrudAasRepository(AasBackendProvider aasBackendProvider, AasServiceFactory aasServiceFactory) {
this.aasBackend = aasBackendProvider.getCrudRepository();
this.aasServiceFactory = aasServiceFactory;
}

public CrudAasRepository(AasBackendProvider aasBackendProvider, AasServiceFactory aasServiceFactory, @Value("${basyx.aasrepo.name:aas-repo}") String aasRepositoryName) {
this(aasBackendProvider, aasServiceFactory);

this.aasRepositoryName = aasRepositoryName;
}

Expand Down Expand Up @@ -147,6 +151,44 @@ public AssetInformation getAssetInformation(String aasId) throws ElementDoesNotE
return getAasServiceOrThrow(aasId).getAssetInformation();
}


@Override
public String getName() {
return aasRepositoryName == null ? AasRepository.super.getName() : aasRepositoryName;
}

@Override
public File getThumbnail(String aasId) {
Resource resource = getAssetInformation(aasId).getDefaultThumbnail();

AASThumbnailHandler.throwIfFileDoesNotExist(aasId, resource);
String filePath = resource.getPath();
return new File(filePath);
}

@Override
public void setThumbnail(String aasId, String fileName, String contentType, InputStream inputStream) {
Resource thumbnail = getAssetInformation(aasId).getDefaultThumbnail();

if (thumbnail != null) {
updateThumbnailFile(aasId, fileName, contentType, inputStream, thumbnail);
return;
}

String filePath = createFile(aasId, fileName, inputStream);
AASThumbnailHandler.setNewThumbnail(this, aasId, contentType, filePath);
}

@Override
public void deleteThumbnail(String aasId) {
Resource thumbnail = getAssetInformation(aasId).getDefaultThumbnail();
AASThumbnailHandler.throwIfFileDoesNotExist(aasId, thumbnail);

deleteThumbnailFile(thumbnail);

updateThumbnailInAssetInformation(aasId);
}

private AasService getAasServiceOrThrow(String aasId) {
AssetAdministrationShell aas = aasBackend.findById(aasId).orElseThrow(() -> new ElementDoesNotExistException(aasId));

Expand All @@ -170,9 +212,30 @@ private void throwIfAasDoesNotExist(String aasId) {
throw new ElementDoesNotExistException(aasId);
}

@Override
public String getName() {
return aasRepositoryName == null ? AasRepository.super.getName() : aasRepositoryName;
private void updateThumbnailInAssetInformation(String aasId) {
AssetInformation assetInfor = getAssetInformation(aasId);
assetInfor.getDefaultThumbnail().setContentType("");
assetInfor.getDefaultThumbnail().setPath("");
setAssetInformation(aasId, assetInfor);
}

private void deleteThumbnailFile(Resource thumbnail) {
String filePath = thumbnail.getPath();
java.io.File tmpFile = new java.io.File(filePath);
tmpFile.delete();
}

private void updateThumbnailFile(String aasId, String fileName, String contentType, InputStream inputStream, Resource thumbnail) {
String path = thumbnail.getPath();
AASThumbnailHandler.deleteExistingFile(path);
String filePath = createFile(aasId, fileName, inputStream);
AASThumbnailHandler.updateThumbnail(this, aasId, contentType, filePath);
}

private String createFile(String aasId, String fileName, InputStream inputStream) {
String filePath = AASThumbnailHandler.createFilePath(AASThumbnailHandler.getTemporaryDirectoryPath(), aasId, fileName);
AASThumbnailHandler.createFileAtSpecifiedPath(fileName, inputStream, filePath);
return filePath;
}

}
8 changes: 7 additions & 1 deletion basyx.aasrepository/basyx.aasrepository-core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -11,6 +12,11 @@
<artifactId>basyx.aasrepository-core</artifactId>

<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
******************************************************************************/
package org.eclipse.digitaltwin.basyx.aasrepository;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
Expand Down Expand Up @@ -121,6 +123,37 @@ public interface AasRepository {
* @return the requested AAS
*/
public AssetInformation getAssetInformation(String aasId) throws ElementDoesNotExistException;

/**
* Get Thumbnail of the specific aas
*
* @param aasID
* the id of the AAS
* @return the file of the thumbnail
*/
public File getThumbnail(String aasId);

/**
* Set Thumbnail of the AAS
*
* @param aasID
* the id of the AAS
* @param fileName
* name of the thumbnail file with extension
* @param contentType
* content type of the file
* @param inputStream
* inputstream of the thumbnail file
*/
public void setThumbnail(String aasId, String fileName, String contentType, InputStream inputStream);

/**
* Delete the thumbnail file of the AAS
*
* @param aasId
* the id of the AAS
*/
public void deleteThumbnail(String aasId);

/**
* Returns the name of the repository
Expand Down
Loading

0 comments on commit bbfdf39

Please sign in to comment.