package com.ejie.m62fb.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.i18n.I18nString;
import com.ejie.m62fb.model.M62fbMunicipios;
import com.ejie.x38.dto.Pagination;

/**
 * M62fbMunicipiosDaoImpl generated by UDA, 22-oct-2012 12:14:19.
 * 
XXXX
 */

@Repository
@Transactional
public class M62fbMunicipiosDaoImpl implements M62fbMunicipiosDao {
	private JdbcTemplate jdbcTemplate;
	private RowMapper<M62fbMunicipios> rwMap = new RowMapper<M62fbMunicipios>() {
		public M62fbMunicipios mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
			HashMap<String, String> mp = new HashMap<String, String>();
			mp.put("es", resultSet.getString("DESMUNICCAS"));
			mp.put("eu", resultSet.getString("DESMUNICEUS"));

			return new M62fbMunicipios(resultSet.getString("CODPROVINCIA"),
					resultSet.getString("CODMUNICIPIO"), new I18nString(
							resultSet.getString("DESMUNICCAS"), mp));
		}
	};

	/**
	 * Method use to set the datasource.
	 * 
	 * @param dataSource
	 *            DataSource
	 * @return
	 */
	@Resource
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	/**
	 * Finds a single row in the M62fbMunicipios table.
	 * 
	 * @param m62fbmunicipios
	 *            Pagination
	 * @return M62fbMunicipios
	 */
	@Transactional(readOnly = true, timeout = 500)
	public M62fbMunicipios find(M62fbMunicipios m62fbmunicipios) {
		String query = "SELECT t1.COD_PROVINCIA CODPROVINCIA, t1.COD_MUNICIPIO CODMUNICIPIO, t1.DES_MUNIC_CAS DESMUNICCAS, t1.DES_MUNIC_EUS DESMUNICEUS FROM M62A10V0S00_MUNICIPIOS t1  WHERE t1.COD_PROVINCIA = ?   AND t1.COD_MUNICIPIO = ?   AND t1.DES_MUNIC_CAS = ?  ";

		List<M62fbMunicipios> m62fbmunicipiosList = this.jdbcTemplate.query(
				query, this.rwMap, m62fbmunicipios.getCodProvincia(),
				m62fbmunicipios.getCodMunicipio(),
				m62fbmunicipios.getDesMunicCas());
		return (M62fbMunicipios) DataAccessUtils
				.uniqueResult(m62fbmunicipiosList);
	}

	/**
	 * Finds a List of rows in the M62fbMunicipios table.
	 * 
	 * @param m62fbmunicipios
	 *            M62fbMunicipios
	 * @param pagination
	 *            Pagination
	 * @return List
	 */
	@Transactional(readOnly = true, timeout = 500)
	public List<M62fbMunicipios> findAll(M62fbMunicipios m62fbmunicipios,
			Pagination pagination) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.COD_PROVINCIA CODPROVINCIA,t1.COD_MUNICIPIO CODMUNICIPIO,t1.DES_MUNIC_CAS DESMUNICCAS, t1.DES_MUNIC_EUS DESMUNICEUS ");
		query.append("FROM M62A10V0S00_MUNICIPIOS t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(m62fbmunicipios);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		List<?> params = (List<?>) mapaWhere.get("params");

		if (pagination != null) {
			query = pagination.getPaginationQuery(query);
		}

		return (List<M62fbMunicipios>) this.jdbcTemplate.query(
				query.toString(), this.rwMap, params.toArray());
	}

	/**
	 * Counts rows in the M62fbMunicipios table.
	 * 
	 * @param m62fbmunicipios
	 *            M62fbMunicipios
	 * @return Long
	 */
	@Transactional(readOnly = true, timeout = 500)
	public Long findAllCount(M62fbMunicipios m62fbmunicipios) {
		StringBuilder query = new StringBuilder(
				"SELECT COUNT(1) FROM M62A10V0S00_MUNICIPIOS t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(m62fbmunicipios);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		List<?> params = (List<?>) mapaWhere.get("params");

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

	/**
	 * Finds rows in the M62fbMunicipios table using like.
	 * 
	 * @param m62fbmunicipios
	 *            M62fbMunicipios
	 * @param pagination
	 *            Pagination
	 * @param startsWith
	 *            Boolean
	 * @return List
	 */
	@Transactional(readOnly = true, timeout = 500)
	public List<M62fbMunicipios> findAllLike(M62fbMunicipios m62fbmunicipios,
			Pagination pagination, Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.COD_PROVINCIA CODPROVINCIA,t1.COD_MUNICIPIO CODMUNICIPIO,t1.DES_MUNIC_CAS DESMUNICCAS,t1.DES_MUNIC_EUS DESMUNICEUS,t1.DEC_MUNIC_CAS DECMUNICCAS,t1.DEC_MUNIC_EUS DECMUNICEUS,t1.FEC_ALTA FECALTA,t1.DIR_IP_USUARIO DIRIPUSUARIO,t1.FEH_ULT_MODIF FEHULTMODIF,t1.COD_USU_ULT_MODIF CODUSUULTMODIF,t1.IDE_REGISTRO IDEREGISTRO ");
		query.append("FROM M62A10V0S00_MUNICIPIOS t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(m62fbmunicipios,
				startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		List<?> params = (List<?>) mapaWhere.get("params");

		if (pagination != null) {
			query = pagination.getPaginationQuery(query);
		}

		return (List<M62fbMunicipios>) this.jdbcTemplate.query(
				query.toString(), this.rwMap, params.toArray());
	}

	/**
	 * Counts rows in the M62fbMunicipios table using like.
	 * 
	 * @param m62fbmunicipios
	 *            M62fbMunicipios
	 * @param startsWith
	 *            Boolean
	 * @return Long
	 */
	@Transactional(readOnly = true, timeout = 500)
	public Long findAllLikeCount(M62fbMunicipios m62fbmunicipios,
			Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT COUNT(1) FROM M62A10V0S00_MUNICIPIOS t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(m62fbmunicipios,
				startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		List<?> params = (List<?>) mapaWhere.get("params");

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

	/**
	 * Returns a map with the needed value to create the conditions to filter by
	 * the M62fbMunicipios entity
	 * 
	 * @param m62fbmunicipios
	 *            M62fbMunicipios Bean with the criteria values to filter by.
	 * @return Map created with two keys key query stores the sql query syntax
	 *         key params stores the parameter values to be used in the
	 *         condition sentence.
	 */
	// CHECKSTYLE:OFF CyclomaticComplexity - Generación de código de UDA
	private Map<String, ?> getWhereMap(M62fbMunicipios m62fbmunicipios) {

		StringBuffer where = new StringBuffer(
				M62fbMunicipiosDaoImpl.STRING_BUILDER_INIT);
		List<Object> params = new ArrayList<Object>();

		if (m62fbmunicipios != null
				&& m62fbmunicipios.getCodProvincia() != null) {
			where.append(" AND t1.COD_PROVINCIA = ?");
			params.add(m62fbmunicipios.getCodProvincia());
		}
		if (m62fbmunicipios != null
				&& m62fbmunicipios.getCodMunicipio() != null) {
			where.append(" AND t1.COD_MUNICIPIO = ?");
			params.add(m62fbmunicipios.getCodMunicipio());
		}
		if (m62fbmunicipios != null && m62fbmunicipios.getDesMunicCas() != null) {
			where.append(" AND t1.DES_MUNIC_CAS = ?");
			params.add(m62fbmunicipios.getDesMunicCas());
		}

		Map<String, Object> mapWhere = new HashMap<String, Object>();
		mapWhere.put("query", where);
		mapWhere.put("params", params);

		return mapWhere;
	}

	// CHECKSTYLE:ON CyclomaticComplexity - Generación de código de UDA

	/**
	 * Returns a map with the needed value to create the conditions to filter by
	 * the M62fbMunicipios entity
	 * 
	 * @param m62fbmunicipios
	 *            M62fbMunicipios Bean with the criteria values to filter by.
	 * @param startsWith
	 *            Boolean
	 * @return Map created with two keys key query stores the sql query syntax
	 *         key params stores the parameter values to be used in the
	 *         condition sentence.
	 */
	// CHECKSTYLE:OFF CyclomaticComplexity - Generación de código de UDA
	private Map<String, ?> getWhereLikeMap(M62fbMunicipios m62fbmunicipios,
			Boolean startsWith) {

		StringBuffer where = new StringBuffer(
				M62fbMunicipiosDaoImpl.STRING_BUILDER_INIT);
		List<Object> params = new ArrayList<Object>();

		if (m62fbmunicipios != null
				&& m62fbmunicipios.getCodProvincia() != null) {
			where.append(" AND UPPER(t1.COD_PROVINCIA) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(m62fbmunicipios.getCodProvincia().toUpperCase()
						+ "%");
			} else {
				params.add("%"
						+ m62fbmunicipios.getCodProvincia().toUpperCase() + "%");
			}
			where.append(" AND t1.COD_PROVINCIA IS NOT NULL");
		}
		if (m62fbmunicipios != null
				&& m62fbmunicipios.getCodMunicipio() != null) {
			where.append(" AND UPPER(t1.COD_MUNICIPIO) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(m62fbmunicipios.getCodMunicipio().toUpperCase()
						+ "%");
			} else {
				params.add("%"
						+ m62fbmunicipios.getCodMunicipio().toUpperCase() + "%");
			}
			where.append(" AND t1.COD_MUNICIPIO IS NOT NULL");
		}
		/*
		 * if (m62fbmunicipios != null && m62fbmunicipios.getDesMunicCas() !=
		 * null) {
		 * where.append(" AND UPPER(t1.DES_MUNIC_CAS) like ? ESCAPE  '\\'"); if
		 * (startsWith) {
		 * params.add(m62fbmunicipios.getDesMunicCas().toUpperCase() + "%"); }
		 * else { params.add("%" +
		 * m62fbmunicipios.getDesMunicCas().toUpperCase() + "%"); }
		 * where.append(" AND t1.DES_MUNIC_CAS IS NOT NULL"); }
		 */

		Map<String, Object> mapWhere = new HashMap<String, Object>();
		mapWhere.put("query", where);
		mapWhere.put("params", params);

		return mapWhere;
	}

	// CHECKSTYLE:ON CyclomaticComplexity - Generación de código de UDA

	/**
	 * StringBuilder initilization value
	 */
	public static final int STRING_BUILDER_INIT = 4096;
}
