/***
 * 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 javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.Version;

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

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="role_type", discriminatorType=DiscriminatorType.STRING, length=3)
public abstract class PersonRole extends AbstractDomainObject {

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

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


    // {{ Version
    private Long version;
    @Hidden
    @Optional
    @Version
	public Long getVersion() {
		return version;
	}
	public void setVersion(Long version) {
		this.version = version;
	}
    // }}
    


	// {{ Person
	private Person person;

	@MemberOrder(sequence = "1")
	@Disabled
	@ManyToOne(fetch=FetchType.LAZY)
	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);
	}
	// }}
	

}
