Search in sources :

Example 11 with EmailRecord

use of org.motechproject.email.domain.EmailRecord in project motech by motech.

the class EmailRecordServiceBundleIT method shouldAddAndUpdateEmail.

@Test
public void shouldAddAndUpdateEmail() {
    DeliveryStatus deliveryStatus = DeliveryStatus.SENT;
    String fromAddress = "f@adr";
    String toAddress = "t@adr";
    String subject = "test-subject";
    String message = "test-message";
    DateTime messageTime = DateUtil.now().toDateTime(DateTimeZone.UTC);
    EmailRecord expected = new EmailRecord(fromAddress, toAddress, subject, message, messageTime, deliveryStatus);
    emailRecordService.create(expected);
    List<EmailRecord> emailRecords = emailRecordService.retrieveAll();
    assertEquals(asList(expected), emailRecords);
    EmailRecord actual = emailRecords.get(0);
    actual.setMessage("test-newmessage");
    emailRecordService.update(actual);
    emailRecords = emailRecordService.retrieveAll();
    assertEquals(asList(actual), emailRecords);
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) DeliveryStatus(org.motechproject.email.domain.DeliveryStatus) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 12 with EmailRecord

use of org.motechproject.email.domain.EmailRecord 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)

Example 13 with EmailRecord

use of org.motechproject.email.domain.EmailRecord in project motech by motech.

the class EmailController method getAllToAddressContaining.

private List<String> getAllToAddressContaining(String partial) {
    List<String> available = new ArrayList<>();
    List<EmailRecord> records = auditService.findAllEmailRecords();
    for (EmailRecord record : records) {
        if (record.getToAddress().contains(partial) && (!available.contains(record.getToAddress()))) {
            available.add(record.getToAddress());
        }
    }
    return available;
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) ArrayList(java.util.ArrayList)

Example 14 with EmailRecord

use of org.motechproject.email.domain.EmailRecord in project motech by motech.

the class EmailController method getEmails.

@RequestMapping(value = "/emails", method = RequestMethod.GET)
@PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE)
@ResponseBody
public EmailRecords<? extends BasicEmailRecordDto> getEmails(GridSettings filter, HttpServletRequest request) {
    EmailRecordSearchCriteria criteria = prepareCriteria(filter);
    List<EmailRecord> filtered = auditService.findEmailRecords(criteria);
    List<? extends BasicEmailRecordDto> rows = hideColumns(filtered, filter);
    long total = auditService.countEmailRecords(criteria);
    if (filter.getRows() == null) {
        int defaultRowsAmount = 10;
        filter.setRows(defaultRowsAmount);
    }
    if (filter.getPage() == null) {
        int defaultPage = 1;
        filter.setPage(defaultPage);
    }
    int totalPages = (int) Math.ceil((double) total / filter.getRows());
    String username = getUsername(request);
    if (username != null) {
        lastFilter.put(username, filter);
    }
    return new EmailRecords<>((int) total, filter.getPage(), totalPages, rows);
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) EmailRecords(org.motechproject.email.domain.EmailRecords) EmailRecordSearchCriteria(org.motechproject.email.builder.EmailRecordSearchCriteria) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

EmailRecord (org.motechproject.email.domain.EmailRecord)14 ArrayList (java.util.ArrayList)6 DateTime (org.joda.time.DateTime)6 Test (org.junit.Test)6 DeliveryStatus (org.motechproject.email.domain.DeliveryStatus)5 EmailRecordSearchCriteria (org.motechproject.email.builder.EmailRecordSearchCriteria)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 HashSet (java.util.HashSet)1 EmailRecords (org.motechproject.email.domain.EmailRecords)1 Mail (org.motechproject.email.domain.Mail)1 EmailSendException (org.motechproject.email.exception.EmailSendException)1 QueryParams (org.motechproject.mds.query.QueryParams)1 Order (org.motechproject.mds.util.Order)1 MailException (org.springframework.mail.MailException)1 Transactional (org.springframework.transaction.annotation.Transactional)1 CsvBeanWriter (org.supercsv.io.CsvBeanWriter)1