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.beans.factory.annotation.Autowired;
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.MunicipioFilter;
import com.ejie.t17i.model.Municipio;
import com.ejie.t17i.model.Provincia;
import com.ejie.x38.dto.JQGridManager;
import com.ejie.x38.dto.JQGridRequestDto;

/**
 * <p>
 * Implementaci&oacute;n de servicios DAO para acceder a la tabla MUNICIPIO de
 * NORA
 * </p>
 * 
XXXX
 * @see com.ejie.t17i.model.Municipio
 * @see com.ejie.t17i.model.Provincia
 * @see com.ejie.t17i.dao.MunicipioDao
 */
@Repository
@Transactional
public class MunicipioDaoImpl implements MunicipioDao {

	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ProvinciaDao provinciaDao;

	private final RowMapper<Municipio> rwMap = new RowMapper<Municipio>() {
		@Override
		public Municipio mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
			return new Municipio(provinciaDao.find(new Provincia(resultSet
					.getString("PROVINCIAID"))), resultSet.getString("ID"),
					resultSet.getString("NOMBRE"),
					resultSet.getString("COMARCAID"));
		}
	};

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

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional(readOnly = true)
	public Municipio find(Municipio municipio) {
		if(municipio.getId()==null){return null;}
		List<Municipio> municipioList = this.jdbcTemplate.query(
				MunicipioDaoSql.SQL_FIND, this.rwMap, municipio.getProvincia()
						.getId(), municipio.getId());
		return DataAccessUtils.uniqueResult(municipioList);
	}

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

		MunicipioFilter filtro = new MunicipioFilter(municipio, params, query,
				null);
		filtro.generarWhereBE();

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

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional(readOnly = true)
	public Long findAllCount(Municipio municipio) {

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

		MunicipioFilter filtro = new MunicipioFilter(municipio, params, query,
				null);
		filtro.generarWhereBE();

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

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

		MunicipioFilter filtro = new MunicipioFilter(municipio, params, query,
				new LikeInWhere(true, startsWith));
		filtro.generarWhereBE();

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