Search in sources :

Example 6 with TemplateMaker

use of com.axelor.tool.template.TemplateMaker in project axelor-open-suite by axelor.

the class STTest method test1.

@Test
public void test1() {
    TemplateMaker maker = new TemplateMaker("Europe/Paris", Locale.FRENCH, '$', '$');
    maker.setTemplate(content);
    maker.setContext(contact, map, "contact");
    String result = maker.make();
    Assert.assertNotNull(result);
    Assert.assertEquals(contentFinal, result);
}
Also used : TemplateMaker(com.axelor.tool.template.TemplateMaker) Test(org.junit.Test)

Example 7 with TemplateMaker

use of com.axelor.tool.template.TemplateMaker in project axelor-open-suite by axelor.

the class STTest method test2.

@Test
public void test2() {
    long start = System.currentTimeMillis();
    TemplateMaker maker = new TemplateMaker("Europe/Paris", Locale.FRENCH, '$', '$');
    for (int i = 0; i < 10000; i++) {
        maker.setTemplate(content);
        maker.setContext(contact, map, "contact");
        String result = maker.make();
        Assert.assertNotNull(result);
        Assert.assertEquals(contentFinal, result);
    }
    // Assert test total time < 15s
    Assert.assertTrue(((System.currentTimeMillis() - start) / 1000) < 15);
}
Also used : TemplateMaker(com.axelor.tool.template.TemplateMaker) Test(org.junit.Test)

Example 8 with TemplateMaker

use of com.axelor.tool.template.TemplateMaker in project axelor-open-suite by axelor.

the class TemplateService method processSubject.

public String processSubject(String timeZone, Template template, Model bean, String beanName, Map<String, Object> context) {
    TemplateMaker maker = new TemplateMaker(timeZone, AppFilter.getLocale(), '$', '$');
    maker.setTemplate(template.getSubject());
    maker.setContext(bean, context, beanName);
    return maker.make();
}
Also used : TemplateMaker(com.axelor.tool.template.TemplateMaker)

Example 9 with TemplateMaker

use of com.axelor.tool.template.TemplateMaker 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)

Example 10 with TemplateMaker

use of com.axelor.tool.template.TemplateMaker in project axelor-open-suite by axelor.

the class UnitConversionService method getCoefficient.

/**
 * Get the conversion coefficient between two units from a conversion list. If the start unit and
 * the end unit can not be found in the list, then the units are swapped. If there still isn't any
 * result, an Exception is thrown.
 *
 * @param unitConversionList A list of conversions between units
 * @param startUnit The start unit
 * @param endUnit The end unit
 * @param product Optional, a product used for complex conversions. INput null if needless.
 * @return A conversion coefficient to convert from startUnit to endUnit.
 * @throws AxelorException The required units are not found in the conversion list.
 * @throws CompilationFailedException
 * @throws ClassNotFoundException
 * @throws IOException
 */
public BigDecimal getCoefficient(List<? extends UnitConversion> unitConversionList, Unit startUnit, Unit endUnit, Product product) throws AxelorException, CompilationFailedException, ClassNotFoundException, IOException {
    /* Looking for the start unit and the end unit in the unitConversionList to get the coefficient */
    if (product != null) {
        this.maker = new TemplateMaker(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null) != null ? Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).map(Company::getTimezone).orElse(null) : "", AppFilter.getLocale(), TEMPLATE_DELIMITER, TEMPLATE_DELIMITER);
        this.maker.setContext(product, "Product");
    }
    String eval = null;
    for (UnitConversion unitConversion : unitConversionList) {
        if (unitConversion.getStartUnit().equals(startUnit) && unitConversion.getEndUnit().equals(endUnit)) {
            if (unitConversion.getTypeSelect() == UnitConversionRepository.TYPE_COEFF) {
                return unitConversion.getCoef();
            } else if (product != null) {
                maker.setTemplate(unitConversion.getFormula());
                eval = maker.make();
                CompilerConfiguration conf = new CompilerConfiguration();
                ImportCustomizer customizer = new ImportCustomizer();
                customizer.addStaticStars("java.lang.Math");
                conf.addCompilationCustomizers(customizer);
                Binding binding = new Binding();
                GroovyShell shell = new GroovyShell(binding, conf);
                return new BigDecimal(shell.evaluate(eval).toString());
            }
        }
        if (unitConversion.getStartUnit().equals(endUnit) && unitConversion.getEndUnit().equals(startUnit)) {
            if (unitConversion.getTypeSelect() == UnitConversionRepository.TYPE_COEFF && unitConversion.getCoef().compareTo(BigDecimal.ZERO) != 0) {
                return BigDecimal.ONE.divide(unitConversion.getCoef(), DEFAULT_COEFFICIENT_SCALE, RoundingMode.HALF_UP);
            } else if (product != null) {
                maker.setTemplate(unitConversion.getFormula());
                eval = maker.make();
                CompilerConfiguration conf = new CompilerConfiguration();
                ImportCustomizer customizer = new ImportCustomizer();
                customizer.addStaticStars("java.lang.Math");
                conf.addCompilationCustomizers(customizer);
                Binding binding = new Binding();
                GroovyShell shell = new GroovyShell(binding, conf);
                BigDecimal result = new BigDecimal(shell.evaluate(eval).toString());
                if (result.compareTo(BigDecimal.ZERO) != 0) {
                    return BigDecimal.ONE.divide(result, DEFAULT_COEFFICIENT_SCALE, RoundingMode.HALF_UP);
                }
            }
        }
    }
    /* If there is no startUnit and endUnit in the UnitConversion list so we throw an exception */
    throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.UNIT_CONVERSION_1), startUnit.getName(), endUnit.getName());
}
Also used : Binding(groovy.lang.Binding) AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) UnitConversion(com.axelor.apps.base.db.UnitConversion) TemplateMaker(com.axelor.tool.template.TemplateMaker) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) GroovyShell(groovy.lang.GroovyShell) BigDecimal(java.math.BigDecimal)

Aggregations

TemplateMaker (com.axelor.tool.template.TemplateMaker)11 Model (com.axelor.db.Model)4 AxelorException (com.axelor.exception.AxelorException)4 MetaModel (com.axelor.meta.db.MetaModel)4 Company (com.axelor.apps.base.db.Company)3 Transactional (com.google.inject.persist.Transactional)3 AppFilter (com.axelor.app.internal.AppFilter)2 Language (com.axelor.apps.base.db.Language)2 Context (com.axelor.rpc.Context)2 Binding (groovy.lang.Binding)2 GroovyShell (groovy.lang.GroovyShell)2 BigDecimal (java.math.BigDecimal)2 Locale (java.util.Locale)2 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)2 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)2 Test (org.junit.Test)2 Print (com.axelor.apps.base.db.Print)1 PrintTemplate (com.axelor.apps.base.db.PrintTemplate)1 PrintTemplateLineTest (com.axelor.apps.base.db.PrintTemplateLineTest)1 UnitConversion (com.axelor.apps.base.db.UnitConversion)1