/**
 * Copyright (c) 2001, Keith Wong
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package mypackage;

import objectivehtml.common.*;
import objectivehtml.htmlwidget.*;
import objectivehtml.htmlwidget.exception.*;
import objectivehtml.oms.exception.*;
import objectivehtml.oms.*;
import java.util.*;
import java.text.*;
import javax.servlet.http.*;

/**
 * CustomerListForm
 * This is an example of how to use the objective html toolkit. Please read all the comments
 * in the code as they give handy tips in how to be use this toolkit.
 * The example was written by Keith Wong, email: keithwong@optushome.com.au
 */
public class CustomerListForm extends CustomerListFormDesign
{

	/** Indicates whether the form was successfully processed or an error occurred */
	private Throwable m_objThrowable = null;

	/**
	 * Constructor
	 */
	public CustomerListForm()
	{
		super();

	} // end constructor

	/**
	 * Lets populate the form with all the customers.
	 */
	public void getAllCustomers()
	{
		Collection clCustomers = CustomerDomain.retrieveAllCustomers();
		boolean bFirstRow = true;
		for (Iterator it = clCustomers.iterator(); it.hasNext(); )
		{
			Customer objCustomer = (Customer)it.next();
			HtmlTableRow objTableRow = null;
			HtmlRadioButton objRadioButton = null;
			if (bFirstRow == true)
			{
				bFirstRow = false;
				// no need to create new row
				objTableRow = m_tblLayout.getTableRow(m_tblLayout.getRows()-1);
				// lets set the control box
				objRadioButton = new HtmlRadioButton(objTableRow.getTableCell(0), "customerid");
				connect(slot("processCustomerSelection(String)"), objRadioButton.signal("dataSubmitted(String)"));
			} // end if first row
			else
			{
				// lets create a new row with the same properties as the previous
				objTableRow = m_tblLayout.appendTableRow(true);
				// lets set the control box
				objRadioButton = new HtmlRadioButton(objTableRow.getTableCell(0), "customerid");
			} // end else not first row

			objRadioButton.setValue(objCustomer.getCustomerId().toString());
			// lets set the first name
			objTableRow.getTableCell(1).setText(objCustomer.getFirstName());
			// lets set the last name
			objTableRow.getTableCell(2).setText(objCustomer.getLastName());
			// lets set the birth date
			SimpleDateFormat objFormatter = new SimpleDateFormat("yyyy-MM-dd");
			objTableRow.getTableCell(3).setText(objFormatter.format(objCustomer.getBirthDate()));

		} // end for more customers
	} // end getAllCustomers

	/**
	 * This method is called when a customer is selected on the form.
	 */
	public void processCustomerSelection(String sCustomerId)
	{
		try
		{
			long lCustomerId = Long.parseLong(sCustomerId);
			emit(customerSelected(lCustomerId));
		} // end try to parse
		catch (NumberFormatException nfe)
		{
		} // end catch nfe
	} // end processCustomerSelection

	/**
	 * This signal is emitted when a new customer is selected.
	 */
	public Signal customerSelected(long lCustomerId)
	{
		ParameterList objParamList = new ParameterList();
		objParamList.addParam(lCustomerId);
		Signal objSignal = null;
		try
		{
			objSignal = new Signal(signal("customerSelected(long)"), objParamList);
		}
		catch (Exception e)
		{
			e.printStackTrace();	// shouldn't happen
		}
		return objSignal;

	} // end customerSelected

	/**
	 * This method clears any stored exceptions.
	 */
	public void clearException()
	{
		m_objThrowable = null;
	} // end clearException

	/**
	 * This method should be called after a business method (or slot method)
	 * is called to check whether an exception occurred.
	 */
	public void checkException(HttpServletRequest objRequest)
	{
		if (objRequest.getAttribute("ohtml.result") != null)
		{
			EventResult objResult = (EventResult)objRequest.getAttribute("ohtml.result");
			if (objResult.getResult() == null
				|| objResult.getResult().equals("success"))
			{
				if (m_objThrowable != null)
				{
					objResult.setResult("failure");
					objResult.setAttribute("error", m_objThrowable);
				} // end if something failed
				else
				{
					objResult.setResult("success");
				} // end else everything ok
			} // end if not failure

			// lets clear the exception as we've processed this exception
			clearException();

		} // end if result exists
	} // end checkException

} // end class