Skip to content
Merged
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 @@ -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.breedinginsight.api.auth.*;
import org.breedinginsight.api.model.v1.request.*;
import org.breedinginsight.api.model.v1.request.query.QueryParams;
Expand All @@ -37,6 +38,7 @@
import org.breedinginsight.model.*;
import org.breedinginsight.services.ProgramObservationLevelService;
import org.breedinginsight.services.exceptions.*;
import org.breedinginsight.utilities.Utilities;
import org.breedinginsight.utilities.response.mappers.ProgramLocationQueryMapper;
import org.breedinginsight.utilities.response.mappers.ProgramQueryMapper;
import org.breedinginsight.api.model.v1.response.DataResponse;
Expand Down Expand Up @@ -302,6 +304,9 @@ public HttpResponse<Response<DataResponse<ProgramLocation>>> getProgramLocations
} catch (DoesNotExistException e){
log.info(e.getMessage());
return HttpResponse.notFound();
} catch (ApiException e) {
log.error("Error fetching program locations: " + Utilities.generateApiExceptionLogMessage(e), e);
return HttpResponse.serverError();
}
}

Expand All @@ -318,8 +323,11 @@ public HttpResponse<Response<DataResponse<ProgramLocation>>> postProgramLocation
return ResponseUtils.getQueryResponse(programLocations, programLocationQueryMapper, searchRequest, queryParams);

} catch (DoesNotExistException e){
log.info(e.getMessage());
log.error(e.getMessage(), e);
return HttpResponse.notFound();
} catch (ApiException e) {
log.error("Error fetching program locations: " + Utilities.generateApiExceptionLogMessage(e), e);
return HttpResponse.serverError();
}

}
Expand All @@ -332,7 +340,13 @@ public HttpResponse<Response<DataResponse<ProgramLocation>>> postProgramLocation
public HttpResponse<Response<ProgramLocation>> getProgramLocations(@PathVariable UUID programId,
@PathVariable UUID locationId) {

Optional<ProgramLocation> programLocation = programLocationService.getById(programId, locationId);
Optional<ProgramLocation> programLocation = null;
try {
programLocation = programLocationService.getById(programId, locationId);
} catch (ApiException e) {
log.error("Error fetching program location: " + Utilities.generateApiExceptionLogMessage(e), e);
return HttpResponse.serverError();
}

if(programLocation.isPresent()) {
Response<ProgramLocation> response = new Response(programLocation.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.breedinginsight.brapi.v1.model.request.query.BrapiQuery;
import org.breedinginsight.brapi.v2.dao.BrAPIGermplasmDAO;
import org.breedinginsight.brapi.v2.model.request.query.GermplasmQuery;
import org.breedinginsight.utilities.Utilities;
import org.breedinginsight.utilities.response.mappers.GermplasmQueryMapper;
import org.breedinginsight.brapi.v2.services.BrAPIGermplasmService;
import org.breedinginsight.brapps.importer.model.exports.FileType;
Expand All @@ -44,10 +45,7 @@

import javax.inject.Inject;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.*;

@Slf4j
@Controller("/${micronaut.bi.api.version}")
Expand Down Expand Up @@ -205,7 +203,9 @@ public HttpResponse<BrAPIGermplasmPedigreeResponse> getGermplasmPedigreeInfo(
metadata.setPagination(pagination);
response = new BrAPIGermplasmPedigreeResponse();
} else {
BrAPIGermplasm germplasm = germplasmService.getGermplasmByDBID(programId, germplasmId);
BrAPIGermplasm germplasm = germplasmService.getGermplasmByDBID(programId, germplasmId)
.orElseThrow(() -> new DoesNotExistException("DBID for this germplasm does not exist"));

//Forward the pedigree call to the backing BrAPI system of the program passing the germplasmDbId that came in the request
GermplasmApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(programId), GermplasmApi.class);
ApiResponse<BrAPIGermplasmPedigreeResponse> pedigreeResponse = api.germplasmGermplasmDbIdPedigreeGet(germplasmId, notation, includeSiblings);
Expand Down Expand Up @@ -238,12 +238,15 @@ public HttpResponse<BrAPIGermplasmPedigreeResponse> getGermplasmPedigreeInfo(
response.setResult(returnNode);
response.setMetadata(metadata);
return HttpResponse.ok(response);
} catch (InternalServerException | ApiException e) {
} catch (InternalServerException e) {
log.error(e.getMessage(), e);
return HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, "Error retrieving pedigree node");
} catch (DoesNotExistException e) {
log.error(e.getMessage(), e);
return HttpResponse.status(HttpStatus.NOT_FOUND, "Pedigree node not found");
} catch (ApiException e) {
log.info(Utilities.generateApiExceptionLogMessage(e), e);
return HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, "Error retrieving pedigree node");
}
}

