org.pentaho.reporting.libraries.formula.typing.TypeRegistry Java Examples

The following examples show how to use org.pentaho.reporting.libraries.formula.typing.TypeRegistry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HourFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  if ( parameters.getParameterCount() != 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Number n = typeRegistry.convertToNumber( parameters.getType( 0 ), parameters.getValue( 0 ) );

  if ( n == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final BigDecimal bd = NumberUtil.getAsBigDecimal( n );
  final BigDecimal day = new BigDecimal( NumberUtil.performIntRounding( bd ).intValue() );
  final BigDecimal dayFraction = bd.subtract( day );

  final BigDecimal hourAndMinutesVal = dayFraction.multiply( HOUR_24 );
  final BigDecimal hours = NumberUtil.performIntRounding( hourAndMinutesVal );
  return new TypeValuePair( NumberType.GENERIC_NUMBER, hours );
}
 
Example #2
Source File: BeginsWithFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final Type textType2 = parameters.getType( 1 );
  final Object textValue2 = parameters.getValue( 1 );

  final String text = typeRegistry.convertToText( textType1, textValue1 );
  final String substring = typeRegistry.convertToText( textType2, textValue2 );

  return text.startsWith( substring ) ? RETURN_TRUE : RETURN_FALSE;
}
 
Example #3
Source File: LikeFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final Type patternType = parameters.getType( 1 );
  final Object patternValue = parameters.getValue( 1 );

  final String text = typeRegistry.convertToText( textType1, textValue1 );

  String regex = typeRegistry.convertToText( patternType, patternValue );

  // replace any * or % with .*
  regex = regex.replaceAll( "\\*", ".*" ).replaceAll( "%", ".*" );

  final Pattern p = Pattern.compile( regex );
  final Matcher m = p.matcher( text );

  return m.find() ? RETURN_TRUE : RETURN_FALSE;
}
 
Example #4
Source File: InFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters )
  throws EvaluationException {
  final int length = parameters.getParameterCount();
  if ( length < 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Object value1Raw = parameters.getValue( 0 );
  final Type type1 = parameters.getType( 0 );
  for ( int i = 1; i < parameters.getParameterCount(); i++ ) {
    final Object value2Raw = parameters.getValue( i );
    if ( value1Raw == null || value2Raw == null ) {
      throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_NA_VALUE );
    }

    final Type type2 = parameters.getType( i );
    final ExtendedComparator comparator = typeRegistry.getComparator( type1, type2 );
    final boolean result = comparator.isEqual( type1, value1Raw, type2, value2Raw );
    if ( result ) {
      return RETURN_TRUE;
    }
  }
  return RETURN_FALSE;
}
 
Example #5
Source File: EndsWithFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final Type textType2 = parameters.getType( 1 );
  final Object textValue2 = parameters.getValue( 1 );

  final String text = typeRegistry.convertToText( textType1, textValue1 );
  final String substring = typeRegistry.convertToText( textType2, textValue2 );

  return text.endsWith( substring ) ? RETURN_TRUE : RETURN_FALSE;
}
 
Example #6
Source File: EqualsFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters )
  throws EvaluationException {
  final int length = parameters.getParameterCount();
  if ( length != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Object value1Raw = parameters.getValue( 0 );
  final Object value2Raw = parameters.getValue( 1 );
  if ( value1Raw == null || value2Raw == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_NA_VALUE );
  }

  final Type type1 = parameters.getType( 0 );
  final Type type2 = parameters.getType( 1 );
  final ExtendedComparator comparator = typeRegistry.getComparator( type1, type2 );
  final boolean result = comparator.isEqual( type1, value1Raw, type2, value2Raw );
  if ( result ) {
    return RETURN_TRUE;
  } else {
    return RETURN_FALSE;
  }

}
 
