/***
 * 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.dom.payment;

import org.nakedobjects.applib.AbstractDomainObject;
import org.nakedobjects.applib.annotation.Bounded;
import org.nakedobjects.applib.annotation.Hidden;
import org.nakedobjects.applib.annotation.Immutable;
import org.nakedobjects.applib.annotation.MemberOrder;
import org.nakedobjects.applib.annotation.When;

@Bounded
@Immutable(When.ONCE_PERSISTED)
public class PaymentMethodType extends AbstractDomainObject {

	// {{ Identification
    public String title() {
        return paymentMethodSubclass().getSimpleName();
    }
    public String iconName() {
		return title();
	}
    // }}

    
    // {{ FullyQualifiedClassName
	private String fullyQualifiedClassName;

	@MemberOrder(sequence = "1")
	public String getFullyQualifiedClassName() {
		return fullyQualifiedClassName;
	}

	public void setFullyQualifiedClassName(final String fullyQualifiedClassName) {
		this.fullyQualifiedClassName = fullyQualifiedClassName;
	}
	// }}


	// {{ create
	@Hidden
    public PaymentMethod create() {
        try {
            PaymentMethod paymentMethod = newTransientInstance(paymentMethodSubclass());
            paymentMethod.setType(this);
            return paymentMethod;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    // }}

	// {{ Helpers
    @SuppressWarnings("unchecked")
    @Hidden
	public Class<? extends PaymentMethod> paymentMethodSubclass() {
        try {
            return (Class<? extends PaymentMethod>) 
                Class.forName(getFullyQualifiedClassName());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("No such payment method type");
        }
    }
    // }}
    
}