/***
 * 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.annotation.Bounded;
import org.nakedobjects.applib.annotation.Disabled;
import org.nakedobjects.applib.annotation.Immutable;
import org.nakedobjects.applib.annotation.MemberOrder;
import org.nakedobjects.applib.util.TitleBuffer;

@Bounded
@Immutable
public class Model extends AbstractDomainObject {

	
	// {{ Identification
	public String title() {
		final TitleBuffer buf = new TitleBuffer();
		buf.append(getName());
		return buf.toString();
	}
	// }}


	// {{ Name
	private String name;

	@MemberOrder(sequence = "1.2")
	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}
	// }}


	// {{ Make
	private Make make;

	@MemberOrder(sequence = "1.1")
	public Make getMake() {
		return make;
	}

	public void setMake(final Make make) {
		this.make = make;
	}

	public void modifyMake(final Make make) {
		Make currentMake = getMake();
		// check for no-op
		if (make == null || currentMake == make) {
			return;
		}
		// delegate to parent to associate
		make.addToModels(this);
		// additional business logic
		onModifyMake(currentMake, make);
	}

	public void clearMake() {
		Make currentMake = getMake();
		// check for no-op
		if (currentMake == null) {
			return;
		}
		// delegate to parent to dissociate
		currentMake.removeFromModels(this);
		// additional business logic
		onClearMake(currentMake);
	}

	protected void onModifyMake(final Make oldMake, final Make newMake) {
	}

	protected void onClearMake(final Make oldMake) {
	}
	// }}
	
	
	// {{ ServiceInterval
	private Integer serviceInterval;

	@MemberOrder(sequence = "1.2")
	public Integer getServiceInterval() {
		return serviceInterval;
	}

	public void setServiceInterval(final Integer serviceInterval) {
		this.serviceInterval = serviceInterval;
	}

	public String validateServiceInterval(final Integer serviceInterval) {
		if (serviceInterval == null)
			return null;
		
		return serviceInterval.intValue()<1?"Must be positive": null;
	}
	// }}


	// {{ VehicleType
	private VehicleType vehicleType;

	@Disabled
	@MemberOrder(sequence = "1.3")
	public VehicleType getVehicleType() {
		return vehicleType;
	}

	public void setVehicleType(final VehicleType vehicleType) {
		this.vehicleType = vehicleType;
	}
	// }}


	// {{ create
	@MemberOrder(sequence="1")
    public Vehicle create() {
        return getVehicleType().create(this);
    }

}
