package com.ejie.ab61.dao;

import com.ejie.x38.dao.RowNumResultSetExtractor;
import com.ejie.x38.dto.JQGridManager;
import com.ejie.x38.dto.JQGridManagerJerarquia;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRowDto;
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.ab61.comun.utilidades.SecuenciasSQL;
import com.ejie.ab61.model.RelExpInsSinExp;

/**
 * RelExpInsSinExpDaoImpl  
 * 
 *  
 */

@Repository
@Transactional
public class RelExpInsSinExpDaoImpl implements RelExpInsSinExpDao {

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDRELEXPINSSINEXP", "IDEXPEDIENTE",
			"IDINSPECCION", "IDENTIFICADORINSPECCION", "TITULO", "IDTERCERO", "IDDIRECCION", "FECHAREGISTRO",
			"USUARIOREGISTRO" };

	private JdbcTemplate jdbcTemplate;

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<RelExpInsSinExp> rwMap = new RowMapper<RelExpInsSinExp>() {
		public RelExpInsSinExp mapRow(ResultSet resultSet, int rowNum) throws SQLException {

			RelExpInsSinExp relExpInsSinExp = new RelExpInsSinExp();
			relExpInsSinExp.setIdRelExpInsSinExp(resultSet.getInt("IDRELEXPINSSINEXP"));
			relExpInsSinExp.setIdExpediente(resultSet.getInt("IDEXPEDIENTE"));
			relExpInsSinExp.setIdInspeccion(resultSet.getInt("IDINSPECCION"));
			relExpInsSinExp.setIdentificadorInspeccion(resultSet.getString("IDENTIFICADORINSPECCION"));
			relExpInsSinExp.setTitulo(resultSet.getString("TITULO"));
			relExpInsSinExp.setIdTercero(resultSet.getInt("IDTERCERO"));
			relExpInsSinExp.setIdDireccion(resultSet.getInt("IDDIRECCION"));
			relExpInsSinExp.setFechaRegistro(resultSet.getDate("FECHAREGISTRO"));
			relExpInsSinExp.setUsuarioRegistro(resultSet.getString("USUARIOREGISTRO"));

			return relExpInsSinExp;
		}
	};

	private RowMapper<RelExpInsSinExp> rwMapPK = new RowMapper<RelExpInsSinExp>() {
		public RelExpInsSinExp mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new RelExpInsSinExp(resultSet.getInt("IDRELEXPINSSINEXP"));
		}
	};

	/**
	 * Rowmapper para Jerarquía
	 *
	 * @param dataSource
	 *            DataSource
	 * @return
	 */
	private RowMapper<JerarquiaDto<RelExpInsSinExp>> rwMapJerarquia = new RowMapper<JerarquiaDto<RelExpInsSinExp>>() {
		public JerarquiaDto<RelExpInsSinExp> mapRow(ResultSet resultSet, int rowNum) throws SQLException {

			RelExpInsSinExp relexpinssinexp = new RelExpInsSinExp(resultSet.getInt("IDRELEXPINSSINEXP"),
					resultSet.getInt("IDEXPEDIENTE"), resultSet.getInt("IDINSPECCION"),
					resultSet.getString("IDENTIFICADORINSPECCION"), resultSet.getString("TITULO"),
					resultSet.getInt("IDTERCERO"), resultSet.getInt("IDDIRECCION"), resultSet.getDate("FECHAREGISTRO"),
					resultSet.getString("USUARIOREGISTRO"));

			JerarquiaDto<RelExpInsSinExp> jerarquia = new JerarquiaDto<RelExpInsSinExp>();
			jerarquia.setModel(relexpinssinexp);
			jerarquia.setLevel(resultSet.getBigDecimal("LEVEL").intValue());
			jerarquia.setParentNodes(resultSet.getString("PARENTNODES"));
			jerarquia.setIsLeaf(Boolean.parseBoolean(resultSet.getString("ISLEAF")));
			jerarquia.setFilter(Boolean.parseBoolean(resultSet.getString("FILTER")));
			return jerarquia;
		}
	};

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

	/*
	 * OPERACIONES CRUD
	 */

	/**
	 * Inserts a single row in the RelExpInsSinExp table.
	 *
	 * @param relexpinssinexp
	 *            RelExpInsSinExp
	 * @return RelExpInsSinExp
	 */
	public RelExpInsSinExp add(RelExpInsSinExp relexpinssinexp) {

		int idRelExpInsSinExp = this.jdbcTemplate.queryForInt(SecuenciasSQL.getSelectNext("S_AB6129T00"));
		relexpinssinexp.setIdRelExpInsSinExp(idRelExpInsSinExp);

		String query = "INSERT INTO REL_EXP_INS_SIN_EXP (ID_REL_EXP_INS_SIN_EXP, ID_EXPEDIENTE, ID_INSPECCION, IDENTIFICADOR_INSPECCION, TITULO, ID_TERCERO, ID_DIRECCION, FECHA_REGISTRO, USUARIO_REGISTRO) VALUES (?,?,?,?,?,?,?,sysdate,?)";
		this.jdbcTemplate.update(query, relexpinssinexp.getIdRelExpInsSinExp(), relexpinssinexp.getIdExpediente(),
				relexpinssinexp.getIdInspeccion(), relexpinssinexp.getIdentificadorInspeccion(),
				relexpinssinexp.getTitulo(), relexpinssinexp.getIdTercero(), relexpinssinexp.getIdDireccion(),
				relexpinssinexp.getUsuarioRegistro());
		return relexpinssinexp;
	}

	/**
	 * Updates a single row in the RelExpInsSinExp table.
	 *
	 * @param relexpinssinexp
	 *            RelExpInsSinExp
	 * @return RelExpInsSinExp
	 */
	public RelExpInsSinExp update(RelExpInsSinExp relexpinssinexp) {
		String query = "UPDATE REL_EXP_INS_SIN_EXP SET ID_EXPEDIENTE=?, ID_INSPECCION=?, IDENTIFICADOR_INSPECCION=?, TITULO=?, ID_TERCERO=?, ID_DIRECCION=?, FECHA_REGISTRO=?, USUARIO_REGISTRO=? WHERE ID_REL_EXP_INS_SIN_EXP=?";
		this.jdbcTemplate.update(query, relexpinssinexp.getIdExpediente(), relexpinssinexp.getIdInspeccion(),
				relexpinssinexp.getIdentificadorInspeccion(), relexpinssinexp.getTitulo(),
				relexpinssinexp.getIdTercero(), relexpinssinexp.getIdDireccion(), relexpinssinexp.getFechaRegistro(),
				relexpinssinexp.getUsuarioRegistro(), relexpinssinexp.getIdRelExpInsSinExp());
		return relexpinssinexp;
	}

	/**
	 * Finds a single row in the RelExpInsSinExp table.
	 *
	 * @param relexpinssinexp
	 *            RelExpInsSinExp
	 * @return RelExpInsSinExp
	 */
	@Transactional(readOnly = true)
	public RelExpInsSinExp find(RelExpInsSinExp relexpinssinexp) {
		String query = "SELECT t1.ID_REL_EXP_INS_SIN_EXP IDRELEXPINSSINEXP, t1.ID_EXPEDIENTE IDEXPEDIENTE, t1.ID_INSPECCION IDINSPECCION, t1.IDENTIFICADOR_INSPECCION IDENTIFICADORINSPECCION, t1.TITULO TITULO, t1.ID_TERCERO IDTERCERO, t1.ID_DIRECCION IDDIRECCION, t1.FECHA_REGISTRO FECHAREGISTRO, t1.USUARIO_REGISTRO USUARIOREGISTRO FROM REL_EXP_INS_SIN_EXP t1  WHERE t1.ID_REL_EXP_INS_SIN_EXP = ?  ";

		List<RelExpInsSinExp> relexpinssinexpList = this.jdbcTemplate.query(query, this.rwMap,
				relexpinssinexp.getIdRelExpInsSinExp());
		return (RelExpInsSinExp) DataAccessUtils.uniqueResult(relexpinssinexpList);
	}

	/**
	 * Removes a single row in the RelExpInsSinExp table.
	 *
	 * @param relexpinssinexp
	 *            RelExpInsSinExp
	 * @return
	 */
	public void remove(RelExpInsSinExp relexpinssinexp) {
		String query = "DELETE FROM REL_EXP_INS_SIN_EXP WHERE ID_REL_EXP_INS_SIN_EXP=?";
		this.jdbcTemplate.update(query, relexpinssinexp.getIdRelExpInsSinExp());
	}

	/**
	 * Finds a list of rows in the RelExpInsSinExp table.
	 *
	 * @param relexpinssinexp
	 *            RelExpInsSinExp
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<RelExpInsSinExp>
	 */
	@Transactional(readOnly = true)
	public List<RelExpInsSinExp> findAll(RelExpInsSinExp relexpinssinexp, JQGridRequestDto jqGridRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_REL_EXP_INS_SIN_EXP IDRELEXPINSSINEXP,t1.ID_EXPEDIENTE IDEXPEDIENTE,t1.ID_INSPECCION IDINSPECCION,t1.IDENTIFICADOR_INSPECCION IDENTIFICADORINSPECCION,t1.TITULO TITULO,t1.ID_TERCERO IDTERCERO,t1.ID_DIRECCION IDDIRECCION,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		query.append("FROM REL_EXP_INS_SIN_EXP t1 ");

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

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

		if (jqGridRequestDto != null) {
			query = JQGridManager.getPaginationQuery(jqGridRequestDto, query,
					RelExpInsSinExpDaoImpl.ORDER_BY_WHITE_LIST);
		}

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

	/**
	 * Finds rows in the RelExpInsSinExp table using like.
	 *
	 * @param relexpinssinexp
	 *            RelExpInsSinExp
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<RelExpInsSinExp>
	 */
	@Transactional(readOnly = true)
	public List<RelExpInsSinExp> findAllLike(RelExpInsSinExp relexpinssinexp, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_REL_EXP_INS_SIN_EXP IDRELEXPINSSINEXP,t1.ID_EXPEDIENTE IDEXPEDIENTE,t1.ID_INSPECCION IDINSPECCION,t1.IDENTIFICADOR_INSPECCION IDENTIFICADORINSPECCION,t1.TITULO TITULO,t1.ID_TERCERO IDTERCERO,t1.ID_DIRECCION IDDIRECCION,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		query.append("FROM REL_EXP_INS_SIN_EXP t1 ");

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

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

		if (jqGridRequestDto != null) {
			query = JQGridManager.getPaginationQuery(jqGridRequestDto, query,
					RelExpInsSinExpDaoImpl.ORDER_BY_WHITE_LIST);
		}

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

	@Override
	public List<RelExpInsSinExp> findByIdExpediente(Integer idExpediente, JQGridRequestDto jqGridRequestDto) {
		String query = "SELECT t1.ID_REL_EXP_INS_SIN_EXP IDRELEXPINSSINEXP, t1. ID_EXPEDIENTE IDEXPEDIENTE, t1.ID_INSPECCION IDINSPECCION, t1.IDENTIFICADOR_INSPECCION IDENTIFICADORINSPECCION, t1.TITULO TITULO, t1.ID_TERCERO IDTERCERO, t1.ID_DIRECCION IDDIRECCION, t1.FECHA_REGISTRO FECHAREGISTRO, t1.USUARIO_REGISTRO USUARIOREGISTRO FROM REL_EXP_INS_SIN_EXP t1 WHERE t1.ID_EXPEDIENTE = ? ";

		return (List<RelExpInsSinExp>) this.jdbcTemplate.query(query.toString(), this.rwMap, idExpediente);
	}

	@Override
	public Long findByIdExpedienteCount(Integer idExpediente) {
		String query = "SELECT COUNT(1) FROM REL_EXP_INS_SIN_EXP t1 WHERE t1.ID_EXPEDIENTE = ?";

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Counts rows in the RelExpInsSinExp table.
	 *
	 * @param relexpinssinexp
	 *            RelExpInsSinExp
	 * @return Long
	 */
	@Transactional(readOnly = true)
	public Long findAllCount(RelExpInsSinExp relexpinssinexp) {
		StringBuilder query = new StringBuilder("SELECT COUNT(1) FROM REL_EXP_INS_SIN_EXP t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(relexpinssinexp);
		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());
	}

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

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(relexpinssinexp, 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());
	}

	/**
	 * Reorder the data list of RelExpInsSinExp selected for rup_table
	 *
	 * @param relexpinssinexp
	 *            RelExpInsSinExp
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelExpInsSinExp>>
	 */
	@Override
	public List<TableRowDto<RelExpInsSinExp>> reorderSelection(RelExpInsSinExp relexpinssinexp,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_EXP_INS_SIN_EXP IDRELEXPINSSINEXP,t1.ID_EXPEDIENTE IDEXPEDIENTE,t1.ID_INSPECCION IDINSPECCION,t1.IDENTIFICADOR_INSPECCION IDENTIFICADORINSPECCION,t1.TITULO TITULO,t1.ID_TERCERO IDTERCERO,t1.ID_DIRECCION IDDIRECCION,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		query.append("FROM REL_EXP_INS_SIN_EXP t1 ");
		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(relexpinssinexp, startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		// Parámetros de filtrado
		@SuppressWarnings("unchecked")
		List<Object> filterParamList = (List<Object>) mapaWhere.get("params");

		// SQL para la reordenación
		StringBuilder sbReorderSelectionSQL = JQGridManager.getReorderQuery(query, jqGridRequestDto,
				RelExpInsSinExp.class, filterParamList, "ID_REL_EXP_INS_SIN_EXP");

		return this.jdbcTemplate.query(sbReorderSelectionSQL.toString(),
				new RowNumResultSetExtractor<RelExpInsSinExp>(this.rwMapPK, jqGridRequestDto),
				filterParamList.toArray());
	}

	/**
	 * Search method for rup_table
	 *
	 * @param filterParams
	 *            RelExpInsSinExp
	 * @param searchParams
	 *            RelExpInsSinExp
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelExpInsSinExp>>
	 */
	@Override
	public List<TableRowDto<RelExpInsSinExp>> search(RelExpInsSinExp filterParams, RelExpInsSinExp searchParams,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_EXP_INS_SIN_EXP IDRELEXPINSSINEXP,t1.ID_EXPEDIENTE IDEXPEDIENTE,t1.ID_INSPECCION IDINSPECCION,t1.IDENTIFICADOR_INSPECCION IDENTIFICADORINSPECCION,t1.TITULO TITULO,t1.ID_TERCERO IDTERCERO,t1.ID_DIRECCION IDDIRECCION,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		query.append("FROM REL_EXP_INS_SIN_EXP t1 ");
		// TABLAS_ALIAS
		List<String> from_alias = new ArrayList<String>();
		from_alias.add(" t1 ");

		// FILTRADO
		Map<String, ?> mapaWhereFilter = this.getWhereLikeMap(filterParams, startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhereFilter.get("query"));
		query.append(where);
		// Filter params
		@SuppressWarnings("unchecked")
		List<Object> filterParamList = (List<Object>) mapaWhereFilter.get("params");

		// BUSQUEDA
		Map<String, Object> mapaWhereSearch = this.getWhereLikeMap(searchParams, startsWith);
		String searchSQL = ((StringBuilder) mapaWhereSearch.get("query")).toString();
		// Search params
		@SuppressWarnings("unchecked")
		List<Object> searchParamList = (List<Object>) mapaWhereSearch.get("params");

		// SQL
		StringBuilder sbReorderSelectionSQL = JQGridManager.getSearchQuery(query, jqGridRequestDto,
				RelExpInsSinExp.class, filterParamList, searchSQL, searchParamList, from_alias,
				"ID_REL_EXP_INS_SIN_EXP");

		return this.jdbcTemplate.query(sbReorderSelectionSQL.toString(),
				new RowNumResultSetExtractor<RelExpInsSinExp>(this.rwMapPK, jqGridRequestDto),
				filterParamList.toArray());
	}

	/**
	 * Remove multiple method for rup_table
	 *
	 * @param filterrelexpinssinexp
	 *            RelExpInsSinExp
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	@Override
	public void removeMultiple(RelExpInsSinExp filterrelexpinssinexp, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		// SELECT
		/** TODO: select por clave */
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_EXP_INS_SIN_EXP IDRELEXPINSSINEXP,t1.ID_EXPEDIENTE IDEXPEDIENTE,t1.ID_INSPECCION IDINSPECCION,t1.IDENTIFICADOR_INSPECCION IDENTIFICADORINSPECCION,t1.TITULO TITULO,t1.ID_TERCERO IDTERCERO,t1.ID_DIRECCION IDDIRECCION,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		query.append("FROM REL_EXP_INS_SIN_EXP t1 ");

		// WHERE
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterrelexpinssinexp, startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		@SuppressWarnings("unchecked")
		List<Object> params = (List<Object>) mapaWhere.get("params");

		StringBuilder sbRemoveMultipleSQL = JQGridManager.getRemoveMultipleQuery(jqGridRequestDto,
				RelExpInsSinExp.class, query, params, "ID_REL_EXP_INS_SIN_EXP");
		this.jdbcTemplate.update(sbRemoveMultipleSQL.toString(), params.toArray());
	}

	/**
	 * Filter in the hierarchical RelExpInsSinExp table.
	 *
	 * @param filterRelExpInsSinExp
	 *            RelExpInsSinExp
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<JerarquiaDto<RelExpInsSinExp>>
	 */
	@Override
	public List<JerarquiaDto<RelExpInsSinExp>> findAllLikeJerarquia(RelExpInsSinExp filterRelExpInsSinExp,
			JQGridRequestDto jqGridRequestDto) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_EXP_INS_SIN_EXP IDRELEXPINSSINEXP,t1.ID_EXPEDIENTE IDEXPEDIENTE,t1.ID_INSPECCION IDINSPECCION,t1.IDENTIFICADOR_INSPECCION IDENTIFICADORINSPECCION,t1.TITULO TITULO,t1.ID_TERCERO IDTERCERO,t1.ID_DIRECCION IDDIRECCION,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		// StringBuilder from = new StringBuilder("FROM REL_EXP_INS_SIN_EXP t1
		// ");
		List<String> from = new ArrayList<String>();
		from.add("REL_EXP_INS_SIN_EXP");
		// TABLAS_ALIAS
		List<String> from_alias = new ArrayList<String>();
		from_alias.add(" t1 ");

		// JOINS TABLAS
		/*
		 * Ejemplo de como indicar joins entre las tablas de la query
		 */
		/*
		 * StringBuilder joins = new StringBuilder("");
		 * joins.append("AND T1.ID=T2.ID_T1");
		 */

		// CONDICIONES (negocio)
		/*
		 * Ejemplo de como incluir condiciones de negocio en la consulta de la
		 * jerarquía
		 */
		/*
		 * StringBuilder businessFilters = new StringBuilder(); List<Object>
		 * businessParams = new ArrayList<Object>();
		 * businessFilters.append("   AND t1.EJIE = ?  ");
		 * businessParams.add("1");
		 */

		// FILTRO
		Map<String, ?> mapaWhere = this.getWhereLikeMap(filterRelExpInsSinExp, false);

		// JERARQUIA
		// Especificar la columna padre correspondiente en lugar de la generada
		// por defecto (ID_PADRE)
		query = JQGridManagerJerarquia.getQuery(jqGridRequestDto, query, mapaWhere, "ID_REL_EXP_INS_SIN_EXP",
				"ID_PADRE", "NOMBRE", from, from_alias);

		/*
		 * Ejemplo de llamada en caso de indicar joins entre tablas y
		 * condiciones de negocio
		 */
		// query = JQGridManagerJerarquia.getQuery(jqGridRequestDto, query,
		// mapaWhere, "ID", "ID_PADRE", "NOMBRE", from, from_alias, joins,
		// businessFilters, businessParams);

		// PAGINACION
		if (jqGridRequestDto != null) {
			query = JQGridManagerJerarquia.getPaginationQuery(jqGridRequestDto, query,
					RelExpInsSinExpDaoImpl.ORDER_BY_WHITE_LIST);
		}

		List<?> params = (List<?>) mapaWhere.get("params");
		return this.jdbcTemplate.query(query.toString(), this.rwMapJerarquia, params.toArray());
	}

	/**
	 * Count rows in hierarchy
	 *
	 * @param filterRelExpInsSinExp
	 *            RelExpInsSinExp
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return Long
	 */
	@Override
	public Long findAllLikeCountJerarquia(RelExpInsSinExp filterRelExpInsSinExp, JQGridRequestDto jqGridRequestDto) {
		// FROM
		// StringBuilder from = new StringBuilder("FROM REL_EXP_INS_SIN_EXP t1
		// ");
		List<String> from = new ArrayList<String>();
		from.add("REL_EXP_INS_SIN_EXP");

		// TABLAS_ALIAS
		List<String> from_alias = new ArrayList<String>();
		from_alias.add(" t1 ");

		// JOINS TABLAS
		/*
		 * Ejemplo de como indicar joins entre las tablas de la query
		 */
		/*
		 * StringBuilder joins = new StringBuilder("");
		 * joins.append("AND T1.ID=T2.ID_T1");
		 */

		// CONDICIONES (negocio)
		/*
		 * Ejemplo de como incluir condiciones de negocio en la consulta de la
		 * jerarquía
		 */
		/*
		 * StringBuilder businessFilters = new StringBuilder(); List<Object>
		 * businessParams = new ArrayList<Object>();
		 * businessFilters.append("   AND t1.EJIE = ?  ");
		 * businessParams.add("1");
		 */

		// FILTRO
		Map<String, ?> mapaWhere = this.getWhereLikeMap(filterRelExpInsSinExp, false);

		// JERARQUIA
		// Especificar la columna padre correspondiente en lugar de la generada
		// por defecto (ID_PADRE)
		StringBuilder query = JQGridManagerJerarquia.getQueryCount(jqGridRequestDto, mapaWhere,
				"ID_REL_EXP_INS_SIN_EXP", "ID_PADRE", from, from_alias);

		/*
		 * Ejemplo de llamada en caso de indicar joins entre tablas y
		 * condiciones de negocio
		 */
		// StringBuilder query =
		// JQGridManagerJerarquia.getQueryCount(jqGridRequestDto, mapaWhere,
		// "ID", "ID_PADRE", from, from_alias, joins, businessFilters,
		// businessParams);

		List<?> params = (List<?>) mapaWhere.get("params");
		return this.jdbcTemplate.queryForLong(query.toString(), params.toArray());
	}

	/**
	 * Find dependent hierarchical rows
	 *
	 * @param filterRelExpInsSinExp
	 *            RelExpInsSinExp
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<TableRowDto<RelExpInsSinExp>>
	 */
	@Override
	public List<TableRowDto<RelExpInsSinExp>> findAllChild(RelExpInsSinExp filterRelExpInsSinExp,
			JQGridRequestDto jqGridRequestDto) {

		// FROM
		// StringBuilder from = new StringBuilder("FROM REL_EXP_INS_SIN_EXP t1
		// ");
		List<String> from = new ArrayList<String>();
		from.add("REL_EXP_INS_SIN_EXP");

		// TABLAS_ALIAS
		List<String> from_alias = new ArrayList<String>();
		from_alias.add(" t1 ");

		// JOINS TABLAS
		/*
		 * Ejemplo de como indicar joins entre las tablas de la query
		 */
		/*
		 * StringBuilder joins = new StringBuilder("");
		 * joins.append("AND T1.ID=T2.ID_T1");
		 */

		// CONDICIONES (negocio)
		/*
		 * Ejemplo de como incluir condiciones de negocio en la consulta de la
		 * jerarquía
		 */
		/*
		 * StringBuilder businessFilters = new StringBuilder(); List<Object>
		 * businessParams = new ArrayList<Object>();
		 * businessFilters.append("   AND t1.EJIE = ?  ");
		 * businessParams.add("1");
		 */

		// FILTRO
		Map<String, ?> mapaWhere = this.getWhereLikeMap(filterRelExpInsSinExp, false);

		// MULTISELECCION
		// Especificar la columna padre correspondiente en lugar de la generada
		// por defecto (ID_PADRE)
		StringBuilder query = JQGridManagerJerarquia.getQueryChildren(jqGridRequestDto, mapaWhere,
				"ID_REL_EXP_INS_SIN_EXP", "ID_PADRE", from, from_alias);

		/*
		 * Ejemplo de llamada en caso de indicar joins entre tablas y
		 * condiciones de negocio
		 */
		// StringBuilder query =
		// JQGridManagerJerarquia.getQueryChildren(jqGridRequestDto, mapaWhere,
		// "ID", "ID_PADRE", from, from_alias, joins, businessFilters,
		// businessParams);

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

		return this.jdbcTemplate.query(query.toString(),
				new RowNumResultSetExtractor<RelExpInsSinExp>(this.rwMapPK, "ID_REL_EXP_INS_SIN_EXP"),
				params.toArray());
	}

	/*
	 * MÉTODOS PRIVADOS
	 */

	/**
	 * Returns a map with the needed value to create the conditions to filter by
	 * the RelExpInsSinExp entity
	 *
	 * @param relexpinssinexp
	 *            RelExpInsSinExp 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(RelExpInsSinExp relexpinssinexp) {

		StringBuilder where = new StringBuilder(RelExpInsSinExpDaoImpl.STRING_BUILDER_INIT);
		List<Object> params = new ArrayList<Object>();

		if (relexpinssinexp != null && relexpinssinexp.getIdRelExpInsSinExp() != null) {
			where.append(" AND t1.ID_REL_EXP_INS_SIN_EXP = ?");
			params.add(relexpinssinexp.getIdRelExpInsSinExp());
		}
		if (relexpinssinexp != null && relexpinssinexp.getIdExpediente() != null) {
			where.append(" AND t1.ID_EXPEDIENTE = ?");
			params.add(relexpinssinexp.getIdExpediente());
		}
		if (relexpinssinexp != null && relexpinssinexp.getIdInspeccion() != null) {
			where.append(" AND t1.ID_INSPECCION = ?");
			params.add(relexpinssinexp.getIdInspeccion());
		}
		if (relexpinssinexp != null && relexpinssinexp.getIdentificadorInspeccion() != null) {
			where.append(" AND t1.IDENTIFICADOR_INSPECCION = ?");
			params.add(relexpinssinexp.getIdentificadorInspeccion());
		}
		if (relexpinssinexp != null && relexpinssinexp.getTitulo() != null) {
			where.append(" AND t1.TITULO = ?");
			params.add(relexpinssinexp.getTitulo());
		}
		if (relexpinssinexp != null && relexpinssinexp.getIdTercero() != null) {
			where.append(" AND t1.ID_TERCERO = ?");
			params.add(relexpinssinexp.getIdTercero());
		}
		if (relexpinssinexp != null && relexpinssinexp.getIdDireccion() != null) {
			where.append(" AND t1.ID_DIRECCION = ?");
			params.add(relexpinssinexp.getIdDireccion());
		}
		if (relexpinssinexp != null && relexpinssinexp.getFechaRegistro() != null) {
			where.append(" AND t1.FECHA_REGISTRO = ?");
			params.add(relexpinssinexp.getFechaRegistro());
		}
		if (relexpinssinexp != null && relexpinssinexp.getUsuarioRegistro() != null) {
			where.append(" AND t1.USUARIO_REGISTRO = ?");
			params.add(relexpinssinexp.getUsuarioRegistro());
		}

		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 RelExpInsSinExp entity
	 *
	 * @param relexpinssinexp
	 *            RelExpInsSinExp 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, Object> getWhereLikeMap(RelExpInsSinExp relexpinssinexp, Boolean startsWith) {

		StringBuilder where = new StringBuilder(RelExpInsSinExpDaoImpl.STRING_BUILDER_INIT);
		List<Object> params = new ArrayList<Object>();

		if (relexpinssinexp != null && relexpinssinexp.getIdRelExpInsSinExp() != null) {
			where.append(" AND t1.ID_REL_EXP_INS_SIN_EXP = ?");
			params.add(relexpinssinexp.getIdRelExpInsSinExp());
		}
		if (relexpinssinexp != null && relexpinssinexp.getIdExpediente() != null) {
			where.append(" AND t1.ID_EXPEDIENTE = ?");
			params.add(relexpinssinexp.getIdExpediente());
		}
		if (relexpinssinexp != null && relexpinssinexp.getIdInspeccion() != null) {
			where.append(" AND t1.ID_INSPECCION = ?");
			params.add(relexpinssinexp.getIdInspeccion());
		}
		if (relexpinssinexp != null && relexpinssinexp.getIdentificadorInspeccion() != null) {
			where.append(" AND UPPER(t1.IDENTIFICADOR_INSPECCION) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(relexpinssinexp.getIdentificadorInspeccion().toUpperCase() + "%");
			} else {
				params.add("%" + relexpinssinexp.getIdentificadorInspeccion().toUpperCase() + "%");
			}
			where.append(" AND t1.IDENTIFICADOR_INSPECCION IS NOT NULL");
		}
		if (relexpinssinexp != null && relexpinssinexp.getTitulo() != null) {
			where.append(" AND UPPER(t1.TITULO) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(relexpinssinexp.getTitulo().toUpperCase() + "%");
			} else {
				params.add("%" + relexpinssinexp.getTitulo().toUpperCase() + "%");
			}
			where.append(" AND t1.TITULO IS NOT NULL");
		}
		if (relexpinssinexp != null && relexpinssinexp.getIdTercero() != null) {
			where.append(" AND t1.ID_TERCERO = ?");
			params.add(relexpinssinexp.getIdTercero());
		}
		if (relexpinssinexp != null && relexpinssinexp.getIdDireccion() != null) {
			where.append(" AND t1.ID_DIRECCION = ?");
			params.add(relexpinssinexp.getIdDireccion());
		}
		if (relexpinssinexp != null && relexpinssinexp.getFechaRegistro() != null) {
			where.append(" AND t1.FECHA_REGISTRO = ?");
			params.add(relexpinssinexp.getFechaRegistro());
		}
		if (relexpinssinexp != null && relexpinssinexp.getUsuarioRegistro() != null) {
			where.append(" AND UPPER(t1.USUARIO_REGISTRO) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(relexpinssinexp.getUsuarioRegistro().toUpperCase() + "%");
			} else {
				params.add("%" + relexpinssinexp.getUsuarioRegistro().toUpperCase() + "%");
			}
			where.append(" AND t1.USUARIO_REGISTRO 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;

}
