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;
}
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);
}
}
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);
}
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;
}
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");
}
Aggregations