use of com.axelor.apps.base.db.UnitConversion 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