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

import java.util.List;

import org.nakedobjects.applib.AbstractFactoryAndRepository;
import org.nakedobjects.applib.Filter;
import org.nakedobjects.applib.annotation.MemberOrder;
import org.nakedobjects.applib.security.UserMemento;

import com.pragprog.dhnako.carserv.dom.customer.Customer;
import com.pragprog.dhnako.carserv.dom.customer.CustomerRepository;
import com.pragprog.dhnako.carserv.dom.person.Person;

public class CustomerRepositoryInMemory 
        extends AbstractFactoryAndRepository
        implements CustomerRepository {

	// {{ Identification
	public String iconName() {
		return "Customer";
	}
	// }}
	
	// {{ Create new (still transient) Customer
	@MemberOrder(sequence="1")
	public Person newCustomer() {
		Person person = newTransientInstance(Person.class);
		person.addToRoles(newTransientInstance(Customer.class));
		return person;
	}
	// }}

	// {{ findByName
	@MemberOrder(sequence="2")
	public List<Customer> findByName(
	        final String lastName,
	        final String firstName) {
	    return allMatches(Customer.class, new Filter<Customer>() {
	        public boolean accept(final Customer customer) {
	            return customer.getPerson().matches(firstName, lastName);
	        }});
	}

	public String validateFindByName(final String lastName, final String firstName) {
		if (lastName == null && firstName == null) {
			return "Must specify at least one name";
		}
		return null;
	}
	// }}
	

	// {{ all Customers
	@MemberOrder(sequence="3")
	public List<Customer> allCustomers() {
	    return allInstances(Customer.class);
	}
	// }}

	// {{ currentUser
	@MemberOrder(sequence="4")
	public UserMemento currentUser() {
		return getUser();
	}
	// }}

	
}
