org.eclipse.birt.core.data.DataTypeUtil Java Examples

The following examples show how to use org.eclipse.birt.core.data.DataTypeUtil. 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: TemplateMessageServiceBaseImpl.java    From axelor-open-suite with GNU Affero General Public License v3.0 6 votes vote down vote up
private Object convertValue(String type, String value) throws BirtException {

    if (DesignChoiceConstants.PARAM_TYPE_BOOLEAN.equals(type)) {
      return DataTypeUtil.toBoolean(value);
    } else if (DesignChoiceConstants.PARAM_TYPE_DATETIME.equals(type)) {
      return DataTypeUtil.toDate(value);
    } else if (DesignChoiceConstants.PARAM_TYPE_DATE.equals(type)) {
      return DataTypeUtil.toSqlDate(value);
    } else if (DesignChoiceConstants.PARAM_TYPE_TIME.equals(type)) {
      return DataTypeUtil.toSqlTime(value);
    } else if (DesignChoiceConstants.PARAM_TYPE_DECIMAL.equals(type)) {
      return DataTypeUtil.toBigDecimal(value);
    } else if (DesignChoiceConstants.PARAM_TYPE_FLOAT.equals(type)) {
      return DataTypeUtil.toDouble(value);
    } else if (DesignChoiceConstants.PARAM_TYPE_STRING.equals(type)) {
      return DataTypeUtil.toLocaleNeutralString(value);
    } else if (DesignChoiceConstants.PARAM_TYPE_INTEGER.equals(type)) {
      return DataTypeUtil.toInteger(value);
    }
    return value;
  }
 
Example #2
Source File: NAccumulator.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
protected double populateNValue( Object N ) throws DataException
{
	double result = 0;
	try
	{
		result = DataTypeUtil.toInteger( N ).intValue( );
	}
	catch ( BirtException e )
	{
		// conversion error
		throw new DataException(ResourceConstants.INVALID_TOP_BOTTOM_ARGUMENT, e);
	}
	if( result < 0 )
		throw new DataException(ResourceConstants.INVALID_TOP_BOTTOM_N_ARGUMENT);
	return result;
}
 
Example #3
Source File: ReportItemExecutor.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
protected Boolean evaluateBoolean( Expression expr )
{
	try
	{
		Object value = context.evaluate( expr );
		return DataTypeUtil.toBoolean( value );
	}
	catch ( BirtException ex )
	{
		getLogger( ).log(
				Level.WARNING,
				"Invalid boolean expression:"
						+ ( expr == null ? "null" : expr.toString( ) ) );
	}
	return null;
}
 
Example #4
Source File: SelectValueDialog.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Return expression string value as expression required format. For example
 * number type: Integer value 1 to String value "1" Boolean type: Boolean
 * value true to String value "true" other types: String value "abc" to
 * String value "\"abc\"" Date value "2000-10-10" to String value
 * "\"2000-10-10\""
 * 
 * @return expression value
 */
public String getSelectedExprValue( IExpressionConverter convert )
{
	String exprValue = null;
	if ( selectedIndices != null && selectedIndices.length > 0 )
	{
		Object modelValue = selectedItems[0];
		String dataType = null;
		if ( !( modelValue instanceof NullValue ) )
		{
			dataType = DataSetUIUtil.toModelDataType( DataTypeUtil.toApiDataType( modelValue.getClass( ) ) );
			String viewerValue = getDataText( modelValue );
			if ( convert != null )
				exprValue = convert.getConstantExpression( viewerValue,
						dataType );
		}
	}
	return exprValue;
}
 
Example #5
Source File: BirtDuration.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
protected Object getValue( Object[] args ) throws BirtException
{
	Duration duration;
	Date date;
	try
	{
		duration = DatatypeFactory.newInstance( )
				.newDuration( args[0].toString( ) );
		date = DataTypeUtil.toDate( args[1] );
	}
	catch ( DatatypeConfigurationException e )
	{
		throw new IllegalArgumentException( Messages.getFormattedString( "error.BirtDuration.literal.invalidArgument",
				args ) );
	}
	duration.addTo( date );
	return date;
}
 
