Search in sources :

Example 1 with Message

use of com.axelor.apps.message.db.Message in project axelor-open-suite by axelor.

the class BatchEventReminder method generateMessageProcess.

protected void generateMessageProcess() {
    MessageRepository messageRepo = Beans.get(MessageRepository.class);
    if (!stop) {
        int i = 0;
        Query q = JPA.em().createQuery(" SELECT er FROM EventReminder as er WHERE er.isReminded = true and ?1 MEMBER OF er.batchSet");
        q.setParameter(1, batch);
        @SuppressWarnings("unchecked") List<EventReminder> eventReminderList = q.getResultList();
        for (EventReminder eventReminder : eventReminderList) {
            try {
                eventReminder = eventReminderRepo.find(eventReminder.getId());
                Message message = messageServiceCrmImpl.createMessage(eventReminder.getEvent());
                // Send reminder to owner of the reminder in any case
                if (eventReminder.getUser().getPartner() != null && eventReminder.getUser().getPartner().getEmailAddress() != null) {
                    message.addToEmailAddressSetItem(eventReminder.getUser().getPartner().getEmailAddress());
                } else if (eventReminder.getUser().getEmail() != null) {
                    message.addToEmailAddressSetItem(findOrCreateEmailAddress(eventReminder.getUser().getEmail(), "[" + eventReminder.getUser().getEmail() + "]"));
                } else {
                    messageRepo.remove(message);
                    throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CRM_CONFIG_USER_EMAIL), eventReminder.getUser().getName());
                }
                // Also send to attendees if needed
                if (EventReminderRepository.ASSIGN_TO_ALL.equals(eventReminder.getAssignToSelect()) && eventReminder.getEvent().getAttendees() != null) {
                    for (ICalendarUser iCalUser : eventReminder.getEvent().getAttendees()) {
                        if (iCalUser.getUser() != null && iCalUser.getUser().getPartner() != null) {
                            message.addToEmailAddressSetItem(iCalUser.getUser().getPartner().getEmailAddress());
                        } else {
                            message.addToEmailAddressSetItem(findOrCreateEmailAddress(iCalUser.getEmail(), iCalUser.getName()));
                        }
                    }
                }
                message = Beans.get(MessageService.class).sendByEmail(message);
            } catch (Exception e) {
                TraceBackService.trace(new Exception(String.format(I18n.get("Event") + " %s", eventRepo.find(eventReminder.getEvent().getId()).getSubject()), e), ExceptionOriginRepository.CRM, batch.getId());
                incrementAnomaly();
                LOG.error("Bug(Anomalie) généré(e) pour l'évènement {}", eventRepo.find(eventReminder.getEvent().getId()).getSubject());
            } finally {
                if (i % 1 == 0) {
                    JPA.clear();
                }
            }
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) ICalendarUser(com.axelor.apps.base.db.ICalendarUser) EventReminder(com.axelor.apps.crm.db.EventReminder) Query(javax.persistence.Query) IExceptionMessage(com.axelor.apps.crm.exception.IExceptionMessage) Message(com.axelor.apps.message.db.Message) MessageRepository(com.axelor.apps.message.db.repo.MessageRepository) AxelorException(com.axelor.exception.AxelorException)

Example 2 with Message

use of com.axelor.apps.message.db.Message in project axelor-open-suite by axelor.

the class DebtRecoveryActionService method runMessage.

