From 270836b820ef105cb8b6bb921a4b808f9788da3c Mon Sep 17 00:00:00 2001 From: mateusmolina Date: Thu, 24 Oct 2024 16:18:24 +0200 Subject: [PATCH] refactor: extract deep copy logic to util class --- .../exceptions/FailedToDeepCopyException.java | 12 ++++++ .../backend/CrudSubmodelRepository.java | 24 +---------- .../serialization/SubmodelDeepCopyUtil.java | 40 +++++++++++++++++++ .../SubmodelServiceHTTPApiController.java | 24 +---------- 4 files changed, 56 insertions(+), 44 deletions(-) create mode 100644 basyx.common/basyx.core/src/main/java/org/eclipse/digitaltwin/basyx/core/exceptions/FailedToDeepCopyException.java create mode 100644 basyx.submodelservice/basyx.submodelservice-core/src/main/java/org/eclipse/digitaltwin/basyx/serialization/SubmodelDeepCopyUtil.java diff --git a/basyx.common/basyx.core/src/main/java/org/eclipse/digitaltwin/basyx/core/exceptions/FailedToDeepCopyException.java b/basyx.common/basyx.core/src/main/java/org/eclipse/digitaltwin/basyx/core/exceptions/FailedToDeepCopyException.java new file mode 100644 index 000000000..fb2386f90 --- /dev/null +++ b/basyx.common/basyx.core/src/main/java/org/eclipse/digitaltwin/basyx/core/exceptions/FailedToDeepCopyException.java @@ -0,0 +1,12 @@ +package org.eclipse.digitaltwin.basyx.core.exceptions; + +public class FailedToDeepCopyException extends RuntimeException { + + public FailedToDeepCopyException(String objId, Throwable e) { + super(getMessage(objId), e); + } + + private static String getMessage(String objId) { + return "Failed to deep copy object with id " + objId; + } +} diff --git a/basyx.submodelrepository/basyx.submodelrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/backend/CrudSubmodelRepository.java b/basyx.submodelrepository/basyx.submodelrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/backend/CrudSubmodelRepository.java index 6fc3bbfe3..a39841414 100644 --- a/basyx.submodelrepository/basyx.submodelrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/backend/CrudSubmodelRepository.java +++ b/basyx.submodelrepository/basyx.submodelrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/backend/CrudSubmodelRepository.java @@ -35,10 +35,6 @@ import java.util.stream.Collectors; import java.util.stream.StreamSupport; -import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.DeserializationException; -import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.SerializationException; -import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonDeserializer; -import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer; import org.eclipse.digitaltwin.aas4j.v3.model.OperationVariable; import org.eclipse.digitaltwin.aas4j.v3.model.Reference; import org.eclipse.digitaltwin.aas4j.v3.model.Submodel; @@ -54,6 +50,7 @@ import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo; import org.eclipse.digitaltwin.basyx.core.pagination.PaginationSupport; import org.eclipse.digitaltwin.basyx.http.Base64UrlEncoder; +import org.eclipse.digitaltwin.basyx.serialization.SubmodelDeepCopyUtil; import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepository; import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelService; import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelServiceFactory; @@ -255,7 +252,7 @@ public Submodel getSubmodelByIdMetadata(String submodelId) throws ElementDoesNot Submodel submodel = getSubmodel(submodelId); - return getSubmodelDeepCopy(submodel); + return SubmodelDeepCopyUtil.deepCopy(submodel); } @Override @@ -303,23 +300,6 @@ private void throwIfMissingId(Collection submodels) { submodels.stream().map(Submodel::getId).forEach(this::throwIfSubmodelIdEmptyOrNull); } - private Submodel getSubmodelDeepCopy(Submodel submodel) { - - try { - String submodelAsJSON = new JsonSerializer().write(submodel); - - Submodel submodelDeepCopy = new JsonDeserializer().read(submodelAsJSON, Submodel.class); - - submodelDeepCopy.setSubmodelElements(null); - - return submodelDeepCopy; - } catch (DeserializationException e) { - throw new RuntimeException("Unable to deserialize the Submodel", e); - } catch (SerializationException e) { - throw new RuntimeException("Unable to serialize the Submodel", e); - } - } - private SubmodelService getSubmodelServiceOrThrow(String submodelId) { Submodel submodel = submodelBackend.findById(submodelId).orElseThrow(() -> new ElementDoesNotExistException(submodelId)); diff --git a/basyx.submodelservice/basyx.submodelservice-core/src/main/java/org/eclipse/digitaltwin/basyx/serialization/SubmodelDeepCopyUtil.java b/basyx.submodelservice/basyx.submodelservice-core/src/main/java/org/eclipse/digitaltwin/basyx/serialization/SubmodelDeepCopyUtil.java new file mode 100644 index 000000000..b1fd254d3 --- /dev/null +++ b/basyx.submodelservice/basyx.submodelservice-core/src/main/java/org/eclipse/digitaltwin/basyx/serialization/SubmodelDeepCopyUtil.java @@ -0,0 +1,40 @@ +package org.eclipse.digitaltwin.basyx.serialization; + +import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.DeserializationException; +import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.SerializationException; +import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonDeserializer; +import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer; +import org.eclipse.digitaltwin.aas4j.v3.model.Submodel; +import org.eclipse.digitaltwin.basyx.core.exceptions.FailedToDeepCopyException; + +public final class SubmodelDeepCopyUtil { + + private SubmodelDeepCopyUtil() { + } + + /** + * Deep copy a submodel. + * + * @param submodel + * The submodel that should be copied. + * @return A deep copy of the submodel. + * + * @throws FailedToDeepCopyException + * If the submodel can not be copied. + */ + public static Submodel deepCopy(Submodel submodel) { + try { + String submodelAsJSON = new JsonSerializer().write(submodel); + + Submodel submodelDeepCopy = new JsonDeserializer().read(submodelAsJSON, Submodel.class); + + submodelDeepCopy.setSubmodelElements(null); + + return submodelDeepCopy; + + } catch (DeserializationException | SerializationException e) { + throw new FailedToDeepCopyException(submodel.getId(), e); + } + } + +} diff --git a/basyx.submodelservice/basyx.submodelservice-http/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/http/SubmodelServiceHTTPApiController.java b/basyx.submodelservice/basyx.submodelservice-http/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/http/SubmodelServiceHTTPApiController.java index 15112608d..41029cc55 100644 --- a/basyx.submodelservice/basyx.submodelservice-http/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/http/SubmodelServiceHTTPApiController.java +++ b/basyx.submodelservice/basyx.submodelservice-http/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/http/SubmodelServiceHTTPApiController.java @@ -31,10 +31,6 @@ import java.util.Arrays; import java.util.List; -import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.DeserializationException; -import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.SerializationException; -import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonDeserializer; -import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer; import org.eclipse.digitaltwin.aas4j.v3.model.OperationRequest; import org.eclipse.digitaltwin.aas4j.v3.model.OperationResult; import org.eclipse.digitaltwin.aas4j.v3.model.OperationVariable; @@ -50,6 +46,7 @@ import org.eclipse.digitaltwin.basyx.http.pagination.PagedResult; import org.eclipse.digitaltwin.basyx.http.pagination.PagedResultPagingMetadata; import org.eclipse.digitaltwin.basyx.pagination.GetSubmodelElementsResult; +import org.eclipse.digitaltwin.basyx.serialization.SubmodelDeepCopyUtil; import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelService; import org.eclipse.digitaltwin.basyx.submodelservice.value.SubmodelElementValue; import org.eclipse.digitaltwin.basyx.submodelservice.value.SubmodelValueOnly; @@ -169,7 +166,7 @@ public ResponseEntity getSubmodelMetadata(@Parameter(in = ParameterIn. Submodel submodel = service.getSubmodel(); - return new ResponseEntity(getSubmodelDeepCopy(submodel), HttpStatus.OK); + return new ResponseEntity<>(SubmodelDeepCopyUtil.deepCopy(submodel), HttpStatus.OK); } @Override @@ -323,21 +320,4 @@ private void closeInputStream(InputStream fileInputstream) { e.printStackTrace(); } } - - private Submodel getSubmodelDeepCopy(Submodel submodel) { - - try { - String submodelAsJSON = new JsonSerializer().write(submodel); - - Submodel submodelDeepCopy = new JsonDeserializer().read(submodelAsJSON, Submodel.class); - - submodelDeepCopy.setSubmodelElements(null); - - return submodelDeepCopy; - } catch (DeserializationException e) { - throw new RuntimeException("Unable to deserialize the Submodel", e); - } catch (SerializationException e) { - throw new RuntimeException("Unable to serialize the Submodel", e); - } - } }