Skip to content

Commit

Permalink
MarsReceiptException (RuntimeException)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedram-A-Keyvani committed Nov 12, 2024
1 parent f3e7f59 commit 2d72fcb
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.elixir.biohackaton.ISAToSRA.biosamples.model.BiosampleAccessionsMap;
import com.elixir.biohackaton.ISAToSRA.biosamples.service.BioSamplesSubmitter;
import com.elixir.biohackaton.ISAToSRA.biosamples.service.MarsReceiptService;
import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.*;
import com.elixir.biohackaton.ISAToSRA.receipt.marsmodel.MarsReceipt;
import com.elixir.biohackaton.ISAToSRA.receipt.MarsReceiptException;
import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.IsaJson;
import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.Study;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
Expand All @@ -42,33 +48,40 @@ public class BioSampleSubmissionController {
consumes = { APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE })
public String performSubmissionToBioSamplesAndEna(
@RequestBody final String submissionPayload,
@RequestParam(value = "webinjwt") String webinJwt)
throws Exception {
String webinToken;
if (webinJwt != null) {
webinToken = webinJwt;
} else {
throw new RuntimeException("Webin Authentication Token is not provided");
}
@RequestParam(value = "webinjwt") String webinJwt) {
try {
String webinToken;
if (webinJwt != null) {
webinToken = webinJwt;
} else {
throw new RuntimeException("Webin Authentication Token is not provided");
}

objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

final IsaJson isaJson = this.objectMapper.readValue(submissionPayload, IsaJson.class);
final List<Study> studies = getStudies(isaJson);
final IsaJson isaJson = this.objectMapper.readValue(submissionPayload, IsaJson.class);
final List<Study> studies = getStudies(isaJson);

final BiosampleAccessionsMap accessionsMap = this.bioSamplesSubmitter.createBioSamples(studies, webinToken);
final MarsReceipt marsReceipt = marsReceiptService.convertReceiptToMars(accessionsMap, isaJson);
final BiosampleAccessionsMap accessionsMap = this.bioSamplesSubmitter.createBioSamples(studies, webinToken);
marsReceiptService.convertReceiptToMars(accessionsMap, isaJson);

return marsReceiptService.convertMarsReceiptToJson(marsReceipt);
return marsReceiptService.convertMarsReceiptToJson();
} catch (final MarsReceiptException e) {
log.error("Mars receipt excption", e);
marsReceiptService.setMarsReceiptErrors(e.getReceiptErrorMessage());
return marsReceiptService.convertMarsReceiptToJson();
} catch (final Exception e) {
log.error("Internal server error", e);
marsReceiptService.setMarsReceiptErrors(e.getMessage());
return marsReceiptService.convertMarsReceiptToJson();
}
}

public List<Study> getStudies(final IsaJson isaJson) {
try {
return isaJson.getInvestigation().getStudies();
} catch (final Exception e) {
log.info("Failed to parse ISA JSON and get studies", e);
throw new MarsReceiptException("Failed to parse ISA JSON and get studies", e);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.elixir.biohackaton.ISAToSRA.biosamples.model.BiosampleAccessionsMap;
import com.elixir.biohackaton.ISAToSRA.biosamples.model.Relationship;
import com.elixir.biohackaton.ISAToSRA.biosamples.model.BioSample;
import com.elixir.biohackaton.ISAToSRA.receipt.MarsReceiptException;
import com.elixir.biohackaton.ISAToSRA.receipt.ReceiptAccessionsMap;
import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.*;

Expand Down Expand Up @@ -80,7 +81,7 @@ public BiosampleAccessionsMap createBioSamples(final List<Study> studies, final
});
}
} catch (final Exception e) {
throw new RuntimeException("Failed to parse ISA Json and create samples in BioSamples", e);
throw new MarsReceiptException("Failed to parse ISA Json and create samples in BioSamples", e);
}

return typeToBioSamplesAccessionMap;
Expand Down Expand Up @@ -122,7 +123,7 @@ private BioSample createAndUpdateChildSampleWithRelationship(
return null;
}
} catch (final Exception e) {
throw new RuntimeException("Failed to handle child samples", e);
throw new MarsReceiptException("Failed to handle child samples", e);
}
}

Expand Down Expand Up @@ -165,7 +166,7 @@ private BioSample createSourceBioSample(final List<Study> studies, final String
sourceCharacteristics.add(biosampleAccessionCharacteristic);
source.setCharacteristics(sourceCharacteristics);
} else {
throw new RuntimeException("Failed to store source sample to BioSamples");
throw new MarsReceiptException("Failed to store source sample to BioSamples");
}
}));

