Search in sources :

Example 1 with TemplateNotFoundException

use of io.jmix.emailtemplates.exception.TemplateNotFoundException in project jmix by jmix-framework.

the class EmailTemplatesImpl method generateEmail.

@Override
public EmailInfo generateEmail(EmailTemplate emailTemplate, Collection<ReportWithParams> params) throws TemplateNotFoundException, ReportParameterTypeChangedException {
    if (emailTemplate == null) {
        throw new TemplateNotFoundException(messages.getMessage(EmailTemplates.class, "nullTemplate"));
    }
    List<ReportWithParams> parameters = new ArrayList<>(params);
    TemplateReport bodyReport = emailTemplate.getEmailBodyReport();
    ReportWithParams bodyReportWithParams = bodyReport != null ? getReportWithParams(bodyReport, parameters) : null;
    Map<TemplateReport, ReportWithParams> attachmentsWithParams = new HashMap<>();
    List<TemplateReport> attachedTemplateReports = emailTemplate.getAttachedTemplateReports();
    if (attachedTemplateReports != null) {
        for (TemplateReport templateReport : attachedTemplateReports) {
            ReportWithParams reportWithParams = getReportWithParams(templateReport, parameters);
            attachmentsWithParams.put(templateReport, reportWithParams);
        }
    }
    EmailInfo emailInfo = generateEmailInfoWithoutAttachments(bodyReportWithParams);
    List<EmailAttachment> templateAttachments = new ArrayList<>();
    templateAttachments.addAll(createReportAttachments(attachmentsWithParams));
    templateAttachments.addAll(createFilesAttachments(emailTemplate.getAttachedFiles()));
    emailInfo.setSubject(Boolean.TRUE.equals(emailTemplate.getUseReportSubject()) ? emailInfo.getSubject() : emailTemplate.getSubject());
    emailInfo.setAddresses(emailTemplate.getTo());
    emailInfo.setCc(emailTemplate.getCc());
    emailInfo.setBcc(emailTemplate.getBcc());
    emailInfo.setFrom(emailTemplate.getFrom());
    emailInfo.setAttachments(templateAttachments);
    return emailInfo;
}
Also used : ReportWithParams(io.jmix.emailtemplates.dto.ReportWithParams) EmailAttachment(io.jmix.email.EmailAttachment) EmailTemplates(io.jmix.emailtemplates.EmailTemplates) TemplateNotFoundException(io.jmix.emailtemplates.exception.TemplateNotFoundException) EmailInfo(io.jmix.email.EmailInfo)

Example 2 with TemplateNotFoundException

use of io.jmix.emailtemplates.exception.TemplateNotFoundException in project jmix by jmix-framework.

the class EmailTemplatesImpl method getEmailTemplateByCode.

protected EmailTemplate getEmailTemplateByCode(String emailTemplateCode) throws TemplateNotFoundException {
    EmailTemplate emailTemplate = dataManager.load(EmailTemplate.class).query("select e from emltmp_EmailTemplate e where e.code = :code").parameter("code", emailTemplateCode).optional().orElse(null);
    if (emailTemplate != null) {
        MetaClass metaClass = metadata.getClass(emailTemplate.getClass());
        emailTemplate = (EmailTemplate) dataManager.load(metaClass.getJavaClass()).query("select e from " + metaClass.getName() + " e where e.code = :code").parameter("code", emailTemplateCode).fetchPlan("emailTemplate-fetchPlan").optional().orElse(null);
    } else {
        throw new TemplateNotFoundException(messages.formatMessage(EmailTemplates.class, "notFoundTemplate", emailTemplateCode));
    }
    return emailTemplate;
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) EmailTemplates(io.jmix.emailtemplates.EmailTemplates) TemplateNotFoundException(io.jmix.emailtemplates.exception.TemplateNotFoundException)

Example 3 with TemplateNotFoundException

use of io.jmix.emailtemplates.exception.TemplateNotFoundException in project jmix by jmix-framework.

the class EmailTemplatesImpl method generateEmail.

@Override
public EmailInfo generateEmail(EmailTemplate emailTemplate, Map<String, Object> params) throws TemplateNotFoundException, ReportParameterTypeChangedException {
    if (emailTemplate == null) {
        throw new TemplateNotFoundException(messages.getMessage(EmailTemplates.class, "nullTemplate"));
    }
    List<ReportWithParams> paramList = new ArrayList<>();
    Report bodyReport = emailTemplate.getReport();
    if (bodyReport == null && emailTemplate instanceof JsonEmailTemplate) {
        bodyReport = templateConverter.convertToReport((JsonEmailTemplate) emailTemplate);
    }
    paramList.add(createParamsMapForReport(bodyReport, params));
    for (TemplateReport templateReport : emailTemplate.getAttachedTemplateReports()) {
        paramList.add(createParamsMapForReport(templateReport.getReport(), params));
    }
    return generateEmail(emailTemplate, paramList);
}
Also used : ReportWithParams(io.jmix.emailtemplates.dto.ReportWithParams) Report(io.jmix.reports.entity.Report) EmailTemplates(io.jmix.emailtemplates.EmailTemplates) TemplateNotFoundException(io.jmix.emailtemplates.exception.TemplateNotFoundException)

Aggregations

EmailTemplates (io.jmix.emailtemplates.EmailTemplates)3 TemplateNotFoundException (io.jmix.emailtemplates.exception.TemplateNotFoundException)3 ReportWithParams (io.jmix.emailtemplates.dto.ReportWithParams)2 MetaClass (io.jmix.core.metamodel.model.MetaClass)1 EmailAttachment (io.jmix.email.EmailAttachment)1 EmailInfo (io.jmix.email.EmailInfo)1 Report (io.jmix.reports.entity.Report)1