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.jdbc.core.RowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.m62fb.utils.sql.LikeInWhere;
import com.ejie.t17i.filters.TipoViaFilter;
import com.ejie.t17i.model.TipoVia;
import com.ejie.x38.dto.JQGridManager;
import com.ejie.x38.dto.JQGridRequestDto;

/**
 * <p>
 * Implementaci&oacute;n de servicios DAO para acceder a la tabla TIPO_VIA de NORA
 * </p> 
XXXX
 * @see com.ejie.t17i.model.TipoVia
 */
@Repository
@Transactional
public class TipoViaDaoImpl implements TipoViaDao {
	private static final String SELECT_SQL = "SELECT t1.ID ID, t1.DS_E DSE, t1.DS_C DSC FROM t17i.T17_TIPO_VIA t1 ";
	private static final String WHERE_ID_SQL = "WHERE t1.ID = ?";
	private static final String FIND_SQL = TipoViaDaoImpl.SELECT_SQL + TipoViaDaoImpl.WHERE_ID_SQL;
	private static final String FIND_ALL_COUNT_SQL = "SELECT COUNT(1) FROM t17i.T17_TIPO_VIA t1 ";
	
	private JdbcTemplate jdbcTemplate;
	private RowMapper<TipoVia> rwMap = new RowMapper<TipoVia>() {
		public TipoVia mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
			String nombreEs = resultSet.getString("DSC");
			int delimiterPos = nombreEs.indexOf("/");
			if(delimiterPos>0){
				nombreEs = nombreEs.substring(0, delimiterPos);
			}
			String nombreEu = resultSet.getString("DSE");
			delimiterPos = nombreEu.indexOf("/");
			if(delimiterPos>0){
				nombreEu = nombreEu.substring(0, delimiterPos);
			}
			return new TipoVia(
					resultSet.getString("ID"),
					nombreEu,
					nombreEs);
		}
	};

	/**
	 * 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 TipoVia find(final TipoVia tipovia) {
		return (TipoVia) this.jdbcTemplate.queryForObject(TipoViaDaoImpl.FIND_SQL, this.rwMap, tipovia.getId());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional(readOnly = true)
	public List<TipoVia> findAll(final TipoVia tipovia,final JQGridRequestDto jqGridRequestDto) {
		StringBuilder query = new StringBuilder(TipoViaDaoImpl.SELECT_SQL.length()).append(TipoViaDaoImpl.SELECT_SQL);
		final List<Object> params = new ArrayList<Object>();
		
		TipoViaFilter filtro= new TipoViaFilter(tipovia,params,query,null);
		filtro.generarWhereBE();

		if (jqGridRequestDto != null) {
			query = JQGridManager.getPaginationQuery(jqGridRequestDto, query);
		}
		return (List<TipoVia>) this.jdbcTemplate.query(query.toString(), this.rwMap,params.toArray());
	}

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

		final StringBuilder query = new StringBuilder(TipoViaDaoImpl.FIND_ALL_COUNT_SQL.length()).append(TipoViaDaoImpl.FIND_ALL_COUNT_SQL);
		final List<Object> params = new ArrayList<Object>();
		
		TipoViaFilter filtro= new TipoViaFilter(tipovia,params,query,null);
		filtro.generarWhereBE();
		
		return (Long) this.jdbcTemplate.queryForLong(query.toString(),params.toArray());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional(readOnly = true)
	public List<TipoVia> findAllLike(final TipoVia tipovia,final JQGridRequestDto jqGridRequestDto,final Boolean startsWith) {
		StringBuilder query = new StringBuilder(TipoViaDaoImpl.SELECT_SQL.length()).append(TipoViaDaoImpl.SELECT_SQL);
		final List<Object> params = new ArrayList<Object>();
		
		TipoViaFilter filtro= new TipoViaFilter(tipovia,params,query,new LikeInWhere(true, startsWith));
		filtro.generarWhereBE();

		if (jqGridRequestDto != null) {
			query = JQGridManager.getPaginationQuery(jqGridRequestDto, query);
		}
		return (List<TipoVia>) this.jdbcTemplate.query(query.toString(), this.rwMap,params.toArray());
	}
}
