Skip to content

Commit

Permalink
Minor changes for code refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Zai Müller-Zhang <[email protected]>
  • Loading branch information
zhangzai123 committed Dec 1, 2023
1 parent 878f387 commit 448c0e1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ThumbnailHandler {
public class AASThumbnailHandler {

private static Logger logger = LoggerFactory.getLogger(ThumbnailHandler.class);
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,6 @@ public AssetInformation getAssetInformation(String aasId) throws ElementDoesNotE
return getAasServiceOrThrow(aasId).getAssetInformation();
}

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

return aasServiceFactory.create(aas);
}

private void throwIfMismatchingIds(String aasId, AssetAdministrationShell newAas) {
String newAasId = newAas.getId();

if (!aasId.equals(newAasId))
throw new IdentificationMismatchException();
}

private void throwIfAasExists(AssetAdministrationShell aas) {
if (aasBackend.existsById(aas.getId()))
throw new CollidingIdentifierException();
}

private void throwIfAasDoesNotExist(String aasId) {
if (!aasBackend.existsById(aasId))
throw new ElementDoesNotExistException(aasId);
}

@Override
public String getName() {
Expand All @@ -183,7 +161,7 @@ public String getName() {
public File getThumbnail(String aasId) {
Resource resource = getAssetInformation(aasId).getDefaultThumbnail();

ThumbnailHandler.throwIfFileDoesNotExist(aasId, resource);
AASThumbnailHandler.throwIfFileDoesNotExist(aasId, resource);
String filePath = resource.getPath();
return new File(filePath);
}
Expand All @@ -198,32 +176,42 @@ public void setThumbnail(String aasId, String fileName, String contentType, Inpu
}

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) {
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) {
String filePath = ThumbnailHandler.createFilePath(ThumbnailHandler.getTemporaryDirectoryPath(), aasId, fileName);
ThumbnailHandler.createFileAtSpecifiedPath(fileName, inputStream, filePath);
return filePath;
AASThumbnailHandler.setNewThumbnail(this, aasId, contentType, filePath);
}

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

deleteThumbnailFile(thumbnail);

updateThumbnailInAssetInformation(aasId);
}

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

return aasServiceFactory.create(aas);
}

private void throwIfMismatchingIds(String aasId, AssetAdministrationShell newAas) {
String newAasId = newAas.getId();

if (!aasId.equals(newAasId))
throw new IdentificationMismatchException();
}

private void throwIfAasExists(AssetAdministrationShell aas) {
if (aasBackend.existsById(aas.getId()))
throw new CollidingIdentifierException();
}

private void throwIfAasDoesNotExist(String aasId) {
if (!aasBackend.existsById(aasId))
throw new ElementDoesNotExistException(aasId);
}

private void updateThumbnailInAssetInformation(String aasId) {
AssetInformation assetInfor = getAssetInformation(aasId);
assetInfor.getDefaultThumbnail().setContentType("");
Expand All @@ -237,4 +225,17 @@ private void deleteThumbnailFile(Resource thumbnail) {
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ public void uploadThumbnailToShell() throws IOException {
CloseableHttpResponse getThumbnailResponse = uploadThumbnail(dummyAasId);

assertEquals(HttpStatus.OK.value(), getThumbnailResponse.getCode());

getThumbnailResponse.close();
}

@Test
Expand All @@ -325,6 +327,8 @@ public void getFileFromNotExistElement() throws FileNotFoundException, Unsupport
CloseableHttpResponse response = BaSyxHttpTestUtils.executeGetOnURL(BaSyxHttpTestUtils.getThumbnailAccessURL(getURL(), dummyAasId));

assertEquals(HttpStatus.NOT_FOUND.value(), response.getCode());

response.close();
}

@Test
Expand Down

0 comments on commit 448c0e1

Please sign in to comment.