{
if (formulaError != null)
{
if (Boolean.TRUE.equals(failOnError))
{
throw new InvalidReportStateException(String.format
("Previously failed to evaluate formula-expression with error %s",// NON-NLS
formulaError));
}
return LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE;
}
try
{
if (compiledFormula == null)
{
compiledFormula = new Formula(formulaExpression);
}
final ExpressionRuntime expressionRuntime = getRuntime();
final ReportFormulaContext context =
new ReportFormulaContext(getFormulaContext(), expressionRuntime);
try
{
compiledFormula.initialize(context);
final Object evaluate = compiledFormula.evaluate();
if (Boolean.TRUE.equals(failOnError))
{
if (evaluate instanceof ErrorValue)
{
throw new InvalidReportStateException(String.format
("Failed to evaluate formula-expression with error %s",// NON-NLS
evaluate));
}
}
return evaluate;
}
finally
{
context.close();
}
}
catch (Exception e)
{
formulaError = e;
if (FormulaFunction.logger.isDebugEnabled())
{
final Configuration config = getReportConfiguration();
if ("true".equals(config.getConfigProperty(
"org.pentaho.reporting.engine.classic.core.function.LogFormulaFailureCause")))
{
FormulaFunction.logger.debug("Failed to compute the regular value [" + formulaExpression + ']', e);
}
else
{
FormulaFunction.logger.debug("Failed to compute the regular value [" + formulaExpression + ']');
}
}
if (Boolean.TRUE.equals(failOnError))
{
throw new InvalidReportStateException(String.format
("Failed to evaluate formula-function with error %s",// NON-NLS
e.getMessage()), e);
}
return LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE;
}