Skip to content

Commit

Permalink
use human readable type for responses (#155)
Browse files Browse the repository at this point in the history
* use human readable type for responses

* fix mas fdo type
  • Loading branch information
southeo authored Dec 4, 2024
1 parent 439fc29 commit c48aaf4
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dissco.backend.domain.OdsType;
import eu.dissco.backend.domain.FdoType;
import lombok.Getter;
import org.springframework.stereotype.Component;

Expand All @@ -15,7 +15,7 @@ public class FdoRecordComponent {
public FdoRecordComponent(ObjectMapper mapper) {
this.postRequest = mapper.createObjectNode()
.set("data", mapper.createObjectNode()
.put("type", OdsType.MJR.getPid())
.put("type", FdoType.MJR.getPid())
.set("attributes", mapper.createObjectNode()));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.dissco.backend.controller;

import static eu.dissco.backend.domain.FdoType.ANNOTATION;
import static eu.dissco.backend.repository.RepositoryUtils.HANDLE_STRING;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -56,7 +57,6 @@
@RequestMapping("/api/v1/annotation")
public class AnnotationController extends BaseController {

public static final String ANNOTATION_TYPE = "ods:Annotation";
private final AnnotationService service;
private final SchemaValidatorComponent schemaValidator;

Expand Down Expand Up @@ -330,19 +330,19 @@ public ResponseEntity<Void> tombstoneAnnotation(Authentication authentication,
}

private AnnotationProcessingRequest getAnnotationFromRequest(AnnotationRequest requestBody) {
if (!requestBody.data().type().equals(ANNOTATION_TYPE)) {
if (!requestBody.data().type().equals(ANNOTATION)) {
throw new IllegalArgumentException(
"Invalid type. Type must be " + ANNOTATION_TYPE + " but was " + requestBody.data()
.type());
"Invalid type. Type must be " + ANNOTATION.getPid() + " or " + ANNOTATION.getName()
+ " but was " + requestBody.data().type());
}
return requestBody.data().attributes();
}

private AnnotationEventRequest getAnnotationFromRequestEvent(BatchAnnotationRequest requestBody) {
if (!requestBody.data().type().equals(ANNOTATION_TYPE)) {
if (!requestBody.data().type().equals(ANNOTATION)) {
throw new IllegalArgumentException(
"Invalid type. Type must be " + ANNOTATION_TYPE + " but was " + requestBody.data()
.type());
"Invalid type. Type must be " + ANNOTATION.getPid() + " or " + ANNOTATION.getName()
+ " but was " + requestBody.data().type());
}
return requestBody.data().attributes();
}
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/eu/dissco/backend/domain/FdoType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package eu.dissco.backend.domain;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;

@Getter
public enum FdoType {
@JsonProperty("https://doi.org/21.T11148/894b1e6cad57e921764e")
@JsonAlias("ods:DigitalSpecimen")
DIGITAL_SPECIMEN("https://doi.org/21.T11148/894b1e6cad57e921764e", "ods:DigitalSpecimen"),

@JsonProperty("https://doi.org/21.T11148/bbad8c4e101e8af01115")
@JsonAlias("ods:DigitalMedia")
DIGITAL_MEDIA("https://doi.org/21.T11148/bbad8c4e101e8af01115", "ods:DigitalMedia"),

@JsonProperty("https://doi.org/21.T11148/cf458ca9ee1d44a5608f")
@JsonAlias("ods:Annotation")
ANNOTATION("https://doi.org/21.T11148/cf458ca9ee1d44a5608f", "ods:Annotation"),

@JsonProperty("https://doi.org/21.T11148/417a4f472f60f7974c12")
@JsonAlias("ods:SourceSystem")
SOURCE_SYSTEM("https://doi.org/21.T11148/417a4f472f60f7974c12", "ods:SourceSystem"),

@JsonProperty("https://doi.org/21.T11148/ce794a6f4df42eb7e77e")
@JsonAlias("ods:DataMapping")
DATA_MAPPING("https://doi.org/21.T11148/ce794a6f4df42eb7e77e", "ods:DataMapping"),

@JsonProperty("https://doi.org/21.T11148/a369e128df5ef31044d4")
@JsonAlias("ods:MachineAnnotationService")
MAS("https://doi.org/21.T11148/a369e128df5ef31044d4", "ods:MachineAnnotationService"),

@JsonProperty("https://doi.org/21.T11148/532ce6796e2828dd2be6")
@JsonAlias("MachineAnnotationServiceJobRecord")
MJR("https://doi.org/21.T11148/532ce6796e2828dd2be6", "MachineAnnotationServiceJobRecord");

private final String pid;
private final String name;


FdoType(String pid, String name){
this.pid = pid;
this.name = name;
}
}
20 changes: 0 additions & 20 deletions src/main/java/eu/dissco/backend/domain/OdsType.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.dissco.backend.domain.openapi.annotation;

import eu.dissco.backend.domain.FdoType;
import eu.dissco.backend.schema.AnnotationProcessingRequest;
import io.swagger.v3.oas.annotations.media.Schema;

Expand All @@ -9,7 +10,7 @@ public record AnnotationRequest(

@Schema
public record AnnotationRequestData(
@Schema(description = "Type of request. For annotations, must be \"ods:Annotation\"") String type,
@Schema(description = "Type of request. For annotations, must be \"ods:Annotation\"") FdoType type,
@Schema(description = "Desired annotation") AnnotationProcessingRequest attributes) {

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.dissco.backend.domain.openapi.annotation;

import eu.dissco.backend.domain.FdoType;
import eu.dissco.backend.domain.annotation.batch.AnnotationEventRequest;
import io.swagger.v3.oas.annotations.media.Schema;

Expand All @@ -9,7 +10,7 @@ public record BatchAnnotationRequest(

@Schema
public record BatchAnnotationRequestData(
@Schema(description = "Type of request, in this case \"ods:Annotation\"") String type,
@Schema(description = "Type of request, in this case \"ods:Annotation\"") FdoType type,
AnnotationEventRequest attributes) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dissco.backend.domain.FdoType;
import eu.dissco.backend.exceptions.DisscoJsonBMappingException;
import eu.dissco.backend.schema.DigitalSpecimen;
import java.util.Date;
Expand Down Expand Up @@ -41,7 +42,7 @@ private DigitalSpecimen mapToDigitalSpecimen(Record dbRecord) {
try {
return mapper.readValue(dbRecord.get(DIGITAL_SPECIMEN.DATA).data(), DigitalSpecimen.class)
.withId("https://doi.org/" + dbRecord.get(DIGITAL_SPECIMEN.ID))
.withType("ods:DigitalSpecimen")
.withType(FdoType.DIGITAL_SPECIMEN.getName())
.withDctermsIdentifier("https://doi.org/" + dbRecord.get(DIGITAL_SPECIMEN.ID))
.withOdsFdoType(dbRecord.get(DIGITAL_SPECIMEN.TYPE))
.withOdsMidsLevel(dbRecord.get(DIGITAL_SPECIMEN.MIDSLEVEL).intValue())
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/eu/dissco/backend/service/AnnotationService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package eu.dissco.backend.service;

import static eu.dissco.backend.domain.FdoType.ANNOTATION;
import static eu.dissco.backend.service.DigitalServiceUtils.createVersionNode;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dissco.backend.client.AnnotationClient;
import eu.dissco.backend.domain.FdoType;
import eu.dissco.backend.domain.annotation.AnnotationTombstoneWrapper;
import eu.dissco.backend.domain.annotation.batch.AnnotationEvent;
import eu.dissco.backend.domain.annotation.batch.AnnotationEventRequest;
Expand Down Expand Up @@ -41,7 +43,6 @@
@RequiredArgsConstructor
public class AnnotationService {

private static final String ANNOTATION = "ods:Annotation";
private static final String ATTRIBUTES = "attributes";
private static final String DATA = "data";
private final AnnotationRepository repository;
Expand All @@ -53,7 +54,7 @@ public class AnnotationService {

public JsonApiWrapper getAnnotation(String id, String path) {
var annotation = repository.getAnnotation(id);
var dataNode = new JsonApiData(id, ANNOTATION, mapper.valueToTree(annotation));
var dataNode = new JsonApiData(id, FdoType.ANNOTATION.getName(), mapper.valueToTree(annotation));
return new JsonApiWrapper(dataNode, new JsonApiLinks(path));
}

Expand All @@ -67,8 +68,7 @@ public JsonApiWrapper getAnnotationByVersion(String id, int version, String path
throws NotFoundException, JsonProcessingException {
var eventNode = mongoRepository.getByVersion(id, version, "annotation_provenance");
validateAnnotationNode(eventNode);
var type = eventNode.get("@type").asText();
var dataNode = new JsonApiData(id, type, eventNode);
var dataNode = new JsonApiData(id, ANNOTATION.getName(), eventNode);
return new JsonApiWrapper(dataNode, new JsonApiLinks(path));
}

Expand Down Expand Up @@ -104,7 +104,7 @@ public JsonApiWrapper formatResponse(JsonNode response, String path)
throws JsonProcessingException {
if (response != null) {
var annotationResponse = parseToAnnotation(response);
var dataNode = new JsonApiData(annotationResponse.getId(), ANNOTATION,
var dataNode = new JsonApiData(annotationResponse.getId(), ANNOTATION.getName(),
response);
return new JsonApiWrapper(dataNode, new JsonApiLinks(path));
}
Expand Down Expand Up @@ -224,14 +224,14 @@ private String getFullId(String id) {

// Response Constructors
private void validateAnnotationNode(JsonNode annotationNode) throws JsonProcessingException {
mapper.treeToValue(annotationNode.get(ANNOTATION), Annotation.class);
mapper.treeToValue(annotationNode.get(ANNOTATION.getName()), Annotation.class);
}

private JsonApiListResponseWrapper wrapListResponse(List<Annotation> annotationsPlusOne,
int pageNumber, int pageSize, String path) {
List<JsonApiData> dataNodePlusOne = new ArrayList<>();
annotationsPlusOne.forEach(annotation -> dataNodePlusOne.add(
new JsonApiData(annotation.getId(), ANNOTATION,
new JsonApiData(annotation.getId(), FdoType.ANNOTATION.getName(),
mapper.valueToTree(annotation))));
return new JsonApiListResponseWrapper(dataNodePlusOne, pageNumber, pageSize, path);
}
Expand All @@ -242,7 +242,7 @@ private JsonApiListResponseWrapper wrapListResponseElasticSearchResults(
List<JsonApiData> dataNodePlusOne = new ArrayList<>();
var annotationsPlusOne = elasticSearchResults.getRight();
annotationsPlusOne.forEach(annotation -> dataNodePlusOne.add(
new JsonApiData(annotation.getId(), ANNOTATION,
new JsonApiData(annotation.getId(), FdoType.ANNOTATION.getName(),
mapper.valueToTree(annotation))));
return new JsonApiListResponseWrapper(dataNodePlusOne, pageNumber, pageSize, path,
new JsonApiMeta(elasticSearchResults.getLeft()));
Expand All @@ -252,7 +252,7 @@ private JsonApiListResponseWrapper wrapListResponse(List<Annotation> annotations
String path) {
List<JsonApiData> dataNode = new ArrayList<>();
annotations.forEach(annotation -> dataNode.add(
new JsonApiData(annotation.getId(), ANNOTATION,
new JsonApiData(annotation.getId(), ANNOTATION.getName(),
mapper.valueToTree(annotation))));
return new JsonApiListResponseWrapper(dataNode, new JsonApiLinksFull(path));
}
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/eu/dissco/backend/service/DigitalMediaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import eu.dissco.backend.database.jooq.enums.MjrTargetType;
import eu.dissco.backend.domain.DigitalMediaFull;
import eu.dissco.backend.domain.MasJobRequest;
import eu.dissco.backend.domain.OdsType;
import eu.dissco.backend.domain.FdoType;
import eu.dissco.backend.domain.jsonapi.JsonApiData;
import eu.dissco.backend.domain.jsonapi.JsonApiLinks;
import eu.dissco.backend.domain.jsonapi.JsonApiLinksFull;
Expand All @@ -30,7 +30,6 @@
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

@Slf4j
Expand All @@ -51,15 +50,15 @@ public JsonApiListResponseWrapper getDigitalMediaObjects(int pageNumber, int pag
String path) {
var mediaPlusOne = repository.getDigitalMediaObjects(pageNumber, pageSize);
var dataNodePlusOne = mediaPlusOne.stream().map(media ->
new JsonApiData(media.getDctermsIdentifier(), media.getOdsFdoType(),
new JsonApiData(media.getDctermsIdentifier(), FdoType.DIGITAL_MEDIA.getName(),
mapper.valueToTree(media))).toList();
return wrapResponse(dataNodePlusOne, pageNumber, pageSize, path);
}

public JsonApiWrapper getDigitalMediaById(String id, String path) {
var mediaObject = repository.getLatestDigitalMediaObjectById(id);
var dataNode = new JsonApiData(mediaObject.getDctermsIdentifier(),
mediaObject.getOdsFdoType(), mediaObject, mapper);
FdoType.DIGITAL_MEDIA.getName(), mediaObject, mapper);
var linksNode = new JsonApiLinks(path);
return new JsonApiWrapper(dataNode, linksNode);
}
Expand All @@ -79,7 +78,7 @@ public JsonApiWrapper getDigitalMediaObjectByVersion(String id, int version, Str
throws JsonProcessingException, NotFoundException {
var digitalMediaNode = mongoRepository.getByVersion(id, version, "digital_media_provenance");
var digitalMedia = mapResultToDigitalMedia(digitalMediaNode);
var dataNode = new JsonApiData(digitalMedia.getDctermsIdentifier(), digitalMedia.getOdsFdoType(), digitalMedia,
var dataNode = new JsonApiData(digitalMedia.getDctermsIdentifier(), FdoType.DIGITAL_MEDIA.getName(), digitalMedia,
mapper);
return new JsonApiWrapper(dataNode, new JsonApiLinks(path));
}
Expand Down Expand Up @@ -112,7 +111,7 @@ public List<JsonApiData> getDigitalMediaForSpecimen(String id) {
var mediaList = repository.getDigitalMediaForSpecimen(id);
List<JsonApiData> dataNode = new ArrayList<>();
mediaList.forEach(media -> dataNode.add(
new JsonApiData(media.getDctermsIdentifier(), media.getOdsFdoType(), mapper.valueToTree(media))));
new JsonApiData(media.getDctermsIdentifier(), FdoType.DIGITAL_MEDIA.getName(), mapper.valueToTree(media))));
return dataNode;
}

Expand All @@ -136,7 +135,7 @@ public JsonApiListResponseWrapper getMass(String id, String path) {
public JsonApiWrapper getOriginalDataForMedia(String targetId, String path) {
var originalData = repository.getMediaOriginalData(targetId);
return new JsonApiWrapper(
new JsonApiData(targetId, OdsType.DIGITAL_MEDIA.getPid(), originalData),
new JsonApiData(targetId, FdoType.DIGITAL_MEDIA.getName(), originalData),
new JsonApiLinks(path));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import eu.dissco.backend.domain.DigitalSpecimenFull;
import eu.dissco.backend.domain.MappingTerm;
import eu.dissco.backend.domain.MasJobRequest;
import eu.dissco.backend.domain.OdsType;
import eu.dissco.backend.domain.FdoType;
import eu.dissco.backend.domain.TaxonMappingTerms;
import eu.dissco.backend.domain.jsonapi.JsonApiData;
import eu.dissco.backend.domain.jsonapi.JsonApiLinks;
Expand Down Expand Up @@ -93,7 +93,7 @@ public JsonApiListResponseWrapper getLatestSpecimen(int pageNumber, int pageSize
public JsonApiWrapper getSpecimenById(String id, String path) {
var digitalSpecimen = repository.getLatestSpecimenById(id);
var dataNode = new JsonApiData(digitalSpecimen.getDctermsIdentifier(),
digitalSpecimen.getOdsFdoType(), digitalSpecimen,
FdoType.DIGITAL_SPECIMEN.getName(), digitalSpecimen,
mapper);
return new JsonApiWrapper(dataNode, new JsonApiLinks(path));
}
Expand All @@ -118,7 +118,7 @@ public JsonApiListResponseWrapper getMasJobRecordsForSpecimen(String targetId,
public JsonApiWrapper getOriginalDataForSpecimen(String targetId, String path) {
var originalData = repository.getSpecimenOriginalData(targetId);
return new JsonApiWrapper(
new JsonApiData(targetId, OdsType.DIGITAL_SPECIMEN.getPid(), originalData),
new JsonApiData(targetId, FdoType.DIGITAL_SPECIMEN.getName(), originalData),
new JsonApiLinks(path));
}

Expand All @@ -128,15 +128,15 @@ private JsonApiWrapper mapFullSpecimen(String id, String path, DigitalSpecimen s
var attributeNode = mapper.valueToTree(
new DigitalSpecimenFull(specimen, digitalMedia, annotation));
return new JsonApiWrapper(
new JsonApiData(specimen.getDctermsIdentifier(), specimen.getOdsFdoType(), attributeNode),
new JsonApiData(specimen.getDctermsIdentifier(), FdoType.DIGITAL_SPECIMEN.getName(), attributeNode),
new JsonApiLinks(path));
}

public JsonApiWrapper getSpecimenByVersion(String id, int version, String path)
throws JsonProcessingException, NotFoundException {
var specimenNode = mongoRepository.getByVersion(id, version, MONGODB_COLLECTION_NAME);
var specimen = mapResultToSpecimen(specimenNode);
var dataNode = new JsonApiData(specimen.getDctermsIdentifier(), specimen.getOdsFdoType(), specimen, mapper);
var dataNode = new JsonApiData(specimen.getDctermsIdentifier(), FdoType.DIGITAL_SPECIMEN.getName(), specimen, mapper);
return new JsonApiWrapper(dataNode, new JsonApiLinks(path));
}

Expand Down Expand Up @@ -167,7 +167,7 @@ private JsonApiListResponseWrapper wrapListResponse(
var digitalSpecimenList = elasticSearchResults.getRight();
var dataNodePlusOne = digitalSpecimenList.stream()
.map(specimen -> new JsonApiData(specimen.getDctermsIdentifier(),
specimen.getOdsFdoType(), specimen, mapper))
FdoType.DIGITAL_SPECIMEN.getName(), specimen, mapper))
.toList();
boolean hasNext = dataNodePlusOne.size() > pageSize;
var linksNode = new JsonApiLinksFull(pageNumber, pageSize, hasNext, path);
Expand All @@ -180,7 +180,7 @@ private JsonApiListResponseWrapper wrapListResponseSearchResults(
Pair<Long, List<DigitalSpecimen>> digitalSpecimenSearchResult,
int pageNumber, int pageSize, MultiValueMap<String, String> params, String path) {
var dataNodePlusOne = digitalSpecimenSearchResult.getRight().stream()
.map(specimen -> new JsonApiData(specimen.getDctermsIdentifier(), specimen.getOdsFdoType(), specimen,
.map(specimen -> new JsonApiData(specimen.getDctermsIdentifier(), FdoType.DIGITAL_SPECIMEN.getName(), specimen,
mapper))
.toList();
boolean hasNext = dataNodePlusOne.size() > pageSize;
Expand Down
Loading

0 comments on commit c48aaf4

Please sign in to comment.