Search in sources :

Example 1 with CsvWriter

use of org.molgenis.data.csv.CsvWriter in project molgenis by molgenis.

the class CsvHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(EntityCollection entities, HttpOutputMessage outputMessage) throws IOException {
    OutputStreamWriter out = new OutputStreamWriter(outputMessage.getBody(), getCharset(outputMessage.getHeaders()));
    CsvWriter writer = new CsvWriter(out);
    try {
        writer.writeAttributeNames(entities.getAttributeNames());
        writer.add(entities.stream());
    } finally {
        IOUtils.closeQuietly(writer);
    }
}
Also used : CsvWriter(org.molgenis.data.csv.CsvWriter) OutputStreamWriter(java.io.OutputStreamWriter)

Example 2 with CsvWriter

use of org.molgenis.data.csv.CsvWriter in project molgenis by molgenis.

the class SortaController method download.

@GetMapping("/match/download/{sortaJobExecutionId}")
public void download(@PathVariable String sortaJobExecutionId, HttpServletResponse response) throws IOException {
    try (CsvWriter csvWriter = new CsvWriter(response.getOutputStream(), SortaServiceImpl.DEFAULT_SEPARATOR)) {
        SortaJobExecution sortaJobExecution = findSortaJobExecution(sortaJobExecutionId);
        response.setContentType("text/csv");
        response.addHeader("Content-Disposition", "attachment; filename=" + generateCsvFileName());
        List<String> columnHeaders = new ArrayList<>();
        EntityType targetMetadata = entityTypeFactory.create("SortaDownload" + sortaJobExecutionId);
        EntityType sourceMetaData = dataService.getEntityType(sortaJobExecution.getSourceEntityName());
        for (Attribute attribute : sourceMetaData.getAttributes()) {
            if (!attribute.getName().equalsIgnoreCase(SortaCsvRepository.ALLOWED_IDENTIFIER)) {
                columnHeaders.add(attribute.getName());
                targetMetadata.addAttribute(attrMetaFactory.create().setName(attribute.getName()));
            }
        }
        columnHeaders.addAll(Arrays.asList(OntologyTermMetaData.ONTOLOGY_TERM_NAME, OntologyTermMetaData.ONTOLOGY_TERM_IRI, MatchingTaskContentMetaData.SCORE, MatchingTaskContentMetaData.VALIDATED));
        targetMetadata.addAttribute(ontologyTermMetaData.getAttribute(OntologyTermMetaData.ONTOLOGY_TERM_NAME));
        targetMetadata.addAttribute(ontologyTermMetaData.getAttribute(OntologyTermMetaData.ONTOLOGY_TERM_IRI));
        targetMetadata.addAttribute(Attribute.newInstance(matchingTaskContentMetaData.getAttribute(MatchingTaskContentMetaData.SCORE), EntityType.AttributeCopyMode.SHALLOW_COPY_ATTRS, attrMetaFactory).setDataType(AttributeType.STRING));
        targetMetadata.addAttribute(matchingTaskContentMetaData.getAttribute(MatchingTaskContentMetaData.VALIDATED));
        csvWriter.writeAttributeNames(columnHeaders);
        dataService.findAll(sortaJobExecution.getResultEntityName(), new QueryImpl<>()).forEach(resultEntity -> csvWriter.add(toDownloadRow(sortaJobExecution, resultEntity, targetMetadata)));
    }
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) QueryImpl(org.molgenis.data.support.QueryImpl) CsvWriter(org.molgenis.data.csv.CsvWriter) Attribute(org.molgenis.data.meta.model.Attribute) SortaJobExecution(org.molgenis.ontology.sorta.job.SortaJobExecution)

Example 3 with CsvWriter

use of org.molgenis.data.csv.CsvWriter in project molgenis by molgenis.

the class DataExplorerDownloadHandler method writeToCsv.

public void writeToCsv(DataRequest dataRequest, OutputStream outputStream, char separator, boolean noQuotes) throws IOException {
    try (CsvWriter csvWriter = new CsvWriter(outputStream, separator, noQuotes)) {
        csvWriter.setEntityWriteMode(getEntityWriteMode(dataRequest.getEntityValues()));
        String entityTypeId = dataRequest.getEntityName();
        writeCsvHeaders(dataRequest, csvWriter);
        csvWriter.add(dataService.findAll(entityTypeId, dataRequest.getQuery()));
    }
}
Also used : CsvWriter(org.molgenis.data.csv.CsvWriter)

Aggregations

CsvWriter (org.molgenis.data.csv.CsvWriter)3 OutputStreamWriter (java.io.OutputStreamWriter)1 Attribute (org.molgenis.data.meta.model.Attribute)1 EntityType (org.molgenis.data.meta.model.EntityType)1 QueryImpl (org.molgenis.data.support.QueryImpl)1 SortaJobExecution (org.molgenis.ontology.sorta.job.SortaJobExecution)1