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

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

public abstract class PersonRole extends AbstractDomainObject {

	// {{ Identification
	public String title() {
		return getPerson() != null? 
			getPerson().getName().title(): null;
	}
	// }}

	
	// {{ Person
	private Person person;

	@MemberOrder(sequence = "1")
	@Disabled
	public Person getPerson() {
		return person;
	}

	public void setPerson(final Person person) {
		this.person = person;
	}

	public void modifyPerson(
			final Person person) {
		Person currentPerson = getPerson();
		// check for no-op
		if (person == null
				|| person.equals(currentPerson)) {
			return;
		}
		// delegate to parent to associate
		person.addToRoles(this);
		// additional business logic
		onModifyPerson(currentPerson, person);
	}

	public void clearPerson() {
		Person currentPerson = getPerson();
		// check for no-op
		if (currentPerson == null) {
			return;
		}
		// delegate to parent to dissociate
		currentPerson.removeFromRoles(this);
		// additional business logic
		onClearPerson(currentPerson);
	}

	protected void onModifyPerson(final Person oldPerson, final Person newPerson) {
	}

	protected void onClearPerson(final Person oldPerson) {
	}
	// }}


	// {{ Lifecycle
	public void persisting() {
		getPerson().addToRoles(this);
	}
	// }}
	

}
