Search in sources :

Example 1 with ICsvBeanWriter

use of org.supercsv.io.ICsvBeanWriter in project jprime by bgjug.

the class AdminVisitorController method exportVisitors.

@RequestMapping(value = "/export", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<byte[]> exportVisitors() throws IOException {
    Iterable<Visitor> visitors = adminFacade.findAllVisitors();
    final String[] header = new String[] { "name", "email", "company", "status" };
    ICsvBeanWriter beanWriter = null;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        beanWriter = new CsvBeanWriter(new BufferedWriter(new OutputStreamWriter(out)), CsvPreference.STANDARD_PREFERENCE);
        beanWriter.writeHeader(header);
        for (Visitor visitor : visitors) {
            beanWriter.write(visitor, header, getProcessors());
        }
    } finally {
        if (beanWriter != null) {
            beanWriter.close();
        }
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    return ResponseEntity.ok().headers(headers).body(out.toByteArray());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Visitor(site.model.Visitor) ICsvBeanWriter(org.supercsv.io.ICsvBeanWriter) CsvBeanWriter(org.supercsv.io.CsvBeanWriter) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ICsvBeanWriter(org.supercsv.io.ICsvBeanWriter) BufferedWriter(java.io.BufferedWriter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with ICsvBeanWriter

use of org.supercsv.io.ICsvBeanWriter in project apex-malhar by apache.

the class CsvFormatter method convert.

@Override
public String convert(Object tuple) {
    incomingTuplesCount++;
    if (tuple == null) {
        errorTupleCount++;
        logger.error(" Null tuple", tuple);
        return null;
    }
    try {
        StringWriter stringWriter = new StringWriter();
        ICsvBeanWriter beanWriter = new CsvBeanWriter(stringWriter, preference);
        beanWriter.write(tuple, nameMapping, processors);
        beanWriter.flush();
        beanWriter.close();
        emittedObjectCount++;
        return stringWriter.toString();
    } catch (SuperCsvException e) {
        logger.error("Error while converting tuple {} {}", tuple, e.getMessage());
        errorTupleCount++;
    } catch (IOException e) {
        DTThrowable.rethrow(e);
    }
    return null;
}
Also used : StringWriter(java.io.StringWriter) ICsvBeanWriter(org.supercsv.io.ICsvBeanWriter) CsvBeanWriter(org.supercsv.io.CsvBeanWriter) IOException(java.io.IOException) ICsvBeanWriter(org.supercsv.io.ICsvBeanWriter) SuperCsvException(org.supercsv.exception.SuperCsvException)

Aggregations

CsvBeanWriter (org.supercsv.io.CsvBeanWriter)2 ICsvBeanWriter (org.supercsv.io.ICsvBeanWriter)2 BufferedWriter (java.io.BufferedWriter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 HttpHeaders (org.springframework.http.HttpHeaders)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 SuperCsvException (org.supercsv.exception.SuperCsvException)1 Visitor (site.model.Visitor)1