package com.ejie.t17i.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
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.utils.sql.LikeInWhere;
import com.ejie.t17i.filters.PaisFilter;
import com.ejie.t17i.model.Pais;
import com.ejie.x38.dto.JQGridManager;
import com.ejie.x38.dto.JQGridRequestDto;

/**
 * <p>
 * Implementaci&oacute;n de servicios DAO para acceder a la tabla PAIS de NORA
 * </p>
 * 
XXXX
 * @see com.ejie.t17i.model.Pais
 * @see com.ejie.t17i.dao.PaisDao
 */

@Repository
@Transactional
public class PaisDaoImpl implements PaisDao {

	private JdbcTemplate jdbcTemplate;
	private final RowMapper<Pais> rwMap = new RowMapper<Pais>() {
		@Override
		public Pais mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new Pais(resultSet.getString("ID"),
					resultSet.getString("NOMBRE"));
		}
	};

	/**
	 * M&eacute;todo que asigna el DataSource a usar para el acceso a BBDD.
	 * 
	 * @param dataSource
	 *            El DataSource a usar.
	 */
	@Resource
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional(readOnly = true)
	public Pais find(final Pais pais) {
		if(pais.getId()==null){return null;}
		List<Pais> paisList = this.jdbcTemplate.query(PaisDaoSql.SQL_FIND,
				this.rwMap, pais.getId());
		return DataAccessUtils.uniqueResult(paisList);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional(readOnly = true)
	public List<Pais> findAll(final Pais pais,
			final JQGridRequestDto jqGridRequestDto) {
		StringBuilder query = new StringBuilder(
				PaisDaoSql.SQL_FIND_ALL.length())
				.append(PaisDaoSql.SQL_FIND_ALL);
		final List<Object> params = new ArrayList<Object>();

		PaisFilter filtro = new PaisFilter(pais, params, query, null);
		filtro.generarWhereBE();

		if (jqGridRequestDto != null) {
			query = JQGridManager.getPaginationQuery(jqGridRequestDto, query);
		} else {
			query.append(PaisDaoSql.ORDER);
		}
		return this.jdbcTemplate.query(query.toString(), this.rwMap,
				params.toArray());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional(readOnly = true)
	public Long findAllCount(final Pais pais) {

		final StringBuilder query = new StringBuilder(
				PaisDaoSql.SQL_FIND_ALL_COUNT.length())
				.append(PaisDaoSql.SQL_FIND_ALL_COUNT);
		final List<Object> params = new ArrayList<Object>();

		PaisFilter filtro = new PaisFilter(pais, params, query, null);
		filtro.generarWhereBE();

		return this.jdbcTemplate.queryForLong(query.toString(),
				params.toArray());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional(readOnly = true)
	public List<Pais> findAllLike(Pais pais, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				PaisDaoSql.SQL_FIND_ALL.length())
				.append(PaisDaoSql.SQL_FIND_ALL);
		final List<Object> params = new ArrayList<Object>();

		PaisFilter filtro = new PaisFilter(pais, params, query,
				new LikeInWhere(true, startsWith));
		filtro.generarWhereBE();

		if (jqGridRequestDto != null) {
			query = JQGridManager.getPaginationQuery(jqGridRequestDto, query);
		} else {
			query.append(PaisDaoSql.ORDER);
		}
		return this.jdbcTemplate.query(query.toString(), this.rwMap,
				params.toArray());
	}
}
