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

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

import org.nakedobjects.applib.AbstractDomainObject;
import org.nakedobjects.applib.annotation.Bounded;
import org.nakedobjects.applib.annotation.Hidden;
import org.nakedobjects.applib.annotation.Immutable;
import org.nakedobjects.applib.annotation.MemberOrder;
import org.nakedobjects.applib.annotation.Optional;
import org.nakedobjects.applib.util.TitleBuffer;

@NamedQueries({
	@NamedQuery(name="findMakeByName", query="from Make where name=:name")
})
@Entity
@Bounded
@Immutable
@DiscriminatorValue("MAK")
public class Make extends AbstractDomainObject {

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

	
    // {{ Id
    private Long id;
    @Hidden
	@Optional
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
    // }}


	// {{ Name
	private String name;

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

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

	
	// {{ Models
	private List<Model> models = new ArrayList<Model>();

	@OneToMany(mappedBy="make", fetch=FetchType.LAZY)
	@MemberOrder(sequence = "1.2")
	public List<Model> getModels() {
		return models;
	}

	public void setModels(final List<Model> models) {
		this.models = models;
	}

	public void addToModels(final Model model) {
		// check for no-op
		if (model == null
				|| getModels().contains(model)) {
			return;
		}
		// dissociate arg from its current parent (if any).
		model.clearMake();
		// associate arg
		model.setMake(this);
		getModels().add(model);
		// additional business logic
		onAddToModels(model);
	}

	public void removeFromModels(
			final Model model) {
		// check for no-op
		if (model == null
				|| !getModels().contains(model)) {
			return;
		}
		// dissociate arg
		model.setMake(null);
		getModels().remove(model);
		// additional business logic
		onRemoveFromModels(model);
	}

	protected void onAddToModels(final Model model) {
	}

	protected void onRemoveFromModels(final Model model) {
	}
	// }}

}
