/* ===================================================================
 * IngredientDaoImpl.java
 * 
 * Copyright (c) 2004 Matt Magoffin. Created Aug 15, 2004 11:41:51 AM.
 * 
 * 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: IngredientDaoImpl.java 39 2009-05-06 03:30:47Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.dao.hbm;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import magoffin.matt.dao.hbm.GenericHibernateDao;
import magoffin.matt.ieat.dao.IngredientDao;
import magoffin.matt.ieat.domain.Ingredient;
import magoffin.matt.ieat.domain.impl.IngredientImpl;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

/**
 * IngredientDao using Hibernate.
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 39 $ $Date: 2009-05-06 15:30:47 +1200 (Wed, 06 May 2009) $
 */
public class IngredientDaoImpl extends GenericHibernateDao<Ingredient,Integer>
implements IngredientDao {
	
	/** The query to find all Ingredient objects. */
	public static final String FIND_ALL = "IngredientAll";
	
	private JdbcTemplate jdbcTemplate;
	private String sqlIndexAll;

	/**
	 * Default constructor.
	 */
	public IngredientDaoImpl() {
		super(IngredientImpl.class);
	}

	/* (non-Javadoc)
	 * @see magoffin.matt.ieat.dao.IngredientDao#getIngredients()
	 */
	public List<Ingredient> getIngredients() {
		return findByNamedQuery(FIND_ALL);
	}
	
	@Override
	protected Integer getPrimaryKey(Ingredient domainObject) {
		if ( domainObject == null ) return null;
		return domainObject.getIngredientId();
	}

	/**
	 * Method to call after all dependency injection has occured.
	 */
	public void init() {
		if ( jdbcTemplate == null ) {
			throw new RuntimeException("jdbcTemplate not configured");
		}
		if ( sqlIndexAll == null ) {
			throw new RuntimeException("sqlIndexAll not configured");
		}
	}

	/* (non-Javadoc)
	 * @see magoffin.matt.ieat.dao.IngredientDao#index(magoffin.matt.ieat.dao.IngredientDao.IngredientIndexCallback)
	 */
	public void index(final IngredientIndexCallback callback) {
		if ( log.isDebugEnabled() ) {
			log.debug("Executing SQL for index all ingredients: " +sqlIndexAll);
		}
		final IngredientIndexCallbackDataImpl callbackData = new IngredientIndexCallbackDataImpl();
		jdbcTemplate.query(sqlIndexAll, 
				new RowCallbackHandler() {
					private ResultSet myRs;
					public void processRow(ResultSet rs) throws SQLException {
						if ( myRs == null ) {
							myRs = rs;
						}
						callbackData.ingredientId = new Integer(rs.getInt(1));
						callbackData.name = rs.getString(2);
						callback.handle(callbackData);
					}
				}
				);
	}
	
	private static class IngredientIndexCallbackDataImpl implements IngredientIndexCallbackData {
		private Integer ingredientId;
		private String name;
		
		/* (non-Javadoc)
		 * @see magoffin.matt.ieat.dao.IngredientDao.IngredientIndexCallbackData#getIngredientId()
		 */
		public Integer getIngredientId() {
			return ingredientId;
		}
		/* (non-Javadoc)
		 * @see magoffin.matt.ieat.dao.IngredientDao.IngredientIndexCallbackData#getName()
		 */
		public String getName() {
			return name;
		}
	}

	
	/**
	 * @return the jdbcTemplate
	 */
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	
	/**
	 * @param jdbcTemplate the jdbcTemplate to set
	 */
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	/**
	 * @return the sqlIndexAll
	 */
	public String getSqlIndexAll() {
		return sqlIndexAll;
	}
	
	/**
	 * @param sqlIndexAll the sqlIndexAll to set
	 */
	public void setSqlIndexAll(String sqlIndexAll) {
		this.sqlIndexAll = sqlIndexAll;
	}
	
}
