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
@@ -1,5 +1,6 @@
package org.breedinginsight.brapi.v2;

import com.drew.lang.annotations.Nullable;
import io.micronaut.context.annotation.Property;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpResponse;
Expand All @@ -19,10 +20,11 @@
import org.brapi.v2.model.BrAPIIndexPagination;
import org.brapi.v2.model.BrAPIMetadata;
import org.brapi.v2.model.BrAPIStatus;
import org.brapi.v2.model.core.BrAPITrial;
import org.brapi.v2.model.germ.*;
import org.brapi.v2.model.germ.request.BrAPIGermplasmSearchRequest;
import org.brapi.v2.model.germ.response.*;
import org.brapi.v2.model.germ.response.BrAPIGermplasmListResponse;
import org.brapi.v2.model.germ.response.BrAPIGermplasmPedigreeResponse;
import org.brapi.v2.model.germ.response.BrAPIGermplasmProgenyResponse;
import org.breedinginsight.api.auth.ProgramSecured;
import org.breedinginsight.api.auth.ProgramSecuredRoleGroup;
import org.breedinginsight.api.model.v1.request.query.SearchRequest;
Expand All @@ -33,27 +35,25 @@
import org.breedinginsight.brapi.v2.constants.BrAPIAdditionalInfoFields;
import org.breedinginsight.brapi.v2.dao.BrAPIGermplasmDAO;
import org.breedinginsight.brapi.v2.model.request.query.GermplasmQuery;
import org.breedinginsight.brapps.importer.services.ExternalReferenceSource;
import org.breedinginsight.model.Program;
import org.breedinginsight.services.ProgramService;
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;
import org.breedinginsight.daos.ProgramDAO;
import org.breedinginsight.model.DownloadFile;
import org.breedinginsight.model.GermplasmGenotype;
import org.breedinginsight.model.Program;
import org.breedinginsight.services.ProgramService;
import org.breedinginsight.services.brapi.BrAPIEndpointProvider;
import org.breedinginsight.services.exceptions.AuthorizationException;
import org.breedinginsight.services.exceptions.DoesNotExistException;
import org.breedinginsight.services.geno.GenotypeService;
import org.breedinginsight.utilities.Utilities;
import org.breedinginsight.utilities.response.ResponseUtils;
import org.breedinginsight.utilities.response.mappers.GermplasmQueryMapper;

import javax.inject.Inject;
import javax.validation.Valid;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Slf4j
@Controller("/${micronaut.bi.api.version}")
Expand Down Expand Up @@ -209,7 +209,8 @@ public HttpResponse<Response<DataResponse<List<BrAPIGermplasm>>>> getGermplasm(
germplasmQueryMapper.setDateDisplayFormat(dateFormatParam);
}

List<BrAPIGermplasm> germplasm = germplasmService.getGermplasm(programId);
// Fetch all germplasm in the program unless a list id is supplied to return only germplasm in that collection
List<BrAPIGermplasm> germplasm = queryParams.getListDbId() == null ? germplasmService.getGermplasm(programId) : germplasmService.getGermplasmByList(programId, queryParams.getListDbId());
SearchRequest searchRequest = queryParams.constructSearchRequest();
return ResponseUtils.getBrapiQueryResponse(germplasm, germplasmQueryMapper, queryParams, searchRequest);
} catch (ApiException e) {
Expand All @@ -221,52 +222,17 @@ public HttpResponse<Response<DataResponse<List<BrAPIGermplasm>>>> getGermplasm(
}
}

@Get("/programs/{programId}/germplasm/lists/{listDbId}/records{?queryParams*}")
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.PROGRAM_SCOPED_ROLES})
public HttpResponse<Response<DataResponse<List<BrAPIGermplasm>>>> getGermplasmListRecords(
@PathVariable("programId") UUID programId,
@PathVariable("listDbId") String listDbId,
@QueryValue @QueryValid(using = GermplasmQueryMapper.class) @Valid GermplasmQuery queryParams) {
try {
List<BrAPIGermplasm> germplasm = germplasmService.getGermplasmByList(programId, listDbId);
SearchRequest searchRequest = queryParams.constructSearchRequest();
return ResponseUtils.getBrapiQueryResponse(germplasm, germplasmQueryMapper, queryParams, searchRequest);
} catch (Exception e) {
log.info(e.getMessage(), e);
return HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, "Error retrieving germplasm list records");
}
}

