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.AbstractTipoGenericoR09;
import com.ejie.m62fb.model.tipos.R09Tipos;

/**
 * ComboR09DaoImpl
 * 
XXXX
 */
@Repository
@Transactional
@SuppressWarnings("unchecked")
public class ComboR09DaoImpl<T extends AbstractTipoGenericoR09> implements
		ComboR09Dao<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 Tipo
			T elemento = null;
			try {
				elemento = (T) R09Tipos.getInstance(resultSet
						.getString("CODTIPOENTIDAD"));
			} catch (Exception e) {
				throw new SQLException("Ha ocurrido un error (R09).", e);
			}
			elemento.setIdElereg(resultSet.getString("IDELEREG"));
			elemento.setCodTipoEntidad(resultSet.getString("CODTIPOENTIDAD"));
			elemento.setDescripcionEs(resultSet.getString("DESCOMBOCAS"));
			elemento.setDescripcionEu(resultSet.getString("DESCOMBOEUS"));
			return elemento;
		}
	};

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

	/**
	 * Devuelve la lista de registros preparados para mostrar en un combo
	 * 
	 * @param filtro
	 *            T
	 * @return List List<? extends AbstractTipoGenericoR09>
	 */
	@Override
	@Transactional(readOnly = true)
	public List<? extends AbstractTipoGenericoR09> findAll(T filtro) {
		StringBuilder query = new StringBuilder(
				"SELECT ID_ELEREG IDELEREG, COD_TIPO_ENTIDAD CODTIPOENTIDAD, COD_ELEM_CLASIF CODELEMCLASIF, ");
		query.append(" LPAD(CHR(46),(NIVEL-1)*3,CHR(46)) || DES_COMBO_CAS as DESCOMBOCAS,");
		query.append(" LPAD(CHR(46),(NIVEL-1)*3,CHR(46)) || DES_COMBO_EUS as DESCOMBOEUS");

		query.append(" FROM TIPOS_CLASIFICACION_VIV ");
		query.append(" WHERE COD_TIPO_ENTIDAD = ? ");
		query.append(" ORDER BY NUM_ORDEN ASC ");

		return this.jdbcTemplate.query(query.toString(), this.rwMap,
				filtro.getCodTipoEntidad());
	}

	/**
	 * Devuelve los datos de un registro
	 * 
	 * @param filtro
	 *            T
	 * @return T
	 */
	@Override
	@Transactional(readOnly = true)
	public T find(T filtro) {
		StringBuilder query = new StringBuilder(
				"SELECT ID_ELEREG IDELEREG, COD_TIPO_ENTIDAD CODTIPOENTIDAD, COD_ELEM_CLASIF CODELEMCLASIF, ");
		query.append(" LPAD(CHR(46),(NIVEL-1)*3,CHR(46)) || DES_COMBO_CAS as DESCOMBOCAS,");
		query.append(" LPAD(CHR(46),(NIVEL-1)*3,CHR(46)) || DES_COMBO_EUS as DESCOMBOEUS");

		query.append(" FROM TIPOS_CLASIFICACION_VIV ");
		query.append(" WHERE ID_ELEREG = ? ");

		return DataAccessUtils.uniqueResult(this.jdbcTemplate.query(
				query.toString(), this.rwMap, filtro.getIdElereg()));
	}
	/**
	 * Devuelve los datos de un registro
	 * 
	 * @param filtro
	 *            T
	 * @return T
	 */
	@Override
	@Transactional(readOnly = true)
	public T findTabla(T filtro) {
		StringBuilder query = new StringBuilder(
				"SELECT ID_ELEREG IDELEREG, COD_TIPO_ENTIDAD CODTIPOENTIDAD, COD_ELEM_CLASIF CODELEMCLASIF, ");
		query.append(" DES_COMBO_CAS as DESCOMBOCAS,");
		query.append(" DES_COMBO_EUS as DESCOMBOEUS");

		query.append(" FROM TIPOS_CLASIFICACION_VIV ");
		query.append(" WHERE ID_ELEREG = ? ");

		return DataAccessUtils.uniqueResult(this.jdbcTemplate.query(
				query.toString(), this.rwMap, filtro.getIdElereg()));
	}
	
}