/**
 * Generate a message from a debtRecovery, save, and send it.
 *
 * @param debtRecovery
 * @throws AxelorException
 * @throws ClassNotFoundException
 * @throws IOException
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
@Transactional(rollbackOn = { Exception.class })
public void runMessage(DebtRecovery debtRecovery) throws AxelorException, ClassNotFoundException, IOException, InstantiationException, IllegalAccessException, JSONException {
    Set<Message> messageSet = this.runStandardMessage(debtRecovery);
    for (Message message : messageSet) {
        message = Beans.get(MessageRepository.class).save(message);
        if (!debtRecovery.getDebtRecoveryMethodLine().getManualValidationOk() && message.getMailAccount() == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.DEBT_RECOVERY_ACTION_4));
        }
        if (CollectionUtils.isEmpty(message.getToEmailAddressSet())) {
            throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.DEBT_RECOVERY_ACTION_5), debtRecovery.getDebtRecoveryMethodLine().getDebtRecoveryLevelLabel());
        }
        Beans.get(MessageService.class).sendMessage(message);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) IExceptionMessage(com.axelor.apps.account.exception.IExceptionMessage) Message(com.axelor.apps.message.db.Message) MessageService(com.axelor.apps.message.service.MessageService) Transactional(com.google.inject.persist.Transactional)

Example 3 with Message

use of com.axelor.apps.message.db.Message in project axelor-open-suite by axelor.

the class TemplateMessageAccountServiceImpl method generateMessage.

@Override
@Transactional(rollbackOn = { Exception.class })
public Message generateMessage(DebtRecoveryHistory debtRecoveryHistory, Template template) throws ClassNotFoundException, IOException, InstantiationException, AxelorException, IllegalAccessException {
    Message message = this.templateMessageService.generateMessage(debtRecoveryHistory, template);
    message.setRelatedTo2Select(Partner.class.getCanonicalName());
    message.setRelatedTo2SelectId((debtRecoveryHistory.getDebtRecovery().getTradingName() == null) ? debtRecoveryHistory.getDebtRecovery().getAccountingSituation().getPartner().getId() : debtRecoveryHistory.getDebtRecovery().getTradingNameAccountingSituation().getPartner().getId());
    return message;
}
Also used : Message(com.axelor.apps.message.db.Message) Partner(com.axelor.apps.base.db.Partner) Transactional(com.google.inject.persist.Transactional)

Example 4 with Message

use of com.axelor.apps.message.db.Message in project axelor-open-suite by axelor.

the class ExpenseController method cancel.

public void cancel(ActionRequest request, ActionResponse response) throws AxelorException {
    try {
        Expense expense = request.getContext().asType(Expense.class);
        expense = Beans.get(ExpenseRepository.class).find(expense.getId());
        ExpenseService expenseService = Beans.get(ExpenseService.class);
        expenseService.cancel(expense);
        Message message = expenseService.sendCancellationEmail(expense);
        if (message != null && message.getStatusSelect() == MessageRepository.STATUS_SENT) {
            response.setFlash(String.format(I18n.get("Email sent to %s"), Beans.get(MessageServiceBaseImpl.class).getToRecipients(message)));
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    } finally {
        response.setReload(true);
    }
}
Also used : IExceptionMessage(com.axelor.apps.hr.exception.IExceptionMessage) Message(com.axelor.apps.message.db.Message) Expense(com.axelor.apps.hr.db.Expense) MessageServiceBaseImpl(com.axelor.apps.base.service.message.MessageServiceBaseImpl) ExpenseService(com.axelor.apps.hr.service.expense.ExpenseService) AxelorException(com.axelor.exception.AxelorException)

Example 5 with Message

use of com.axelor.apps.message.db.Message in project axelor-open-suite by axelor.

the class TimesheetReportServiceImpl method sendEmailMessage.

private List<Message> sendEmailMessage(List<TimesheetReminder> timesheetReminders, Template reminderTemplate) {
    List<Message> messages = new ArrayList<>();
    for (TimesheetReminder timesheetReminder : timesheetReminders) {
        try {
            Message message = templateMessageService.generateMessage(timesheetReminder, reminderTemplate);
            message = messageService.sendMessage(message);
            timesheetReminder.setEmailSentDateT(LocalDateTime.now());
            messages.add(message);
        } catch (Exception e) {
            TraceBackService.trace(e);
        }
    }
    return messages;
}
Also used : IExceptionMessage(com.axelor.apps.hr.exception.IExceptionMessage) Message(com.axelor.apps.message.db.Message) ArrayList(java.util.ArrayList) TimesheetReminder(com.axelor.apps.hr.db.TimesheetReminder) 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