@Test public void shouldExportEmailAllAsCsv() throws Exception { StringWriter writer = new StringWriter(); when(response.getWriter()).thenReturn(new PrintWriter(writer)); emailController.exportEmailLog("all", null, response, request); assertEquals(getTestEmailRecordsAsCsv(), writer.toString()); }
@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 DateTime(Integer.parseInt(fixedMonth.substring(3 - moved, 7 - moved)), Integer.parseInt(fixedMonth.substring(0, 2 - moved)), 1, 0, 0); DateTime monthFall = 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); } } }
EmailController { @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 DateTime(Integer.parseInt(fixedMonth.substring(3 - moved, 7 - moved)), Integer.parseInt(fixedMonth.substring(0, 2 - moved)), 1, 0, 0); DateTime monthFall = 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); } } } }
EmailController { @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 DateTime(Integer.parseInt(fixedMonth.substring(3 - moved, 7 - moved)), Integer.parseInt(fixedMonth.substring(0, 2 - moved)), 1, 0, 0); DateTime monthFall = 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); } } }  }
EmailController { @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 DateTime(Integer.parseInt(fixedMonth.substring(3 - moved, 7 - moved)), Integer.parseInt(fixedMonth.substring(0, 2 - moved)), 1, 0, 0); DateTime monthFall = 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); } } }  @RequestMapping(value = "/emails", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody EmailRecords<? extends BasicEmailRecordDto> getEmails(GridSettings filter, HttpServletRequest request); @RequestMapping(value = "/emails/{mailid}", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody EmailRecords<EmailRecordDto> getEmail(@PathVariable int mailid); @RequestMapping(value = "/emails/months/", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody List<String> getAvailableMonths(); @RequestMapping(value = "/emails/export", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) void exportEmailLog(@RequestParam("range") String range,
                               @RequestParam(value = "month", required = false) String month,
                               HttpServletResponse response,
                               HttpServletRequest request); @RequestMapping(value = "/emails/available/", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody List<String> getAvailableMails(@RequestParam("autoComplete") String autoComplete,
                                          @RequestParam("term") String partialAddress); }
EmailController { @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 DateTime(Integer.parseInt(fixedMonth.substring(3 - moved, 7 - moved)), Integer.parseInt(fixedMonth.substring(0, 2 - moved)), 1, 0, 0); DateTime monthFall = 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); } } }  @RequestMapping(value = "/emails", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody EmailRecords<? extends BasicEmailRecordDto> getEmails(GridSettings filter, HttpServletRequest request); @RequestMapping(value = "/emails/{mailid}", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody EmailRecords<EmailRecordDto> getEmail(@PathVariable int mailid); @RequestMapping(value = "/emails/months/", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody List<String> getAvailableMonths(); @RequestMapping(value = "/emails/export", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) void exportEmailLog(@RequestParam("range") String range,
                               @RequestParam(value = "month", required = false) String month,
                               HttpServletResponse response,
                               HttpServletRequest request); @RequestMapping(value = "/emails/available/", method = RequestMethod.GET) @PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE) @ResponseBody List<String> getAvailableMails(@RequestParam("autoComplete") String autoComplete,
                                          @RequestParam("term") String partialAddress);  }