Package org.pentaho.reporting.libraries.formula

Examples of org.pentaho.reporting.libraries.formula.EvaluationException


                                final ParameterCallback parameters)
      throws EvaluationException
  {
    if (parameters.getParameterCount() != 3)
    {
      throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }

    final TypeRegistry typeRegistry = context.getTypeRegistry();
    final Date date1 = typeRegistry.convertToDate
        (parameters.getType(0), parameters.getValue(0));
    final Date date2 = typeRegistry.convertToDate
        (parameters.getType(1), parameters.getValue(1));
    final String formatCode = typeRegistry.convertToText
        (parameters.getType(2), parameters.getValue(2));

    if (date1 == null || date2 == null || formatCode == null || "".equals(
        formatCode))
    {
      throw new EvaluationException(
          LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
    }

    final LocalizationContext localizationContext = context.getLocalizationContext();
    final TimeZone timeZone = localizationContext.getTimeZone();
    final Locale locale = localizationContext.getLocale();
    final GregorianCalendar calandar1 =
        new GregorianCalendar(timeZone, locale);
    calandar1.setTime(date1);

    final GregorianCalendar calandar2 =
        new GregorianCalendar(timeZone, locale);
    calandar2.setTime(date2);

    int res;

    if (DateDifFunction.YEARS_CODE.equals(formatCode))
    {
      res = calandar2.get(Calendar.YEAR) - calandar1.get(Calendar.YEAR);
    }
    else if (DateDifFunction.MONTHS_CODE.equals(formatCode))
    {
      final int month1 = calandar1.get(Calendar.MONTH);
      final int month2 = calandar2.get(Calendar.MONTH);
      final int year1 = calandar1.get(Calendar.YEAR);
      final int year2 = calandar2.get(Calendar.YEAR);

      res = (year2 - year1) * 12 +
            month2 - month1;
    }
    else if (DateDifFunction.DAYS_IGNORING_MONTHS_YEARS.equals(formatCode))
    {
      // The number of days between Date1 and Date2, as if Date1 and
      // Date2 were in the same month and the same year.

      // Not sure what happens to leap years, so this solution may be invalid.
      calandar1.set(Calendar.YEAR, calandar2.get(Calendar.YEAR));
      calandar1.set(Calendar.MONTH, calandar2.get(Calendar.MONTH));

      res = calandar2.get(Calendar.DAY_OF_MONTH) -
            calandar1.get(Calendar.DAY_OF_MONTH);
    }
    else if (DateDifFunction.DAYS_CODE.equals(formatCode))
    {
      final int dayOfYear1 = calandar1.get(Calendar.DAY_OF_YEAR);
      final int dayOfYear2 = calandar2.get(Calendar.DAY_OF_YEAR);
      final int year1 = calandar1.get(Calendar.YEAR);
      final int year2 = calandar2.get(Calendar.YEAR);

      final GregorianCalendar workingCalandar =
          new GregorianCalendar(timeZone, locale);

      res = dayOfYear2 - dayOfYear1;

      // run through the inner years, without counting the border years
      // Always run from the lower to the higher, so that we prevent infinite
      // loops ..
      final int targetYear = Math.max(year1, year2);
      for (int i = Math.min(year1, year2); i < targetYear; i++)
      {
        workingCalandar.set(Calendar.YEAR, i);
        res += workingCalandar.getActualMaximum(Calendar.DAY_OF_YEAR);
      }
    }
    else if (DateDifFunction.MONTHS_IGNORING_YEARS.equals(formatCode))
    {
      final int month1 = calandar1.get(Calendar.MONTH);
      final int month2 = calandar2.get(Calendar.MONTH);

      res = month2 - month1;
    }
    else if (DateDifFunction.DAYS_IGNORING_YEARS.equals(formatCode))
    {
      //Isn't that a stupid case? How could we count the days while ignoring
      //how much days there are in each months without using the year?

      // The number of days between Date1 and Date2, as if Date1 and Date2
      // were in the same year.

      // Not sure what happens to leap years, so this solution may be invalid.
      calandar1.set(Calendar.YEAR, calandar2.get(Calendar.YEAR));
      final int dayOne = calandar1.get(Calendar.DAY_OF_YEAR);
      final int dayTwo = calandar2.get(Calendar.DAY_OF_YEAR);
      res = Math.abs(dayOne - dayTwo);
    }
    else
    {
      throw new EvaluationException(
          LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
    }

    if (res < 0)
    {
      throw new EvaluationException(
          LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
    }

    //noinspection UnpredictableBigDecimalConstructorCall
    return new TypeValuePair(NumberType.GENERIC_NUMBER, new BigDecimal((double) res));
View Full Code Here


  public TypeValuePair evaluate(final FormulaContext context, final ParameterCallback parameters) throws EvaluationException
  {
    final int parameterCount = parameters.getParameterCount();
    if (parameterCount < 1)
    {
      throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }

    final Type type1 = parameters.getType(0);
    final Object value1 = parameters.getValue(0);
    if(type1.isFlagSet(Type.TEXT_TYPE) || value1 instanceof String)
View Full Code Here

    BigDecimal computedResult = ZERO;
    final int parameterCount = parameters.getParameterCount();

    if (parameterCount == 0)
    {
      throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }

    final TypeRegistry typeRegistry = context.getTypeRegistry();
    for (int paramIdx = 0; paramIdx < parameterCount; paramIdx++)
    {
View Full Code Here

  public TypeValuePair evaluate(final FormulaContext context,
      final ParameterCallback parameters) throws EvaluationException
  {
    if (parameters.getParameterCount() != 3)
    {
      throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }

    final TypeRegistry typeRegistry = context.getTypeRegistry();

    final Number n1 = typeRegistry.convertToNumber(parameters.getType(0), parameters.getValue(0));
    final Number n2 = typeRegistry.convertToNumber(parameters.getType(1), parameters.getValue(1));
    final Number n3 = typeRegistry.convertToNumber(parameters.getType(2), parameters.getValue(2));

    if (n1 == null || n2 == null || n3 == null)
    {
      throw new EvaluationException(
          LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
    }

    final LocalizationContext localizationContext = context
        .getLocalizationContext();
View Full Code Here

                                final ParameterCallback parameters) throws EvaluationException
  {
    final int parameterCount = parameters.getParameterCount();
    if (parameterCount < 1 || parameterCount > 2)
    {
      throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }
    final TypeRegistry typeRegistry = context.getTypeRegistry();

    final Type textType = parameters.getType(0);
    final Object textValue = parameters.getValue(0);

    final String text = typeRegistry.convertToText(textType, textValue);
    final int length;
    if(parameterCount == 2)
    {
      final Number lengthVal = typeRegistry.convertToNumber(parameters.getType(1), parameters.getValue(1));
      if (lengthVal.doubleValue() < 0)
      {
        throw new EvaluationException(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
      }
      length = lengthVal.intValue();
    }
    else
    {
      length = 1;
    }

    if(text == null || length < 0)
    {
      throw new EvaluationException(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
    }

    int s = text.length()-length +1;
    if(s < 1)
    {
View Full Code Here

      final ParameterCallback parameters) throws EvaluationException
  {
    final int parameterCount = parameters.getParameterCount();
    if (parameterCount < 1)
    {
      throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }
    final Object value = parameters.getValue(0);

    final LValue raw = parameters.getRaw(0);
    if (raw instanceof ContextLookup)
View Full Code Here

  public TypeValuePair evaluate(final FormulaContext context,
      final ParameterCallback parameters) throws EvaluationException
  {
    if (parameters.getParameterCount() != 1)
    {
      throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }

    final TypeRegistry typeRegistry = context.getTypeRegistry();
    final Type type = parameters.getType(0);
    final Object value = parameters.getValue(0);
View Full Code Here

  public TypeValuePair evaluate(final FormulaContext context, final ParameterCallback parameters) throws EvaluationException
  {
    final int parameterCount = parameters.getParameterCount();
    if (parameterCount < 1)
    {
      throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }
    final Type type1 = parameters.getType(0);
    final Object value1 = parameters.getValue(0);
    final String result = context.getTypeRegistry().convertToText(type1, value1);

    if(result == null)
    {
      throw new EvaluationException(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
    }

    // remove all unnecessary spaces ..
    // we dont use regexps, because they are JDK 1.4, but this library is aimed
    // for JDK 1.2.2
View Full Code Here

      final ParameterCallback parameters) throws EvaluationException
  {
    final int parameterCount = parameters.getParameterCount();
    if (parameterCount < 1)
    {
      throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }

     final Type type1 = parameters.getType(0);
     final Object value1 = parameters.getValue(0);
     if(value1 instanceof Boolean || type1.isFlagSet(Type.LOGICAL_TYPE))
View Full Code Here

      while (sequence.hasNext())
      {
        final LValue rawValue = sequence.nextRawValue();
        if (rawValue == null)
        {
          throw new EvaluationException(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
        }
        final TypeValuePair nextValue = rawValue.evaluate();
        final Number number = typeRegistry.convertToNumber(nextValue.getType(), nextValue.getValue());
        final BigDecimal next = NumberUtil.getAsBigDecimal(number);
        if (last == null)
        {
          last = next;
        }
        else
        {
          if (last.compareTo(next) == -1)
          {
            last = next;
          }
        }
      }
    }
    if (last == null)
    {
      throw new EvaluationException(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
    }

    return new TypeValuePair(NumberType.GENERIC_NUMBER, last);
  }
View Full Code Here

TOP

Related Classes of org.pentaho.reporting.libraries.formula.EvaluationException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.