Example #7
Source File: ContainsFunction.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters )
  throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 2 ) {
    throw new EvaluationException( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final Type textType2 = parameters.getType( 1 );
  final Object textValue2 = parameters.getValue( 1 );

  final String text = typeRegistry.convertToText( textType1, textValue1 );
  final String substring = typeRegistry.convertToText( textType2, textValue2 );

  return text.contains( substring ) ? RETURN_TRUE : RETURN_FALSE;
}
 
Example #8
Source File: InFunction.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters )
  throws EvaluationException {
  final int length = parameters.getParameterCount();
  if ( length < 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Object value1Raw = parameters.getValue( 0 );
  final Type type1 = parameters.getType( 0 );
  for ( int i = 1; i < parameters.getParameterCount(); i++ ) {
    final Object value2Raw = parameters.getValue( i );
    if ( value1Raw == null || value2Raw == null ) {
      throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_NA_VALUE );
    }

    final Type type2 = parameters.getType( i );
    final ExtendedComparator comparator = typeRegistry.getComparator( type1, type2 );
    final boolean result = comparator.isEqual( type1, value1Raw, type2, value2Raw );
    if ( result ) {
      return RETURN_TRUE;
    }
  }
  return RETURN_FALSE;
}
 
Example #9
Source File: LikeFunction.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters )
  throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 2 ) {
    throw new EvaluationException( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final Type textType2 = parameters.getType( 1 );
  final Object textValue2 = parameters.getValue( 1 );

  final String text = typeRegistry.convertToText( textType1, textValue1 );

  String regex = typeRegistry.convertToText( textType2, textValue2 );

  // replace any * or % with .*
  regex = regex.replaceAll( "\\*", ".*" ).replaceAll( "%", ".*" );

  Pattern p = Pattern.compile( regex );
  Matcher m = p.matcher( text );

  return m.find() ? RETURN_TRUE : RETURN_FALSE;
}
 
Example #10
Source File: ArrayMidFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 3 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Object textValue = parameters.getValue( 0 );
  final Type startType = parameters.getType( 1 );
  final Object startValue = parameters.getValue( 1 );
  final Type lengthType = parameters.getType( 2 );
  final Object lengthValue = parameters.getValue( 2 );

  final Sequence text = new RecursiveSequence( textValue, context );
  final Number start = typeRegistry.convertToNumber( startType, startValue );
  final Number length = typeRegistry.convertToNumber( lengthType, lengthValue );

  if ( length.doubleValue() < 0 || start.doubleValue() < 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  return new TypeValuePair( AnyType.ANY_ARRAY, process( text, start.intValue(), length.intValue() ) );
}
 
Example #11
Source File: BeginsWithFunction.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters )
  throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 2 ) {
    throw new EvaluationException( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final Type textType2 = parameters.getType( 1 );
  final Object textValue2 = parameters.getValue( 1 );

  final String text = typeRegistry.convertToText( textType1, textValue1 );
  final String substring = typeRegistry.convertToText( textType2, textValue2 );

  return text.startsWith( substring ) ? RETURN_TRUE : RETURN_FALSE;
}
 
Example #12
Source File: EqualsFunction.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters )
  throws EvaluationException {
  final int length = parameters.getParameterCount();
  if ( length != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Object value1Raw = parameters.getValue( 0 );
  final Object value2Raw = parameters.getValue( 1 );
  if ( value1Raw == null || value2Raw == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_NA_VALUE );
  }

  final Type type1 = parameters.getType( 0 );
  final Type type2 = parameters.getType( 1 );
  final ExtendedComparator comparator = typeRegistry.getComparator( type1, type2 );
  final boolean result = comparator.isEqual( type1, value1Raw, type2, value2Raw );
  if ( result ) {
    return RETURN_TRUE;
  } else {
    return RETURN_FALSE;
  }

}
 
Example #13
Source File: ContainsFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final Type textType2 = parameters.getType( 1 );
  final Object textValue2 = parameters.getValue( 1 );

  final String text = typeRegistry.convertToText( textType1, textValue1 );
  final String substring = typeRegistry.convertToText( textType2, textValue2 );

  return text.contains( substring ) ? RETURN_TRUE : RETURN_FALSE;
}
 
