Search in sources :

Example 1 with Message

use of org.openmrs.notification.Message in project openmrs-core by openmrs.

the class MessageServiceImpl method createMessage.

/**
 * @see org.openmrs.notification.MessageService#createMessage(java.lang.String,
 *      java.lang.String, java.lang.String, java.lang.String, java.lang.String,
 *      java.lang.String, java.lang.String)
 */
@Override
public Message createMessage(String recipients, String sender, String subject, String content, String attachment, String attachmentContentType, String attachmentFileName) throws MessageException {
    Message message = new Message();
    message.setRecipients(recipients);
    message.setSender(sender);
    message.setContent(content);
    message.setSubject(subject);
    message.setAttachment(attachment);
    message.setAttachmentContentType(attachmentContentType);
    message.setAttachmentFileName(attachmentFileName);
    return message;
}
Also used : Message(org.openmrs.notification.Message)

Example 2 with Message

use of org.openmrs.notification.Message in project openmrs-core by openmrs.

the class AlertReminderTask method sendAlertNotifications.

/**
 * Send alerts
 *
 * @param alerts the unread alerts
 * @param users the users who have not read the alerts
 */
private void sendAlertNotifications(Collection<Alert> alerts) {
    try {
        // Create a new message
        Message message = Context.getMessageService().createMessage("Alert Reminder", "You have unread alerts.");
        // Get all recipients
        Collection<User> users = getRecipients(alerts);
        // Send a message to each person only once
        Context.getMessageService().sendMessage(message, users);
    } catch (MessageException e) {
        log.error("Failed to send message", e);
    }
}
Also used : User(org.openmrs.User) Message(org.openmrs.notification.Message) MessageException(org.openmrs.notification.MessageException)

Example 3 with Message

use of org.openmrs.notification.Message in project openmrs-core by openmrs.

the class MessageServiceImpl method sendMessage.

/**
 * Send a message using the given parameters. This is a convenience method so that the client
 * does not need to create its own Message object.
 */
@Override
public void sendMessage(String recipients, String sender, String subject, String content) throws MessageException {
    Message message = createMessage(recipients, sender, subject, content);
    Context.getMessageService().sendMessage(message);
}
Also used : Message(org.openmrs.notification.Message)

Example 4 with Message

use of org.openmrs.notification.Message in project openmrs-core by openmrs.

the class VelocityMessagePreparator method prepare.

// TODO: need better error handling
@Override
public Message prepare(Template template) throws MessageException {
    VelocityContext context = new VelocityContext(template.getData());
    StringWriter writer = new StringWriter();
    try {
        // I have no idea what this is used for
        engine.evaluate(// I have no idea what this is used for
        context, // I have no idea what this is used for
        writer, // I have no idea what this is used for
        "template", template.getTemplate());
    } catch (Exception e) {
        // need better error handling
        log.error("Failed to prepare message using template " + e.getMessage(), e);
        throw new MessageException(e);
    }
    // Prepare the message
    Message message = new Message();
    message.setSubject(template.getSubject());
    message.setRecipients(template.getRecipients());
    message.setSender(template.getSender());
    message.setContent(writer.toString());
    return message;
}
Also used : StringWriter(java.io.StringWriter) Message(org.openmrs.notification.Message) MessageException(org.openmrs.notification.MessageException) VelocityContext(org.apache.velocity.VelocityContext) MessageException(org.openmrs.notification.MessageException)

Example 5 with Message

use of org.openmrs.notification.Message in project openmrs-module-pihcore by PIH.

the class EmailTestPageController method post.

public String post(@RequestParam(value = "recipients", required = true) String recipients, @RequestParam(value = "sender", required = true) String sender, @RequestParam(value = "subject", required = true) String subject, @RequestParam(value = "message", required = true) String message, @SpringBean("messageService") MessageService messageService, UiUtils ui, UiSessionContext context, PageRequest request) {
    if (Context.hasPrivilege(REQUIRED_PRIVILEGE)) {
        try {
            Message msg = messageService.createMessage(recipients, sender, subject, message);
            // If a custom mail.properties file is found, then create a custom message sender for testing this
            File propertiesFile = new File(OpenmrsUtil.getApplicationDataDirectory(), "mail.properties");
            if (propertiesFile.exists()) {
                Properties p = new Properties();
                OpenmrsUtil.loadProperties(p, propertiesFile);
                Session mailSession = Session.getInstance(p, new Authenticator() {

                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(p.getProperty("mail.user"), p.getProperty("mail.password"));
                    }
                });
                MailMessageSender messageSender = new MailMessageSender(mailSession);
                messageSender.send(msg);
            } else // Otherwise, use the core message sender and configuration from the runtime properties
            {
                messageService.sendMessage(msg);
            }
            request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, "Email Sent Successfully");
        } catch (Exception e) {
            log.error("Unable to send email", e);
            request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
        }
    } else {
        request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE, "You are not authorized to send test emails");
    }
    return "redirect:" + ui.pageLink("pihcore", "admin/emailTest");
}
Also used : MailMessageSender(org.openmrs.notification.mail.MailMessageSender) Message(org.openmrs.notification.Message) Properties(java.util.Properties) File(java.io.File) Authenticator(javax.mail.Authenticator) IOException(java.io.IOException) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Aggregations

Message (org.openmrs.notification.Message)5 MessageException (org.openmrs.notification.MessageException)2 File (java.io.File)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Properties (java.util.Properties)1 Authenticator (javax.mail.Authenticator)1 PasswordAuthentication (javax.mail.PasswordAuthentication)1 Session (javax.mail.Session)1 VelocityContext (org.apache.velocity.VelocityContext)1 User (org.openmrs.User)1 MailMessageSender (org.openmrs.notification.mail.MailMessageSender)1