Example #6
Source File: BirtDuration.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
protected Object getValue( Object[] args ) throws BirtException
{
	Duration duration;
	int factor;
	try
	{
		duration = DatatypeFactory.newInstance( )
				.newDuration( args[0].toString( ) );

		factor = DataTypeUtil.toInteger( args[1] ).intValue( );
	}
	catch ( DatatypeConfigurationException e )
	{
		throw new IllegalArgumentException( Messages.getFormattedString( "error.BirtDuration.literal.invalidArgument",
				args ) );
	}
	return duration.multiply( factor ).toString( );
}
 
Example #7
Source File: ParameterValidationUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Validates the input value at the given locale. The format is: short date
 * and medium time.
 * 
 * @param value
 *            the value to validate
 * @param locale
 *            the locale information
 * @param timeZone
 *            the time zone information
 * @return the date value if validation is successful
 * @throws ValidationValueException
 *             if the value is invalid
 */

private static final Date doValidateDateTime( String value, ULocale locale,
		TimeZone timeZone ) throws ValidationValueException
{
	try
	{
		return DataTypeUtil.toDate( value, locale, timeZone );
	}
	catch ( BirtException e )
	{
		throw new ValidationValueException( value,
				PropertyValueException.DESIGN_EXCEPTION_INVALID_VALUE,
				DesignChoiceConstants.PARAM_TYPE_DATETIME );
	}
}
 
Example #8
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
protected Object getValue( Object[] args, IScriptFunctionContext context ) throws BirtException
{
	if ( existNullValue( args ) )
	{
		return null;
	}
	Calendar current;
	if ( args[0] instanceof Number )
	{
		current = getFiscalYearStateDate( context, args );
		// Week starts with 1
		current.add( Calendar.WEEK_OF_YEAR,
				( (Number) args[0] ).intValue( ) - 1 );
	}
	else
	{
		current = getCalendar( DataTypeUtil.toDate( args[0] ) );
	}
	current.set( Calendar.DAY_OF_WEEK, current.getFirstDayOfWeek( ) );
	return current.getTime( );
}
 
Example #9
Source File: PreparedDummyQuery.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public Object getValue( String name ) throws BirtException
{
	checkOpened( );

	if ( exprManager.getExpr( name ) == null )
		throw new DataException( ResourceConstants.INVALID_BOUND_COLUMN_NAME,
				name );

	Object o = exprValueMap.get( name );
	IBinding b = exprManager.getBinding( name );
	if ( o != null && b != null )
	{
		o = DataTypeUtil.convert( o, b.getDataType( ) );
	}
	return o;
}
 
Example #10
Source File: DataUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public static <T> T convertType( Object tempValue, Class<T> type )
		throws BirtException
{
	T value = null;
	if ( tempValue == null || type.isAssignableFrom( tempValue.getClass( ) ) )
	{
		value = (T) tempValue;
	}
	else if ( type == DimensionType.class )
	{
		// todo
	}
	else
	{
		value = (T) DataTypeUtil.convert( tempValue, type );
	}
	return value;
}
 
Example #11
Source File: NumericGroupCalculator.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public Object calculate( Object value ) throws BirtException
{
	double dValue = -1;
	if( value!= null )
		dValue = ( DataTypeUtil.toDouble( value ) ).doubleValue( );

	if ( dValue < doubleStartValue )
	{
		if( this.firstValue == Double.MIN_VALUE )
		{
			this.firstValue = dValue;
		}
		return new Double( this.firstValue );
	}
	else
	{
		return new Double( doubleStartValue + Math.floor( ( dValue - doubleStartValue )
				/ intervalRange ) * intervalRange );
	}
}
 
Example #12
Source File: TotalConcatenate.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public void onRow( Object[] args ) throws DataException
{
	assert ( args != null && args.length >= 1 );

	try
	{
		if ( !isInitialized )
		{
			isInitialized = true;
			setSeparator( args.length >= 2 ? args[1] : null );
			setMaxLength( args.length >= 3 ? args[2]
					: DEFAULT_MAX_LENGTH );
			setShowAllValues( args.length == 4 ? args[3] : false );
		}
		if ( args[0] != null )
		{
			values.add( DataTypeUtil.toString( args[0] ) );
		}
	}
	catch ( BirtException e )
	{
		throw DataException.wrap( e );
	}
}
 
