Search in sources :

Example 1 with CsvBeanWriter

use of org.supercsv.io.CsvBeanWriter 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 CsvBeanWriter

use of org.supercsv.io.CsvBeanWriter 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)

Example 3 with CsvBeanWriter

use of org.supercsv.io.CsvBeanWriter in project motech by motech.

the class EmailController method exportEmailLog.

@RequestMapping(value = "/emails/export", method = RequestMethod.GET)
@PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE)
public void exportEmailLog(@RequestParam("range") String range, @RequestParam(value = "month", required = false) String month, HttpServletResponse response, HttpServletRequest request) throws IOException {
    DateTime now = new DateTime();
    List<? extends BasicEmailRecordDto> toSave = new ArrayList<>();
    if ("all".equals(range)) {
        GridSettings allEmailsFilter = new GridSettings();
        List<EmailRecord> allEmails = auditService.findAllEmailRecords();
        allEmailsFilter.setPage(1);
        allEmailsFilter.setRows(allEmails.size());
        toSave = hideColumns(allEmails, allEmailsFilter);
    } else if ("table".equals(range)) {
        GridSettings filter = lastFilter.get(getUsername(request));
        toSave = getEmails(filter, request).getRows();
    } else if ("month".equals(range) && (!month.isEmpty())) {
        int moved = 0;
        String fixedMonth;
        if (month.charAt(0) == '0') {
            fixedMonth = month.substring(1);
            moved++;
        } else {
            fixedMonth = month;
        }
        GridSettings oneMonthFilter = new GridSettings();
        DateTime monthBegin = new // NO CHECKSTYLE MagicNumber
        DateTime(// NO CHECKSTYLE MagicNumber
        Integer.parseInt(fixedMonth.substring(3 - moved, 7 - moved)), Integer.parseInt(fixedMonth.substring(0, 2 - moved)), 1, 0, 0);
        DateTime monthFall = // NO CHECKSTYLE MagicNumber
        new DateTime().withYear(Integer.parseInt(fixedMonth.substring(3 - moved, 7 - moved))).withMonthOfYear(Integer.parseInt(fixedMonth.substring(0, 2 - moved))).dayOfMonth().withMaximumValue().hourOfDay().withMaximumValue().minuteOfHour().withMaximumValue().secondOfMinute().withMaximumValue().millisOfSecond().withMaximumValue();
        Set<DeliveryStatus> allDeliveryStatuses = Sets.newHashSet(DeliveryStatus.values());
        List<EmailRecord> monthEmails = auditService.findEmailRecords(new EmailRecordSearchCriteria().withMessageTimeRange(new Range<>(monthBegin, monthFall)).withDeliveryStatuses(allDeliveryStatuses));
        oneMonthFilter.setPage(1);
        oneMonthFilter.setRows(monthEmails.size());
        toSave = hideColumns(monthEmails, oneMonthFilter);
    }
    String fileName = "motech_email_logs_" + now.toString("yyyy-MM-dd_HH-kk-mm");
    response.setContentType("text/csv;charset=utf-8");
    response.setCharacterEncoding(UTF_8);
    response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
    try (CsvBeanWriter csvBeanWriter = new CsvBeanWriter(response.getWriter(), CsvPreference.STANDARD_PREFERENCE)) {
        String[] headers;
        String[] fieldMapping;
        if (toSave.size() == 0 || toSave.get(0) instanceof EmailRecordDto) {
            headers = new String[] { "Delivery Status", "Delivery Time", "From Address", "To Address", "Subject", "Message" };
            fieldMapping = new String[] { "deliveryStatus", "deliveryTime", "fromAddress", "toAddress", "subject", "message" };
        } else {
            headers = new String[] { "Delivery Status", "Delivery Time" };
            fieldMapping = new String[] { "deliveryStatus", "deliveryTime" };
        }
        csvBeanWriter.writeHeader(headers);
        for (BasicEmailRecordDto email : toSave) {
            csvBeanWriter.write(email, fieldMapping);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) EmailRecord(org.motechproject.email.domain.EmailRecord) CsvBeanWriter(org.supercsv.io.CsvBeanWriter) DeliveryStatus(org.motechproject.email.domain.DeliveryStatus) EmailRecordSearchCriteria(org.motechproject.email.builder.EmailRecordSearchCriteria) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

CsvBeanWriter (org.supercsv.io.CsvBeanWriter)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)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 ArrayList (java.util.ArrayList)1 DateTime (org.joda.time.DateTime)1 EmailRecordSearchCriteria (org.motechproject.email.builder.EmailRecordSearchCriteria)1 DeliveryStatus (org.motechproject.email.domain.DeliveryStatus)1 EmailRecord (org.motechproject.email.domain.EmailRecord)1 HttpHeaders (org.springframework.http.HttpHeaders)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 SuperCsvException (org.supercsv.exception.SuperCsvException)1 Visitor (site.model.Visitor)1