use of com.axelor.text.Templates 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());
}
use of com.axelor.text.Templates 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