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.PlantaFilter;
import com.ejie.t17i.model.Planta;
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 PLANTA de NORA
 * </p>
 * 
XXXX
 * @see com.ejie.t17i.model.Planta
 * @see com.ejie.t17i.dao.PlantaDao
 */
@Repository
@Transactional
public class PlantaDaoImpl implements PlantaDao {

	private JdbcTemplate jdbcTemplate;
	@Autowired
	private PortalDao portalDao;
	private final RowMapper<Planta> rwMapDetail = new RowMapper<Planta>() {
		@Override
		public Planta mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
			return new Planta(resultSet.getBigDecimal("ID"),
					portalDao.find(new Portal(resultSet
							.getBigDecimal("PORTALID"))),
					resultSet.getString("ESCALERA"),
					resultSet.getString("PISO"));
		}
	};
	private final RowMapper<Planta> rwMap = new RowMapper<Planta>() {
		@Override
		public Planta mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
			return new Planta(resultSet.getBigDecimal("ID"), new Portal(
					resultSet.getBigDecimal("PORTALID")),
					resultSet.getString("ESCALERA"),
					resultSet.getString("PISO"));
		}
	};

	/**
	 * 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 Planta find(final Planta planta) {
		List<Planta> plantaList = this.jdbcTemplate.query(
				PlantaDaoSql.SQL_FIND, this.rwMapDetail, planta.getId());
		return DataAccessUtils.uniqueResult(plantaList);
	}

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

		PlantaFilter filtro = new PlantaFilter(planta, params, query, null);
		filtro.generarWhereBE();
		if (jqGridRequestDto != null) {
			query = JQGridManager.getPaginationQuery(jqGridRequestDto, query);
		} else {
			query.append(PlantaDaoSql.ORDER);
		}

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

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

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

		PlantaFilter filtro = new PlantaFilter(planta, params, query, null);
		filtro.generarWhereBE();

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

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional(readOnly = true)
	public Long findAllCountEscalera(final Planta planta) {
		final StringBuilder query = new StringBuilder(
				PlantaDaoSql.SQL_FIND_ALL_COUNT.length())
				.append(PlantaDaoSql.SQL_FIND_ALL_COUNT);
		final List<Object> params = new ArrayList<Object>();

		PlantaFilter filtro = new PlantaFilter(planta, params, query, null);
		filtro.generarWhereBE();
		query.append(" AND trim(ESCALERA) IS NOT NULL");
		return this.jdbcTemplate.queryForLong(query.toString(),
				params.toArray());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional(readOnly = true)
	public Long findAllCountPiso(final Planta planta) {
		final StringBuilder query = new StringBuilder(
				PlantaDaoSql.SQL_FIND_ALL_COUNT.length())
				.append(PlantaDaoSql.SQL_FIND_ALL_COUNT);
		final List<Object> params = new ArrayList<Object>();

		PlantaFilter filtro = new PlantaFilter(planta, params, query, null);
		filtro.generarWhereBE();

		query.append(" AND trim(PISO) IS NOT NULL");
		return this.jdbcTemplate.queryForLong(query.toString(),
				params.toArray());
	}

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

		PlantaFilter filtro = new PlantaFilter(planta, params, query,
				new LikeInWhere(true, startsWith));
		filtro.generarWhereBE();

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

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional(readOnly = true)
	public List<Planta> findAllLikeEscalera(final Planta planta,
			final JQGridRequestDto jqGridRequestDto, final Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				PlantaDaoSql.SQL_FIND_ALL_ESCALERA.length())
				.append(PlantaDaoSql.SQL_FIND_ALL_ESCALERA);
		final List<Object> params = new ArrayList<Object>();

		PlantaFilter filtro = new PlantaFilter(planta, params, query,
				new LikeInWhere(true, startsWith));
		filtro.generarWhereBE();

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