/*
 * Copyright (c) 2011, Patrik Dufresne. All rights reserved.
 * Patrik Dufresne PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.planimod.core;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import com.patrikdufresne.managers.AbstractArchivableManager;
import com.patrikdufresne.managers.Exec;
import com.patrikdufresne.managers.ManagerContext;
import com.patrikdufresne.managers.ManagerException;

/**
 * Object manager for employees.
 * 
 * @author Patrik Dufresne
 */
public class EmployeeManager extends AbstractArchivableManager<Employee> {

	public EmployeeManager(PlanimodManagers managers) {
		super(managers);
	}

	/**
	 * Return the managers
	 * 
	 * @return
	 */
	@Override
	public PlanimodManagers getManagers() {
		return (PlanimodManagers) super.getManagers();
	}

	@Override
	public Class<Employee> objectClass() {
		return Employee.class;
	}

	/**
	 * This implementation remove the {@link Employee} and the
	 * {@link EmployeePreference} objects from the managers.
	 * 
	 * @see com.patrikdufresne.managers.IManager#remove(java.util.Collection)
	 */
	@Override
	public void remove(final Collection<? extends Employee> s)
			throws ManagerException {
		checkObject(s);
		getManagers().exec(new Exec() {
			@Override
			public void run() throws ManagerException {
				List<EmployeePreference> prefs = getManagers()
						.getEmployeePreferenceManager().listByEmployees(s);
				for (EmployeePreference pref : prefs) {
					ManagerContext.getDefaultSession().delete(pref);
					ManagerContext.getDefaultSession().delete(
							pref.getEmployee());
				}
			}
		});
	}

	/**
	 * This implementation add an Employee preference for the employee.
	 * 
	 * @param t
	 *            the employee object
	 * @throws ManagerException
	 */
	@Override
	protected void postAddObject(Employee e) throws ManagerException {
		super.postAddObject(e);

		// Add a employee-preference
		EmployeePreference pref = new EmployeePreference();
		pref.setEmployee(e);
		getManagers().getEmployeePreferenceManager().add(Arrays.asList(pref));
	}

}
