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

import java.util.List;

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

import com.pragprog.dhnako.carserv.dom.employee.Employee;
import com.pragprog.dhnako.carserv.dom.employee.EmployeeRepository;
import com.pragprog.dhnako.carserv.dom.person.Person;

public class EmployeeRepositoryInMemory extends AbstractFactoryAndRepository implements EmployeeRepository {

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


	// {{ findByName
	@MemberOrder(sequence="2")
	public List<Employee> findByName(
	        final String lastName,
	        final String firstName) {
	    return allMatches(Employee.class, new Filter<Employee>() {
	        public boolean accept(final Employee employee) {
	            return employee.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<Employee> allEmployees() {
	    return allInstances(Employee.class);
	}
	// }}

	
}
