Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AAS Thumbnail operations #150

Merged
merged 10 commits into from
Dec 1, 2023
Merged
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
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 @@ -175,4 +179,62 @@ public String getName() {
return aasRepositoryName == null ? AasRepository.super.getName() : aasRepositoryName;
}

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

ThumbnailHandler.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);
ThumbnailHandler.setNewThumbnail(this, aasId, contentType, filePath);
}

private void updateThumbnailFile(String aasId, String fileName, String contentType, InputStream inputStream, Resource thumbnail) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move private methods to the bottom of the file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, move to thumbnailHandler?

String path = thumbnail.getPath();
ThumbnailHandler.deleteExistingFile(path);
String filePath = createFile(aasId, fileName, inputStream);
ThumbnailHandler.updateThumbnail(this, aasId, contentType, filePath);
}

private String createFile(String aasId, String fileName, InputStream inputStream) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to ThumbnailHandler?

String filePath = ThumbnailHandler.createFilePath(ThumbnailHandler.getTemporaryDirectoryPath(), aasId, fileName);
ThumbnailHandler.createFileAtSpecifiedPath(fileName, inputStream, filePath);
return filePath;
}

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

deleteThumbnailFile(thumbnail);

updateThumbnailInAssetInformation(aasId);
}

private void updateThumbnailInAssetInformation(String aasId) {
AssetInformation assetInfor = getAssetInformation(aasId);
assetInfor.getDefaultThumbnail().setContentType("");
assetInfor.getDefaultThumbnail().setPath("");
setAssetInformation(aasId, assetInfor);
}

private void deleteThumbnailFile(Resource thumbnail) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to ThumbnailHandle?

String filePath = thumbnail.getPath();
java.io.File tmpFile = new java.io.File(filePath);
tmpFile.delete();
}

}
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 ThumbnailHandler {

private static Logger logger = LoggerFactory.getLogger(ThumbnailHandler.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);
}

private static void throwIfFilePathIsNotValid(String aasId, String filePath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move private methods to bottom of the file

if (filePath.isEmpty())
throw new FileDoesNotExistException(aasId);
try {
Paths.get(filePath);
} catch (InvalidPathException | NullPointerException ex) {
throw new FileDoesNotExistException(aasId);
}
}

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;
}
}
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