Expand Down Expand Up @@ -302,8 +305,11 @@ public HttpResponse<BrAPIGermplasmProgenyResponse> getGermplasmProgenyInfo(
}
return HttpResponse.ok(progenyResponse.getBody());
}
} catch (InternalServerException | ApiException e) {
log.info(e.getMessage(), e);
} catch (InternalServerException e) {
log.error(e.getMessage(), e);
return HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, "Error retrieving pedigree node");
} catch (ApiException e) {
log.error(Utilities.generateApiExceptionLogMessage(e), e);
return HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, "Error retrieving pedigree node");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,22 @@ public BrAPIGermplasm getGermplasmByUUID(String germplasmId, UUID programId) thr
return germplasm;
}

public BrAPIGermplasm getGermplasmByDBID(String germplasmDbId, UUID programId) throws ApiException, DoesNotExistException {
public Optional<BrAPIGermplasm> getGermplasmByDBID(String germplasmDbId, UUID programId) throws ApiException {
Map<String, BrAPIGermplasm> cache = programGermplasmCache.get(programId);
//key is UUID, want to filter by DBID
BrAPIGermplasm germplasm = null;
if (cache != null) {
germplasm = cache.values().stream().filter(x -> x.getGermplasmDbId().equals(germplasmDbId)).collect(Collectors.toList()).get(0);
}
if (germplasm == null) {
throw new DoesNotExistException("DBID for this germplasm does not exist");
return Optional.ofNullable(germplasm);
}

public List<BrAPIGermplasm> getGermplasmsByDBID(Collection<String> germplasmDbIds, UUID programId) throws ApiException {
Map<String, BrAPIGermplasm> cache = programGermplasmCache.get(programId);
//key is UUID, want to filter by DBID
List<BrAPIGermplasm> germplasm = new ArrayList<>();
if (cache != null) {
germplasm = cache.values().stream().filter(x -> germplasmDbIds.contains(x.getGermplasmDbId())).collect(Collectors.toList());
}
return germplasm;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,8 @@ public BrAPIGermplasm getGermplasmByUUID(UUID programId, String germplasmId) thr
}
}

public BrAPIGermplasm getGermplasmByDBID(UUID programId, String germplasmId) throws DoesNotExistException {
try {
return germplasmDAO.getGermplasmByDBID(germplasmId, programId);
} catch (ApiException e) {
throw new InternalServerException(e.getMessage(), e);
}
public Optional<BrAPIGermplasm> getGermplasmByDBID(UUID programId, String germplasmId) throws ApiException {
return germplasmDAO.getGermplasmByDBID(germplasmId, programId);
}

public List<BrAPIListSummary> getGermplasmListsByProgramId(UUID programId, HttpRequest<String> request) throws DoesNotExistException, ApiException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import org.breedinginsight.utilities.Utilities;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

Expand All @@ -42,6 +44,10 @@ public BrAPIListDAO(ProgramDAO programDAO, ImportDAO importDAO, BrAPIDAOUtil brA
}

public List<BrAPIListSummary> getListByName(List<String> listNames, UUID programId) throws ApiException {
if(listNames.isEmpty()) {
return Collections.emptyList();
}

BrAPIListSearchRequest listSearch = new BrAPIListSearchRequest();
listSearch.listNames(listNames);
ListsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(programId), ListsApi.class);
Expand All @@ -58,7 +64,7 @@ public BrAPIListsSingleResponse getListById(String listId, UUID programId) throw
return response.getBody();
}

public List<BrAPIListSummary> getListByTypeAndExternalRef(BrAPIListTypes listType, UUID programId, String externalReferenceSource, UUID externalReferenceId) throws ApiException {
public List<BrAPIListSummary> getListByTypeAndExternalRef(@NotNull BrAPIListTypes listType, UUID programId, String externalReferenceSource, UUID externalReferenceId) throws ApiException {
BrAPIListSearchRequest searchRequest = new BrAPIListSearchRequest()
.externalReferenceIDs(List.of(externalReferenceId.toString()))
.externalReferenceSources(List.of(externalReferenceSource))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;

@Singleton
public class BrAPILocationDAO {
Expand All @@ -49,6 +47,9 @@ public BrAPILocationDAO(ProgramDAO programDAO, ImportDAO importDAO, BrAPIDAOUtil
}

public List<BrAPILocation> getLocationsByName(List<String> locationNames, UUID programId) throws ApiException {
if(locationNames.isEmpty()) {
return Collections.emptyList();
}

BrAPILocationSearchRequest locationSearchRequest = new BrAPILocationSearchRequest();
locationSearchRequest.setLocationNames(new ArrayList<>(locationNames));
Expand All @@ -61,9 +62,24 @@ public List<BrAPILocation> getLocationsByName(List<String> locationNames, UUID p
);
}

public List<BrAPILocation> createBrAPILocation(List<BrAPILocation> brAPILocationList, UUID programId, ImportUpload upload) throws ApiException {
public List<BrAPILocation> createBrAPILocations(List<BrAPILocation> brAPILocationList, UUID programId, ImportUpload upload) throws ApiException {
LocationsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(programId), LocationsApi.class);
return brAPIDAOUtil.post(brAPILocationList, upload, api::locationsPost, importDAO::update);
}

public List<BrAPILocation> getLocationsByDbId(Collection<String> locationDbIds, UUID programId) throws ApiException {
if(locationDbIds.isEmpty()) {
return Collections.emptyList();
}

BrAPILocationSearchRequest locationSearchRequest = new BrAPILocationSearchRequest();
locationSearchRequest.setLocationDbIds(new ArrayList<>(locationDbIds));
//TODO: Locations don't connect to programs. How to get locations for the program?
LocationsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(programId), LocationsApi.class);
return brAPIDAOUtil.search(
api::searchLocationsPost,
api::searchLocationsSearchResultsDbIdGet,
locationSearchRequest
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.brapi.v2.model.pheno.request.BrAPIObservationSearchRequest;
import org.brapi.v2.model.pheno.response.BrAPIObservationListResponse;
import org.breedinginsight.brapps.importer.model.ImportUpload;
import org.breedinginsight.daos.ObservationDAO;
import org.breedinginsight.daos.ProgramDAO;
import org.breedinginsight.model.Program;
import org.breedinginsight.services.brapi.BrAPIEndpointProvider;
Expand All @@ -34,10 +33,7 @@

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;

import static org.brapi.v2.model.BrAPIWSMIMEDataTypes.APPLICATION_JSON;

Expand All @@ -58,6 +54,9 @@ public BrAPIObservationDAO(ProgramDAO programDAO, ImportDAO importDAO, BrAPIDAOU
}

public List<BrAPIObservation> getObservationsByStudyName(List<String> studyNames, Program program) throws ApiException {
if(studyNames.isEmpty()) {
return Collections.emptyList();
}

BrAPIObservationSearchRequest observationSearchRequest = new BrAPIObservationSearchRequest();
observationSearchRequest.setProgramDbIds(List.of(program.getBrapiProgram().getProgramDbId()));
Expand All @@ -70,14 +69,30 @@ public List<BrAPIObservation> getObservationsByStudyName(List<String> studyNames
);
}

public List<BrAPIObservation> getObservationsByObservationUnitsAndVariables(Collection<String> ouDbIds, Collection<String> variableDbIds, Program program) throws ApiException {
if(ouDbIds.isEmpty() || variableDbIds.isEmpty()) {
return Collections.emptyList();
}

BrAPIObservationSearchRequest observationSearchRequest = new BrAPIObservationSearchRequest();
observationSearchRequest.setProgramDbIds(List.of(program.getBrapiProgram().getProgramDbId()));
observationSearchRequest.setObservationUnitDbIds(new ArrayList<>(ouDbIds));
observationSearchRequest.setObservationVariableDbIds(new ArrayList<>(variableDbIds));
ObservationsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(program.getId()), ObservationsApi.class);
return brAPIDAOUtil.search(
api::searchObservationsPost,
(brAPIWSMIMEDataTypes, searchResultsDbId, page, pageSize) -> searchObservationsSearchResultsDbIdGet(program.getId(), searchResultsDbId, page, pageSize),
observationSearchRequest
);
}

@NotNull
private ApiResponse<Pair<Optional<BrAPIObservationListResponse>, Optional<BrAPIAcceptedSearchResponse>>>
searchObservationsSearchResultsDbIdGet(UUID programId, String searchResultsDbId, Integer page, Integer pageSize) throws ApiException {
private ApiResponse<Pair<Optional<BrAPIObservationListResponse>, Optional<BrAPIAcceptedSearchResponse>>> searchObservationsSearchResultsDbIdGet(UUID programId, String searchResultsDbId, Integer page, Integer pageSize) throws ApiException {
ObservationsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(programId), ObservationsApi.class);
return api.searchObservationsSearchResultsDbIdGet(APPLICATION_JSON, searchResultsDbId, page, pageSize);
}

public List<BrAPIObservation> createBrAPIObservation(List<BrAPIObservation> brAPIObservationList, UUID programId, ImportUpload upload) throws ApiException {
public List<BrAPIObservation> createBrAPIObservations(List<BrAPIObservation> brAPIObservationList, UUID programId, ImportUpload upload) throws ApiException {
ObservationsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(programId), ObservationsApi.class);
return brAPIDAOUtil.post(brAPIObservationList, upload, api::observationsPost, importDAO::update);
}
Expand Down
Loading