org.pentaho.reporting.libraries.formula.FormulaContext Java Examples

The following examples show how to use org.pentaho.reporting.libraries.formula.FormulaContext. 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: ChooseFunction.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() <= 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final Type indexType = parameters.getType( 0 );
  final Object indexValue = parameters.getValue( 0 );

  final int index = context.getTypeRegistry().convertToNumber( indexType, indexValue ).intValue();
  if ( index < 1 || index >= parameters.getParameterCount() ) {
    // else
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  return new TypeValuePair( parameters.getType( index ), parameters.getValue( index ) );
}
 
Example #2
Source File: AndFunction.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 < 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  for ( int i = 0; i < length; i++ ) {
    final Object value = parameters.getValue( i );
    final Type type1 = parameters.getType( i );
    final Boolean condition = context.getTypeRegistry().convertToLogical( type1, value );
    if ( condition == null ) {
      throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
    }
    if ( Boolean.FALSE.equals( condition ) ) {
      return RETURN_FALSE;
    }
  }
  return RETURN_TRUE;
}
 
Example #3
Source File: MessageFunction.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 String message = context.getTypeRegistry().convertToText( type1, value1 );

  final MessageFormat format = new MessageFormat( message, context.getLocalizationContext().getLocale() );
  final Object[] args = new Object[ parameterCount - 1 ];
  for ( int i = 1; i < parameterCount; i += 1 ) {
    args[ i - 1 ] = parameters.getValue( i );
  }
  return new TypeValuePair( TextType.TYPE, format.format( args ) );
}
 
Example #4
Source File: PowerFunction.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 Type type1 = parameters.getType( 0 );
  final Object value1 = parameters.getValue( 0 );
  final Type type2 = parameters.getType( 1 );
  final Object value2 = parameters.getValue( 1 );

  final Number result1 = context.getTypeRegistry().convertToNumber( type1, value1 );
  final Number result2 = context.getTypeRegistry().convertToNumber( type2, value2 );
  if ( result1 == null || result2 == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }


  final double power = Math.pow( result1.doubleValue(), result2.doubleValue() );
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( power ) );
}
 
Example #5
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 #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: 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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
Source File: PatternLinkCustomizerTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Before
public void setup() {
  formulaContext = mock( FormulaContext.class );
  config = mock( Configuration.class );
  profileMeta = mock( DrillDownProfileMetaData.class );
  profile = mock( DrillDownProfile.class );
  localContext = mock( LocalizationContext.class );
  PowerMockito.mockStatic( DrillDownProfileMetaData.class );
  when( DrillDownProfileMetaData.getInstance() ).thenReturn( profileMeta );
  when( profileMeta.getDrillDownProfile( "generic-url" ) ).thenReturn( profile );
  when( formulaContext.getConfiguration() ).thenReturn( config );
  when( formulaContext.getLocalizationContext() ).thenReturn( localContext );
  when( localContext.getLocale() ).thenReturn( Locale.ENGLISH );
  when( config.getConfigProperty( "org.pentaho.reporting.libraries.formula.URLEncoding", "UTF-8" ) ).thenReturn( "UTF-8" );
  Whitebox.setInternalState( profile, "attributes", getProfileAttributes() );
  when( profile.getAttribute( anyString() ) ).thenCallRealMethod();
}
 
Example #14
Source File: UrlParameterSeparatorFunction.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() == 0 ) {
    return new TypeValuePair( TextType.TYPE, "?" );
  }
  final String text = context.getTypeRegistry().convertToText( parameters.getType( 0 ), parameters.getValue( 0 ) );
  if ( text == null ) {
    return new TypeValuePair( TextType.TYPE, "?" );
  }
  if ( text.indexOf( '?' ) == -1 ) {
    return new TypeValuePair( TextType.TYPE, "?" );
  }

  if ( text.endsWith( "?" ) ) {
    return new TypeValuePair( TextType.TYPE, text );
  }

  return new TypeValuePair( TextType.TYPE, text + "&" );
}
 
