Search in sources :

Example 1 with TemplateContext

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

the class PrintTemplateServiceImpl method processPrintTemplateLineList.

private void processPrintTemplateLineList(List<PrintTemplateLine> templateLineList, Print print, PrintLine parent, Context scriptContext, TemplateMaker maker, int level) throws AxelorException {
    int seq = 1;
    if (ObjectUtils.notEmpty(templateLineList.get(0)) && ObjectUtils.notEmpty(templateLineList.get(0).getSequence())) {
        seq = templateLineList.get(0).getSequence().intValue();
    }
    for (PrintTemplateLine printTemplateLine : templateLineList) {
        if (printTemplateLine.getIgnoreTheLine()) {
            continue;
        }
        try {
            boolean present = true;
            if (StringUtils.notEmpty(printTemplateLine.getConditions())) {
                Object evaluation = templateContextService.computeTemplateContext(printTemplateLine.getConditions(), scriptContext);
                if (evaluation instanceof Boolean) {
                    present = (Boolean) evaluation;
                } else {
                    throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.PRINT_TEMPLATE_CONDITION_MUST_BE_BOOLEAN));
                }
            }
            if (present) {
                ++rank;
                String title = null;
                String content = null;
                if (StringUtils.notEmpty(printTemplateLine.getTitle())) {
                    maker.setTemplate(printTemplateLine.getTitle());
                    addSequencesInContext(maker, level, seq, parent);
                    title = maker.make();
                }
                if (StringUtils.notEmpty(printTemplateLine.getContent())) {
                    maker.setTemplate(printTemplateLine.getContent());
                    addSequencesInContext(maker, level, seq, parent);
                    if (CollectionUtils.isNotEmpty(printTemplateLine.getTemplateContextList())) {
                        for (TemplateContext templateContext : printTemplateLine.getTemplateContextList()) {
                            Object result = templateContextService.computeTemplateContext(templateContext, scriptContext);
                            maker.addContext(templateContext.getName(), result);
                        }
                    }
                    content = maker.make();
                }
                PrintLine printLine = new PrintLine();
                printLine.setRank(rank);
                printLine.setSequence(seq);
                printLine.setTitle(title);
                printLine.setContent(content);
                printLine.setIsEditable(printTemplateLine.getIsEditable());
                printLine.setIsSignature(printTemplateLine.getIsSignature());
                printLine.setNbColumns(printTemplateLine.getNbColumns());
                printLine.setParent(parent);
                printLine.setIsWithPageBreakAfter(printTemplateLine.getIsWithPageBreakAfter());
                if (CollectionUtils.isNotEmpty(printTemplateLine.getPrintTemplateLineList())) {
                    processPrintTemplateLineList(printTemplateLine.getPrintTemplateLineList(), print, printLine, scriptContext, maker, level + 1);
                }
                print.addPrintLineListItem(printLine);
                seq++;
            }
        } catch (Exception e) {
            throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.PRINT_TEMPLATE_ERROR_ON_LINE_WITH_SEQUENCE_AND_TITLE), printTemplateLine.getSequence(), printTemplateLine.getTitle());
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) PrintLine(com.axelor.apps.base.db.PrintLine) PrintTemplateLine(com.axelor.apps.base.db.PrintTemplateLine) TemplateContext(com.axelor.apps.message.db.TemplateContext) Print(com.axelor.apps.base.db.Print) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException)

Example 2 with TemplateContext

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

the class TemplateMessageServiceImpl method computeTemplateContexts.

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> computeTemplateContexts(List<TemplateContext> templateContextList, long objectId, String model, boolean isJson, Map<String, Object> templatesContext) throws ClassNotFoundException {
    if (templateContextList == null) {
        return templatesContext;
    }
    Context context = null;
    if (isJson) {
        context = new com.axelor.rpc.Context(objectId, MetaJsonRecord.class);
    } else {
        Class<? extends Model> myClass = (Class<? extends Model>) Class.forName(model);
        context = new com.axelor.rpc.Context(objectId, myClass);
    }
    for (TemplateContext templateContext : templateContextList) {
        Object result = templateContextService.computeTemplateContext(templateContext, context);
        templatesContext.put(templateContext.getName(), result);
    }
    return templatesContext;
}
Also used : TemplateContext(com.axelor.apps.message.db.TemplateContext) Context(com.axelor.rpc.Context) MetaJsonModel(com.axelor.meta.db.MetaJsonModel) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) TemplateContext(com.axelor.apps.message.db.TemplateContext) Context(com.axelor.rpc.Context) MetaJsonRecord(com.axelor.meta.db.MetaJsonRecord)

Example 3 with TemplateContext

use of com.axelor.apps.message.db.TemplateContext 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

TemplateContext (com.axelor.apps.message.db.TemplateContext)3 AxelorException (com.axelor.exception.AxelorException)2 Context (com.axelor.rpc.Context)2 Print (com.axelor.apps.base.db.Print)1 PrintLine (com.axelor.apps.base.db.PrintLine)1 PrintTemplateLine (com.axelor.apps.base.db.PrintTemplateLine)1 Message (com.axelor.apps.message.db.Message)1 IExceptionMessage (com.axelor.apps.message.exception.IExceptionMessage)1 Model (com.axelor.db.Model)1 MetaJsonModel (com.axelor.meta.db.MetaJsonModel)1 MetaJsonRecord (com.axelor.meta.db.MetaJsonRecord)1 MetaModel (com.axelor.meta.db.MetaModel)1 GroovyTemplates (com.axelor.text.GroovyTemplates)1 StringTemplates (com.axelor.text.StringTemplates)1 Templates (com.axelor.text.Templates)1 Transactional (com.google.inject.persist.Transactional)1 IOException (java.io.IOException)1