/* ===================================================================
 * RemoveStaleUserRegistrations.java
 * 
 * Created Feb 3, 2005 4:28:09 PM
 * 
 * Copyright (c) 2004 Matt Magoffin (spamsqr@msqr.us)
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of 
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 
 * 02111-1307 USA
 * ===================================================================
 * $Id: RemoveStaleUserRegistrations.java 26 2009-05-03 23:35:54Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.task;

import magoffin.matt.ieat.dao.UserDao;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * Task to remove users that have registered but not confirmed 
 * that registration after a period of time.
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 26 $ $Date: 2009-05-04 11:35:54 +1200 (Mon, 04 May 2009) $
 */

public class RemoveStaleUserRegistrations extends QuartzJobBean {
	
	private static final Logger LOG = Logger.getLogger(RemoveStaleUserRegistrations.class);
	
	/* Injected fields */
	
	/** The UserDao object. */
	private UserDao userDao = null;
	
	@Override
	protected void executeInternal(JobExecutionContext context)
			throws JobExecutionException {
		verifyConfiguration();
		if ( LOG.isInfoEnabled() ) {
			LOG.info("Removing stale user registrations");
		}
		int numDeleted = userDao.removeStaleUserRegistrations();
		if ( LOG.isInfoEnabled() ) {
			if ( numDeleted > 0 ) {
				LOG.info("Removed " +numDeleted +" users whose registration was stale.");
			} else {
				LOG.info("No stale user registrations were found.");
			}
		}
	}
	
	/**
	 * Verify the task configuration is valid.
	 * @throws RuntimeException if not configured properly
	 */
	public void verifyConfiguration() {
		if ( userDao == null ) {
			throw new RuntimeException("userDao not configured");
		}
	}

	/**
	 * @return the userDao
	 */
	public UserDao getUserDao() {
		return userDao;
	}

	/**
	 * @param userDao the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
}
