Skip to content

Commit

Permalink
refactor: extract deep copy logic to util class
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusmolina-iese committed Oct 24, 2024
1 parent a04e1b0 commit 270836b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -255,7 +252,7 @@ public Submodel getSubmodelByIdMetadata(String submodelId) throws ElementDoesNot

Submodel submodel = getSubmodel(submodelId);

return getSubmodelDeepCopy(submodel);
return SubmodelDeepCopyUtil.deepCopy(submodel);
}

@Override
Expand Down Expand Up @@ -303,23 +300,6 @@ private void throwIfMissingId(Collection<Submodel> 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));

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -169,7 +166,7 @@ public ResponseEntity<Submodel> getSubmodelMetadata(@Parameter(in = ParameterIn.

Submodel submodel = service.getSubmodel();

return new ResponseEntity<Submodel>(getSubmodelDeepCopy(submodel), HttpStatus.OK);
return new ResponseEntity<>(SubmodelDeepCopyUtil.deepCopy(submodel), HttpStatus.OK);
}

@Override
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 270836b

Please sign in to comment.