package com.aucfox.common.reporting;


import java.util.Enumeration;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

import com.webobjects.eocontrol.EOGenericRecord;
import com.webobjects.foundation.NSArray;
import com.webobjects.foundation.NSKeyValueCodingAdditions;
import com.webobjects.foundation.NSLog;




/**
 *	This class is meant to be a utility wrapper to wrap data from an EditingContext so that 
 *	JasperReports can use it.
 * 
 **/
public class WCJRDataSource implements JRDataSource {
	
	/**
	 * Private NSArray that contains the database values we wish to use in the report.
	 *
	 */
	private NSArray<? extends EOGenericRecord> contents;
	private EOGenericRecord currRow;
	private Enumeration<? extends EOGenericRecord> e;
	private boolean filterNulls = true;
	/**
	 * Default constructor.
	 */
	public WCJRDataSource() {
		currRow = null;
		e = null;
		contents = null;
		return;
	}
	
	public WCJRDataSource(NSArray<? extends EOGenericRecord> arr) {
		this();
		setContents(arr);
	}

	/**
	 * public setter for contents.
	 * @param arr
	 */
	public void setContents(NSArray<? extends EOGenericRecord> arr) {
		e = arr.objectEnumerator();
		contents = arr;
	}

	/**
	 * public getter for contents
	 * @return
	 */
	public NSArray<? extends EOGenericRecord> getContents() {
		return contents; 
	}


	/* (non-Javadoc)
	 * @see net.sf.jasperreports.engine.JRDataSource#next()
	 */
	public boolean next() throws JRException {

		if (e.hasMoreElements()) {
			currRow = e.nextElement();
			return true;
		}
		return false;
	}
	
	public boolean getFilterNulls() {
		return this.filterNulls;
	}
	
	public void setFilterNulls(boolean filters) {
		this.filterNulls = filters;
	}
	
	/* (non-Javadoc)
	 * @see net.sf.jasperreports.engine.JRDataSource#getFieldValue(net.sf.jasperreports.engine.JRField)
	 */
	public Object getFieldValue(JRField jrField) throws JRException {
		// Fields in JasperReports become Java variables in .java files when
		// the report is compiled.  Because we are using key-path coding, 
		// we would normally have periods in the field name.  However, this makes
		// Jasper Reports very unhappy, and it will not compile the report.
		// Therefore, the field names as entered in Jasper Reports use
		// underscores instead of periods.  This also means that to get the value
		// we need to replace the underscores in the field name to periods so that
		// key-path coding will work and so that the report will compile.
		// This has the obvious caveat that the key-path cannot contain an underscore
		// or this conversion will mess things up
		//
		//
		Object ret_val = null;
		try {
			ret_val = ((NSKeyValueCodingAdditions)currRow).valueForKeyPath(jrField.getName().replaceAll("_", "."));
		}
		catch (Exception ex) {
			NSLog.err.appendln("Error while retrieving value" + jrField.getName().replaceAll("_", "."));
			NSLog.err.appendln(ex);
		}
		if (filterNulls && ret_val == null)
			return "";
		else
			return ret_val;
	}
}
