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.PortalFilter;
import com.ejie.t17i.model.Calle;
import com.ejie.t17i.model.Localidad;
import com.ejie.t17i.model.Portal;
import com.ejie.x38.dto.JQGridManager;
import com.ejie.x38.dto.JQGridRequestDto;

/**
 * <p>
 * Implementaci&oacute;n de servicios DAO para acceder a la tabla PORTAL de NORA
 * </p>
 * 
XXXX
 * @see com.ejie.t17i.model.Portal
 * @see com.ejie.t17i.dao.PortalDao
 */
@Repository
@Transactional
public class PortalDaoImpl implements PortalDao {

	private JdbcTemplate jdbcTemplate;
	@Autowired
	private CalleDao calleDao;
	@Autowired
	private LocalidadDao localidadDao;
	private final RowMapper<Portal> rwMapDetail = new RowMapper<Portal>() {
		@Override
		public Portal mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
			return new Portal(
					resultSet.getBigDecimal("ID"),
					resultSet.getString("BLOQUE"),
					resultSet.getString("NUMERO"),
					resultSet.getString("BIS"),
					resultSet.getString("ESTADO"),
					resultSet.getString("CODIGOPOSTAL") != null ? resultSet
							.getString("CODIGOPOSTAL").trim() : "",
					resultSet.getBigDecimal("TRAMOID"),
					resultSet.getString("ACEPCION"),
					resultSet.getBigDecimal("DXED50"),
					resultSet.getBigDecimal("DYED50"),
					resultSet.getBigDecimal("DXETRS89"),
					resultSet.getBigDecimal("DYETRS89"),
					calleDao.find(new Calle(resultSet.getBigDecimal("CALLEID"))),
					localidadDao.find(new Localidad(resultSet
							.getBigDecimal("LOCALIDADID"))));
		}
	};
	private final RowMapper<Portal> rwMap = new RowMapper<Portal>() {
		@Override
		public Portal mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
			return new Portal(resultSet.getBigDecimal("ID"),
					resultSet.getString("BLOQUE"), resultSet.getString("NUMERO"),
					resultSet.getString("BIS"), resultSet.getString("ESTADO"),
					resultSet.getString("CODIGOPOSTAL") != null ? resultSet
							.getString("CODIGOPOSTAL").trim() : "",
					resultSet.getBigDecimal("TRAMOID"),
					resultSet.getString("ACEPCION"),
					resultSet.getBigDecimal("DXED50"),
					resultSet.getBigDecimal("DYED50"),
					resultSet.getBigDecimal("DXETRS89"),
					resultSet.getBigDecimal("DYETRS89"), null, null);
		}
	};
	private final RowMapper<Portal> rwMapCP = new RowMapper<Portal>() {
		@Override
		public Portal mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
			return new Portal(resultSet.getString("CODIGOPOSTAL"));
		}
	};
	/**
	 * 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 Portal find(final Portal portal) {
		List<Portal> portalList = this.jdbcTemplate.query(
				PortalDaoSql.SQL_FIND, this.rwMapDetail, portal.getId());
		return DataAccessUtils.uniqueResult(portalList);
	}

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

		PortalFilter filtro = new PortalFilter(portal, params, query, null);
		filtro.generarWhereBE();
		if (jqGridRequestDto != null) {
			query = JQGridManager.getPaginationQuery(jqGridRequestDto, query);
		} else {
			query.append(PortalDaoSql.ORDER);
		}

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

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

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

		PortalFilter filtro = new PortalFilter(portal, params, query, null);
		filtro.generarWhereBE();

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

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

		PortalFilter filtro = new PortalFilter(portal, params, query,
				new LikeInWhere(true, startsWith));
		filtro.generarWhereBE();

		if (jqGridRequestDto != null) {
			query = JQGridManager.getPaginationQuery(jqGridRequestDto, query);
		} else {
			query.append(PortalDaoSql.ORDER);
		}
		return this.jdbcTemplate.query(query.toString(), this.rwMap,
				params.toArray());
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional(readOnly = true)
	public List<Portal> findAllLikeCP(final Portal portal,
			final JQGridRequestDto jqGridRequestDto, final Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				PortalDaoSql.SQL_FIND_ALL_CP.length())
				.append(PortalDaoSql.SQL_FIND_ALL_CP);
		final List<Object> params = new ArrayList<Object>();

		PortalFilter filtro = new PortalFilter(portal, params, query,
				new LikeInWhere(true, startsWith));
		filtro.generarWhereBE();

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