Skip to content

Commit

Permalink
Fixed a workflow bug with previous registrations and an enum serializ…
Browse files Browse the repository at this point in the history
…ation issue
  • Loading branch information
tilovillwock committed Dec 9, 2024
1 parent fa0169f commit 0055512
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -511,7 +512,10 @@ public void sendDaraPidRegistrationErrorEmail(User user, List<User> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -110,10 +115,20 @@ RegistrationResponse register(List<VariableMetadata> 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());
}
Expand Down Expand Up @@ -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<String> violations;
public VerificationException(List<String> 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 .
Expand Down Expand Up @@ -231,11 +258,10 @@ public RegistrationClientException(RestClientException e) {
}

/**
* Details on which constraints were violated when a registration fails.
* @see <a href="https://labs.da-ra.de/nfdi/api/swagger-ui/index.html">da|ra PID API</a>
* Details on which constraints were violated when metadata verification fails.
* @see <a href="https://labs.da-ra.de/nfdi/api/swagger-ui/index.html#/Registration/verifyVariables">PID Metadata Verification Endpoint</a>
*/
record ConstraintViolation(
int id,
public record ConstraintViolation(
String message,
String locationInfo
) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -242,13 +243,15 @@ public record StudyCreator(

/**
* The types of availability for a variable used at da|ra (PID).
* @see <a href="https://labs.da-ra.de/nfdi/api/swagger-ui/index.html#/Registration/verifyVariables">Metadata Verification Endpoint</a>
*/
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) {
Expand Down

0 comments on commit 0055512

Please sign in to comment.