/***
 * Excerpted from "Domain-Driven Design Using Naked Objects",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/dhnako for more book information.
***/
package com.pragprog.dhnako.carserv.wicket.ui;

import org.apache.wicket.WicketRuntimeException;
import org.nakedobjects.plugins.headless.applib.DisabledException;
import org.nakedobjects.plugins.headless.applib.HeadlessViewer;
import org.nakedobjects.plugins.headless.applib.HiddenException;
import org.nakedobjects.plugins.headless.applib.InvalidException;

import com.pragprog.dhnako.carserv.dom.customer.Customer;

class EditCustomerFormUsingViewObjects extends EditCustomerForm {
	private static final long serialVersionUID = 1L;

	EditCustomerFormUsingViewObjects(String id, HeadlessViewer headlessViewer, Customer customer) {
		super(id, headlessViewer.view(customer));
	}

	protected void configureEmailFieldRules(LabelAndFormComponent lafc) {
		try {
			String emailAddress = getCustomer().getEmailAddress();
			getCustomer().setEmailAddress(emailAddress);
		} catch (HiddenException ex) {
			lafc.setVisible(false);
		} catch (DisabledException ex) {
			lafc.setEnabled(false);
		}
	}

	protected void configureNotesFieldRules(LabelAndFormComponent lafc) {
		try {
			String notes = getCustomer().getNotes();
			getCustomer().setNotes(notes);
		} catch (HiddenException ex) {
			lafc.setVisible(false);
		} catch (DisabledException ex) {
			lafc.setEnabled(false);
		}
	}

	protected void configureFeedbackFieldRules(LabelAndFormComponent lafc) {
		try {
			String feedback = getCustomer().getFeedback();
			getCustomer().setFeedback(feedback);
		} catch (HiddenException ex) {
			lafc.setVisible(false);
		} catch (DisabledException ex) {
			lafc.setEnabled(false);
		}
	}

	protected void configureSinceFieldRules(LabelAndFormComponent lafc) {
		try {
			java.sql.Date since = getCustomer().getSince();
			getCustomer().setSince(since);
		} catch (HiddenException ex) {
			lafc.setVisible(false);
		} catch (DisabledException ex) {
			lafc.setEnabled(false);
		}
	}

	@Override
	public boolean process() {
		try {
			return super.process();
		} catch (WicketRuntimeException ex) {
			Throwable cause = ex.getCause();
			if (cause instanceof InvalidException) {
				error(cause.getMessage());
				return false;
			} else {
				throw ex;
			}
		}
	}

}