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 @@ -31,15 +31,13 @@
import org.brapi.client.v2.modules.phenotype.ObservationUnitsApi;
import org.brapi.v2.model.BrAPIIndexPagination;
import org.brapi.v2.model.BrAPIMetadata;
import org.brapi.v2.model.core.BrAPIStudy;
import org.brapi.v2.model.core.BrAPITrial;
import org.brapi.v2.model.pheno.BrAPIObservationUnitHierarchyLevel;
import org.brapi.v2.model.pheno.response.BrAPIObservationLevelListResponse;
import org.brapi.v2.model.pheno.response.BrAPIObservationLevelListResponseResult;
import org.breedinginsight.api.auth.ProgramSecured;
import org.breedinginsight.api.auth.ProgramSecuredRoleGroup;
import org.breedinginsight.brapi.v1.controller.BrapiVersion;
import org.breedinginsight.brapi.v2.dao.BrAPIStudyDAO;
import org.breedinginsight.brapi.v2.dao.BrAPITrialDAO;
import org.breedinginsight.daos.ProgramDAO;
import org.breedinginsight.model.BrAPIConstants;
Expand All @@ -65,15 +63,13 @@ public class BrAPIObservationLevelsController {
private final ProgramDAO programDAO;
private final ProgramService programService;
private final BrAPITrialDAO trialDAO;
private final BrAPIStudyDAO studyDAO;

@Inject
public BrAPIObservationLevelsController(BrAPIEndpointProvider brAPIEndpointProvider, ProgramDAO programDAO, ProgramService programService, BrAPITrialDAO trialDAO, BrAPIStudyDAO studyDAO) {
public BrAPIObservationLevelsController(BrAPIEndpointProvider brAPIEndpointProvider, ProgramDAO programDAO, ProgramService programService, BrAPITrialDAO trialDAO) {
this.brAPIEndpointProvider = brAPIEndpointProvider;
this.programDAO = programDAO;
this.programService = programService;
this.trialDAO = trialDAO;
this.studyDAO = studyDAO;
}

@Get("/observationlevels")
Expand All @@ -93,25 +89,13 @@ public HttpResponse<BrAPIObservationLevelListResponse> observationlevelsGet(@Pat
}

String programDbId = program.get().getBrapiProgram().getProgramDbId();
String studyDbId = null;
String studyDbId = environmentId;
String trialDbId = null;

if(environmentId != null) {
try {
Optional<BrAPIStudy> study = studyDAO.getStudyByEnvironmentId(UUID.fromString(environmentId), program.get());
if(study.isPresent()) {
studyDbId = study.get().getStudyDbId();
} else {
studyDbId = environmentId;
}
} catch (ApiException e) {
log.error(Utilities.generateApiExceptionLogMessage(e), "Error fetching environment");
return HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, "Error finding observation levels");
}
} else if(experimentId != null) {
if(environmentId == null && experimentId != null) {
try {
List<BrAPITrial> trial = trialDAO.getTrialsByExperimentIds(List.of(UUID.fromString(experimentId)), program.get());
if(trial.size() == 1) {
if (trial.size() == 1) {
trialDbId = trial.get(0).getTrialDbId();
} else {
trialDbId = experimentId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,6 @@ public HttpResponse<?> observationunitsTableGet(@PathVariable("programId") UUID
}

private void setDbIds(BrAPIObservationUnit ou) {
ou.studyDbId(Utilities.getExternalReference(ou.getExternalReferences(), Utilities.generateReferenceSource(referenceSource, ExternalReferenceSource.STUDIES))
.orElseThrow(() -> new IllegalStateException("No BI external reference found"))
.getReferenceID());
ou.programDbId(Utilities.getExternalReference(ou.getExternalReferences(), Utilities.generateReferenceSource(referenceSource, ExternalReferenceSource.PROGRAMS))
.orElseThrow(() -> new IllegalStateException("No BI external reference found"))
.getReferenceID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,17 @@ public class BrAPIObservationsController {

private final ProgramService programService;
private final ProgramDAO programDAO;
private final BrAPIStudyDAO brAPIStudyDAO;
private final BrAPIEndpointProvider brAPIEndpointProvider;
private final BrAPIObservationDAO observationDAO;

@Inject
public BrAPIObservationsController(ProgramService programService,
ProgramDAO programDAO,
ProgramDAO programDAO1,
BrAPIStudyDAO brAPIStudyDAO,
BrAPIEndpointProvider brAPIEndpointProvider,
BrAPIObservationDAO brAPIObservationDAO) {
this.programService = programService;
this.programDAO = programDAO1;
this.brAPIStudyDAO = brAPIStudyDAO;
this.brAPIEndpointProvider = brAPIEndpointProvider;
this.observationDAO = brAPIObservationDAO;
}
Expand Down Expand Up @@ -254,16 +251,6 @@ public HttpResponse<BrAPIObservationTableResponse> observationsTableGet(
}

try {
// Translate studyDbId if provided.
if (queryParams.getStudyDbId() != null) {
Optional<BrAPIStudy> study = brAPIStudyDAO.getStudyByEnvironmentId(UUID.fromString(queryParams.getStudyDbId()), program.get());
if (study.isEmpty()) {
return HttpResponse.notFound();
}
queryParams.setStudyDbId(study.get().getStudyDbId());
}
// TODO: Translate other DbIds if provided as well (but studyDbId is sufficient for Mr. Bean).

ObservationsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(programId), ObservationsApi.class);
ApiResponse<BrAPIObservationTableResponse> response = api.observationsTableGet(BrAPIWSMIMEDataTypes.APPLICATION_JSON, queryParams.toBrAPIQueryParams());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.breedinginsight.brapi.v2;

import io.micronaut.context.annotation.Property;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.MediaType;
Expand All @@ -30,21 +29,22 @@
import org.brapi.v2.model.BrAPIStatus;
import org.brapi.v2.model.core.BrAPIStudy;
import org.brapi.v2.model.core.response.BrAPIStudySingleResponse;
import org.breedinginsight.api.auth.*;
import org.breedinginsight.api.auth.ProgramSecured;
import org.breedinginsight.api.auth.ProgramSecuredRole;
import org.breedinginsight.api.auth.ProgramSecuredRoleGroup;
import org.breedinginsight.api.auth.SecurityService;
import org.breedinginsight.api.model.v1.request.query.SearchRequest;
import org.breedinginsight.api.model.v1.response.DataResponse;
import org.breedinginsight.api.model.v1.response.Response;
import org.breedinginsight.api.model.v1.validators.QueryValid;
import org.breedinginsight.brapi.v1.controller.BrapiVersion;
import org.breedinginsight.brapi.v2.model.request.query.StudyQuery;
import org.breedinginsight.brapi.v2.services.BrAPIStudyService;
import org.breedinginsight.brapps.importer.services.ExternalReferenceSource;
import org.breedinginsight.model.Program;
import org.breedinginsight.model.ProgramUser;
import org.breedinginsight.services.ExperimentalCollaboratorService;
import org.breedinginsight.services.ProgramService;
import org.breedinginsight.services.ProgramUserService;
import org.breedinginsight.services.exceptions.DoesNotExistException;
import org.breedinginsight.utilities.Utilities;
import org.breedinginsight.utilities.response.ResponseUtils;
import org.breedinginsight.utilities.response.mappers.StudyQueryMapper;
Expand All @@ -54,14 +54,12 @@
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

@Slf4j
@Controller("/${micronaut.bi.api.version}/programs/{programId}" + BrapiVersion.BRAPI_V2)
@Secured(SecurityRule.IS_AUTHENTICATED)
public class BrAPIStudiesController {

private final String referenceSource;
private final BrAPIStudyService studyService;
private final StudyQueryMapper studyQueryMapper;
private final ProgramService programService;
Expand All @@ -73,14 +71,12 @@ public class BrAPIStudiesController {
@Inject
public BrAPIStudiesController(BrAPIStudyService studyService,
StudyQueryMapper studyQueryMapper,
@Property(name = "brapi.server.reference-source") String referenceSource,
ProgramService programService,
SecurityService securityService,
ProgramUserService programUserService,
ExperimentalCollaboratorService experimentalCollaboratorService) {
this.studyService = studyService;
this.studyQueryMapper = studyQueryMapper;
this.referenceSource = referenceSource;
this.programService = programService;
this.securityService = securityService;
this.programUserService = programUserService;
Expand All @@ -107,18 +103,14 @@ public HttpResponse<Response<DataResponse<List<BrAPIStudy>>>> getStudies(
Optional<ProgramUser> experimentalCollaborator = programUserService.getIfExperimentalCollaborator(programId, securityService.getUser().getId());
if (experimentalCollaborator.isPresent()) {
List<UUID> authorizedExperimentIds = experimentalCollaboratorService.getAuthorizedExperimentIds(experimentalCollaborator.get().getId());
List<BrAPIStudy> authorizedStudies = studyService.getStudiesByExperimentIds(program.get(), authorizedExperimentIds)
.stream()
.peek(this::setDbIds)
.collect(Collectors.toList());
List<BrAPIStudy> authorizedStudies = studyService.getStudiesByExperimentIds(
program.get(),
authorizedExperimentIds);
return ResponseUtils.getBrapiQueryResponse(authorizedStudies, studyQueryMapper, queryParams, searchRequest);
}

// TODO: Instead of getting all studies for a program and filtering, doing the filtering on brapi side [BI-2922]
List<BrAPIStudy> studies = studyService.getStudies(programId)
.stream()
.peek(this::setDbIds)
.collect(Collectors.toList());
List<BrAPIStudy> studies = studyService.getStudies(programId);
return ResponseUtils.getBrapiQueryResponse(studies, studyQueryMapper, queryParams, searchRequest);
} catch (ApiException e) {
log.info(e.getMessage(), e);
Expand All @@ -138,25 +130,31 @@ public HttpResponse studiesPost(@PathVariable("programId") UUID programId, @Body

@Get("/studies/{studyDbId}")
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.PROGRAM_SCOPED_ROLES})
public HttpResponse<BrAPIStudySingleResponse> studiesStudyDbIdGet(@PathVariable("programId") UUID programId, @PathVariable("studyDbId") String environmentId) {
public HttpResponse<BrAPIStudySingleResponse> studiesStudyDbIdGet(@PathVariable("programId") UUID programId, @PathVariable("studyDbId") String studyDbId) {
Optional<Program> program = programService.getById(programId);
if(program.isEmpty()) {
if (program.isEmpty()) {
log.warn("program id: " + programId + " not found");
return HttpResponse.notFound();
}

try {
Optional<BrAPIStudy> study = studyService.getStudyByEnvironmentId(program.get(), UUID.fromString(environmentId));
if(study.isPresent()) {
setDbIds(study.get());
return HttpResponse.ok(new BrAPIStudySingleResponse().result(study.get()));
Optional<BrAPIStudy> study = studyService.getStudyByDbId(program.get(), studyDbId);

if (study.isPresent()) {
return HttpResponse.ok(
new BrAPIStudySingleResponse()
.result(study.get()));
} else {
log.warn("studyDbId: " + environmentId + " not found");
log.warn("studyDbId: " + studyDbId + " not found");
return HttpResponse.notFound();
}
} catch (ApiException e) {
log.error(Utilities.generateApiExceptionLogMessage(e), e);
return HttpResponse.serverError(new BrAPIStudySingleResponse().metadata(new BrAPIMetadata().addStatusItem(new BrAPIStatus().message("Error fetching study")
.messageType(BrAPIStatus.MessageTypeEnum.ERROR))));
return HttpResponse.serverError(new BrAPIStudySingleResponse()
.metadata(new BrAPIMetadata()
.addStatusItem(new BrAPIStatus()
.message("Error fetching study")
.messageType(BrAPIStatus.MessageTypeEnum.ERROR))));
}
}

Expand All @@ -168,11 +166,4 @@ public HttpResponse studiesStudyDbIdPut(@PathVariable("programId") UUID programI
return HttpResponse.notFound();
}

private void setDbIds(BrAPIStudy study) {
study.studyDbId(Utilities.getExternalReference(study.getExternalReferences(), Utilities.generateReferenceSource(referenceSource, ExternalReferenceSource.STUDIES))
.orElseThrow(() -> new IllegalStateException("No BI external reference found"))
.getReferenceID());

//TODO update locationDbId
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@
import org.brapi.client.v2.model.queryParams.phenotype.ObservationQueryParams;
import org.brapi.client.v2.modules.phenotype.ObservationsApi;
import org.brapi.v2.model.BrAPIAcceptedSearchResponse;
import org.brapi.v2.model.BrAPIExternalReference;
import org.brapi.v2.model.core.BrAPIProgram;
import org.brapi.v2.model.pheno.BrAPIObservation;
import org.brapi.v2.model.pheno.BrAPIObservationUnit;
import org.brapi.v2.model.pheno.request.BrAPIObservationSearchRequest;
import org.brapi.v2.model.pheno.response.BrAPIObservationListResponse;
import org.breedinginsight.brapps.importer.daos.ImportDAO;
import org.breedinginsight.brapps.importer.model.ImportUpload;
import org.breedinginsight.brapps.importer.services.ExternalReferenceSource;
import org.breedinginsight.daos.ProgramDAO;
import org.breedinginsight.model.Program;
import org.breedinginsight.services.TraitService;
Expand All @@ -60,7 +58,6 @@ public class BrAPIObservationDAO {
private BrAPIObservationUnitDAO observationUnitDAO;
private final BrAPIDAOUtil brAPIDAOUtil;
private final BrAPIEndpointProvider brAPIEndpointProvider;
private final String referenceSource;
private final TraitService traitService;

private final int brapiMaxPageSize;
Expand All @@ -71,15 +68,13 @@ public BrAPIObservationDAO(ProgramDAO programDAO,
BrAPIObservationUnitDAO observationUnitDAO,
BrAPIDAOUtil brAPIDAOUtil,
BrAPIEndpointProvider brAPIEndpointProvider,
@Property(name = "brapi.server.reference-source") String referenceSource,
@Property(name = "brapi.cache.fetch-page-size") int brapiFetchPageSize,
TraitService traitService) {
this.programDAO = programDAO;
this.importDAO = importDAO;
this.observationUnitDAO = observationUnitDAO;
this.brAPIDAOUtil = brAPIDAOUtil;
this.brAPIEndpointProvider = brAPIEndpointProvider;
this.referenceSource = referenceSource;
this.traitService = traitService;
this.brapiMaxPageSize = brapiFetchPageSize;
}
Expand Down Expand Up @@ -184,32 +179,21 @@ public List<BrAPIObservation> getObservationsByObservationUnits(Collection<Strin
.collect(Collectors.toList());
}

// TODO: implement other filters in BI-2506.
public List<BrAPIObservation> getObservationsByFilters(Program program, String studyDbId) throws ApiException, DoesNotExistException {

String studySource = Utilities.generateReferenceSource(referenceSource, ExternalReferenceSource.STUDIES);

// Get all observations for the program.
Collection<BrAPIObservation> observations = getProgramObservations(program.getId());
// Build a hashmap of traits for fast lookup. The key is ObservationVariableDbId, the value is the Trait Id.
HashMap<String, String> traitIdsByObservationVariableDbId = traitService.getIdsByObservationVariableDbIds(program.getId(), observations.stream().map(BrAPIObservation::getObservationVariableDbId).collect(Collectors.toList()));

// Lookup studyDbId.
HashMap<String, String> traitIdsByObservationVariableDbId =
traitService.getIdsByObservationVariableDbIds(
program.getId(),
observations.stream()
.map(BrAPIObservation::getObservationVariableDbId)
.collect(Collectors.toList()));

return observations.stream()
.filter(o -> {
// Short circuit if filter is null.
if (studyDbId == null) return true;
Optional<BrAPIExternalReference> xref = Utilities.getExternalReference(o.getExternalReferences(), studySource);
return xref.filter(brAPIExternalReference -> studyDbId.equals(brAPIExternalReference.getReferenceId())).isPresent();
})
.peek(o -> {
// Translate ObservationVariableDbId.
o.setObservationVariableDbId(traitIdsByObservationVariableDbId.get(o.getObservationVariableDbId()));
// Translate StudyDbId.
o.setStudyDbId(Utilities.getExternalReference(o.getExternalReferences(), studySource)
.orElseThrow(() -> new RuntimeException("study xref not found on observation")).getReferenceId());
// TODO: consider translating germplasmDbId in BI-2506.
}).collect(Collectors.toList());
.filter(o -> studyDbId == null || studyDbId.equals(o.getStudyDbId()))
.peek(o ->
o.setObservationVariableDbId(traitIdsByObservationVariableDbId.get(o.getObservationVariableDbId())))
.collect(Collectors.toList());
}

@NotNull
Expand Down
Loading
Loading