@Get("/programs/{programId}/germplasm/lists/{listDbId}/export{?fileExtension}")
@Produces(value = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.PROGRAM_SCOPED_ROLES})
public HttpResponse<StreamedFile> germplasmListExport(
@PathVariable("programId") UUID programId, @PathVariable("listDbId") String listDbId, @QueryValue(defaultValue = "XLSX") String fileExtension) {
String downloadErrorMessage = "An error occurred while generating the download file. Contact the development team at bidevteam@cornell.edu.";
try {
FileType extension = Enum.valueOf(FileType.class, fileExtension);
DownloadFile germplasmListFile = germplasmService.exportGermplasmList(programId, listDbId, extension);
HttpResponse<StreamedFile> germplasmListExport = HttpResponse.ok(germplasmListFile.getStreamedFile()).header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename="+germplasmListFile.getFileName()+extension.getExtension());
return germplasmListExport;
}
catch (Exception e) {
log.info(e.getMessage(), e);
e.printStackTrace();
HttpResponse response = HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, downloadErrorMessage).contentType(MediaType.TEXT_PLAIN).body(downloadErrorMessage);
return response;
}
}

@Get("/programs/{programId}/germplasm/export{?fileExtension}")
@Get("/programs/{programId}/germplasm/export{?fileExtension,list}")
@Produces(value = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.PROGRAM_SCOPED_ROLES})
public HttpResponse<StreamedFile> germplasmExport(
@PathVariable("programId") UUID programId, @QueryValue(defaultValue = "XLSX") String fileExtension) {
@PathVariable("programId") UUID programId,
@QueryValue(defaultValue = "XLSX") String fileExtension,
@QueryValue Optional<String> list) {
String downloadErrorMessage = "An error occurred while generating the download file. Contact the development team at bidevteam@cornell.edu.";
try {
FileType extension = Enum.valueOf(FileType.class, fileExtension);
DownloadFile germplasmListFile = germplasmService.exportGermplasm(programId, extension);
DownloadFile germplasmListFile = list.isEmpty() ? germplasmService.exportGermplasm(programId, extension) : germplasmService.exportGermplasmList(programId, list.get(), extension);
HttpResponse<StreamedFile> germplasmExport = HttpResponse.ok(germplasmListFile.getStreamedFile()).header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename="+germplasmListFile.getFileName()+extension.getExtension());
return germplasmExport;
}
Expand Down
64 changes: 37 additions & 27 deletions src/main/java/org/breedinginsight/brapi/v2/BrAPIListController.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
/*
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.breedinginsight.brapi.v2;

import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.rules.SecurityRule;
import io.reactivex.rxjava3.core.Single;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.brapi.client.v2.model.exceptions.ApiException;
import org.brapi.v2.model.core.BrAPIListSummary;
import org.brapi.v2.model.core.BrAPIListTypes;
Expand All @@ -41,31 +27,35 @@
import org.breedinginsight.services.exceptions.DoesNotExistException;
import org.breedinginsight.utilities.response.ResponseUtils;
import org.breedinginsight.utilities.response.mappers.ListQueryMapper;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuples;

import javax.inject.Inject;
import javax.validation.Valid;
import javax.validation.Validator;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

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

private final ProgramService programService;
private final BrAPIListService listService;
private final BrAPIListService brapiListService;
private final ListQueryMapper listQueryMapper;
private final Validator validator;

@Inject
public BrAPIListController(ProgramService programService, BrAPIListService listService,
ListQueryMapper listQueryMapper) {
public BrAPIListController(ProgramService programService, BrAPIListService brapiListService,
ListQueryMapper listQueryMapper, Validator validator) {
this.programService = programService;
this.listService = listService;
this.brapiListService = brapiListService;
this.listQueryMapper = listQueryMapper;
this.validator = validator;
}

//@Get(BrapiVersion.BRAPI_V2 + "/lists")
@Get("/${micronaut.bi.api.version}/programs/{programId}" + BrapiVersion.BRAPI_V2 + "/lists{?queryParams*}")
@Get("/lists{?queryParams*}")
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.PROGRAM_SCOPED_ROLES})
public HttpResponse<Response<DataResponse<Object>>> getLists(
Expand All @@ -77,7 +67,6 @@ public HttpResponse<Response<DataResponse<Object>>> getLists(
.getById(programId)
.orElseThrow(() -> new DoesNotExistException("Program does not exist"));

// get germplasm lists by default
BrAPIListTypes type = BrAPIListTypes.fromValue(queryParams.getListType());
String source = null;
String id = null;
Expand All @@ -93,7 +82,7 @@ public HttpResponse<Response<DataResponse<Object>>> getLists(
if (dateFormatParam != null) {
listQueryMapper.setDateDisplayFormat(dateFormatParam);
}
List<BrAPIListSummary> brapiLists = listService.getListSummariesByTypeAndXref(type, source, id, program);
List<BrAPIListSummary> brapiLists = brapiListService.getListSummariesByTypeAndXref(type, source, id, program);
SearchRequest searchRequest = queryParams.constructSearchRequest();

return ResponseUtils.getBrapiQueryResponse(brapiLists, listQueryMapper, queryParams, searchRequest);
Expand All @@ -105,4 +94,25 @@ public HttpResponse<Response<DataResponse<Object>>> getLists(
throw new RuntimeException(e);
}
}

@Delete("/lists/{listDbId}")
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.PROGRAM_SCOPED_ROLES})
public HttpResponse deleteListById(
@PathVariable("programId") UUID programId,
@PathVariable("listDbId") String listDbId,
HttpRequest<Void> request
) {
boolean hardDelete = false;
if (request.getParameters().contains("hardDelete")) {
String paramValue = request.getParameters().get("hardDelete");
hardDelete = "true".equals(paramValue);
}
try {
return brapiListService.deleteBrAPIList(listDbId, programId, hardDelete);
} catch (Exception e) {
log.info(e.getMessage(), e);
return HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, "Error deleting germplasm list");
}
}
}
41 changes: 37 additions & 4 deletions src/main/java/org/breedinginsight/brapi/v2/dao/BrAPIListDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

package org.breedinginsight.brapi.v2.dao;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.exceptions.HttpStatusException;
import lombok.extern.slf4j.Slf4j;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.brapi.client.v2.ApiResponse;
import org.brapi.client.v2.model.exceptions.ApiException;
import org.brapi.client.v2.model.queryParams.core.ListQueryParams;
import org.brapi.client.v2.modules.core.ListsApi;
import org.brapi.v2.model.BrAPIExternalReference;
import org.brapi.v2.model.BrAPIResponse;
Expand All @@ -29,21 +34,31 @@
import org.brapi.v2.model.core.BrAPIListTypes;
import org.brapi.v2.model.core.request.BrAPIListNewRequest;
import org.brapi.v2.model.core.request.BrAPIListSearchRequest;
import org.brapi.v2.model.core.response.*;
import org.brapi.v2.model.pheno.BrAPIObservation;
import org.brapi.v2.model.core.response.BrAPIListDetails;
import org.brapi.v2.model.core.response.BrAPIListsListResponse;
import org.brapi.v2.model.core.response.BrAPIListsListResponseResult;
import org.brapi.v2.model.core.response.BrAPIListsSingleResponse;
import org.breedinginsight.api.model.v1.response.DataResponse;
import org.breedinginsight.api.model.v1.response.Response;
import org.breedinginsight.brapi.v1.controller.BrapiVersion;
import org.breedinginsight.brapps.importer.daos.ImportDAO;
import org.breedinginsight.brapps.importer.model.ImportUpload;
import org.breedinginsight.daos.ProgramDAO;
import org.breedinginsight.model.ProgramBrAPIEndpoints;
import org.breedinginsight.services.ProgramService;
import org.breedinginsight.services.brapi.BrAPIEndpointProvider;
import org.breedinginsight.services.exceptions.DoesNotExistException;
import org.breedinginsight.utilities.BrAPIDAOUtil;
import org.breedinginsight.utilities.Utilities;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
public class BrAPIListDAO {
Expand All @@ -54,7 +69,10 @@ public class BrAPIListDAO {
private final BrAPIEndpointProvider brAPIEndpointProvider;

@Inject
public BrAPIListDAO(ProgramDAO programDAO, ImportDAO importDAO, BrAPIDAOUtil brAPIDAOUtil, BrAPIEndpointProvider brAPIEndpointProvider) {
public BrAPIListDAO(ProgramDAO programDAO,
ImportDAO importDAO,
BrAPIDAOUtil brAPIDAOUtil,
BrAPIEndpointProvider brAPIEndpointProvider) {
this.programDAO = programDAO;
this.importDAO = importDAO;
this.brAPIDAOUtil = brAPIDAOUtil;
Expand Down Expand Up @@ -196,4 +214,19 @@ public List<BrAPIListSummary> createBrAPILists(List<BrAPIListNewRequest> brapiLi

throw new ApiException("No response after creating list");
}

public HttpResponse<String> deleteBrAPIList(String listDbId, UUID programId, boolean hardDelete) throws ApiException {
// TODO: Switch to using the ListsApi from the BrAPI client library once the delete endpoints from BI-2397 are merged.
var programBrAPIBaseUrl = brAPIDAOUtil.getProgramBrAPIBaseUrl(programId);
var requestUrl = HttpUrl.parse(programBrAPIBaseUrl + "/lists/" + listDbId).newBuilder();
requestUrl.addQueryParameter("hardDelete", Boolean.toString(hardDelete));
HttpUrl url = requestUrl.build();
var brapiRequest = new Request.Builder().url(url)
.method("DELETE", null)
.addHeader("Content-Type", "application/json")
.build();

return brAPIDAOUtil.makeCall(brapiRequest);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class GermplasmQuery extends BrapiQuery {
// This is a meta-parameter, it describes the display format of any date fields.
private String dateDisplayFormat;

// The list id used to get a collection of germplasm
private String listDbId;

public SearchRequest constructSearchRequest() {
List<FilterRequest> filters = new ArrayList<>();
if (!StringUtils.isBlank(getImportEntryNumber())) {
Expand Down
Loading