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());
}
}
}
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;
}
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;
}
Aggregations