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

import org.nakedobjects.applib.AbstractDomainObject;
import org.nakedobjects.applib.annotation.Disabled;
import org.nakedobjects.applib.annotation.MemberOrder;
import org.nakedobjects.applib.util.TitleBuffer;

import com.domainlanguage.time.CalendarInterval;
import com.pragprog.dhnako.carserv.dom.vehicle.Vehicle;

public class Service extends AbstractDomainObject {


	// {{ Identification
	public String title() {
        TitleBuffer buf = new TitleBuffer();
        if (getVehicle() != null) {
	        buf.append(getVehicle().getRegistrationNumber());
        }
        if (getBookedInAndReady() != null) {
	        buf.append(":", 
	    		getBookedInAndReady().start().toString("MMM dd, yyyy"));
        }
        return buf.toString();
	}
	// }}

	
	// {{ BookedInAndReady
	private CalendarInterval bookedInAndReady;

	@MemberOrder(sequence = "1.1")
	public CalendarInterval getBookedInAndReady() {
		return bookedInAndReady;
	}

	public void setBookedInAndReady(final CalendarInterval bookedInAndReady) {
		this.bookedInAndReady = bookedInAndReady;
	}
	// }}



	// {{ Vehicle
	private Vehicle vehicle;

	@MemberOrder(sequence = "1.3")
	@Disabled
	public Vehicle getVehicle() {
		return vehicle;
	}

	public void setVehicle(final Vehicle vehicle) {
		this.vehicle = vehicle;
	}

	public void modifyVehicle(final Vehicle vehicle) {
        Vehicle currentVehicle = getVehicle();
		// check for no-op
		if (vehicle == null || vehicle.equals(currentVehicle)) {
			return;
		}
		// delegate to parent to associate
		vehicle.addToServices(this);
		// additional business logic
		onModifyVehicle(currentVehicle, vehicle);
	}

	public void clearVehicle() {
        Vehicle currentVehicle = getVehicle();
		// check for no-op
		if (currentVehicle == null) {
			return;
		}
		// delegate to parent to dissociate
		currentVehicle.removeFromServices(this);
		// additional business logic
		onClearVehicle(currentVehicle);
	}

	protected void onModifyVehicle(final Vehicle oldVehicle, final Vehicle newVehicle) {
	}

	protected void onClearVehicle(final Vehicle oldVehicle) {
	}
	// }}


	
	
	
	// {{ Lifecycle methods
	public void persisting() {
		getVehicle().addToServices(this);
	}
	// }}


}
