Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ public HttpResponse<BrAPIObservationUnitListResponse> observationunitsGet(@PathV
Optional.ofNullable(studyDbId),
Optional.ofNullable(germplasmDbId))
.stream()
.peek(this::setDbIds)
.collect(Collectors.toList());

return HttpResponse.ok(new BrAPIObservationUnitListResponse().metadata(new BrAPIMetadata().pagination(new BrAPIIndexPagination().currentPage(0)
Expand Down Expand Up @@ -161,7 +160,6 @@ public HttpResponse<BrAPIObservationUnitSingleResponse> observationunitsObservat
log.warn("did not find a single ou with dbId: " + observationUnitDbId);
return HttpResponse.notFound();
}
setDbIds(ous.get(0));
return HttpResponse.ok(new BrAPIObservationUnitSingleResponse().result(ous.get(0)));
} catch (ApiException e) {
log.error(Utilities.generateApiExceptionLogMessage(e), e);
Expand Down Expand Up @@ -218,18 +216,4 @@ public HttpResponse<?> observationunitsTableGet(@PathVariable("programId") UUID
@Nullable @QueryValue("observationUnitLevelRelationshipDbId") String observationUnitLevelRelationshipDbId) {
return HttpResponse.notFound();
}

private void setDbIds(BrAPIObservationUnit ou) {
ou.programDbId(Utilities.getExternalReference(ou.getExternalReferences(), Utilities.generateReferenceSource(referenceSource, ExternalReferenceSource.PROGRAMS))
.orElseThrow(() -> new IllegalStateException("No BI external reference found"))
.getReferenceID());
// TODO: Remove this as part of [BI-3006]
if (ou.getAdditionalInfo().has(BrAPIAdditionalInfoFields.GERMPLASM_UUID)) {
ou.setGermplasmDbId(ou.getAdditionalInfo()
.get(BrAPIAdditionalInfoFields.GERMPLASM_UUID)
.getAsString());
}

//TODO update locationDbId
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.rules.SecurityRule;
import lombok.extern.slf4j.Slf4j;
import org.brapi.client.v2.model.exceptions.ApiException;
import org.brapi.v2.model.BrAPIIndexPagination;
import org.brapi.v2.model.BrAPIMetadata;
import org.brapi.v2.model.core.BrAPIProgram;
Expand All @@ -33,7 +34,6 @@
import org.breedinginsight.api.auth.ProgramSecured;
import org.breedinginsight.api.auth.ProgramSecuredRoleGroup;
import org.breedinginsight.api.auth.SecurityService;
import org.breedinginsight.api.model.v1.response.DataResponse;
import org.breedinginsight.brapi.v1.controller.BrapiVersion;
import org.breedinginsight.model.Program;
import org.breedinginsight.services.ProgramService;
Expand Down Expand Up @@ -82,17 +82,28 @@ public HttpResponse<BrAPIProgramListResponse> rootProgramsGet(
Optional<String> programDbIdOptional = Optional.ofNullable(programDbId);
Optional<String> programNameOptional = Optional.ofNullable(programName);

List<BrAPIProgram> programs = programService.getAll(securityService.getUser()).stream().filter(program -> {
List<Program> programs = programService.getAll(securityService.getUser());

for (Program program : programs) {
try {
program.setBrapiProgram(new BrAPIProgram().programDbId(programService.getBrAPIProgramDbId(program.getId())));
} catch (ApiException e) {
log.info(e.getMessage(), e);
return HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, String.format("Error retrieving programs: [%s]", e.getMessage()));
}
}

List<BrAPIProgram> brapiPrograms = programs.stream().filter(program -> {
boolean matches = abbreviationOptional.map(abbr -> abbr.equals(program.getKey())).orElse(true);
matches = matches && programDbIdOptional.map(id -> id.equals(program.getId().toString())).orElse(true);
matches = matches && programDbIdOptional.map(id -> id.equals(program.getBrapiProgram().getProgramDbId())).orElse(true);
return matches && programNameOptional.map(name -> name.equals(program.getName())).orElse(true);
}).map(this::convertToBrAPI).collect(Collectors.toList());

return HttpResponse.ok(new BrAPIProgramListResponse().metadata(new BrAPIMetadata().pagination(new BrAPIIndexPagination().currentPage(0)
.totalPages(1)
.totalCount(programs.size())
.pageSize(programs.size())))
.result(new BrAPIProgramListResponseResult().data(programs)));
.totalCount(brapiPrograms.size())
.pageSize(brapiPrograms.size())))
.result(new BrAPIProgramListResponseResult().data(brapiPrograms)));
}

@Post(BrapiVersion.BRAPI_V2 + "/programs")
Expand Down Expand Up @@ -140,35 +151,35 @@ public HttpResponse<?> rootProgramsProgramDbIdPut(@PathVariable("programDbId") S
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.PROGRAM_SCOPED_ROLES})
public HttpResponse<BrAPIProgramListResponse> programsGet(@PathVariable("programId") UUID programId,
@QueryValue("abbreviation") Optional<String> abbreviation,
@QueryValue("programType") Optional<String> programType,
@QueryValue("commonCropName") Optional<String> commonCropName,
@QueryValue("programDbId") Optional<String> programDbId,
@QueryValue("programName") Optional<String> programName,
@QueryValue("externalReferenceID") Optional<String> externalReferenceID,
@QueryValue("externalReferenceId") Optional<String> externalReferenceId,
@QueryValue("externalReferenceSource") Optional<String> externalReferenceSource,
@QueryValue("page") Optional<Integer> page,
@QueryValue("pageSize") Optional<Integer> pageSize) {
@QueryValue("abbreviation") Optional<String> abbreviation,
@QueryValue("programType") Optional<String> programType,
@QueryValue("commonCropName") Optional<String> commonCropName,
@QueryValue("programDbId") Optional<String> programDbId,
@QueryValue("programName") Optional<String> programName,
@QueryValue("externalReferenceID") Optional<String> externalReferenceID,
@QueryValue("externalReferenceId") Optional<String> externalReferenceId,
@QueryValue("externalReferenceSource") Optional<String> externalReferenceSource,
@QueryValue("page") Optional<Integer> page,
@QueryValue("pageSize") Optional<Integer> pageSize) {

//If programId supplied, check if program exists
if (programId != null) {
Optional<Program> optProgram = programService.getById(programId);
if (optProgram.isEmpty()) {
if (optProgram.isEmpty() || optProgram.get().getBrapiProgram() == null) {
return HttpResponse.status(HttpStatus.NOT_FOUND, "Program not found");
}

List<BrAPIProgram> programs = optProgram.stream().filter(program -> {
boolean matches = abbreviation.map(abbr -> abbr.equals(program.getKey())).orElse(true);
matches = matches && programDbId.map(id -> id.equals(program.getId().toString())).orElse(true);
return matches && programName.map(name -> name.equals(program.getName())).orElse(true);
}).map(this::convertToBrAPI).collect(Collectors.toList());

return HttpResponse.ok(new BrAPIProgramListResponse().metadata(new BrAPIMetadata().pagination(new BrAPIIndexPagination().currentPage(0)
.totalPages(1)
.totalCount(programs.size())
.pageSize(programs.size())))
.result(new BrAPIProgramListResponseResult().data(programs)));
List<BrAPIProgram> programs = optProgram.stream().filter(program -> {
boolean matches = abbreviation.map(abbr -> abbr.equals(program.getKey())).orElse(true);
matches = matches && programDbId.map(id -> id.equals(program.getBrapiProgram().getProgramDbId())).orElse(true);
return matches && programName.map(name -> name.equals(program.getName())).orElse(true);
}).map(this::convertToBrAPI).collect(Collectors.toList());

return HttpResponse.ok(new BrAPIProgramListResponse().metadata(new BrAPIMetadata().pagination(new BrAPIIndexPagination().currentPage(0)
.totalPages(1)
.totalCount(programs.size())
.pageSize(programs.size())))
.result(new BrAPIProgramListResponseResult().data(programs)));
} else {
return HttpResponse.status(HttpStatus.NOT_FOUND, "Program not found");
}
Expand All @@ -181,12 +192,13 @@ public HttpResponse<?> programsPost(@PathVariable("programId") UUID programId, @
return HttpResponse.notFound();
}

@Get("/programs/{programId}" + BrapiVersion.BRAPI_V2 + "/programs/{programDbId}")
@Get("/programs/{programId}" + BrapiVersion.BRAPI_V2 + "/programs/{brapiProgramDbId}")
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.PROGRAM_SCOPED_ROLES})
public HttpResponse<BrAPIProgramSingleResponse> programsProgramDbIdGet(@PathVariable("programId") UUID programId, @PathVariable("programDbId") String programDbId) {
Optional<BrAPIProgram> program = programService.getById(programId)
public HttpResponse<BrAPIProgramSingleResponse> programsProgramDbIdGet(@PathVariable("programId") UUID biProgramId,
@PathVariable("brapiProgramDbId") String brapiProgramDbId) {
Optional<BrAPIProgram> program = programService.getById(biProgramId)
.stream()
.filter(p -> programDbId.equals(programId.toString()))
.filter(p -> p.getBrapiProgram() != null && brapiProgramDbId.equals(p.getBrapiProgram().getProgramDbId()))
.map(this::convertToBrAPI)
.findFirst();

Expand All @@ -203,7 +215,8 @@ public HttpResponse<?> programsProgramDbIdPut(@PathVariable("programId") UUID pr

private BrAPIProgram convertToBrAPI(Program program) {
return new BrAPIProgram().programName(program.getName())
.programDbId(program.getId().toString())
// Assumes brapiProgram is always set with the BrAPI programDbId
.programDbId(program.getBrapiProgram().getProgramDbId())
.abbreviation(program.getKey())
.commonCropName(program.getSpecies().getCommonName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public HttpResponse<Response<DataResponse<BrAPITrial>>> getExperiments(
BrAPITrialListResponse brapiResponse = experimentService.searchTrials(program.get(), authorizedExperimentIds, queryParams);

List<BrAPITrial> foundTrials = brapiResponse.getResult().getData();
foundTrials.forEach(this::setDbIds);

return ResponseUtils.getBrapiQueryResponse(foundTrials, brapiResponse);
}
Expand All @@ -118,7 +117,6 @@ public HttpResponse<Response<DataResponse<BrAPITrial>>> getExperiments(
BrAPITrialListResponse brapiResponse = experimentService.searchTrials(program.get(), queryParams);

List<BrAPITrial> foundTrials = brapiResponse.getResult().getData();
foundTrials.forEach(this::setDbIds);
return ResponseUtils.getBrapiQueryResponse(foundTrials, brapiResponse);
} catch (ApiException e) {
log.info(e.getMessage(), e);
Expand All @@ -144,7 +142,6 @@ public HttpResponse<BrAPITrialSingleResponse> getExperimentById(
}
log.debug(logMsg);
BrAPITrial trial = experimentService.getTrialDataByUUID(programId, trialId, stats);
setDbIds(trial);
return HttpResponse.ok(new BrAPITrialSingleResponse().result(trial));
} catch (DoesNotExistException e) {
log.info(e.getMessage(), e);
Expand All @@ -166,13 +163,4 @@ public HttpResponse<?> trialsTrialDbIdPut(@PathVariable("programId") UUID progra
//DO NOT IMPLEMENT - Users are only able to update trials via the DeltaBreed UI
return HttpResponse.notFound();
}

// TODO: Verify that the programDbId does not need to be reset and that the BrAPI programDbId can be used instead [BI-2931]
private void setDbIds(BrAPITrial trial) {
trial.programDbId(Utilities.getExternalReference(trial.getExternalReferences(), Utilities.generateReferenceSource(referenceSource, ExternalReferenceSource.PROGRAMS))
.orElseThrow(() -> new IllegalStateException("No BI external reference found"))
.getReferenceID());

//TODO update locationDbId
}
}
Loading