use of com.axelor.apps.message.db.Message in project axelor-open-suite by axelor.
the class MessageServiceImpl method regenerateMessage.
@Override
@Transactional(rollbackOn = { Exception.class })
public Message regenerateMessage(Message message) throws Exception {
Preconditions.checkNotNull(message.getTemplate(), I18n.get("Cannot regenerate message without template associated to message."));
Preconditions.checkNotNull(message.getRelatedTo1Select(), I18n.get("Cannot regenerate message without related model."));
Class m = Class.forName(message.getRelatedTo1Select());
Model model = JPA.all(m).filter("self.id = ?", message.getRelatedTo1SelectId()).fetchOne();
Message newMessage = Beans.get(TemplateMessageService.class).generateMessage(model, message.getTemplate());
newMessage.setRelatedTo2Select(message.getRelatedTo2Select());
newMessage.setRelatedTo2SelectId(message.getRelatedTo2SelectId());
message.setArchived(true);
return newMessage;
}
use of com.axelor.apps.message.db.Message in project axelor-open-suite by axelor.
the class SendMailQueueService method submitMailJob.
/**
* Submit a mail job to an executor which will send mails in a separate thread.
*
* @param mailBuilder
* @param message
*/
public void submitMailJob(MailBuilder mailBuilder, Message message) {
long messageId = message.getId();
log.debug("Submitting job to executor for message {}...", messageId);
User currentUser = AuthUtils.getUser();
executor.submit(() -> {
try {
final long startTime = System.currentTimeMillis();
boolean done = false;
PersistenceException persistenceException = null;
log.debug("Sending message {}...", messageId);
mailBuilder.send();
log.debug("Message {} sent.", messageId);
do {
try {
inTransaction(() -> {
final Message updateMessage = findMessage(messageId);
getEntityManager().lock(updateMessage, LockModeType.PESSIMISTIC_WRITE);
updateMessage.setSentByEmail(true);
updateMessage.setStatusSelect(MessageRepository.STATUS_SENT);
updateMessage.setSentDateT(LocalDateTime.now());
if (currentUser != null) {
updateMessage.setSenderUser(userRepository.find(currentUser.getId()));
}
messageRepository.save(updateMessage);
});
done = true;
} catch (PersistenceException e) {
persistenceException = e;
sleep();
}
} while (!done && System.currentTimeMillis() - startTime < ENTITY_FIND_TIMEOUT);
if (!done) {
throw persistenceException;
}
} catch (Exception e) {
log.debug("Exception when sending email", e);
TraceBackService.trace(e);
}
return true;
});
}
use of com.axelor.apps.message.db.Message in project axelor-open-suite by axelor.
the class TemplateMessageServiceImpl method generateMessage.
protected Message generateMessage(String model, Long objectId, Template template, Templates templates, Map<String, Object> templatesContext, Boolean isForTemporaryEmail) {
String content = "";
String subject = "";
String replyToRecipients = "";
String toRecipients = "";
String ccRecipients = "";
String bccRecipients = "";
String addressBlock = "";
int mediaTypeSelect;
String signature = "";
if (!Strings.isNullOrEmpty(template.getContent())) {
content = templates.fromText(template.getContent()).make(templatesContext).render();
}
if (!Strings.isNullOrEmpty(template.getAddressBlock())) {
addressBlock = templates.fromText(template.getAddressBlock()).make(templatesContext).render();
}
if (!Strings.isNullOrEmpty(template.getSubject())) {
subject = templates.fromText(template.getSubject()).make(templatesContext).render();
log.debug("Subject ::: {}", subject);
}
if (!Strings.isNullOrEmpty(template.getReplyToRecipients())) {
replyToRecipients = templates.fromText(template.getReplyToRecipients()).make(templatesContext).render();
log.debug("Reply to ::: {}", replyToRecipients);
}
if (template.getToRecipients() != null) {
toRecipients = templates.fromText(template.getToRecipients()).make(templatesContext).render();
log.debug("To ::: {}", toRecipients);
}
if (template.getCcRecipients() != null) {
ccRecipients = templates.fromText(template.getCcRecipients()).make(templatesContext).render();
log.debug("CC ::: {}", ccRecipients);
}
if (template.getBccRecipients() != null) {
bccRecipients = templates.fromText(template.getBccRecipients()).make(templatesContext).render();
log.debug("BCC ::: {}", bccRecipients);
}
mediaTypeSelect = this.getMediaTypeSelect(template);
log.debug("Media ::: {}", mediaTypeSelect);
if (template.getSignature() != null) {
signature = templates.fromText(template.getSignature()).make(templatesContext).render();
log.debug("Signature ::: {}", signature);
}
EmailAccount mailAccount = getMailAccount();
EmailAddress fromAddress = null;
if (mailAccount == null) {
TraceBackService.trace(new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MAIL_ACCOUNT_6)));
} else {
fromAddress = getEmailAddress(mailAccount.getFromAddress());
}
Message message = messageService.createMessage(model, Math.toIntExact(objectId), subject, content, fromAddress, getEmailAddresses(replyToRecipients), getEmailAddresses(toRecipients), getEmailAddresses(ccRecipients), getEmailAddresses(bccRecipients), null, addressBlock, mediaTypeSelect, mailAccount, signature, isForTemporaryEmail);
return message;
}
use of com.axelor.apps.message.db.Message in project axelor-open-suite by axelor.
the class TemplateMessageServiceImpl method generateAndSendMessage.
@Override
public Message generateAndSendMessage(Model model, Template template) throws MessagingException, IOException, AxelorException, ClassNotFoundException, InstantiationException, IllegalAccessException, JSONException {
Message message = this.generateMessage(model, template);
messageService.sendMessage(message);
return message;
}
use of com.axelor.apps.message.db.Message in project axelor-open-suite by axelor.
the class MessageController method sendMessage.
public void sendMessage(ActionRequest request, ActionResponse response) {
Message message = request.getContext().asType(Message.class);
try {
Beans.get(MessageService.class).sendMessage(Beans.get(MessageRepository.class).find(message.getId()));
response.setReload(true);
response.setFlash(I18n.get(IExceptionMessage.MESSAGE_4));
} catch (Exception e) {
TraceBackService.trace(response, e);
}
}
Aggregations