Example #14
Source File: NotEqualOperator.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final TypeValuePair value1, final TypeValuePair value2 )
  throws EvaluationException {
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final ExtendedComparator comparator =
    typeRegistry.getComparator( value1.getType(), value2.getType() );
  final boolean result = comparator.isEqual
    ( value1.getType(), value1.getValue(),
      value2.getType(), value2.getValue() );

  if ( result == false ) {
    return RETURN_TRUE;
  } else {
    return RETURN_FALSE;
  }
}
 
Example #15
Source File: AbstractCompareOperator.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public final TypeValuePair evaluate( final FormulaContext context,
                                     final TypeValuePair value1,
                                     final TypeValuePair value2 )
  throws EvaluationException {
  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Type type1 = value1.getType();
  final Type type2 = value2.getType();
  final Object value1Raw = value1.getValue();
  final Object value2Raw = value2.getValue();
  if ( value1Raw == null || value2Raw == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_NA_VALUE );
  }

  final ExtendedComparator comparator = typeRegistry.getComparator( type1, type2 );
  final int result = comparator.compare( type1, value1Raw, type2, value2Raw );
  if ( evaluate( result ) ) {
    return RETURN_TRUE;
  }
  return RETURN_FALSE;
}
 
Example #16
Source File: AscFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final Type type1 = parameters.getType( 0 );
  final Object value1 = parameters.getValue( 0 );
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final String result = typeRegistry.convertToText( type1, value1 );
  if ( result == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final char[] chars = result.toCharArray();
  final StringBuffer b = new StringBuffer( chars.length );
  for ( int i = 0; i < chars.length; i++ ) {
    final char c = chars[ i ];
    convert( c, b );
  }
  return new TypeValuePair( TextType.TYPE, b.toString() );
}
 
Example #17
Source File: PercentageOperator.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context, final TypeValuePair value1 )
  throws EvaluationException {
  final Object rawValue = value1.getValue();
  if ( rawValue == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_NA_VALUE );
  }

  final Type type = value1.getType();
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  if ( type.isFlagSet( Type.NUMERIC_TYPE ) == false &&
    type.isFlagSet( Type.ANY_TYPE ) == false ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  // return the same as zero minus value.
  final Number number = typeRegistry.convertToNumber( type, rawValue );
  final BigDecimal value = NumberUtil.getAsBigDecimal( number );
  final BigDecimal percentage = NumberUtil.divide( value, HUNDRED );
  return new TypeValuePair( NumberType.GENERIC_NUMBER, percentage );
}
 
Example #18
Source File: AbstractNumericOperator.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public final TypeValuePair evaluate( final FormulaContext context,
                                     final TypeValuePair value1,
                                     final TypeValuePair value2 )
  throws EvaluationException {
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  if ( value1 == null || value2 == null ) {
    // If this happens, then one of the implementations has messed up.
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE );
  }

  final Object raw1 = value1.getValue();
  final Object raw2 = value2.getValue();
  if ( raw1 == null || raw2 == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_NA_VALUE );
  }

  final Number number1 = convertToNumber( typeRegistry, value1.getType(), raw1, ZERO );
  final Number number2 = convertToNumber( typeRegistry, value2.getType(), raw2, ZERO );
  return new TypeValuePair( NumberType.GENERIC_NUMBER, evaluate( number1, number2 ) );
}
 
Example #19
Source File: EqualOperator.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final TypeValuePair value1,
                               final TypeValuePair value2 )
  throws EvaluationException {
  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Object value1Raw = value1.getValue();
  final Object value2Raw = value2.getValue();
  if ( value1Raw == null || value2Raw == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_NA_VALUE );
  }

  final Type type1 = value1.getType();
  final Type type2 = value2.getType();
  final ExtendedComparator comparator = typeRegistry.getComparator( type1, type2 );
  final boolean result = comparator.isEqual( type1, value1Raw, type2, value2Raw );
  if ( result ) {
    return RETURN_TRUE;
  } else {
    return RETURN_FALSE;
  }
}
 
