Search in sources :

Example 1 with StringTemplates

use of com.axelor.text.StringTemplates in project axelor-open-suite by axelor.

the class MessageServiceBaseImpl method printMessage.

@SuppressWarnings("unchecked")
@Override
public String printMessage(Message message) throws AxelorException {
    Company company = message.getCompany();
    if (company == null) {
        return null;
    }
    PrintingSettings printSettings = company.getPrintingSettings();
    if (printSettings == null || printSettings.getDefaultMailBirtTemplate() == null) {
        return null;
    }
    BirtTemplate birtTemplate = printSettings.getDefaultMailBirtTemplate();
    logger.debug("Default BirtTemplate : {}", birtTemplate);
    Templates templates = new StringTemplates('$', '$');
    Map<String, Object> templatesContext = Maps.newHashMap();
    try {
        Class<? extends Model> className = (Class<? extends Model>) Class.forName(message.getClass().getName());
        templatesContext.put("Message", JPA.find(className, message.getId()));
    } catch (ClassNotFoundException e) {
        TraceBackService.trace(e);
    }
    String fileName = "Message " + message.getSubject() + "-" + appBaseService.getTodayDate(company).format(DateTimeFormatter.BASIC_ISO_DATE);
    return Beans.get(TemplateMessageServiceBaseImpl.class).generateBirtTemplateLink(templates, templatesContext, fileName, birtTemplate.getTemplateLink(), birtTemplate.getFormat(), birtTemplate.getBirtTemplateParameterList());
}
Also used : Company(com.axelor.apps.base.db.Company) BirtTemplate(com.axelor.apps.base.db.BirtTemplate) Templates(com.axelor.text.Templates) StringTemplates(com.axelor.text.StringTemplates) StringTemplates(com.axelor.text.StringTemplates) PrintingSettings(com.axelor.apps.base.db.PrintingSettings) Model(com.axelor.db.Model)

Example 2 with StringTemplates

use of com.axelor.text.StringTemplates in project axelor-open-suite by axelor.

the class TemplateMessageServiceImpl method generateMessage.

@Override
@Transactional(rollbackOn = { Exception.class })
public Message generateMessage(Long objectId, String model, String tag, Template template, Boolean isForTemporaryEmail) throws ClassNotFoundException, InstantiationException, IllegalAccessException, AxelorException, IOException {
    Templates templates;
    Map<String, Object> templatesContext = Maps.newHashMap();
    if (template.getTemplateEngineSelect() == TemplateRepository.TEMPLATE_ENGINE_GROOVY_TEMPLATE) {
        templates = Beans.get(GroovyTemplates.class);
    } else {
        templates = new StringTemplates(TEMPLATE_DELIMITER, TEMPLATE_DELIMITER);
    }
    Object modelObj = template.getIsJson() ? template.getMetaJsonModel() : template.getMetaModel();
    if (modelObj != null) {
        String modelName = template.getIsJson() ? ((MetaJsonModel) modelObj).getName() : ((MetaModel) modelObj).getFullName();
        if (!model.equals(modelName)) {
            throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, String.format(I18n.get(IExceptionMessage.INVALID_MODEL_TEMPLATE_EMAIL), modelName, model));
        }
        if (objectId != 0L) {
            initMaker(objectId, model, tag, template.getIsJson(), templatesContext);
            computeTemplateContexts(template.getTemplateContextList(), objectId, model, template.getIsJson(), templatesContext);
        } else {
            Class<?> klass = EntityHelper.getEntityClass(this.modelObject);
            Context context = new Context(Mapper.toMap(this.modelObject), klass);
            templatesContext.put(tag, context.asType(klass));
            if (template.getTemplateContextList() != null) {
                for (TemplateContext templateContext : template.getTemplateContextList()) {
                    Object result = templateContextService.computeTemplateContext(templateContext, context);
                    templatesContext.put(templateContext.getName(), result);
                }
            }
        }
    }
    log.debug("model : {}", model);
    log.debug("tag : {}", tag);
    log.debug("object id : {}", objectId);
    log.debug("template : {}", template);
    Message message = generateMessage(model, objectId, template, templates, templatesContext, isForTemporaryEmail);
    if (!isForTemporaryEmail) {
        message.setTemplate(Beans.get(TemplateRepository.class).find(template.getId()));
        message = Beans.get(MessageRepository.class).save(message);
        messageService.attachMetaFiles(message, getMetaFiles(template, templates, templatesContext));
    }
    return message;
}
Also used : TemplateContext(com.axelor.apps.message.db.TemplateContext) Context(com.axelor.rpc.Context) AxelorException(com.axelor.exception.AxelorException) StringTemplates(com.axelor.text.StringTemplates) Message(com.axelor.apps.message.db.Message) IExceptionMessage(com.axelor.apps.message.exception.IExceptionMessage) Templates(com.axelor.text.Templates) StringTemplates(com.axelor.text.StringTemplates) GroovyTemplates(com.axelor.text.GroovyTemplates) TemplateContext(com.axelor.apps.message.db.TemplateContext) GroovyTemplates(com.axelor.text.GroovyTemplates) Transactional(com.google.inject.persist.Transactional)

Aggregations

StringTemplates (com.axelor.text.StringTemplates)2 Templates (com.axelor.text.Templates)2 BirtTemplate (com.axelor.apps.base.db.BirtTemplate)1 Company (com.axelor.apps.base.db.Company)1 PrintingSettings (com.axelor.apps.base.db.PrintingSettings)1 Message (com.axelor.apps.message.db.Message)1 TemplateContext (com.axelor.apps.message.db.TemplateContext)1 IExceptionMessage (com.axelor.apps.message.exception.IExceptionMessage)1 Model (com.axelor.db.Model)1 AxelorException (com.axelor.exception.AxelorException)1 Context (com.axelor.rpc.Context)1 GroovyTemplates (com.axelor.text.GroovyTemplates)1 Transactional (com.google.inject.persist.Transactional)1