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());
}
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;
}
Aggregations