Example #20
Source File: MidFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 3 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType = parameters.getType( 0 );
  final Object textValue = parameters.getValue( 0 );
  final Type startType = parameters.getType( 1 );
  final Object startValue = parameters.getValue( 1 );
  final Type lengthType = parameters.getType( 2 );
  final Object lengthValue = parameters.getValue( 2 );

  final String text = typeRegistry.convertToText( textType, textValue );
  final Number start = typeRegistry.convertToNumber( startType, startValue );
  final Number length = typeRegistry.convertToNumber( lengthType, lengthValue );

  if ( length.doubleValue() < 0 || start.doubleValue() < 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  return new TypeValuePair( TextType.TYPE, process( text, start.intValue(), length.intValue() ) );
}
 
Example #21
Source File: FormulaFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private TypeValuePair get( final int pos ) throws EvaluationException {
  final LValue parameter = function.parameters[ pos ];
  final Type paramType = function.metaData.getParameterType( pos );
  if ( parameter != null ) {
    final TypeValuePair result = parameter.evaluate();
    if ( result.getValue() == null ) {
      return result;
    }

    // lets do some type checking, right?
    final TypeRegistry typeRegistry = function.getContext().getTypeRegistry();
    final TypeValuePair converted = typeRegistry.convertTo( paramType, result );
    if ( converted == null ) {
      if ( logger.isDebugEnabled() ) {
        logger.debug( "Failed to evaluate parameter " + pos + " on function " + function );
      }
      throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_AUTO_ARGUMENT_VALUE );
    }
    return converted;
  } else {
    return new TypeValuePair( paramType, function.metaData.getDefaultValue( pos ) );
  }
}
 
