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