Example #15
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 #16
Source File: Atan2Function.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( FormulaContext context, ParameterCallback parameters ) throws EvaluationException {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final Type type1 = parameters.getType( 0 );
  final Object value1 = parameters.getValue( 0 );
  final Number result = context.getTypeRegistry().convertToNumber( type1, value1 );
  if ( result == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }
  final Type type2 = parameters.getType( 0 );
  final Object value2 = parameters.getValue( 0 );
  final Number result2 = context.getTypeRegistry().convertToNumber( type2, value2 );
  if ( result2 == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }
  final double d1 = result.doubleValue();
  final double d2 = result2.doubleValue();
  if ( d1 == 0 || d2 == 0 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( Math.atan2( d1, d2 ) ) );
}
 
Example #17
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 #18
Source File: CharFunction.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 Number result = context.getTypeRegistry().convertToNumber( type1, value1 );
  if ( result == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }
  if ( result.intValue() > 255 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  return new TypeValuePair( TextType.TYPE, String.valueOf( ( (char) result.intValue() ) ) );
}
 
Example #19
Source File: AsinFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public TypeValuePair evaluate( FormulaContext context, 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 Number result = context.getTypeRegistry().convertToNumber( type1, value1 );
  if ( result == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }
  final double d = result.doubleValue();
  if ( d < -1.0 || d > 1.0 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( Math.asin( d ) ) );
}
 
Example #20
Source File: IntFunction.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 Type type1 = parameters.getType( 0 );
  final Object value1 = parameters.getValue( 0 );
  final Number result = context.getTypeRegistry().convertToNumber( type1, value1 );
  if ( result == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final Number ret = NumberUtil.performIntRounding( NumberUtil.getAsBigDecimal( result ) );
  return new TypeValuePair( NumberType.GENERIC_NUMBER, ret );
}
 
Example #21
Source File: IsExportTypeFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Return Boolean.TRUE, if the specified export type matches the used export type, Boolean.FALSE otherwise.
 *
 * @param context
 *          the formula context, which allows access to the runtime.
 * @param parameters
 *          the parameter callback is used to retrieve parameter values.
 * @return the computed result wrapped in a TypeValuePair.
 * @throws EvaluationException
 *           if an error occurs.
 */
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters )
  throws EvaluationException {
  if ( context instanceof ReportFormulaContext == false ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE );
  }
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount < 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final Object value = parameters.getValue( 0 );
  final ReportFormulaContext rfc = (ReportFormulaContext) context;
  if ( value != null && rfc.getExportType().startsWith( String.valueOf( value ) ) ) {
    return new TypeValuePair( LogicalType.TYPE, Boolean.TRUE );
  }

  return new TypeValuePair( LogicalType.TYPE, Boolean.FALSE );
}
 
Example #22
Source File: IsTextFunction.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 {
  try {
    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 );
    if ( type1.isFlagSet( Type.TEXT_TYPE ) || value1 instanceof String ) {
      return RETURN_TRUE;
    }

    return RETURN_FALSE;
  } catch ( EvaluationException e ) {
    return RETURN_FALSE;
  }
}
 
Example #23
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 #24
Source File: LowerFunction.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 String result = context.getTypeRegistry().convertToText( type1, value1 );

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

  return new TypeValuePair( TextType.TYPE, result.toLowerCase( context.getLocalizationContext().getLocale() ) );
}
 
Example #25
Source File: UpperFunction.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 String result = context.getTypeRegistry().convertToText( type1, value1 );

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

  return new TypeValuePair( TextType.TYPE, result.toUpperCase( context.getLocalizationContext().getLocale() ) );
}
 
