package com.ejie.m62fb.dao;

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

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.m62fb.model.tipos.AbstractTipoGenericoP28;
import com.ejie.m62fb.model.tipos.P28Tipos;

/**
 * TODO
 * 
XXXX
 */

@Repository
@Transactional
public class ComboP28DaoImpl<T extends AbstractTipoGenericoP28> implements
		ComboP28Dao<T> {

	private JdbcTemplate jdbcTemplate;
	private final RowMapper<T> rwMap = new RowMapper<T>() {
		@Override
		public T mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			// LLamar factory devuelve tipoElemento
			T instance = null;
			try {
				instance = (T) P28Tipos.getInstance(resultSet
						.getLong("NUM_REGISTRO"));
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			instance.setNumRegistro(resultSet.getLong("NUM_REGISTRO"));
			instance.setCodTabla(resultSet.getString("COD_TABLA"));
			instance.setDescripcionEs(resultSet.getString("DES_TABLA_CAS"));
			instance.setDescripcionEu(resultSet.getString("DES_TABLA_EUS"));
			return instance;

		}
	};

	/**
	 * Method use to set the datasource.
	 * 
	 * @param dataSource
	 *            DataSource
	 * @return
	 */
	@Resource
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	@Override
	@Transactional(readOnly = true)
	public List<? extends AbstractTipoGenericoP28> findAll(T combo) {
		StringBuilder query = new StringBuilder(
				"SELECT  NUM_REGISTRO,COD_TABLA,DES_TABLA_CAS,DES_TABLA_EUS FROM VALOR_COMBOS WHERE NUM_REGISTRO = ? ORDER BY COD_TABLA ASC");
		return this.jdbcTemplate.query(query.toString(), this.rwMap,
				combo.getNumRegistro());
	}

	@Override
	@Transactional(readOnly = true)
	public T find(T combo) {
		StringBuilder query = new StringBuilder(
				"SELECT  NUM_REGISTRO,COD_TABLA,DES_TABLA_CAS,DES_TABLA_EUS FROM VALOR_COMBOS WHERE NUM_REGISTRO = ? AND COD_TABLA = ?");

		return DataAccessUtils.uniqueResult(this.jdbcTemplate.query(
				query.toString(), this.rwMap, combo.getNumRegistro(),
				combo.getCodTabla()));
	}

	/**
	 * Finds a List of rows in the Combo table.
	 * 
	 * @param combo
	 *            Combo
	 * @return List
	 */
	// @Override
	// @Transactional(readOnly = true)
	// public List<ComboP28> findAll(ComboP28 combo) {
	//
	// StringBuilder query = new StringBuilder(
	// "SELECT  NUM_REGISTRO,COD_TABLA,DES_TABLA_CAS,DES_TABLA_EUS FROM M62P28S00 WHERE NUM_REGISTRO = ? ORDER BY COD_TABLA ASC");
	// return this.jdbcTemplate.query(query.toString(), this.rwMap,
	// combo.getNumRegistro());
	// }

	// /**
	// * Finds a single rows in the Combo table.
	// *
	// * @param combo
	// * Combo
	// * @return ComboP28
	// */
	// @Override
	// @Transactional(readOnly = true)
	// public ComboP28 find(ComboP28 combo) {
	//
	// StringBuilder query = new StringBuilder(
	// "SELECT  NUM_REGISTRO,COD_TABLA,DES_TABLA_CAS,DES_TABLA_EUS FROM M62P28S00 WHERE NUM_REGISTRO = ? AND COD_TABLA = ?");
	//
	// return DataAccessUtils.uniqueResult(this.jdbcTemplate.query(
	// query.toString(), this.rwMap, combo.getNumRegistro(),
	// combo.getCodTabla()));
	//
	// }

}