Expand Down Expand Up @@ -205,7 +206,7 @@ private BioSample updateSampleWithRelationshipsToBioSamples(
});
return biosamplesResponse.getBody().getContent();
} catch (final Exception ex) {
throw new RuntimeException("Failed to add relationships to child samples", ex);
throw new MarsReceiptException("Failed to add relationships to child samples", ex);
}
}

Expand All @@ -227,7 +228,7 @@ private EntityModel<BioSample> createSampleInBioSamples(

return biosamplesResponse.getBody();
} catch (final Exception ex) {
throw new RuntimeException("Failed to create samples in BioSamples", ex);
throw new MarsReceiptException("Failed to create samples in BioSamples", ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
/** Elixir BioHackathon 2022 */
package com.elixir.biohackaton.ISAToSRA.biosamples.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;

import com.elixir.biohackaton.ISAToSRA.biosamples.model.BiosampleAccessionsMap;
import com.elixir.biohackaton.ISAToSRA.receipt.MarsReceiptProvider;
import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.*;
import com.elixir.biohackaton.ISAToSRA.receipt.marsmodel.*;
import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.IsaJson;
import com.elixir.biohackaton.ISAToSRA.receipt.marsmodel.MarsErrorType;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.stereotype.Service;

@Service
public class MarsReceiptService extends MarsReceiptProvider {
public class MarsReceiptService extends MarsReceiptProvider implements HandlerInterceptor {
private final ObjectMapper jsonMapper = new ObjectMapper();

private void setupJsonMapper() {
Expand All @@ -21,29 +26,42 @@ private void setupJsonMapper() {
}

public MarsReceiptService() {
super("biosamples"); // TODO decide whether to use instead
// https://registry.identifiers.org/registry/biosample
setupJsonMapper();
}

public String convertMarsReceiptToJson(final MarsReceipt marsReceipt) {
// Reset MARS receipt per request
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
resetMarsReceipt();
return HandlerInterceptor.super.preHandle(request, response, handler);
}

public String convertMarsReceiptToJson() {
try {
return jsonMapper.writeValueAsString(marsReceipt);
return jsonMapper.writeValueAsString(getMarsReceipt());
} catch (Exception ex) {
throw new RuntimeException("receipt", ex);
throw new RuntimeException("Receipt", ex);
}
}

public void setMarsReceiptErrors(String... errors) {
super.setMarsReceiptErrors(MarsErrorType.INTERNAL_SERVER_ERROR, errors);
}

/**
* Converting BioSample receipt to Mars data format
*
* @see
* https://github.com/elixir-europe/MARS/blob/refactor/repository-services/repository-api.md#response
* @param biosampleAccessionsMap {@link BiosampleAccessionsMap} Receipt from Biosample
* @param isaJson {@link IsaJson} Requested ISA-Json
* @return {@link MarsReceipt} Mars response data
* @param biosampleAccessionsMap {@link BiosampleAccessionsMap} Receipt from
* Biosample
* @param isaJson {@link IsaJson} Requested ISA-Json
*/
public MarsReceipt convertReceiptToMars(final BiosampleAccessionsMap biosampleAccessionsMap, final IsaJson isaJson) {
return buildMarsReceipt(
"biosamples", // https://registry.identifiers.org/registry/biosample
public void convertReceiptToMars(final BiosampleAccessionsMap biosampleAccessionsMap, final IsaJson isaJson) {
buildMarsReceipt(
biosampleAccessionsMap.studyAccessionsMap,
biosampleAccessionsMap.sampleAccessionsMap,
biosampleAccessionsMap.sourceAccessionsMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;

import com.elixir.biohackaton.ISAToSRA.receipt.MarsReceiptException;
import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.*;
import com.elixir.biohackaton.ISAToSRA.sra.model.Receipt;
import com.elixir.biohackaton.ISAToSRA.sra.service.MarsReceiptService;
Expand Down Expand Up @@ -69,11 +70,11 @@ public String performSubmissionToEna(
@RequestParam(value = "webinPassword") String webinPassword) {
try {
if (webinUserName == null || webinUserName.isEmpty()) {
throw new Exception("Webin Authentication username is not provided");
throw new MarsReceiptException("Webin Authentication username is not provided");
}

if (webinPassword == null || webinPassword.isEmpty()) {
throw new Exception("Webin Authentication password is not provided");
throw new MarsReceiptException("Webin Authentication password is not provided");
}

final IsaJson isaJson = this.objectMapper.readValue(submissionPayload, IsaJson.class);
Expand Down Expand Up @@ -111,7 +112,11 @@ public String performSubmissionToEna(

return marsReceiptService.convertMarsReceiptToJson();

} catch (Exception e) {
} catch (final MarsReceiptException e) {
log.error("Mars receipt excption", e);
marsReceiptService.setMarsReceiptErrors(e.getReceiptErrorMessage());
return marsReceiptService.convertMarsReceiptToJson();
} catch (final Exception e) {
log.error("Internal server error", e);
marsReceiptService.setMarsReceiptErrors(e.getMessage());
return marsReceiptService.convertMarsReceiptToJson();
Expand All @@ -122,23 +127,19 @@ public List<Study> getStudies(final IsaJson isaJson) {
try {
return isaJson.getInvestigation().getStudies();
} catch (final Exception e) {
log.info("Failed to parse ISA JSON and get studies", e);
throw new MarsReceiptException("Failed to parse ISA JSON and get studies", e);
}

return null;
}

public Investigation getInvestigation(final IsaJson isaJson) {
try {
return isaJson.getInvestigation();
} catch (final Exception e) {
log.info("Failed to parse ISA JSON and get studies", e);
throw new MarsReceiptException("Failed to parse ISA JSON and get studies", e);
}

return null;
}

public Map<String, String> getBiosamples(List<Study> studies) {
public Map<String, String> getBiosamples(List<Study> studies) throws Exception {
HashMap<String, String> biosamples = new HashMap<>();
for (Study study : studies) {
for (Source source : study.materials.sources) {
Expand All @@ -162,13 +163,11 @@ private String getCharacteresticAnnotation(List<Characteristic> characteristics)
.collect(Collectors.toList());

if (filteredCharacteristics.isEmpty()) {
log.error("No accession found in the characteristics");
throw new RuntimeException("No accession found in the characteristics");
throw new MarsReceiptException("No accession found in the characteristics");
}

if (filteredCharacteristics.size() > 1) {
log.error("More than one accession found in the characteristics");
throw new RuntimeException("Too many accessions found in the characteristics");
throw new MarsReceiptException("Too many accessions found in the characteristics");
}
return filteredCharacteristics.get(0).value.annotationValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String convertMarsReceiptToJson() {
try {
return jsonMapper.writeValueAsString(getMarsReceipt());
} catch (Exception ex) {
throw new RuntimeException("receipt", ex);
throw new RuntimeException("Receipt", ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** Elixir BioHackathon 2022 */
package com.elixir.biohackaton.ISAToSRA.sra.service;

import com.elixir.biohackaton.ISAToSRA.receipt.MarsReceiptException;
import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.*;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -75,7 +76,7 @@ private void convertDataFileToFileElement(DataFile dataFile, Element filesElemen
});

if (Objects.isNull(checksum.get()) || Objects.isNull(checksumType.get())) {
log.error("Checksum and checksum type not found");
throw new MarsReceiptException("Checksum and checksum type not found");
} else {
Element fileElement = filesElement.addElement("FILE");
fileElement.addAttribute("filename", filename);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** Elixir BioHackathon 2022 */
package com.elixir.biohackaton.ISAToSRA.sra.service;

import com.elixir.biohackaton.ISAToSRA.receipt.MarsReceiptException;
import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.*;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -32,10 +33,9 @@ public Map<String, String> createENAExperimentSetElement(
typeToBioSamplesAccessionMap,
randomSubmissionIdentifier);
} catch (final Exception e) {
log.info("Failed to parse experiments from ISA Json file and create ENA Experiments");
throw new MarsReceiptException(
"Failed to parse experiments from ISA Json file and create ENA Experiments", e);
}

return null;
}

private String populateProcessSequenceToParameterValuesMapAndGetExecutesProtocolId(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** Elixir BioHackathon 2022 */
package com.elixir.biohackaton.ISAToSRA.sra.service;

import com.elixir.biohackaton.ISAToSRA.receipt.MarsReceiptException;
import com.elixir.biohackaton.ISAToSRA.receipt.isamodel.*;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -48,7 +49,7 @@ public void createENAStudySetElement(
});
});
} catch (final Exception e) {
log.info("Failed to parse ISA JSON and create ENA study");
throw new MarsReceiptException("Failed to parse ISA JSON and create ENA study");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.elixir.biohackaton.ISAToSRA.receipt;

import lombok.Getter;

public class MarsReceiptException extends RuntimeException {
@Getter
private final String receiptErrorMessage;

public MarsReceiptException(final String receiptErrorMessage) {
this.receiptErrorMessage = receiptErrorMessage;
}

public MarsReceiptException(final String receiptErrorMessage, final Exception exception) {
this.addSuppressed(exception);
this.receiptErrorMessage = receiptErrorMessage;
}
}

0 comments on commit 2d72fcb

Please sign in to comment.