Search in sources :

Example 6 with EmailRecord

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

the class EmailControllerTest method getTestEmailRecordsAsCsv.

private String getTestEmailRecordsAsCsv() {
    List<EmailRecordDto> emailRecordDtos = new ArrayList<>();
    for (EmailRecord email : getTestEmailRecords()) {
        emailRecordDtos.add(new EmailRecordDto(email));
    }
    StringBuilder sb = new StringBuilder();
    sb.append("Delivery Status,Delivery Time,From Address,To Address,Subject,Message\r\n");
    for (EmailRecordDto dto : emailRecordDtos) {
        sb.append(String.format("%s,%s,%s,%s,%s,%s\r\n", dto.getDeliveryStatus(), dto.getDeliveryTime(), dto.getFromAddress(), dto.getToAddress(), dto.getSubject(), dto.getMessage()));
    }
    return sb.toString();
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) ArrayList(java.util.ArrayList)

Example 7 with EmailRecord

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

the class EmailSenderServiceImpl method send.

@Override
@Transactional
public void send(String fromAddress, String toAddress, String subject, String message) throws EmailSendException {
    Mail mail = new Mail(fromAddress, toAddress, subject, message);
    LOGGER.info(String.format("Sending message [%s] from [%s] to [%s] with subject [%s].", mail.getMessage(), mail.getFromAddress(), mail.getToAddress(), mail.getSubject()));
    try {
        mailSender.send(getMimeMessagePreparator(mail));
        log(new EmailRecord(mail.getFromAddress(), mail.getToAddress(), mail.getSubject(), mail.getMessage(), now(), DeliveryStatus.SENT));
    } catch (MailException e) {
        log(new EmailRecord(mail.getFromAddress(), mail.getToAddress(), mail.getSubject(), mail.getMessage(), now(), DeliveryStatus.ERROR));
        throw new EmailSendException("Unable to send an email to " + mail.getToAddress(), e);
    }
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) Mail(org.motechproject.email.domain.Mail) EmailSendException(org.motechproject.email.exception.EmailSendException) MailException(org.springframework.mail.MailException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with EmailRecord

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

the class EmailController method getAvailableMonths.

@RequestMapping(value = "/emails/months/", method = RequestMethod.GET)
@PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE)
@ResponseBody
public List<String> getAvailableMonths() {
    List<String> availableMonths = new ArrayList<>();
    List<EmailRecord> records = auditService.findAllEmailRecords();
    for (EmailRecord record : records) {
        String month = record.getDeliveryTime().monthOfYear().getAsText();
        String year = record.getDeliveryTime().year().getAsText();
        if (!availableMonths.contains(month + " " + year)) {
            availableMonths.add(month + " " + year);
        }
    }
    return availableMonths;
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) ArrayList(java.util.ArrayList) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 9 with EmailRecord

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

the class EmailController method getAllFromAddressContaining.

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

Example 10 with EmailRecord

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

the class EmailRecordServiceBundleIT method shouldCreateIdenticalMessages.

@Test
public void shouldCreateIdenticalMessages() {
    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 emailRecord = new EmailRecord(fromAddress, toAddress, subject, message, messageTime, deliveryStatus);
    emailRecordService.create(emailRecord);
    EmailRecord duplicateMessage = new EmailRecord(fromAddress, toAddress, subject, message, messageTime, deliveryStatus);
    emailRecordService.create(duplicateMessage);
    List<EmailRecord> allMessages = emailRecordService.findByRecipientAddress(toAddress);
    assertEquals(2, allMessages.size());
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) DeliveryStatus(org.motechproject.email.domain.DeliveryStatus) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

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