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 @@ -221,6 +221,25 @@ public HttpResponse<Response<DataResponse<List<BrAPIGermplasm>>>> getGermplasm(
return HttpResponse.status(HttpStatus.UNPROCESSABLE_ENTITY, "Error parsing requested date format");
}
}
@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,list}")
@Produces(value = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,22 @@
import org.breedinginsight.model.Program;
import org.breedinginsight.model.Species;
import org.breedinginsight.services.SpeciesService;
import org.breedinginsight.utilities.FileUtil;
import org.breedinginsight.utilities.response.mappers.GermplasmQueryMapper;
import org.jooq.DSLContext;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import tech.tablesaw.api.Table;

import javax.inject.Inject;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static io.micronaut.http.HttpRequest.GET;
import static io.micronaut.http.HttpRequest.POST;
Expand Down Expand Up @@ -237,7 +243,38 @@ public void getAllGermplasmListsSuccess() {
}
}
}
@ParameterizedTest
@CsvSource(value = {"CSV", "XLSX", "XLS"})
@SneakyThrows
public void germplasmListExport(String extension) {
String programId = validProgram.getId().toString();
String germplasmListDbId = fetchGermplasmListDbId(programId);

// Build the endpoint to get germplasm by germplasm list.
String endpoint = String.format("/programs/%s/germplasm/lists/%s/export?fileExtension=%s", programId, germplasmListDbId, extension);

// Get germplasm by list.
Flowable<HttpResponse<byte[]>> call = client.exchange(
GET(endpoint).cookie(new NettyCookie("phylo-token", "test-registered-user")), byte[].class
);

HttpResponse<byte[]> response = call.blockingFirst();

assertEquals(HttpStatus.OK, response.getStatus());


ByteArrayInputStream bodyStream = new ByteArrayInputStream(Objects.requireNonNull(response.body()));

Table download = Table.create();
if (extension.equals("CSV")) {
download = FileUtil.parseTableFromCsv(bodyStream);
}
if (extension.equals("XLS") || extension.equals("XLSX")) {
download = FileUtil.parseTableFromExcel(bodyStream, 0);
}
int dataSize = download.rowCount();
assertEquals(3, dataSize, "Wrong number of germplasm were returned");
}
@Test
@SneakyThrows
public void getAllGermplasmByListSuccess() {
Expand Down
Loading