use of com.axelor.apps.base.db.PrintTemplateLineTest in project axelor-open-suite by axelor.
the class PrintTemplateLineController method checkTemplateLineExpression.
public void checkTemplateLineExpression(ActionRequest request, ActionResponse response) {
Context context = request.getContext();
PrintTemplateLineTest printTemplateLineTest = context.asType(PrintTemplateLineTest.class);
printTemplateLineTest = Beans.get(PrintTemplateLineTestRepository.class).find(printTemplateLineTest.getId());
MetaModel metaModel = Beans.get(MetaModelRepository.class).all().filter("self.fullName = ?", printTemplateLineTest.getReference()).fetchOne();
try {
Beans.get(PrintTemplateLineService.class).checkExpression(Long.valueOf(printTemplateLineTest.getReferenceId().toString()), metaModel, printTemplateLineTest.getPrintTemplateLine());
} catch (NumberFormatException | ClassNotFoundException | AxelorException | IOException e) {
TraceBackService.trace(response, e);
}
response.setReload(true);
}
use of com.axelor.apps.base.db.PrintTemplateLineTest in project axelor-open-suite by axelor.
the class PrintTemplateLineServiceImpl method checkExpression.
@SuppressWarnings("unchecked")
@Transactional
@Override
public void checkExpression(Long objectId, MetaModel metaModel, PrintTemplateLine printTemplateLine) throws ClassNotFoundException, AxelorException {
if (metaModel == null) {
return;
}
String model = metaModel.getFullName();
String simpleModel = metaModel.getName();
PrintTemplateLineTest test = printTemplateLine.getPrintTemplateLineTest();
Context scriptContext = null;
if (StringUtils.notEmpty(model)) {
Class<? extends Model> modelClass = (Class<? extends Model>) Class.forName(model);
Model modelObject = JPA.find(modelClass, objectId);
if (ObjectUtils.notEmpty(modelObject)) {
scriptContext = new Context(Mapper.toMap(modelObject), modelClass);
}
}
String resultOfTitle = null;
String resultOfContent = null;
Locale locale = Optional.ofNullable(printTemplateLine.getPrintTemplate().getLanguage()).map(Language::getCode).map(Locale::new).orElseGet(AppFilter::getLocale);
TemplateMaker maker = initMaker(objectId, model, simpleModel, locale);
try {
if (StringUtils.notEmpty(printTemplateLine.getTitle())) {
maker.setTemplate(printTemplateLine.getTitle());
resultOfTitle = maker.make();
}
} catch (Exception e) {
resultOfTitle = "Error in title";
}
try {
if (StringUtils.notEmpty(printTemplateLine.getContent())) {
maker.setTemplate(printTemplateLine.getContent());
resultOfContent = maker.make();
}
} catch (Exception e) {
resultOfContent = "Error in content";
}
test.setContentResult(resultOfTitle + "<br>" + resultOfContent);
Boolean present = Boolean.TRUE;
if (StringUtils.notEmpty(printTemplateLine.getConditions())) {
Object evaluation = null;
try {
evaluation = templateContextService.computeTemplateContext(printTemplateLine.getConditions(), scriptContext);
} catch (Exception e) {
throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.PRINT_TEMPLATE_CONDITION_MUST_BE_BOOLEAN));
}
if (evaluation instanceof Boolean) {
present = (Boolean) evaluation;
}
}
test.setConditionsResult(present);
printTemplateLineRepo.save(printTemplateLine);
}
Aggregations