Commit e1cf32bc authored by Jeremy Destin's avatar Jeremy Destin Committed by Célia Michotey
Browse files

Feat: Add web service for mcpd export.

parent 1c8d8a7f
......@@ -126,6 +126,27 @@ public class GnpISGermplasmController {
return null;
}
@PostMapping(value = "/germplasm-mcpd-csv", produces = "text/csv", consumes = APPLICATION_JSON_VALUE)
public FileSystemResource exportMcpd(@RequestBody @Valid FaidareGermplasmPOSTShearchCriteria criteria, HttpServletResponse response) {
long limitResult = 50000L;
long nbResult = germplasmService.germplasmFind(criteria).getMetadata().getPagination().getTotalCount();
if (!(nbResult > limitResult)) {
try {
File exportFile = germplasmService.exportGermplasmMcpd(criteria);
response.setHeader("Content-Disposition", "attachment; filename=germplasm.gnpis.csv");
return new FileSystemResource(exportFile);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("An error occurred when exporting germplasm: " + e.getMessage() + ".", e);
}
}
return null;
}
@ApiOperation("Search list of germplasm")
@PostMapping(value = "/search", consumes = APPLICATION_JSON_VALUE)
public GermplasmSearchResponse germplasmSearch(@RequestBody @Valid FaidareGermplasmPOSTShearchCriteria criteria) {
......
package fr.inra.urgi.faidare.elasticsearch.repository.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import fr.inra.urgi.faidare.elasticsearch.ESRequestFactory;
import fr.inra.urgi.faidare.elasticsearch.ESResponseParser;
import fr.inra.urgi.faidare.elasticsearch.document.DocumentAnnotationUtil;
......@@ -65,6 +67,18 @@ public class ESGenericGetByIdRepository<VO> implements ESGetByIdRepository<VO> {
// Parse result list
List<? extends VO> resultList = parser.parseHits(result, voClass);
System.out.println("");
System.out.println("BEGIN");
System.out.println("");
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(result);
System.out.println(json);
System.out.println("END");
System.out.println("");
if (resultList != null && !resultList.isEmpty()) {
if (resultList.size() > 1) {
// Should never happen
......
......@@ -2,13 +2,13 @@ package fr.inra.urgi.faidare.repository.es;
import fr.inra.urgi.faidare.domain.criteria.FaidareGermplasmPOSTShearchCriteria;
import fr.inra.urgi.faidare.domain.criteria.GermplasmSearchCriteria;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmMcpdVO;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmVO;
import fr.inra.urgi.faidare.domain.data.germplasm.PedigreeVO;
import fr.inra.urgi.faidare.domain.data.germplasm.ProgenyVO;
import fr.inra.urgi.faidare.domain.datadiscovery.response.GermplasmSearchResponse;
import fr.inra.urgi.faidare.domain.response.PaginatedList;
import fr.inra.urgi.faidare.elasticsearch.repository.ESFindRepository;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmMcpdVO;
import java.util.Iterator;
......@@ -41,6 +41,11 @@ public interface GermplasmRepository
*/
Iterator<GermplasmVO> scrollAllGermplasm(FaidareGermplasmPOSTShearchCriteria criteria);
/**
* Scroll through all germplasmMcpd matching the given FAIDARE search criteria.
*/
Iterator<GermplasmMcpdVO> scrollAllGermplasmMcpd(FaidareGermplasmPOSTShearchCriteria criteria);
/**
* Find pedigree for germplasm by id.
*/
......
......@@ -3,6 +3,7 @@ package fr.inra.urgi.faidare.repository.es;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.inra.urgi.faidare.domain.criteria.FaidareGermplasmPOSTShearchCriteria;
import fr.inra.urgi.faidare.domain.criteria.GermplasmSearchCriteria;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmMcpdVO;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmVO;
import fr.inra.urgi.faidare.domain.data.germplasm.PedigreeVO;
import fr.inra.urgi.faidare.domain.data.germplasm.ProgenyVO;
......@@ -38,8 +39,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmMcpdVO;
import java.io.IOException;
import java.util.ArrayList;
......@@ -105,6 +104,13 @@ public class GermplasmRepositoryImpl implements GermplasmRepository {
return new ESScrollIterator<>(client, requestFactory, parser, GermplasmVO.class, query, fetchSize);
}
@Override
public Iterator<GermplasmMcpdVO> scrollAllGermplasmMcpd(FaidareGermplasmPOSTShearchCriteria criteria) {
QueryBuilder query = queryFactory.createShouldFilterQuery(criteria);
int fetchSize = criteria.getPageSize().intValue();
return new ESScrollIterator<>(client, requestFactory, parser, GermplasmMcpdVO.class, query, fetchSize);
}
@Override
public GermplasmVO getById(String germplasmDbId) {
return getByIdRepository.getById(germplasmDbId);
......
......@@ -2,12 +2,12 @@ package fr.inra.urgi.faidare.service.es;
import fr.inra.urgi.faidare.domain.criteria.FaidareGermplasmPOSTShearchCriteria;
import fr.inra.urgi.faidare.domain.criteria.GermplasmSearchCriteria;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmMcpdVO;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmVO;
import fr.inra.urgi.faidare.domain.data.germplasm.PedigreeVO;
import fr.inra.urgi.faidare.domain.data.germplasm.ProgenyVO;
import fr.inra.urgi.faidare.domain.datadiscovery.response.GermplasmSearchResponse;
import fr.inra.urgi.faidare.domain.response.PaginatedList;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmMcpdVO;
import java.io.File;
......@@ -34,5 +34,7 @@ public interface GermplasmService {
GermplasmMcpdVO getMcpdById(String germplasmDbId);
File exportGermplasmMcpd(FaidareGermplasmPOSTShearchCriteria criteria);
}
......@@ -4,18 +4,13 @@ import com.opencsv.CSVWriter;
import fr.inra.urgi.faidare.api.faidare.v1.GnpISGermplasmController;
import fr.inra.urgi.faidare.domain.criteria.FaidareGermplasmPOSTShearchCriteria;
import fr.inra.urgi.faidare.domain.criteria.GermplasmSearchCriteria;
import fr.inra.urgi.faidare.domain.data.germplasm.CollPopVO;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmVO;
import fr.inra.urgi.faidare.domain.data.germplasm.PedigreeVO;
import fr.inra.urgi.faidare.domain.data.germplasm.ProgenyVO;
import fr.inra.urgi.faidare.domain.data.germplasm.*;
import fr.inra.urgi.faidare.domain.datadiscovery.response.GermplasmSearchResponse;
import fr.inra.urgi.faidare.domain.response.PaginatedList;
import fr.inra.urgi.faidare.repository.es.GermplasmRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmMcpdVO;
import javax.annotation.Resource;
import java.io.*;
......@@ -68,6 +63,19 @@ public class GermplasmServiceImpl implements GermplasmService {
return germplasmRepository.getMcpdById(germplasmDbId);
}
@Override
public File exportGermplasmMcpd(FaidareGermplasmPOSTShearchCriteria criteria) {
try {
Iterator<GermplasmMcpdVO> allGermplasm = germplasmRepository.scrollAllGermplasmMcpd(criteria);
Path tmpDirPath = Files.createTempDirectory("germplasm");
Path tmpFile = Files.createTempFile(tmpDirPath, "germplasm_", ".csv");
writeMcpdToCSV(tmpFile, allGermplasm);
return tmpFile.toFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void writeToCSV(Path tmpFile, Iterator<GermplasmVO> germplasms) throws IOException {
Writer fileStream = new OutputStreamWriter(new FileOutputStream(tmpFile.toFile()), StandardCharsets.UTF_8);
......@@ -107,6 +115,41 @@ public class GermplasmServiceImpl implements GermplasmService {
csvWriter.close();
}
private void writeMcpdToCSV(Path tmpFile, Iterator<GermplasmMcpdVO> germplasms) throws IOException {
Writer fileStream = new OutputStreamWriter(new FileOutputStream(tmpFile.toFile()), StandardCharsets.UTF_8);
CSVWriter csvWriter = new CSVWriter(fileStream, ';', '"', '\\', "\n");
String[] header = new String[]{
"PUI", "AccessionNumber", "AccessionNames", "TaxonGroup", "geneticNature", "HoldingInstitution",
"mls"
};
csvWriter.writeNext(header);
while (germplasms.hasNext()) {
List<String> collectionNames = new ArrayList<>();
List<String> panelNames = new ArrayList<>();
GermplasmMcpdVO germplasmMcpdVO = germplasms.next();
String[] line = new String[header.length];
line[0] = germplasmMcpdVO.getGermplasmPUI();
line[1] = germplasmMcpdVO.getAccessionNumber();
line[2] = String.join(", ", germplasmMcpdVO.getAccessionNames());
line[3] = germplasmMcpdVO.getCommonCropName();
line[4] = germplasmMcpdVO.getGeneticNature();
line[5] = germplasmMcpdVO.getHoldingInstitute().getInstituteName();
line[5] = germplasmMcpdVO.getMlsStatus();
csvWriter.writeNext(line);
}
csvWriter.close();
}
@Override
public GermplasmVO getById(String germplasmDbId) {
return germplasmRepository.getById(germplasmDbId);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment