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

import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
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;
import org.nakedobjects.applib.util.TitleBuffer;

@Entity
@DiscriminatorValue("PRS")
public class Person extends AbstractDomainObject {

	// {{ Identification
	public String title() {
		TitleBuffer buf = new TitleBuffer();
		if (getName() != null) {
			buf.append(getName().title());
		}
		buf.append(" (Person)");
		return buf.toString();
	}
	public String iconName() {
		return getName() != null? getName().getTitle().getIconName(): null;
	}
	// }}


	// {{ Lifecycle methods
	public void created() {
		name = newTransientInstance(Name.class);
	}
	// }}

	
    // {{ 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;
	}
    // }}
    


	// {{ Name
	private Name name;
	@Disabled
	@MemberOrder(sequence = "1")
	@Embedded
	public Name getName() {
		return name;
	}

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


	// {{ Roles
	private List<PersonRole> roles = new ArrayList<PersonRole>();

	@MemberOrder(sequence = "2")
	@Disabled
    @OneToMany(fetch=FetchType.LAZY, mappedBy="person", cascade=CascadeType.ALL)
	public List<PersonRole> getRoles() {
		return roles;
	}

	public void setRoles(final List<PersonRole> roles) {
		this.roles = roles;
	}

	public void addToRoles(final PersonRole role) {
		// check for no-op
		if (role == null
				|| getRoles().contains(role)) {
			return;
		}
		// dissociate arg from its current parent (if any).
		role.clearPerson();
		// associate arg
		role.setPerson(this);
		getRoles().add(role);
		// additional business logic
		onAddToRoles(role);
	}

	public void removeFromRoles(
			final PersonRole role) {
		// check for no-op
		if (role == null
				|| !getRoles().contains(role)) {
			return;
		}
		// dissociate arg
		role.setPerson(null);
		getRoles().remove(role);
		// additional business logic
		onRemoveFromRoles(role);
	}

	protected void onAddToRoles(final PersonRole role) {
	}

	protected void onRemoveFromRoles(final PersonRole role) {
	}
	
	@SuppressWarnings("unchecked")
	@Hidden
	public <T extends PersonRole> T getRole(Class<T> roleType) {
		for(PersonRole role: getRoles()) {
			if (roleType.isAssignableFrom(role.getClass())) {
				return (T) role;
			}
		}
		return null;
	}
	@Hidden
	public boolean hasRole(Class<? extends PersonRole> roleType) {
		return getRole(roleType) != null;
	}
	// }}



	// {{ Helpers
	@Hidden
	public boolean matches(final String firstName, final String lastName) {
		return getName() != null ? getName().matches(firstName, lastName) : false;
	}
	// }}

}
