/*
 * Created on Feb 7, 2004
 *
 */
package LumiSoft.Data;

import java.util.*;
import java.text.*;

/**
 * 
 */
public class DataRow 
{
	private DataTable m_pOwner     = null;
	private Object[]  m_pRowValues = null;
		
	protected DataRow(DataTable owner,Object[] rowValues)
	{
		m_pOwner     = owner;
		m_pRowValues = rowValues;
	}
	
	
//	/**
//	 * Gets the child rows of a DataRow using the specified RelationName of a DataRelation.
//	 * @param relationName
//	 */
//	public DataRow[] Get(String relationName)
//	{
//		return null;
//	}
	
	
	/**
	 * Gets data stored in a specified column.
	 * @param fieldName
	 * @return
	 */
	public Object get(String fieldName)
	{
		return get(m_pOwner.getColumns().get(fieldName));
	}
	/**
	 * Gets or sets the data stored in the column specified by index.
	 * @param fieldIndex
	 * @return
	 */
	public Object get(int fieldIndex)
	{
		return get(m_pOwner.getColumns().get(fieldIndex));
	}
	/**
	 * Gets the data stored in the specified DataColumn.
	 * @param column
	 * @return
	 */
	public Object get(DataColumn column)
	{	
		return m_pRowValues[column.getIndex()];
	}
	/**
	 * Sets the data stored in the column specified by index.
	 * @param columnIndex The zero-based index of the column.
	 * @param value
	 */
	public void set(int columnIndex,Object value)
	{
		// TODO: see if column exists	
		
		set(m_pOwner.getColumns().get(columnIndex),value);
	}
	/**
	 * Sets the data stored in the column specified by name.
	 * @param fieldName The name of the column.
	 * @param value
	 */
	public void set(String fieldName,Object value)
	{
		// TODO: see if column exists	
		
		set(m_pOwner.getColumns().get(fieldName),value);	
	}
	/**
	 * Sets the data stored in the specified DataColumn.
	 * @param column A DataColumn where to store data. 
	 * @param value
	 */
	public void set(DataColumn column,Object value)
	{			
		// Worng datatype, try to convert
		if(value != null && !value.getClass().equals(column.getDataType())){
	//		System.out.println("Convert:" + value.getClass().getName() + " " + dc.getDataType().getName());
			if(column.getDataType() == java.util.Date.class){
				if(value.getClass().equals(java.sql.Timestamp.class)){
					value = new Date(((java.util.Date)value).getTime());
				}
				else{
					// yyyy-MM-ddTHH:mm:ss.sssssss 
					// yyyy-MM-ddTHH:mm:ss.sssssssZ 
					// yyyy-MM-ddTHH:mm:ss.sssssss+hh:mm 
					// yyyy-MM-ddTHH:mm:ss.sssssss-hh:mm 
					// yyyy-MM-dd HH:mm:ss.S             // Java sql datetime
					SimpleDateFormat[] dateFormats = new SimpleDateFormat[]{
							new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS"),
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S")
					};
					// Try diffrent foramts, see is some suits
					boolean converted = false;
					for(SimpleDateFormat dateFormat : dateFormats){
						try{
							value = dateFormat.parse(value.toString());
							
							// This format suited, don't check others
							converted = true;
							break;
						}
						catch(Exception x){						
						}
					}
					if(!converted){
						new Exception("DataRow.set[Date] can't parse: " + value.toString()).printStackTrace();
					}
				}
			}
			else if(column.getDataType() == byte[].class){
				try{
					value = new sun.misc.BASE64Decoder().decodeBuffer(value.toString());
				}
				catch(Exception x){
					x.printStackTrace();			
				}
			}
			else if(column.getDataType() == java.math.BigDecimal.class){
				try{
					value = new java.math.BigDecimal(value.toString());
				}
				catch(Exception x){
					x.printStackTrace();			
				}
			}
			else if(column.getDataType() == boolean.class){
				try{
					value = Boolean.valueOf(value.toString());
				}
				catch(Exception x){
					x.printStackTrace();			
				}
			}
			else if(column.getDataType() == int.class){
				try{
					value = Integer.valueOf(value.toString());
				}
				catch(Exception x){
					x.printStackTrace();			
				}
			}
		}
		m_pRowValues[column.getIndex()] = value;
	}
		
	/**
	 * Gets all of the values for this row through an array.
	 * @return
	 */
	public Object[] getItemArray()
	{
		return m_pRowValues;
	}
	/**
	 * Sets all of the values for this row through an array.
	 * @param value
	 */
	public void setItemArray(Object[] value)
	{
		// TODO: check type and count
		
		m_pRowValues = (Object[])value.clone();
	}
	
	/**
	 * Gets the DataTable for which this row has a schema.
	 * @return
	 */
	public DataTable getTable()
	{
		return m_pOwner;
	}
	
}
