From 005551239057375d1e3fdc80ec15045c205c68f8 Mon Sep 17 00:00:00 2001 From: Tilo Villwock Date: Mon, 9 Dec 2024 14:58:12 +0100 Subject: [PATCH 1/2] Fixed a workflow bug with previous registrations and an enum serialization issue --- .../release-project-dialog.controller.js | 2 +- .../mailmanagement/service/MailService.java | 6 ++- .../service/DaraPidClientService.java | 38 ++++++++++++++++--- .../service/DaraPidRegistrationService.java | 9 +++-- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/mdm-frontend/src/app/legacy/dataacquisitionprojectmanagement/views/release-project-dialog.controller.js b/mdm-frontend/src/app/legacy/dataacquisitionprojectmanagement/views/release-project-dialog.controller.js index 44b63a41a9..398c7bf6bd 100644 --- a/mdm-frontend/src/app/legacy/dataacquisitionprojectmanagement/views/release-project-dialog.controller.js +++ b/mdm-frontend/src/app/legacy/dataacquisitionprojectmanagement/views/release-project-dialog.controller.js @@ -260,7 +260,7 @@ angular.module('metadatamanagementApp') DaraReleaseResource.release({ registerVars }, project) .$promise.then(function() { // show warning if PIDs had been registered before - if (release.registerPID && $scope.hasRegistrations) { + if (release.registerPID && $scope.variablesCheck.hasRegistrations) { SimpleMessageToastService.openSimpleMessageToast(i18nPrefix + 'dara-pid-previous-registration'); } DataAcquisitionProjectResource.save(project).$promise diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/mailmanagement/service/MailService.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/mailmanagement/service/MailService.java index f9b39d6d56..1a9ecf5172 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/mailmanagement/service/MailService.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/mailmanagement/service/MailService.java @@ -19,6 +19,7 @@ import eu.dzhw.fdz.metadatamanagement.projectmanagement.service.DaraPidClientService.RegistrationClientException; import eu.dzhw.fdz.metadatamanagement.projectmanagement.service.DaraPidClientService.RegistrationResponseException; import eu.dzhw.fdz.metadatamanagement.projectmanagement.service.DaraPidClientService.RegistrationResponseParsingException; +import eu.dzhw.fdz.metadatamanagement.projectmanagement.service.DaraPidClientService.VerificationException; import eu.dzhw.fdz.metadatamanagement.projectmanagement.service.DaraPidRegistrationService.RegistrationFailedException; import org.apache.commons.lang3.exception.ExceptionUtils; import org.jsoup.Jsoup; @@ -511,7 +512,10 @@ public void sendDaraPidRegistrationErrorEmail(User user, List admins, Data ? dataPackage.getTitle().getDe() : dataPackage.getTitle().getEn()); - if (exception instanceof RegistrationResponseException rre) { + if (exception instanceof VerificationException ve) { + context.setVariable("errorMessage", ve.getMessage()); + context.setVariable("failedEntries", ve.getViolations()); + } else if (exception instanceof RegistrationResponseException rre) { context.setVariable("errorMessage", rre.getMessage()); context.setVariable("statusCode", rre.getStatusCode()); context.setVariable("body", rre.getBody()); diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/projectmanagement/service/DaraPidClientService.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/projectmanagement/service/DaraPidClientService.java index a7ac6449a7..9306240017 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/projectmanagement/service/DaraPidClientService.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/projectmanagement/service/DaraPidClientService.java @@ -4,7 +4,9 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; import eu.dzhw.fdz.metadatamanagement.common.config.MetadataManagementProperties; import eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.DataPackage; import eu.dzhw.fdz.metadatamanagement.projectmanagement.domain.DataAcquisitionProject; @@ -26,10 +28,13 @@ import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; +import javax.xml.bind.ValidationException; import java.io.IOException; import java.net.URI; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Stream; /** * A client service that's solely responsible for @@ -110,10 +115,20 @@ RegistrationResponse register(List variables) throws Registrat headers.add("Content-Type", "application/json"); headers.add("Authorization", "Basic " + new String(authHash, StandardCharsets.UTF_8)); - var uri = this.getRegistationEndpoint(); var entity = new HttpEntity<>(new RegistrationRequestBody(variables), headers); try { - var response = this.restTemplate.postForEntity(uri, entity, String.class); + log.debug(this.objectMapper.writeValueAsString(new RegistrationRequestBody(variables))); + // verify variables + var response = this.restTemplate.postForEntity( + this.config.getDaraPid().getEndpoint() + PATH_VERIFY, entity, String.class); + var responseNode = this.objectMapper.readTree(response.getBody()); + if (responseNode.path("constraintViolation").isArray()) { + var violations = Stream.of((ArrayNode) responseNode.path("constraintViolation")) + .map(JsonNode::toPrettyString).toList(); + throw new VerificationException(violations); + } + // register variables + response = this.restTemplate.postForEntity(this.getRegistationEndpoint(), entity, String.class); if (!response.getStatusCode().is2xxSuccessful()) { throw new RegistrationResponseException(response.getStatusCodeValue(), response.getBody()); } @@ -168,6 +183,18 @@ VariableStatus[] getJobStatus(String jobId) throws JobStatusException { } } + /** + * This exception is thrown when verification of variable metadata fails. + */ + @Getter + public static class VerificationException extends RegistrationException implements DaraPidApiException { + private final ArrayList violations; + public VerificationException(List violations) { + super("Verification of variable metadata has failed"); + this.violations = new ArrayList<>(violations); + } + } + /** * A wrapper type that provides the expected request body * JSON structure for the registration API call . @@ -231,11 +258,10 @@ public RegistrationClientException(RestClientException e) { } /** - * Details on which constraints were violated when a registration fails. - * @see da|ra PID API + * Details on which constraints were violated when metadata verification fails. + * @see PID Metadata Verification Endpoint */ - record ConstraintViolation( - int id, + public record ConstraintViolation( String message, String locationInfo ) {} diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/projectmanagement/service/DaraPidRegistrationService.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/projectmanagement/service/DaraPidRegistrationService.java index b9dc0c6037..6d90b9b0ce 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/projectmanagement/service/DaraPidRegistrationService.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/projectmanagement/service/DaraPidRegistrationService.java @@ -1,6 +1,7 @@ package eu.dzhw.fdz.metadatamanagement.projectmanagement.service; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonValue; import eu.dzhw.fdz.metadatamanagement.common.config.MetadataManagementProperties; import eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.DataPackage; import eu.dzhw.fdz.metadatamanagement.datapackagemanagement.repository.DataPackageRepository; @@ -242,13 +243,15 @@ public record StudyCreator( /** * The types of availability for a variable used at da|ra (PID). + * @see Metadata Verification Endpoint */ public enum VariableAvailability { - DELIVERY("delivery"), - NOT_AVAILABLE("not available"), - UNKNOWN("unknown"); + DELIVERY("Delivery"), + NOT_AVAILABLE("NotAvailable"), + UNKNOWN("Unknown"); + @JsonValue public final String value; VariableAvailability(String value) { From dc8ebe57b622d883172af5167274132393d1e210 Mon Sep 17 00:00:00 2001 From: Tilo Villwock Date: Mon, 9 Dec 2024 15:09:01 +0100 Subject: [PATCH 2/2] Removed unused import --- .../projectmanagement/service/DaraPidClientService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/projectmanagement/service/DaraPidClientService.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/projectmanagement/service/DaraPidClientService.java index 9306240017..4e54142bbb 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/projectmanagement/service/DaraPidClientService.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/projectmanagement/service/DaraPidClientService.java @@ -28,7 +28,6 @@ import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; -import javax.xml.bind.ValidationException; import java.io.IOException; import java.net.URI; import java.nio.charset.StandardCharsets;