Search in sources :

Example 1 with PrintTemplateLineTest

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);
}
Also used : Context(com.axelor.rpc.Context) AxelorException(com.axelor.exception.AxelorException) MetaModel(com.axelor.meta.db.MetaModel) MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) PrintTemplateLineTest(com.axelor.apps.base.db.PrintTemplateLineTest) IOException(java.io.IOException) PrintTemplateLineService(com.axelor.apps.base.service.PrintTemplateLineService)

Example 2 with PrintTemplateLineTest

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);
}
Also used : Context(com.axelor.rpc.Context) Locale(java.util.Locale) AxelorException(com.axelor.exception.AxelorException) PrintTemplateLineTest(com.axelor.apps.base.db.PrintTemplateLineTest) AxelorException(com.axelor.exception.AxelorException) AppFilter(com.axelor.app.internal.AppFilter) Language(com.axelor.apps.base.db.Language) TemplateMaker(com.axelor.tool.template.TemplateMaker) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) Transactional(com.google.inject.persist.Transactional)

Aggregations

PrintTemplateLineTest (com.axelor.apps.base.db.PrintTemplateLineTest)2 AxelorException (com.axelor.exception.AxelorException)2 MetaModel (com.axelor.meta.db.MetaModel)2 Context (com.axelor.rpc.Context)2 AppFilter (com.axelor.app.internal.AppFilter)1 Language (com.axelor.apps.base.db.Language)1 PrintTemplateLineService (com.axelor.apps.base.service.PrintTemplateLineService)1 Model (com.axelor.db.Model)1 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)1 TemplateMaker (com.axelor.tool.template.TemplateMaker)1 Transactional (com.google.inject.persist.Transactional)1 IOException (java.io.IOException)1 Locale (java.util.Locale)1