Example #26
Source File: RowCountFunction.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 ReportFormulaContext rfc = (ReportFormulaContext) context;
  final int groupStart;
  if ( parameterCount == 0 ) {
    groupStart = rfc.getRuntime().getGroupStartRow( -1 );
  } else {
    final String groupName =
        context.getTypeRegistry().convertToText( parameters.getType( 0 ), parameters.getValue( 0 ) );
    groupStart = rfc.getRuntime().getGroupStartRow( groupName );
  }
  final int row = rfc.getRuntime().getCurrentRow();
  // noinspection UnpredictableBigDecimalConstructorCall
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( (double) ( row - groupStart ) ) );
}
 
Example #27
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 #28
Source File: IsNonTextFunction.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 {
  final TypeValuePair typeValuePair = super.evaluate( context, parameters );
  if ( typeValuePair.getValue().equals( Boolean.TRUE ) ) {
    return RETURN_FALSE;
  } else {
    return RETURN_TRUE;
  }
}
 
Example #29
Source File: DateConversionTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testDateConversion1904() {
  final FormulaContext context = getContext();
  final Date januaryFirst1904 = DateUtil.createDate( 1904, 1, 1, context.getLocalizationContext() );
  final Date januaryFirst1900 = DateUtil.createDate( 1900, 1, 1, context.getLocalizationContext() );
  final Date marchFirst1904 = DateUtil.createDate( 1904, 3, 1, context.getLocalizationContext() );
  final Date marchFirst1900 = DateUtil.createDate( 1900, 3, 1, context.getLocalizationContext() );

  // these numbers must match whatever OpenOffice computes ..
  assertEqual( HSSFDateUtil.getExcelDate( januaryFirst1900, false, HSSFDateUtil.computeZeroDate( "1904", false ) ),
    new BigDecimal( -1460 ) );
  assertEqual( HSSFDateUtil.getExcelDate( marchFirst1900, false, HSSFDateUtil.computeZeroDate( "1904", false ) ),
    new BigDecimal( -1401 ) );
  assertEqual( HSSFDateUtil.getExcelDate( januaryFirst1904, false, HSSFDateUtil.computeZeroDate( "1904", false ) ),
    new BigDecimal( 0 ) );
  assertEqual( HSSFDateUtil.getExcelDate( marchFirst1904, false, HSSFDateUtil.computeZeroDate( "1904", false ) ),
    new BigDecimal( 60 ) );

  assertEqual( HSSFDateUtil.getExcelDate( januaryFirst1900, true, HSSFDateUtil.computeZeroDate( "1904", true ) ),
    new BigDecimal( -1461 ) );
  assertEqual( HSSFDateUtil.getExcelDate( marchFirst1900, true, HSSFDateUtil.computeZeroDate( "1904", true ) ),
    new BigDecimal( -1401 ) );
  assertEqual( HSSFDateUtil.getExcelDate( januaryFirst1904, true, HSSFDateUtil.computeZeroDate( "1904", true ) ),
    new BigDecimal( 0 ) );
  assertEqual( HSSFDateUtil.getExcelDate( marchFirst1904, true, HSSFDateUtil.computeZeroDate( "1904", true ) ),
    new BigDecimal( 60 ) );

}
 
Example #30
Source File: URLEncodeFunction.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 {
  final int parameterCount = parameters.getParameterCount();
  if ( parameterCount < 1 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }
  final Type textType = parameters.getType( 0 );
  final Object textValue = parameters.getValue( 0 );
  final String textResult =
    context.getTypeRegistry().convertToText( textType, textValue );

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

  final String encodingResult;
  if ( parameterCount == 2 ) {
    final Type encodingType = parameters.getType( 1 );
    final Object encodingValue = parameters.getValue( 1 );
    encodingResult = context.getTypeRegistry().convertToText( encodingType, encodingValue );
    if ( encodingResult == null ) {
      throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
    }
  } else {
    encodingResult = context.getConfiguration().getConfigProperty
      ( "org.pentaho.reporting.libraries.formula.URLEncoding", "UTF-8" );
  }
  try {
    return new TypeValuePair
      ( TextType.TYPE, URLEncoder.encode( textResult, encodingResult ) );

  } catch ( final UnsupportedEncodingException use ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }
}