/***
 * 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 javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

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.Optional;
import org.nakedobjects.applib.annotation.When;

@NamedQueries({
	@NamedQuery(
		name="findPaymentMethodTypeByFullyQualifiedClassName", 
		query="from PaymentMethodType where fullyQualifiedClassName=:fullyQualifiedClassName")
})
@Entity
@Bounded
@Immutable(When.ONCE_PERSISTED)
@DiscriminatorValue("PMT")
public class PaymentMethodType extends AbstractDomainObject {

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

    
    // {{ Id
    private Long id;
    @Hidden
    @Optional
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
    // }}
    


    // {{ 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");
        }
    }
    // }}
    
}