/***
 * 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 java.util.List;

import org.nakedobjects.applib.fixtures.AbstractFixture;

import com.pragprog.dhnako.carserv.dom.customer.Customer;
import com.pragprog.dhnako.carserv.dom.customer.CustomerRepository;
import com.pragprog.dhnako.carserv.dom.service.Service;
import com.pragprog.dhnako.carserv.dom.vehicle.Car;
import com.pragprog.dhnako.carserv.dom.vehicle.Make;
import com.pragprog.dhnako.carserv.dom.vehicle.MakeRepository;
import com.pragprog.dhnako.carserv.dom.vehicle.Model;
import com.pragprog.dhnako.carserv.dom.vehicle.ModelRepository;
import com.pragprog.dhnako.carserv.dom.vehicle.RegistrationNumber;

public abstract class AbstractCarFixture extends AbstractFixture {

    public abstract void install();
    
    protected Car createCar(
    		final Customer customer, 
    		final Model model, 
    		final String registrationNumber) {
    	Car car = customer.newCar();
        car.setModel(model);
        car.setRegistrationNumber(new RegistrationNumber(registrationNumber));
        persist(car);
        return car;
    }

	protected Make findMake(final String name) {
		return makeRepository.findMake(name);
	}

	protected Model findModel(final Make make, final String name) {
		return modelRepository.findModel(make, name);
	}

	protected Customer findCustomer(final String lastName, final String firstName) {
		List<Customer> customers = customerRepository.findByName(lastName, firstName);
		return customers.size() > 0 ? customers.get(0): null;
	}
	
	protected Service bookService(Car car, int year, int month, int day) {
		setDate(year, month, day);
		Service service = car.bookService();
    	persist(service);
    	return service;
	}

	

	// {{ injected dependencies
	// {{ injected: CustomerRepository
	private CustomerRepository customerRepository;

	public void setCustomerRepository(final CustomerRepository customerRepository) {
		this.customerRepository = customerRepository;
	}
	// }}
	// {{ injected: ModelRepository
	private ModelRepository modelRepository;
	public void setModelRepository(final ModelRepository modelRepository) {
		this.modelRepository = modelRepository;
	}
	// }}
	// {{ injected: MakeRepository
	private MakeRepository makeRepository;
	public void setMakeRepository(final MakeRepository makeRepository) {
		this.makeRepository = makeRepository;
	}
	// }}
	
	// }}


}
