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

import org.nakedobjects.applib.AbstractDomainObject;
import org.nakedobjects.applib.ApplicationException;
import org.nakedobjects.applib.annotation.Bounded;
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 VehicleType extends AbstractDomainObject {

    public String title() {
        return vehicleSubclass().getSimpleName();
    }
    public String iconName() {
        return title();
    }

    private String fullyQualifiedClassName;
    public String getFullyQualifiedClassName() { ... }
    public void setFullyQualifiedClassName( ... }

    @Hidden
    public Vehicle create() {
        try {
            Vehicle vehicle = newTransientInstance(
                                           vehicleSubclass());
            vehicle.setType(this);
            return vehicle;
        } catch (Exception ex) {
            throw new ApplicationException(ex);
        }
    }

    @SuppressWarnings("unchecked")
    private Class<? extends Vehicle> vehicleSubclass() {
        try {
            return (Class<? extends Vehicle>) 
                Class.forName(getFullyQualifiedClassName());
        } catch (ClassNotFoundException e) {
            throw new ApplicationException("No such vehicle type");
        }
    }
}

