diff --git a/src/main/java/org/breedinginsight/brapi/v2/GermplasmController.java b/src/main/java/org/breedinginsight/brapi/v2/GermplasmController.java index bbf17db26..568e58d99 100644 --- a/src/main/java/org/breedinginsight/brapi/v2/GermplasmController.java +++ b/src/main/java/org/breedinginsight/brapi/v2/GermplasmController.java @@ -157,6 +157,25 @@ public HttpResponse germplasmListExport( } } + @Get("/programs/{programId}/germplasm/export{?fileExtension}") + @Produces(value = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") + @ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.ALL}) + public HttpResponse germplasmExport( + @PathVariable("programId") UUID programId, @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.exportGermplasm(programId, extension); + HttpResponse germplasmExport = HttpResponse.ok(germplasmListFile.getStreamedFile()).header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename="+germplasmListFile.getFileName()+extension.getExtension()); + return germplasmExport; + } + catch (Exception e) { + log.error(e.getMessage(), e); + HttpResponse response = HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, downloadErrorMessage).contentType(MediaType.TEXT_PLAIN).body(downloadErrorMessage); + return response; + } + } + @Get("/programs/{programId}" + BrapiVersion.BRAPI_V2 + "/germplasm/{germplasmId}") @Produces(MediaType.APPLICATION_JSON) @ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.ALL}) diff --git a/src/main/java/org/breedinginsight/brapi/v2/services/BrAPIGermplasmService.java b/src/main/java/org/breedinginsight/brapi/v2/services/BrAPIGermplasmService.java index 719b76b5e..cfd44f64b 100644 --- a/src/main/java/org/breedinginsight/brapi/v2/services/BrAPIGermplasmService.java +++ b/src/main/java/org/breedinginsight/brapi/v2/services/BrAPIGermplasmService.java @@ -1,25 +1,21 @@ package org.breedinginsight.brapi.v2.services; import io.micronaut.context.annotation.Property; -import io.micronaut.http.HttpRequest; import io.micronaut.http.server.exceptions.InternalServerException; import io.micronaut.http.server.types.files.StreamedFile; import org.brapi.client.v2.model.exceptions.ApiException; import org.brapi.v2.model.BrAPIExternalReference; import lombok.extern.slf4j.Slf4j; -import org.brapi.v2.model.core.BrAPIListSummary; -import org.brapi.v2.model.core.BrAPIListTypes; import org.brapi.v2.model.core.request.BrAPIListNewRequest; import org.brapi.v2.model.core.response.BrAPIListDetails; import org.brapi.v2.model.core.response.BrAPIListsSingleResponse; import org.brapi.v2.model.germ.BrAPIGermplasm; +import org.brapi.v2.model.germ.BrAPIGermplasmSynonyms; import org.breedinginsight.brapi.v2.constants.BrAPIAdditionalInfoFields; import org.breedinginsight.brapps.importer.daos.BrAPIListDAO; import org.breedinginsight.brapps.importer.model.exports.FileType; -import org.breedinginsight.brapps.importer.services.ExternalReferenceSource; import org.breedinginsight.model.Column; import org.breedinginsight.model.DownloadFile; -import org.breedinginsight.model.Pedigree; import org.breedinginsight.model.Program; import org.breedinginsight.services.ProgramService; import org.breedinginsight.services.exceptions.DoesNotExistException; @@ -28,7 +24,6 @@ import org.breedinginsight.services.writers.ExcelWriter; import org.breedinginsight.brapi.v2.dao.BrAPIGermplasmDAO; import org.breedinginsight.brapps.importer.model.ImportUpload; -import org.breedinginsight.utilities.Utilities; import javax.inject.Inject; import javax.inject.Singleton; @@ -77,39 +72,6 @@ public Optional getGermplasmByDBID(UUID programId, String germpl return germplasmDAO.getGermplasmByDBID(germplasmId, programId); } - public List getGermplasmListsByProgramId(UUID programId, HttpRequest request) throws DoesNotExistException, ApiException { - - if (!programService.exists(programId)) { - throw new DoesNotExistException("Program does not exist"); - } - - Optional optionalProgram = programService.getById(programId); - if(optionalProgram.isPresent()) { - Program program = optionalProgram.get(); - - List germplasmLists = brAPIListDAO.getListByTypeAndExternalRef(BrAPIListTypes.GERMPLASM, programId, Utilities.generateReferenceSource(referenceSource, ExternalReferenceSource.PROGRAMS), programId); - - for (BrAPIListSummary germplasmList: germplasmLists) { - String listName = germplasmList.getListName(); - String newListName = removeAppendedKey(listName, program.getKey()); - germplasmList.setListName(newListName); - - //Retrieve germplasm details to get list owner name - //Due to listOwnerName not being stored in breedbase - BrAPIListDetails listData = brAPIListDAO.getListById(germplasmList.getListDbId(), programId).getResult(); - List germplasmNames = listData.getData().subList(0,1); - List germplasm = germplasmDAO.getGermplasmByRawName(germplasmNames, programId); - String createdBy = germplasm.get(0).getAdditionalInfo().getAsJsonObject("createdBy").get("userName").getAsString(); - germplasmList.setListOwnerName(createdBy); - } - - return germplasmLists; - } - else { - throw new DoesNotExistException("Program does not exist"); - } - } - public List> processListData(List germplasm, UUID germplasmListId){ List> processedData = new ArrayList<>(); @@ -164,7 +126,7 @@ public List> processListData(List germplasm, // Synonyms if (germplasmEntry.getSynonyms() != null && !germplasmEntry.getSynonyms().isEmpty()) { String joinedSynonyms = germplasmEntry.getSynonyms().stream() - .map(synonym -> synonym.getSynonym()) + .map(BrAPIGermplasmSynonyms::getSynonym) .collect(Collectors.joining(";")); row.put("Synonyms", joinedSynonyms); } @@ -191,6 +153,38 @@ public List getGermplasmByList(UUID programId, String listDbId) return germplasm; } else throw new ApiException(); } + public DownloadFile exportGermplasm(UUID programId, FileType fileExtension) throws IllegalArgumentException, ApiException, IOException { + List columns = GermplasmFileColumns.getOrderedColumns(); + + //Retrieve germplasm list data + List germplasm = germplasmDAO.getGermplasm(programId); + germplasm.sort(Comparator.comparingInt(germ -> Integer.parseInt(germ.getAccessionNumber()))); + + // make file Name + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd:hh-mm-ssZ"); + String timestamp = formatter.format(OffsetDateTime.now()); + StringBuilder fileNameSB = new StringBuilder(); + Optional optionalProgram = programService.getById(programId); + if (optionalProgram.isPresent()) { + Program program = optionalProgram.get(); + fileNameSB.append( program.getName() ); + fileNameSB.append("_"); + } + String fileName = fileNameSB.append("germplasm").append("_").append(timestamp).toString(); + + StreamedFile downloadFile; + //Convert list data to List> data to pass into file writer + List> processedData = processListData(germplasm, new UUID(0,0)); + + if (fileExtension == FileType.CSV){ + downloadFile = CSVWriter.writeToDownload(columns, processedData, fileExtension); + } else { + downloadFile = ExcelWriter.writeToDownload("Data", columns, processedData, fileExtension); + } + + return new DownloadFile(fileName, downloadFile); + } + public DownloadFile exportGermplasmList(UUID programId, String listId, FileType fileExtension) throws IllegalArgumentException, ApiException, IOException { List columns = GermplasmFileColumns.getOrderedColumns(); @@ -219,7 +213,7 @@ public DownloadFile exportGermplasmList(UUID programId, String listId, FileType if (fileExtension == FileType.CSV){ downloadFile = CSVWriter.writeToDownload(columns, processedData, fileExtension); } else { - downloadFile = ExcelWriter.writeToDownload("Germplasm Import", columns, processedData, fileExtension); + downloadFile = ExcelWriter.writeToDownload("Data", columns, processedData, fileExtension); } return new DownloadFile(fileName, downloadFile); @@ -229,7 +223,7 @@ public UUID getGermplasmListId(BrAPIListDetails listData) { if(Objects.nonNull(listData.getExternalReferences()) && hasListExternalReference(listData.getExternalReferences())) { return UUID.fromString(listData.getExternalReferences().stream() .filter(e -> referenceSource.concat("/lists").equals(e.getReferenceSource())) - .map(e -> e.getReferenceID()).findAny().orElse("00000000-0000-0000-000000000000")); + .map(BrAPIExternalReference::getReferenceID).findAny().orElse("00000000-0000-0000-000000000000")); } else { return new UUID(0,0); } @@ -239,7 +233,7 @@ public UUID getGermplasmListId(BrAPIListNewRequest importList) { if(Objects.nonNull(importList.getExternalReferences()) && hasListExternalReference(importList.getExternalReferences())) { return UUID.fromString(importList.getExternalReferences().stream() .filter(e -> referenceSource.concat("/lists").equals(e.getReferenceSource())) - .map(e -> e.getReferenceID()).findAny().orElse("00000000-0000-0000-000000000000")); + .map(BrAPIExternalReference::getReferenceID).findAny().orElse("00000000-0000-0000-000000000000")); } else { return new UUID(0,0); } @@ -306,11 +300,6 @@ public List updateBrAPIGermplasm(List putBrAPIGe return germplasmDAO.updateBrAPIGermplasm(putBrAPIGermplasmList, programId, upload); } - public List importBrAPIGermplasm(List postBrAPIGermplasmList, List putBrAPIGermplasmList, - UUID programId, ImportUpload upload) throws ApiException { - return germplasmDAO.createBrAPIGermplasm(postBrAPIGermplasmList, programId, upload); - } - public List getRawGermplasmByAccessionNumber(ArrayList germplasmAccessionNumbers, UUID programId) throws ApiException { List germplasmList = germplasmDAO.getRawGermplasm(programId); List resultGermplasm = new ArrayList<>();