Search in sources :

Example 21 with Message

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;
}
Also used : Message(com.axelor.apps.message.db.Message) IExceptionMessage(com.axelor.apps.message.exception.IExceptionMessage) Model(com.axelor.db.Model) Transactional(com.google.inject.persist.Transactional)

Example 22 with Message

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;
    });
}
Also used : User(com.axelor.auth.db.User) Message(com.axelor.apps.message.db.Message) PersistenceException(javax.persistence.PersistenceException) PersistenceException(javax.persistence.PersistenceException) EntityNotFoundException(javax.persistence.EntityNotFoundException)

Example 23 with Message

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;
}
Also used : AxelorException(com.axelor.exception.AxelorException) EmailAccount(com.axelor.apps.message.db.EmailAccount) Message(com.axelor.apps.message.db.Message) IExceptionMessage(com.axelor.apps.message.exception.IExceptionMessage) EmailAddress(com.axelor.apps.message.db.EmailAddress)

Example 24 with 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;
}
Also used : Message(com.axelor.apps.message.db.Message) IExceptionMessage(com.axelor.apps.message.exception.IExceptionMessage)

Example 25 with 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);
    }
}
Also used : Message(com.axelor.apps.message.db.Message) IExceptionMessage(com.axelor.apps.message.exception.IExceptionMessage) MessageService(com.axelor.apps.message.service.MessageService) AxelorException(com.axelor.exception.AxelorException)

Aggregations

Message (com.axelor.apps.message.db.Message)54 AxelorException (com.axelor.exception.AxelorException)25 MessageServiceBaseImpl (com.axelor.apps.base.service.message.MessageServiceBaseImpl)16 IExceptionMessage (com.axelor.apps.message.exception.IExceptionMessage)14 Transactional (com.google.inject.persist.Transactional)13 IExceptionMessage (com.axelor.apps.hr.exception.IExceptionMessage)11 Template (com.axelor.apps.message.db.Template)7 Timesheet (com.axelor.apps.hr.db.Timesheet)6 ArrayList (java.util.ArrayList)6 Employee (com.axelor.apps.hr.db.Employee)5 EmailAddress (com.axelor.apps.message.db.EmailAddress)5 IExceptionMessage (com.axelor.apps.base.exceptions.IExceptionMessage)4 Expense (com.axelor.apps.hr.db.Expense)4 ExtraHours (com.axelor.apps.hr.db.ExtraHours)4 LeaveRequest (com.axelor.apps.hr.db.LeaveRequest)4 ExpenseService (com.axelor.apps.hr.service.expense.ExpenseService)4 ExtraHoursService (com.axelor.apps.hr.service.extra.hours.ExtraHoursService)4 LeaveService (com.axelor.apps.hr.service.leave.LeaveService)4 TimesheetService (com.axelor.apps.hr.service.timesheet.TimesheetService)4 User (com.axelor.auth.db.User)4