Commit 5eca149a authored by Jean-Baptiste Nizet's avatar Jean-Baptiste Nizet
Browse files

feat: export plant material in addition to mcpd

parent 95bb54d6
......@@ -58,6 +58,11 @@ public interface GermplasmRepository
*/
Iterator<GermplasmMcpdVO> scrollGermplasmMcpdsByIds(Set<String> ids, int fetchSize);
/**
* Scroll through all germplasm having one of the given IDs.
*/
Iterator<GermplasmVO> scrollGermplasmsByIds(Set<String> ids, int fetchSize);
/**
* Find pedigree for germplasm by id.
*/
......
......@@ -125,6 +125,13 @@ public class GermplasmRepositoryImpl implements GermplasmRepository {
return new ESScrollIterator<>(client, requestFactory, parser, GermplasmMcpdVO.class, query, fetchSize);
}
@Override
public Iterator<GermplasmVO> scrollGermplasmsByIds(Set<String> ids,
int fetchSize) {
QueryBuilder query = QueryBuilders.termsQuery("germplasmDbId", ids);
return new ESScrollIterator<>(client, requestFactory, parser, GermplasmVO.class, query, fetchSize);
}
@Override
public GermplasmVO getById(String germplasmDbId) {
return getByIdRepository.getById(germplasmDbId);
......
......@@ -49,17 +49,20 @@ public class GermplasmController {
private final FaidareProperties faidareProperties;
private final XRefDocumentRepository xRefDocumentRepository;
private final GermplasmAttributeRepository germplasmAttributeRepository;
private final GermplasmMcpdExportService germplasmMcpdExportService;
private final GermplasmExportService germplasmExportService;
public GermplasmController(GermplasmRepository germplasmRepository,
FaidareProperties faidareProperties,
XRefDocumentRepository xRefDocumentRepository,
GermplasmAttributeRepository germplasmAttributeRepository,
GermplasmMcpdExportService germplasmMcpdExportService,
GermplasmExportService germplasmExportService) {
this.germplasmRepository = germplasmRepository;
this.faidareProperties = faidareProperties;
this.xRefDocumentRepository = xRefDocumentRepository;
this.germplasmAttributeRepository = germplasmAttributeRepository;
this.germplasmMcpdExportService = germplasmMcpdExportService;
this.germplasmExportService = germplasmExportService;
}
......@@ -86,13 +89,25 @@ public class GermplasmController {
return toModelAndView(germplasms.get(0));
}
@PostMapping("/exports")
@PostMapping("/exports/mcpd")
@ResponseBody
public ResponseEntity<StreamingResponseBody> export(@Validated @RequestBody GermplasmMcpdExportCommand command) {
List<GermplasmMcpdExportableField> fields = getFieldsToExport(command);
StreamingResponseBody body = out -> {
Iterator<GermplasmMcpdVO> iterator = germplasmRepository.scrollGermplasmMcpdsByIds(command.getIds(), 1000);
germplasmMcpdExportService.export(out, iterator, fields);
};
return ResponseEntity.ok().contentType(MediaType.parseMediaType("text/csv")).body(body);
}
@PostMapping("/exports/plant-material")
@ResponseBody
public ResponseEntity<StreamingResponseBody> export(@Validated @RequestBody GermplasmExportCommand command) {
List<GermplasmExportableField> fields = getFieldsToExport(command);
StreamingResponseBody body = out -> {
Iterator<GermplasmMcpdVO> iterator = germplasmRepository.scrollGermplasmMcpdsByIds(command.getIds(), 1000);
Iterator<GermplasmVO> iterator = germplasmRepository.scrollGermplasmsByIds(command.getIds(), 1000);
germplasmExportService.export(out, iterator, fields);
};
return ResponseEntity.ok().contentType(MediaType.parseMediaType("text/csv")).body(body);
......@@ -192,7 +207,17 @@ public class GermplasmController {
return germplasmRepository.findPedigree(germplasm.getGermplasmDbId());
}
private List<GermplasmExportableField> getFieldsToExport(GermplasmExportCommand command) {
private List<GermplasmMcpdExportableField> getFieldsToExport(
GermplasmMcpdExportCommand command) {
List<GermplasmMcpdExportableField> fields = command.getFields();
if (fields.isEmpty()) {
fields = Arrays.asList(GermplasmMcpdExportableField.values());
}
return fields;
}
private List<GermplasmExportableField> getFieldsToExport(
GermplasmExportCommand command) {
List<GermplasmExportableField> fields = command.getFields();
if (fields.isEmpty()) {
fields = Arrays.asList(GermplasmExportableField.values());
......
......@@ -17,9 +17,9 @@ import java.util.function.Function;
import java.util.stream.Collectors;
import com.opencsv.CSVWriter;
import fr.inra.urgi.faidare.domain.data.germplasm.DonorInfoVO;
import fr.inra.urgi.faidare.domain.data.germplasm.CollPopVO;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmMcpdVO;
import fr.inra.urgi.faidare.domain.data.germplasm.InstituteVO;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmVO;
import org.springframework.stereotype.Component;
/**
......@@ -34,85 +34,19 @@ public class GermplasmExportService {
public GermplasmExportService() {
Map<GermplasmExportableField, GermplasmExportableFieldDescriptor> map = new HashMap<>();
map.put(PUID, withFieldAsHeader(PUID, vo -> vo.getGermplasmPUI()));
map.put(INSTCODE, withFieldAsHeader(INSTCODE, vo -> vo.getInstituteCode()));
map.put(ACCENUMB, withFieldAsHeader(ACCENUMB, vo -> vo.getAccessionNumber()));
map.put(COLLNUMB, withFieldAsHeader(COLLNUMB, vo -> vo.getCollectingInfo().getCollectingNumber()));
map.put(COLLCODE, withFieldAsHeader(COLLCODE, vo ->
vo.getCollectingInfo()
.getCollectingInstitutes()
.stream()
.map(InstituteVO::getInstituteCode).collect(Collectors.joining(";"))));
map.put(COLLNAME, withFieldAsHeader(COLLNAME, vo ->
vo.getCollectingInfo()
.getCollectingInstitutes()
.stream()
.map(InstituteVO::getInstituteName)
.collect(Collectors.joining(";"))));
map.put(COLLINSTADDRESS, withFieldAsHeader(COLLINSTADDRESS, vo ->
vo.getCollectingInfo()
.getCollectingInstitutes()
.stream()
.map(InstituteVO::getAddress)
.collect(Collectors.joining(";"))));
map.put(COLLMISSID, withFieldAsHeader(COLLMISSID, vo -> vo.getCollectingInfo().getCollectingMissionIdentifier()));
map.put(GENUS, withFieldAsHeader(GENUS, vo -> vo.getGenus()));
map.put(SPECIES, withFieldAsHeader(SPECIES, vo -> vo.getSpecies()));
map.put(SPAUTHOR, withFieldAsHeader(SPAUTHOR, vo -> vo.getSpeciesAuthority()));
map.put(SUBTAXA, withFieldAsHeader(SUBTAXA, vo -> vo.getSubtaxon()));
map.put(SUBTAUTHOR, withFieldAsHeader(SUBTAUTHOR, vo -> vo.getSubtaxonAuthority()));
map.put(CROPNAME, withFieldAsHeader(CROPNAME, vo -> vo.getCommonCropName()));
map.put(ACCENAME, withFieldAsHeader(ACCENAME, vo -> String.join(";", vo.getAccessionNames())));
map.put(ACQDATE, withFieldAsHeader(ACQDATE, vo -> vo.getAcquisitionDate()));
map.put(ORIGCTY, withFieldAsHeader(ORIGCTY, vo -> vo.getCountryOfOriginCode()));
map.put(COLLSITE, withFieldAsHeader(COLLSITE, vo -> vo.getCollectingInfo().getCollectingSite().getSiteName()));
map.put(DECLATITUDE, withFieldAsHeader(DECLATITUDE, vo -> vo.getCollectingInfo().getCollectingSite().getLatitudeDecimal()));
map.put(LATITUDE, withFieldAsHeader(LATITUDE, vo -> vo.getCollectingInfo().getCollectingSite().getLatitudeDegrees()));
map.put(DECLONGITUDE, withFieldAsHeader(DECLONGITUDE, vo -> vo.getCollectingInfo().getCollectingSite().getLongitudeDecimal()));
map.put(LONGITUDE, withFieldAsHeader(LONGITUDE, vo -> vo.getCollectingInfo().getCollectingSite().getLongitudeDegrees()));
map.put(COORDUNCERT, withFieldAsHeader(COORDUNCERT, vo -> vo.getCollectingInfo().getCollectingSite().getCoordinateUncertainty()));
map.put(COORDDATUM, withFieldAsHeader(COORDDATUM, vo -> vo.getCollectingInfo().getCollectingSite().getSpatialReferenceSystem()));
map.put(GEOREFMETH, withFieldAsHeader(GEOREFMETH, vo -> vo.getCollectingInfo().getCollectingSite().getGeoreferencingMethod()));
map.put(ELEVATION, withFieldAsHeader(ELEVATION, vo -> vo.getCollectingInfo().getCollectingSite().getElevation()));
map.put(COLLDATE, withFieldAsHeader(COLLDATE, vo -> vo.getCollectingInfo().getCollectingDate()));
map.put(BREDCODE, withFieldAsHeader(BREDCODE, vo ->
vo.getBreedingInstitutes()
.stream()
.map(InstituteVO::getInstituteCode)
.collect(Collectors.joining(";"))));
map.put(BREDNAME, withFieldAsHeader(BREDNAME, vo ->
vo.getBreedingInstitutes()
.stream()
.map(InstituteVO::getInstituteName)
.collect(Collectors.joining(";"))));
map.put(SAMPSTAT, withFieldAsHeader(SAMPSTAT, vo -> vo.getBiologicalStatusOfAccessionCode()));
map.put(ANCEST, withFieldAsHeader(ANCEST, vo -> vo.getAncestralData()));
map.put(COLLSRC, withFieldAsHeader(COLLSRC, vo -> vo.getAcquisitionSourceCode()));
map.put(DONORCODE, withFieldAsHeader(DONORCODE, vo ->
vo.getDonorInfo()
.stream()
.map(donorInfoVO -> donorInfoVO.getDonorInstitute().getInstituteCode())
.collect(Collectors.joining(";"))));
map.put(DONORNAME, withFieldAsHeader(DONORNAME, vo ->
vo.getDonorInfo()
.stream()
.map(donorInfoVO -> donorInfoVO.getDonorInstitute().getInstituteName())
.collect(Collectors.joining(";"))));
map.put(DONORNUMB, withFieldAsHeader(DONORNUMB, vo ->
vo.getDonorInfo()
.stream()
.map(DonorInfoVO::getDonorAccessionNumber)
.collect(Collectors.joining(";"))));
map.put(OTHERNUMB, withFieldAsHeader(OTHERNUMB, vo -> String.join(";", vo.getAlternateIDs())));
map.put(DUPLSITE, withFieldAsHeader(DUPLSITE, vo -> null)); // no value available for DUPLSITE
map.put(DUPLINSTNAME, withFieldAsHeader(DUPLINSTNAME, vo ->
vo.getSafetyDuplicateInstitutes()
.stream()
.map(InstituteVO::getInstituteName)
.collect(Collectors.joining(";"))));
map.put(STORAGE, withFieldAsHeader(STORAGE, vo -> String.join(";", vo.getStorageTypeCodes())));
map.put(MLSSTAT, withFieldAsHeader(MLSSTAT, vo -> vo.getMlsStatus()));
map.put(REMARKS, withFieldAsHeader(REMARKS, vo -> vo.getRemarks()));
map.put(DOI, new GermplasmExportableFieldDescriptor("DOI", vo -> vo.getGermplasmPUI()));
map.put(ACCESSION_NUMBER, new GermplasmExportableFieldDescriptor("Accession number", vo -> vo.getAccessionNumber()));
map.put(ACCESSION_NAME, new GermplasmExportableFieldDescriptor("Accession name", vo -> vo.getGermplasmName()));
map.put(TAXON_GROUP, new GermplasmExportableFieldDescriptor("Taxon group", vo -> vo.getCommonCropName()));
map.put(HOLDING_INSTITUTION, new GermplasmExportableFieldDescriptor("Holding institution", vo -> vo.getInstituteName()));
map.put(LOT_NAME, new GermplasmExportableFieldDescriptor("Lot name", vo -> null));
map.put(LOT_SYNONYM, new GermplasmExportableFieldDescriptor("Lot synonym", vo -> null));
map.put(COLLECTION_NAME, new GermplasmExportableFieldDescriptor("Collection name", vo -> vo.getCollection().stream().map(
CollPopVO::getName).collect(Collectors.joining(", "))));
map.put(COLLECTION_TYPE, new GermplasmExportableFieldDescriptor("Collection type", vo -> null));
map.put(PANEL_NAME, new GermplasmExportableFieldDescriptor("Panel name", vo -> vo.getPanel().stream().map(CollPopVO::getName).collect(
Collectors.joining(", "))));
map.put(PANEL_SIZE, new GermplasmExportableFieldDescriptor("Panel size", vo -> null));
this.descriptors = Collections.unmodifiableMap(map);
if (map.size() != GermplasmExportableField.values().length) {
......@@ -120,7 +54,7 @@ public class GermplasmExportService {
}
}
public void export(OutputStream out, Iterator<GermplasmMcpdVO> germplasms, List<GermplasmExportableField> fields) {
public void export(OutputStream out, Iterator<GermplasmVO> germplasms, List<GermplasmExportableField> fields) {
try {
CSVWriter csvWriter = new CSVWriter(new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)), ';', '"', '\\', "\n");
String[] header = fields.stream()
......@@ -130,7 +64,7 @@ public class GermplasmExportService {
csvWriter.writeNext(header);
while (germplasms.hasNext()) {
GermplasmMcpdVO vo = germplasms.next();
GermplasmVO vo = germplasms.next();
String[] line =
fields.stream()
.map(descriptors::get)
......@@ -144,17 +78,12 @@ public class GermplasmExportService {
}
}
private GermplasmExportableFieldDescriptor withFieldAsHeader(GermplasmExportableField field,
Function<GermplasmMcpdVO, String> exporter) {
return new GermplasmExportableFieldDescriptor(field.name(), exporter);
}
private static class GermplasmExportableFieldDescriptor {
private final String header;
private final Function<GermplasmMcpdVO, String> exporter;
private final Function<GermplasmVO, String> exporter;
public GermplasmExportableFieldDescriptor(String header,
Function<GermplasmMcpdVO, String> exporter) {
Function<GermplasmVO, String> exporter) {
this.header = header;
this.exporter = exporter;
}
......@@ -163,7 +92,7 @@ public class GermplasmExportService {
return this.header;
}
public String export(GermplasmMcpdVO germplasm) {
public String export(GermplasmVO germplasm) {
return this.exporter.apply(germplasm);
}
}
......
package fr.inra.urgi.faidare.web.germplasm;
import com.fasterxml.jackson.annotation.JsonValue;
/**
* The fields of a germplasm that can be exported
*
* @author JB Nizet
*/
public enum GermplasmExportableField {
PUID,
INSTCODE,
ACCENUMB,
COLLNUMB,
COLLCODE,
COLLNAME,
COLLINSTADDRESS,
COLLMISSID,
GENUS,
SPECIES,
SPAUTHOR,
SUBTAXA,
SUBTAUTHOR,
CROPNAME,
ACCENAME,
ACQDATE,
ORIGCTY,
COLLSITE,
DECLATITUDE,
LATITUDE,
DECLONGITUDE,
LONGITUDE,
COORDUNCERT,
COORDDATUM,
GEOREFMETH,
ELEVATION,
COLLDATE,
BREDCODE,
BREDNAME,
SAMPSTAT,
ANCEST,
COLLSRC,
DONORCODE,
DONORNAME,
DONORNUMB,
OTHERNUMB,
DUPLSITE,
DUPLINSTNAME,
STORAGE,
MLSSTAT,
REMARKS
DOI,
ACCESSION_NUMBER,
ACCESSION_NAME,
TAXON_GROUP,
HOLDING_INSTITUTION,
LOT_NAME,
LOT_SYNONYM,
COLLECTION_NAME,
COLLECTION_TYPE,
PANEL_NAME,
PANEL_SIZE
}
package fr.inra.urgi.faidare.web.germplasm;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.validation.constraints.NotEmpty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Command sent to export a list of germplasm MCPD IDs
* @author JB Nizet
*/
public class GermplasmMcpdExportCommand {
@NotEmpty
private final Set<String> ids;
/**
* The ordered list of fields to export. If empty, all fields are exported
*/
private final List<GermplasmMcpdExportableField> fields;
@JsonCreator
public GermplasmMcpdExportCommand(@JsonProperty("ids") Set<String> ids,
@JsonProperty("fields") List<GermplasmMcpdExportableField> fields) {
this.ids = ids;
this.fields = fields == null ? Collections.emptyList() : fields;
}
public Set<String> getIds() {
return ids;
}
public List<GermplasmMcpdExportableField> getFields() {
return fields;
}
}
package fr.inra.urgi.faidare.web.germplasm;
import static fr.inra.urgi.faidare.web.germplasm.GermplasmMcpdExportableField.*;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.opencsv.CSVWriter;
import fr.inra.urgi.faidare.domain.data.germplasm.DonorInfoVO;
import fr.inra.urgi.faidare.domain.data.germplasm.GermplasmMcpdVO;
import fr.inra.urgi.faidare.domain.data.germplasm.InstituteVO;
import org.springframework.stereotype.Component;
/**
* Service allowing to export germplasm MCPDs as CSV
* @author JB Nizet
*/
@Component
public class GermplasmMcpdExportService {
private final Map<GermplasmMcpdExportableField, GermplasmMcpdExportableFieldDescriptor> descriptors;
public GermplasmMcpdExportService() {
Map<GermplasmMcpdExportableField, GermplasmMcpdExportableFieldDescriptor> map = new HashMap<>();
map.put(PUID, withFieldAsHeader(PUID, vo -> vo.getGermplasmPUI()));
map.put(INSTCODE, withFieldAsHeader(INSTCODE, vo -> vo.getInstituteCode()));
map.put(ACCENUMB, withFieldAsHeader(ACCENUMB, vo -> vo.getAccessionNumber()));
map.put(COLLNUMB, withFieldAsHeader(COLLNUMB, vo -> vo.getCollectingInfo().getCollectingNumber()));
map.put(COLLCODE, withFieldAsHeader(COLLCODE, vo ->
vo.getCollectingInfo()
.getCollectingInstitutes()
.stream()
.map(InstituteVO::getInstituteCode).collect(Collectors.joining(";"))));
map.put(COLLNAME, withFieldAsHeader(COLLNAME, vo ->
vo.getCollectingInfo()
.getCollectingInstitutes()
.stream()
.map(InstituteVO::getInstituteName)
.collect(Collectors.joining(";"))));
map.put(COLLINSTADDRESS, withFieldAsHeader(COLLINSTADDRESS, vo ->
vo.getCollectingInfo()
.getCollectingInstitutes()
.stream()
.map(InstituteVO::getAddress)
.collect(Collectors.joining(";"))));
map.put(COLLMISSID, withFieldAsHeader(COLLMISSID, vo -> vo.getCollectingInfo().getCollectingMissionIdentifier()));
map.put(GENUS, withFieldAsHeader(GENUS, vo -> vo.getGenus()));
map.put(SPECIES, withFieldAsHeader(SPECIES, vo -> vo.getSpecies()));
map.put(SPAUTHOR, withFieldAsHeader(SPAUTHOR, vo -> vo.getSpeciesAuthority()));
map.put(SUBTAXA, withFieldAsHeader(SUBTAXA, vo -> vo.getSubtaxon()));
map.put(SUBTAUTHOR, withFieldAsHeader(SUBTAUTHOR, vo -> vo.getSubtaxonAuthority()));
map.put(CROPNAME, withFieldAsHeader(CROPNAME, vo -> vo.getCommonCropName()));
map.put(ACCENAME, withFieldAsHeader(ACCENAME, vo -> String.join(";", vo.getAccessionNames())));
map.put(ACQDATE, withFieldAsHeader(ACQDATE, vo -> vo.getAcquisitionDate()));
map.put(ORIGCTY, withFieldAsHeader(ORIGCTY, vo -> vo.getCountryOfOriginCode()));
map.put(COLLSITE, withFieldAsHeader(COLLSITE, vo -> vo.getCollectingInfo().getCollectingSite().getSiteName()));
map.put(DECLATITUDE, withFieldAsHeader(DECLATITUDE, vo -> vo.getCollectingInfo().getCollectingSite().getLatitudeDecimal()));
map.put(LATITUDE, withFieldAsHeader(LATITUDE, vo -> vo.getCollectingInfo().getCollectingSite().getLatitudeDegrees()));
map.put(DECLONGITUDE, withFieldAsHeader(DECLONGITUDE, vo -> vo.getCollectingInfo().getCollectingSite().getLongitudeDecimal()));
map.put(LONGITUDE, withFieldAsHeader(LONGITUDE, vo -> vo.getCollectingInfo().getCollectingSite().getLongitudeDegrees()));
map.put(COORDUNCERT, withFieldAsHeader(COORDUNCERT, vo -> vo.getCollectingInfo().getCollectingSite().getCoordinateUncertainty()));
map.put(COORDDATUM, withFieldAsHeader(COORDDATUM, vo -> vo.getCollectingInfo().getCollectingSite().getSpatialReferenceSystem()));
map.put(GEOREFMETH, withFieldAsHeader(GEOREFMETH, vo -> vo.getCollectingInfo().getCollectingSite().getGeoreferencingMethod()));
map.put(ELEVATION, withFieldAsHeader(ELEVATION, vo -> vo.getCollectingInfo().getCollectingSite().getElevation()));
map.put(COLLDATE, withFieldAsHeader(COLLDATE, vo -> vo.getCollectingInfo().getCollectingDate()));
map.put(BREDCODE, withFieldAsHeader(BREDCODE, vo ->
vo.getBreedingInstitutes()
.stream()
.map(InstituteVO::getInstituteCode)
.collect(Collectors.joining(";"))));
map.put(BREDNAME, withFieldAsHeader(BREDNAME, vo ->
vo.getBreedingInstitutes()
.stream()
.map(InstituteVO::getInstituteName)
.collect(Collectors.joining(";"))));
map.put(SAMPSTAT, withFieldAsHeader(SAMPSTAT, vo -> vo.getBiologicalStatusOfAccessionCode()));
map.put(ANCEST, withFieldAsHeader(ANCEST, vo -> vo.getAncestralData()));
map.put(COLLSRC, withFieldAsHeader(COLLSRC, vo -> vo.getAcquisitionSourceCode()));
map.put(DONORCODE, withFieldAsHeader(DONORCODE, vo ->
vo.getDonorInfo()
.stream()
.map(donorInfoVO -> donorInfoVO.getDonorInstitute().getInstituteCode())
.collect(Collectors.joining(";"))));
map.put(DONORNAME, withFieldAsHeader(DONORNAME, vo ->
vo.getDonorInfo()
.stream()
.map(donorInfoVO -> donorInfoVO.getDonorInstitute().getInstituteName())
.collect(Collectors.joining(";"))));
map.put(DONORNUMB, withFieldAsHeader(DONORNUMB, vo ->
vo.getDonorInfo()
.stream()
.map(DonorInfoVO::getDonorAccessionNumber)
.collect(Collectors.joining(";"))));
map.put(OTHERNUMB, withFieldAsHeader(OTHERNUMB, vo -> String.join(";", vo.getAlternateIDs())));
map.put(DUPLSITE, withFieldAsHeader(DUPLSITE, vo ->
vo.getSafetyDuplicateInstitutes()
.stream()
.map(InstituteVO::getInstituteCode)
.collect(Collectors.joining(";"))));
map.put(DUPLINSTNAME, withFieldAsHeader(DUPLINSTNAME, vo ->
vo.getSafetyDuplicateInstitutes()
.stream()
.map(InstituteVO::getInstituteName)
.collect(Collectors.joining(";"))));
map.put(STORAGE, withFieldAsHeader(STORAGE, vo -> String.join(";", vo.getStorageTypeCodes())));
map.put(MLSSTAT, withFieldAsHeader(MLSSTAT, vo -> vo.getMlsStatus()));
map.put(REMARKS, withFieldAsHeader(REMARKS, vo -> vo.getRemarks()));
this.descriptors = Collections.unmodifiableMap(map);
if (map.size() != GermplasmMcpdExportableField.values().length) {
throw new IllegalStateException("Missing field descriptor");
}
}
public void export(OutputStream out, Iterator<GermplasmMcpdVO> germplasms, List<GermplasmMcpdExportableField> fields) {
try {
CSVWriter csvWriter = new CSVWriter(new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)), ';', '"', '\\', "\n");
String[] header = fields.stream()
.map(descriptors::get)
.map(GermplasmMcpdExportableFieldDescriptor::getHeader)
.toArray(String[]::new);
csvWriter.writeNext(header);
while (germplasms.hasNext()) {
GermplasmMcpdVO vo = germplasms.next();
String[] line =
fields.stream()
.map(descriptors::get)
.map(descriptor -> descriptor.export(vo))
.toArray(String[]::new);
csvWriter.writeNext(line);
}
csvWriter.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private GermplasmMcpdExportableFieldDescriptor withFieldAsHeader(
GermplasmMcpdExportableField field,
Function<GermplasmMcpdVO, String> exporter) {
return new GermplasmMcpdExportableFieldDescriptor(field.name(), exporter);
}
private static class GermplasmMcpdExportableFieldDescriptor {
private final String header;
private final Function<GermplasmMcpdVO, String> exporter;
public GermplasmMcpdExportableFieldDescriptor(String header,
Function<GermplasmMcpdVO, String> exporter) {
this.header = header;
this.exporter = exporter;
}
public String getHeader() {
return this.header;
}
public String export(GermplasmMcpdVO germplasm) {
return this.exporter.apply(germplasm);
}
}
}
package fr.inra.urgi.faidare.web.germplasm;
import com.fasterxml.jackson.annotation.JsonValue;
/**
* The fields of a germplasm MCPD that can be exported
*
* @author JB Nizet
*/
public enum GermplasmMcpdExportableField {
PUID,
INSTCODE,
ACCENUMB,
COLLNUMB,