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

import org.nakedobjects.applib.fixtures.AbstractFixture;

import com.pragprog.dhnako.carserv.dom.vehicle.Car;
import com.pragprog.dhnako.carserv.dom.vehicle.Make;
import com.pragprog.dhnako.carserv.dom.vehicle.Model;
import com.pragprog.dhnako.carserv.dom.vehicle.Motorcycle;
import com.pragprog.dhnako.carserv.dom.vehicle.Van;
import com.pragprog.dhnako.carserv.dom.vehicle.Vehicle;
import com.pragprog.dhnako.carserv.dom.vehicle.VehicleType;

public class MakesAndModelsFixture extends AbstractFixture {


    public void install() {
	    @SuppressWarnings("unused")
		VehicleType carVehicleType = createVehicleType(Car.class);
	    @SuppressWarnings("unused")
		VehicleType vanVehicleType = createVehicleType(Van.class);
	    @SuppressWarnings("unused")
		VehicleType motorcycleVehicleType = createVehicleType(Motorcycle.class);
	    
        Make fordMake = createMake("Ford");
        Make toyotaMake = createMake("Toyota");
        Make yamahaMake = createMake("Yamaha");
        Make hondaMake = createMake("Honda");
        
        // cars
        createModel(fordMake, "Focus");
        createModel(fordMake, "Mustang");
        createModel(toyotaMake, "Corolla");
        createModel(toyotaMake, "Yaris");
        createModel(hondaMake, "Civic");
        createModel(hondaMake, "Accord");
        
        // vans
        createModel(fordMake, "Ranger");
        createModel(fordMake, "Transit");
        createModel(toyotaMake, "Hiace");
        createModel(toyotaMake, "Hilux");
        
        // motorcycles
        createModel(hondaMake, "Varadero");
        createModel(hondaMake, "Shadow");
        createModel(hondaMake, "VFR800");
        createModel(yamahaMake, "Tenere");
        createModel(yamahaMake, "FZ1 Fazer");

    }

	private VehicleType createVehicleType(Class<? extends Vehicle> subclass) {
	    VehicleType vehicleType = newTransientInstance(VehicleType.class);
	    vehicleType.setFullyQualifiedClassName(subclass.getName());
	    persist(vehicleType);
	    return vehicleType;
	}
    private Make createMake(String name) {
        Make make = newTransientInstance(Make.class);
        make.setName(name);
        persist(make);
        return make;
    }
    private Model createModel(Make make, String name) {
        Model model = newTransientInstance(Model.class);
        model.setName(name);
        make.addToModels(model);
        persist(model);
        return model;
    }

}
