
/*******************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/
package org.eclipse.birt.data.engine.olap.data.document;

import java.io.IOException;
import java.util.Date;

import org.eclipse.birt.core.data.DataTypeUtil;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.olap.data.util.Bytes;
import org.eclipse.birt.data.engine.olap.data.util.DataType;

/**
 * 
 */

public class DocumentObjectUtil
{
	/**
	 * 
	 * @param documentObject
	 * @param dataType
	 * @param value
	 * @throws IOException
	 * @throws DataException 
	 */
	public static void writeValue( IDocumentObject documentObject, int[] dataType, Object[] value ) throws IOException, DataException
	{
		for( int i=0;i<dataType.length;i++)
		{
			writeValue( documentObject, dataType[i], value[i] );
		}
	}
	/**
	 * 
	 * @param documentObject
	 * @param dataType
	 * @param value
	 * @throws IOException
	 * @throws DataException 
	 */
	public static void writeValue( IDocumentObject documentObject, int dataType, Object value ) throws IOException, DataException
	{
		try{
		if (value == null) 
		{
			documentObject.writeByte(0);
			return;
		} 
		else 
		{
			documentObject.writeByte(1);
		}
		switch ( dataType )
		{
			case DataType.BOOLEAN_TYPE :
				documentObject.writeBoolean( DataTypeUtil.toBoolean( value ).booleanValue( ) );
				break;
			case DataType.INTEGER_TYPE :
				documentObject.writeInt( DataTypeUtil.toInteger( value ).intValue( ) );
				break;
			case DataType.DOUBLE_TYPE :
				documentObject.writeDouble( DataTypeUtil.toDouble( value ).doubleValue( ) );
				break;
			case DataType.STRING_TYPE :
				documentObject.writeString( DataTypeUtil.toString( value ) );
				break;
			case DataType.DATE_TYPE :
				documentObject.writeDate( DataTypeUtil.toDate( value ) );
				break;
			case DataType.BIGDECIMAL_TYPE :
				documentObject.writeBigDecimal( DataTypeUtil.toBigDecimal( value ) );
				break;
			case DataType.BYTES_TYPE :
				documentObject.writeBytes( (Bytes) value );
				break;
			case DataType.SQL_DATE_TYPE :
				documentObject.writeDate( DataTypeUtil.toSqlDate( value ) );
				break;
			case DataType.SQL_TIME_TYPE :
				documentObject.writeDate( DataTypeUtil.toSqlTime( value ) );
				break;
			default :
				assert false;
				break;
		}
		}catch ( BirtException e )
		{
			throw DataException.wrap( e );
		}
	}
	
	/**
	 * 
	 * @param documentObject
	 * @param dataType
	 * @return
	 * @throws IOException
	 */
	public static Object[] readValue( IDocumentObject documentObject, int[] dataType ) throws IOException
	{
		Object[] result = new Object[dataType.length];
		for ( int i = 0; i < result.length; i++ )
		{
			result[i] = readValue( documentObject, dataType[i] );
		}
		return result;
	}
	/**
	 * 
	 * @param documentObject
	 * @param dataType
	 * @return
	 * @throws IOException
	 */
	public static Object readValue( IDocumentObject documentObject, int dataType ) throws IOException
	{
		byte nullSign = documentObject.readByte( );
		if( nullSign == 0 )
		{
			return null;
		}
		switch ( dataType )
		{
			case DataType.BOOLEAN_TYPE :
				return new Boolean( documentObject.readBoolean( ) );
			case DataType.INTEGER_TYPE :
				return new Integer( documentObject.readInt( ) );
			case DataType.DOUBLE_TYPE :
				return new Double( documentObject.readDouble( ) );
			case DataType.STRING_TYPE :
				return documentObject.readString( );
			case DataType.DATE_TYPE :
				return documentObject.readDate( );
			case DataType.BIGDECIMAL_TYPE :
				return documentObject.readBigDecimal( );
			case DataType.BYTES_TYPE :
				return documentObject.readBytes( );
			case DataType.SQL_DATE_TYPE:
				Date date = documentObject.readDate();
				if (date == null) 
				{
					return null;
				}
				return new java.sql.Date(date.getTime());
			case DataType.SQL_TIME_TYPE :
				Date time = documentObject.readDate();
				if (time == null) 
				{
					return null;
				}
				return new java.sql.Time(time.getTime());
			default :
				assert false;
				return null;
		}
	}
}

@@ -2560,32 +2560,129 @@
 SQL_DATE_TYPE :%0A
+%09%09%09%09if( value instanceof java.sql.Date )%0A%09%09%09%09%09documentObject.writeDate( (Date)value );%0A%09%09%09%09else%0A%09
 %09%09%09%09documentObje
@@ -2766,32 +2766,129 @@
 SQL_TIME_TYPE :%0A
+%09%09%09%09if( value instanceof java.sql.Time )%0A%09%09%09%09%09documentObject.writeDate( (Date)value );%0A%09%09%09%09else%0A%09
 %09%09%09%09documentObje

Fix a little bug in cube writing.