Example #13
Source File: DataSetIterator.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public Object getValue( int fieldIndex ) throws BirtException
{
	Object value = it.getValue( this.metadata.getFieldName( fieldIndex ) );
	if ( value == null )
	{
		return this.metadata.getNullValueReplacer( fieldIndex );
	} 
	if( byte[].class.equals( value.getClass( ) ) )
	{
		return value;
	}
	
	Object convertedValue = this.metadata.getDataProcessor( fieldIndex )
			.process( value );
	
	return convertedValue.getClass().isAssignableFrom( value.getClass()) ? convertedValue : DataTypeUtil.convert( convertedValue,
			value.getClass( ) );
}
 
Example #14
Source File: SecuredHierarchy.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 
 * @param fieldType
 * @return
 */
private Object createNullValueReplacer( Object o )
{
	switch ( DataTypeUtil.toApiDataType( o.getClass( ) ) )
	{
		case DataType.DATE_TYPE :
			return new java.util.Date( nullTime );
		case DataType.SQL_DATE_TYPE :
			return new java.sql.Date( nullTime );
		case DataType.SQL_TIME_TYPE :
			return new Time( nullTime );
		case DataType.BOOLEAN_TYPE :
			return Boolean.FALSE;
		case DataType.DECIMAL_TYPE :
			return new BigDecimal( 0 );
		case DataType.DOUBLE_TYPE :
			return new Double( 0 );
		case DataType.INTEGER_TYPE :
			return Integer.valueOf( 0 );
		case DataType.STRING_TYPE :
			return "";
		default :
			return "";
	}
}
 
Example #15
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected Object getValue( Object[] args ) throws BirtException
{
	if( existNullValue( args ) )
	{
		return null;
	}
	return addMinute(DataTypeUtil.toDate(args[0]), ((Number)args[1]).intValue( ));
}
 
Example #16
Source File: ResultObjectUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static Object convert( Object o, int type ) throws DataException
{
	try
	{
		return DataTypeUtil.convert( o, type );
	}
	catch ( BirtException e )
	{
		throw DataException.wrap( e );
	}
}
 
Example #17
Source File: DataUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Try convert an object to given type Types supported:
 * <p>
 * <ul>
 * <li>IScalarParameterDefn.TYPE_INTEGER</li>
 * <li>IScalarParameterDefn.TYPE_DECIMAL</li>
 * <li>IScalarParameterDefn.TYPE_BOOLEAN</li>
 * <li>IScalarParameterDefn.TYPE_DATE_TIME</li>
 * <li>IScalarParameterDefn.TYPE_FLOAT</li>
 * <li>IScalarParameterDefn.TYPE_STRING</li>
 * <li>IScalarParameterDefn.TYPE_DATE</li>
 * <li>IScalarParameterDefn.TYPE_TIME</li>
 * <ul>
 * </p>
 * 
 * @param source
 * @param toType
 * @return
 * @throws BirtException
 */
public static Object convert( Object source, int toType )
		throws BirtException
{
	if ( source == null )
		return null;

	// if any type, return directly.
	if ( toType == IScalarParameterDefn.TYPE_ANY )
		return source;

	switch ( toType )
	{
		case IScalarParameterDefn.TYPE_INTEGER :
			return DataTypeUtil.toInteger( source );
		case IScalarParameterDefn.TYPE_DECIMAL :
			return DataTypeUtil.toBigDecimal( source );
		case IScalarParameterDefn.TYPE_BOOLEAN :
			return DataTypeUtil.toBoolean( source );
		case IScalarParameterDefn.TYPE_DATE_TIME :
			return DataTypeUtil.toDate( source );
		case IScalarParameterDefn.TYPE_FLOAT :
			return DataTypeUtil.toDouble( source );
		case IScalarParameterDefn.TYPE_STRING :
			return DataTypeUtil.toString( source );
		case IScalarParameterDefn.TYPE_DATE :
			return DataTypeUtil.toSqlDate( source );
		case IScalarParameterDefn.TYPE_TIME :
			return DataTypeUtil.toSqlTime( source );
		default :
			throw new CoreException( "Invalid type." ); //$NON-NLS-1$
	}
}
 
Example #18
Source File: ParameterSelectionChoice.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * set parameter choice value. The string value is in English locale, and needs to be parsed
 * back into object value based on the data type. 
 * 
 * @param value the string value for the object
 * @param type the parameter data type
 */