Example #22
Source File: YearFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  if ( parameters.getParameterCount() != 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Date d = typeRegistry.convertToDate( parameters.getType( 0 ), parameters.getValue( 0 ) );

  if ( d == null ) {
    throw EvaluationException.getInstance(
      LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final Calendar gc = DateUtil.createCalendar( d, context.getLocalizationContext() );
  final int year = gc.get( Calendar.YEAR );
  //noinspection UnpredictableBigDecimalConstructorCall
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( (double) year ) );
}
 
Example #23
Source File: DateFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  if ( parameters.getParameterCount() != 3 ) {
    throw EvaluationException.getInstance( 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 EvaluationException.getInstance(
      LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final LocalizationContext localizationContext = context
    .getLocalizationContext();
  final java.sql.Date date = DateUtil.createDate( n1.intValue(),
    n2.intValue(), n3.intValue(), localizationContext );

  return new TypeValuePair( DateTimeType.DATE_TYPE, date );
}
 
Example #24
Source File: MonthFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  if ( parameters.getParameterCount() != 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Date d = typeRegistry.convertToDate( parameters.getType( 0 ), parameters.getValue( 0 ) );

  if ( d == null ) {
    throw EvaluationException.getInstance(
      LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final Calendar gc = DateUtil.createCalendar( d, context.getLocalizationContext() );

  final int month = gc.get( Calendar.MONTH ) + 1;
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( month ) );
}
 
Example #25
Source File: DayFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  if ( parameters.getParameterCount() != 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Date d = typeRegistry.convertToDate( parameters.getType( 0 ), parameters.getValue( 0 ) );
  if ( d == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final Calendar gc = DateUtil.createCalendar( d, context.getLocalizationContext() );

  final int dayOfMonth = gc.get( Calendar.DAY_OF_MONTH );
  //noinspection UnpredictableBigDecimalConstructorCall
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( (double) dayOfMonth ) );
}
 
Example #26
Source File: MonthEndFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  if ( parameters.getParameterCount() != 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Date d = typeRegistry.convertToDate( parameters.getType( 0 ), parameters.getValue( 0 ) );
  if ( d == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final LocalizationContext localizationContext = context.getLocalizationContext();
  final Date monthend = monthend( d, localizationContext );


  final Date date = DateUtil.normalizeDate( monthend, DateTimeType.DATE_TYPE );
  return new TypeValuePair( DateTimeType.DATE_TYPE, date );
}
 
Example #27
Source File: EndsWithFunction.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters )
  throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 2 ) {
    throw new EvaluationException( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Type textType1 = parameters.getType( 0 );
  final Object textValue1 = parameters.getValue( 0 );
  final Type textType2 = parameters.getType( 1 );
  final Object textValue2 = parameters.getValue( 1 );

  final String text = typeRegistry.convertToText( textType1, textValue1 );
  final String substring = typeRegistry.convertToText( textType2, textValue2 );

  return text.endsWith( substring ) ? RETURN_TRUE : RETURN_FALSE;
}
 
Example #28
Source File: DateDifFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected long computeDays( final ParameterCallback parameters,
                            final TypeRegistry typeRegistry ) throws EvaluationException {
  final Number date1 = typeRegistry.convertToNumber( parameters.getType( 0 ), parameters.getValue( 0 ) );
  final Number date2 = typeRegistry.convertToNumber( parameters.getType( 1 ), parameters.getValue( 1 ) );

  final BigDecimal dn1 = NumberUtil.performIntRounding( NumberUtil.getAsBigDecimal( date1 ) );
  final BigDecimal dn2 = NumberUtil.performIntRounding( NumberUtil.getAsBigDecimal( date2 ) );
  return dn2.longValue() - dn1.longValue();
}
 
Example #29
Source File: WeekDayFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  if ( parameters.getParameterCount() > 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();

  final Date d = typeRegistry.convertToDate( parameters.getType( 0 ), parameters.getValue( 0 ) );
  int type = 1; // default is Type 1
  if ( parameters.getParameterCount() == 2 ) {
    final Number n = typeRegistry.convertToNumber( parameters.getType( 1 ), parameters.getValue( 1 ) );
    if ( n == null ) {
      throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
    }
    type = n.intValue();
    if ( type < 1 || type > 3 ) {
      throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
    }
  }

  if ( d == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final Calendar gc = DateUtil.createCalendar( d, context.getLocalizationContext() );

  final int dayOfWeek = gc.get( Calendar.DAY_OF_WEEK );
  // in java Sunday = 1 (= Type 1 of openformula)
  final int result = convertType( dayOfWeek, type );
  //noinspection UnpredictableBigDecimalConstructorCall
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( (double) result ) );
}
 
Example #30
Source File: DrillDownFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context,
                               final ParameterCallback parameters ) throws EvaluationException {
  if ( parameters.getParameterCount() != 3 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  // 0: Configuration-indicator (gives the pattern indirectly) never the pattern itself
  // 1: the report-path
  // 2: the parameter as 2d-array (name value pairs)

  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final String configIndicator = typeRegistry.convertToText( parameters.getType( 0 ), parameters.getValue( 0 ) );
  String path;
  try {
    path = typeRegistry.convertToText( parameters.getType( 1 ), parameters.getValue( 1 ) );
  } catch ( EvaluationException ee ) {
    if ( LibFormulaErrorValue.ERROR_NA_VALUE.equals( ee.getErrorValue() ) ) {
      path = null;
    } else {
      throw ee;
    }
  }
  final ArrayCallback parameter = typeRegistry.convertToArray( parameters.getType( 2 ), parameters.getValue( 2 ) );

  final LinkCustomizer pattern = createLinkCustomizer( configIndicator );
  return new TypeValuePair( TextType.TYPE, pattern.format( context, configIndicator, path,
    computeParameterEntries( parameter, typeRegistry ) ) );
}