public void setValue(String value, int type) {
	try {
		switch (type) {
			case IScalarParameterDefn.TYPE_BOOLEAN:
				this.value = DataTypeUtil.toBoolean(value);
				break;
			case IScalarParameterDefn.TYPE_DATE_TIME:
				this.value = DataTypeUtil.toDate(value);
				break;
			case IScalarParameterDefn.TYPE_DECIMAL:
				this.value = DataTypeUtil.toBigDecimal(value);
				break;
			case IScalarParameterDefn.TYPE_FLOAT:
				this.value = DataTypeUtil.toDouble(value);
				break;
			case IScalarParameterDefn.TYPE_INTEGER:
				this.value = DataTypeUtil.toInteger( value );
				break;
			case IScalarParameterDefn.TYPE_DATE:
				this.value = DataTypeUtil.toSqlDate( value );
				break;
			case IScalarParameterDefn.TYPE_TIME:
				this.value = DataTypeUtil.toSqlTime( value );
				break;
			case IScalarParameterDefn.TYPE_STRING:
			default:
				this.value = DataTypeUtil.toString(value);
				break;
		}
	} 
	catch (BirtException e) {
		log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		this.value = null;
	}
}
 
Example #19
Source File: BirtComp.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static int compareAsString( Object obj1, Object obj2, Collator comp )
		throws BirtException
{
	return comp == null ? DataTypeUtil.toString( obj1 )
			.compareTo( DataTypeUtil.toString( obj2 ) )
			: comp.compare( DataTypeUtil.toString( obj1 ),
					DataTypeUtil.toString( obj2 ) );
}
 
Example #20
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static Calendar getFiscalYearStateDate(
		IScriptFunctionContext context, Object[] args ) throws BirtException
{
	if ( args.length > 1 )
	{
		return getCalendar( DataTypeUtil.toDate( args[1] ) );
	}
	return getDefaultFiscalYearStartDate( context );
}
 
Example #21
Source File: FunctionProviderBaseImpl.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean extractBoolean( String strValue, boolean ifNull )
		throws BirtException
{
	boolean booleanValue = strValue == null ? ifNull
			: DataTypeUtil.toBoolean( strValue );
	return booleanValue;
}
 
Example #22
Source File: BirtComp.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Compare 2 object of String type by the given condition
 * 
 * @param obj1
 * @param obj2
 * @param ignoreCase
 * @param trimed
 * @return
 * @throws BirtException
 */
private static int compareString( Object obj1, Object obj2,
		boolean ignoreCase, boolean trimed ) throws BirtException
{
	if ( obj1 == null && obj2 == null )
	{
		return 0;
	}
	if ( obj1 == null )
	{
		return -1;
	}
	if ( obj2 == null )
	{
		return 1;
	}		
	if ( !( obj1 instanceof String ) || !( obj2 instanceof String ) )
	{
		throw new IllegalArgumentException( );
	}
	String str1 = DataTypeUtil.toString( obj1 );
	String str2 = DataTypeUtil.toString( obj2 );
	if ( ignoreCase )
	{
		if ( trimed )
		{
			return str1.trim( ).compareToIgnoreCase( str2.trim( ) );
		}
		return str1.compareToIgnoreCase( str2 );
	}
	else
	{
		if ( trimed )
		{
			return str1.trim( ).compareTo( str2.trim( ) );
		}
		return str1.compareTo( str2 );
	}
}
 
Example #23
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected Object getValue( Object[] args ) throws BirtException
{
	if( existNullValue( args ) )
	{
		return null;
	}
	return Long.valueOf( diffDay(DataTypeUtil.toDate(args[0]), DataTypeUtil.toDate(args[1])));
}
 
Example #24
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected Object getValue( Object[] args ) throws BirtException
{
	if( existNullValue( args ) )
	{
		return null;
	}
	return Long.valueOf(diffSecond(DataTypeUtil.toDate(args[0]), DataTypeUtil.toDate(args[1])));
}
 
Example #25
Source File: ParameterUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public static Object convert( Object value, String dataType )
		throws BirtException
{
	if ( DesignChoiceConstants.PARAM_TYPE_BOOLEAN.equals( dataType ) )
	{
		return DataTypeUtil.toBoolean( value );
	}
	else if ( DesignChoiceConstants.PARAM_TYPE_DATETIME.equals( dataType ) )
	{
		return DataTypeUtil.toDate( value );
	}
	else if ( DesignChoiceConstants.PARAM_TYPE_DATE.equals( dataType ) )
	{
		return DataTypeUtil.toSqlDate( value );
	}
	else if ( DesignChoiceConstants.PARAM_TYPE_TIME.equals( dataType ) )
	{
		return DataTypeUtil.toSqlTime( value );
	}
	else if ( DesignChoiceConstants.PARAM_TYPE_DECIMAL.equals( dataType ) )
	{
		return DataTypeUtil.toBigDecimal( value );
	}
	else if ( DesignChoiceConstants.PARAM_TYPE_FLOAT.equals( dataType ) )
	{
		return DataTypeUtil.toDouble( value );
	}
	else if ( DesignChoiceConstants.PARAM_TYPE_STRING.equals( dataType ) )
	{
		return DataTypeUtil.toString( value );
	}
	else if ( DesignChoiceConstants.PARAM_TYPE_INTEGER.equals( dataType ) )
	{
		return DataTypeUtil.toInteger( value );
	}
	return value;
}
 
Example #26
Source File: ReportDataServiceProvider.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param source
 * @return
 * @throws NumberFormatException
 * @throws BirtException
 */
private String valueAsString( Object source ) throws NumberFormatException,
		BirtException
{
	if ( source == null )
		return null;
	if ( source instanceof Number )
	{
		ULocale locale = ULocale.getDefault( );
		String value = source.toString( );
		int index = value.indexOf( 'E' );
		// It means the value is very larger, using E marked.
		if ( index >= 0 )
		{
			String returnValue = DataTypeUtil.toString( Double.valueOf( value.substring( 0,
					index ) ),
					locale )
					+ "E"; //$NON-NLS-1$
			String exponent = value.substring( index + 1 );
			if ( exponent.matches( "[\\+-]+[1-9]{1}[0-9]*" ) ) //$NON-NLS-1$
			{
				returnValue += exponent.substring( 0, 1 )
						+ DataTypeUtil.toString( Integer.valueOf( exponent.substring( 1 ) ),
								locale );
			}
			else
			{
				returnValue += DataTypeUtil.toString( Integer.valueOf( exponent ),
						locale );
			}
			return returnValue;
		}
	}
	return DataTypeUtil.toString( source );
}
 
Example #27
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected Object getValue( Object[] args ) throws BirtException
{
	if( existNullValue( args ) )
	{
		return null;
	}
	return addDay(DataTypeUtil.toDate(args[0]), ((Number)args[1]).intValue( ));
}
 
Example #28
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected Object getValue( Object[] args ) throws BirtException
{
	if( existNullValue( args ) )
	{
		return null;
	}
	return addWeek(DataTypeUtil.toDate(args[0]), ((Number)args[1]).intValue( ));
}
 
Example #29
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected Object getValue( Object[] args ) throws BirtException
{
	if( existNullValue( args ) )
	{
		return null;
	}
	return addMonth(DataTypeUtil.toDate(args[0]), ((Number)args[1]).intValue( ));
}
 
Example #30
Source File: OdiAdapter.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private IResultObject getConvertedResultObject( IResultObject resultObject ) throws DataException
{
	if ( resultObject == null )
		return null;
	if ( columnIndexListForTypeConvert == null )
		return resultObject;
	Object[] obj = new Object[resultClass.getFieldCount( )];
	for ( int i = 1; i <= resultClass.getFieldCount( ); i++ )
	{
		if ( columnIndexListForTypeConvert.contains( i ) )
		{
			try
			{
				obj[i - 1] = DataTypeUtil.convert( resultObject.getFieldValue( i ),
						DataTypeUtil.toApiDataType( resultClass.getFieldMetaData( i )
								.getDataType( ) ) );
			}
			catch ( BirtException e )
			{
				throw DataException.wrap( e );
			}
		}
		else
		{
			obj[i - 1] = resultObject.getFieldValue( i );
		}
	}
	IResultObject result = new ResultObject( resultClass, obj );
	return result;
}