package com.ejie.ab79.dao.inspecciones;

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.ab79.model.inspecciones.Inspecciones;
import com.ejie.ab79.model.inspecciones.RelExpIns;
import com.ejie.x38.dao.RowNumResultSetExtractor;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableManager;
import com.ejie.x38.dto.TableManagerJerarquia;
import com.ejie.x38.dto.TableRequestDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * RelExpInsDaoImpl  
 * 
 *  
 */

@Repository
@Transactional
public class RelExpInsDaoImpl implements RelExpInsDao {

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDRELEXPINS", "IDEXPEDIENTE", "IDINSPECCION",
			"TIPOINSPECCION", "FECHAREGISTRO", "USUARIOREGISTRO" };

	private JdbcTemplate jdbcTemplate;

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<RelExpIns> rwMap = new RowMapper<RelExpIns>() {
		@Override
		public RelExpIns mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new RelExpIns(resultSet.getInt("IDRELEXPINS"), resultSet.getInt("IDEXPEDIENTE"),
					new Inspecciones(resultSet.getInt("IDINSPECCION")), resultSet.getInt("TIPOINSPECCION"),
					resultSet.getString("USUARIOREGISTRO"));
		}
	};

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

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

			RelExpIns relexpins = new RelExpIns(resultSet.getInt("IDRELEXPINS"), resultSet.getInt("IDEXPEDIENTE"),
					new Inspecciones(resultSet.getInt("IDINSPECCION")), resultSet.getInt("TIPOINSPECCION"),
					resultSet.getString("USUARIOREGISTRO"));

			JerarquiaDto<RelExpIns> jerarquia = new JerarquiaDto<RelExpIns>();
			jerarquia.setModel(relexpins);
			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 RelExpIns table.
	 *
	 * @param relexpins
	 *            RelExpIns
	 * @return RelExpIns
	 */
	@Override
	public RelExpIns add(RelExpIns relexpins) {
		int idRelExpIns = this.jdbcTemplate.queryForObject("SELECT AB61.S_AB6143T00.NEXTVAL FROM DUAL", Integer.class);
		String query = "INSERT INTO REL_EXP_INS (ID_REL_EXP_INS, ID_EXPEDIENTE, ID_INSPECCION, TIPO_INSPECCION, FECHA_REGISTRO, USUARIO_REGISTRO, IDENTIFICADOR_INSPECCION) VALUES (?,?,?,?,SYSDATE,?,?)";
		this.jdbcTemplate.update(query, idRelExpIns, relexpins.getIdExpediente(),
				relexpins.getInspecciones().getIdInspeccion(), relexpins.getTipoInspeccion(),
				relexpins.getUsuarioRegistro(), relexpins.getInspecciones().getIdentificadorInspeccion());
		return relexpins;
	}

	/**
	 * Finds a single row in the RelExpIns table.
	 *
	 * @param relexpins
	 *            RelExpIns
	 * @return RelExpIns
	 */
	@Override
	@Transactional(readOnly = true)
	public RelExpIns find(RelExpIns relexpins) {
		String query = "SELECT t1.ID_REL_EXP_INS IDRELEXPINS, t1.ID_EXPEDIENTE IDEXPEDIENTE, t1.ID_INSPECCION IDINSPECCION, t1.TIPO_INSPECCION TIPOINSPECCION, t1.FECHA_REGISTRO FECHAREGISTRO, t1.USUARIO_REGISTRO USUARIOREGISTRO FROM REL_EXP_INS t1  WHERE t1.ID_REL_EXP_INS = ?  ";

		List<RelExpIns> relexpinsList = this.jdbcTemplate.query(query, this.rwMap, relexpins.getIdRelExpIns());
		return DataAccessUtils.uniqueResult(relexpinsList);
	}

	/**
	 * Removes a single row in the RelExpIns table.
	 *
	 * @param relexpins
	 *            RelExpIns
	 * @return
	 */
	@Override
	public void remove(RelExpIns relexpins) {
		String query = "DELETE FROM REL_EXP_INS WHERE ID_REL_EXP_INS=?";
		this.jdbcTemplate.update(query, relexpins.getIdRelExpIns());
	}

	@Override
	public void removeByInspeccion(Inspecciones inspeccion) {
		String query = "DELETE FROM REL_EXP_INS WHERE ID_INSPECCION = ?";
		this.jdbcTemplate.update(query, inspeccion.getIdInspeccion());
	}

	/**
	 * Exporta Datos al clipBoard
	 *
	 */
	@Override
	public List<RelExpIns> getMultiple(RelExpIns filterRelExpIns, TableRequestDto tableRequestDto, Boolean startsWith) {

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

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

		StringBuilder sbMultipleSQL = TableManager.getSelectMultipleQuery(tableRequestDto, RelExpIns.class, params,
				"ID_REL_EXP_INS");

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

	/**
	 * Finds a list of rows in the RelExpIns table.
	 *
	 * @param relexpins
	 *            RelExpIns
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @return List<RelExpIns>
	 */
	@Override
	@Transactional(readOnly = true)
	public List<RelExpIns> findAll(RelExpIns relexpins, TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_REL_EXP_INS IDRELEXPINS,t1.ID_EXPEDIENTE IDEXPEDIENTE,t1.ID_INSPECCION IDINSPECCION,t1.TIPO_INSPECCION TIPOINSPECCION,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		query.append("FROM REL_EXP_INS t1 ");

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

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

		if (tableRequestDto != null) {
			query = TableManager.getPaginationQuery(tableRequestDto, query, RelExpInsDaoImpl.ORDER_BY_WHITE_LIST);
		}

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

	/**
	 * Finds rows in the RelExpIns table using like.
	 *
	 * @param relexpins
	 *            RelExpIns
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<RelExpIns>
	 */
	@Override
	@Transactional(readOnly = true)
	public List<RelExpIns> findAllLike(RelExpIns relexpins, TableRequestDto tableRequestDto, Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_REL_EXP_INS IDRELEXPINS,t1.ID_EXPEDIENTE IDEXPEDIENTE,t1.ID_INSPECCION IDINSPECCION,t1.TIPO_INSPECCION TIPOINSPECCION,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		query.append("FROM REL_EXP_INS t1 ");

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

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

		if (tableRequestDto != null) {
			query = TableManager.getPaginationQuery(tableRequestDto, query, RelExpInsDaoImpl.ORDER_BY_WHITE_LIST);
		}

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

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

		return this.jdbcTemplate.queryForObject(query.toString(), params.toArray(), Long.class);
	}

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

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(relexpins, 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.queryForObject(query.toString(), params.toArray(), Long.class);
	}

	/**
	 * Reorder the data list of RelExpIns selected for rup_table
	 *
	 * @param relexpins
	 *            RelExpIns
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelExpIns>>
	 */
	@Override
	public List<TableRowDto<RelExpIns>> reorderSelection(RelExpIns relexpins, TableRequestDto tableRequestDto,
			Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_EXP_INS IDRELEXPINS,t1.ID_EXPEDIENTE IDEXPEDIENTE,t1.ID_INSPECCION IDINSPECCION,t1.TIPO_INSPECCION TIPOINSPECCION,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		query.append("FROM REL_EXP_INS t1 ");
		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(relexpins, 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 = TableManager.getReorderQuery(query, tableRequestDto, RelExpIns.class,
				filterParamList, "ID_REL_EXP_INS");

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

	/**
	 * Search method for rup_table
	 *
	 * @param filterParams
	 *            RelExpIns
	 * @param searchParams
	 *            RelExpIns
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelExpIns>>
	 */
	@Override
	public List<TableRowDto<RelExpIns>> search(RelExpIns filterParams, RelExpIns searchParams,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_EXP_INS IDRELEXPINS,t1.ID_EXPEDIENTE IDEXPEDIENTE,t1.ID_INSPECCION IDINSPECCION,t1.TIPO_INSPECCION TIPOINSPECCION,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		query.append("FROM REL_EXP_INS 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 = TableManager.getSearchQuery(query, tableRequestDto, RelExpIns.class,
				filterParamList, searchSQL, searchParamList, from_alias, "ID_REL_EXP_INS");

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

	/**
	 * Remove multiple method for rup_table
	 *
	 * @param filterrelexpins
	 *            RelExpIns
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	@Override
	public void removeMultiple(RelExpIns filterrelexpins, TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		/** TODO: select por clave */
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_EXP_INS IDRELEXPINS,t1.ID_EXPEDIENTE IDEXPEDIENTE,t1.ID_INSPECCION IDINSPECCION,t1.TIPO_INSPECCION TIPOINSPECCION,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		query.append("FROM REL_EXP_INS t1 ");

		// WHERE
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterrelexpins, 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 = TableManager.getRemoveMultipleQuery(tableRequestDto, RelExpIns.class, query,
				params, "ID_REL_EXP_INS");
		this.jdbcTemplate.update(sbRemoveMultipleSQL.toString(), params.toArray());
	}

	/**
	 * Filter in the hierarchical RelExpIns table.
	 *
	 * @param filterRelExpIns
	 *            RelExpIns
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @return List<JerarquiaDto<RelExpIns>>
	 */
	@Override
	public List<JerarquiaDto<RelExpIns>> findAllLikeJerarquia(RelExpIns filterRelExpIns,
			TableRequestDto tableRequestDto) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_EXP_INS IDRELEXPINS,t1.ID_EXPEDIENTE IDEXPEDIENTE,t1.ID_INSPECCION IDINSPECCION,t1.TIPO_INSPECCION TIPOINSPECCION,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		// StringBuilder from = new StringBuilder("FROM REL_EXP_INS t1 ");
		List<String> from = new ArrayList<String>();
		from.add("REL_EXP_INS");
		// 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(filterRelExpIns, false);

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

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

		// PAGINACION
		if (tableRequestDto != null) {
			query = TableManagerJerarquia.getPaginationQuery(tableRequestDto, query,
					RelExpInsDaoImpl.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 filterRelExpIns
	 *            RelExpIns
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @return Long
	 */
	@Override
	public Long findAllLikeCountJerarquia(RelExpIns filterRelExpIns, TableRequestDto tableRequestDto) {
		// FROM
		// StringBuilder from = new StringBuilder("FROM REL_EXP_INS t1 ");
		List<String> from = new ArrayList<String>();
		from.add("REL_EXP_INS");

		// 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(filterRelExpIns, false);

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

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

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

	/**
	 * Find dependent hierarchical rows
	 *
	 * @param filterRelExpIns
	 *            RelExpIns
	 * @param tableRequestDto
	 *            TableRequestDto
	 * @return List<TableRowDto<RelExpIns>>
	 */
	@Override
	public List<TableRowDto<RelExpIns>> findAllChild(RelExpIns filterRelExpIns, TableRequestDto tableRequestDto) {

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

		// 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(filterRelExpIns, false);

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

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

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

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

	/*
	 * MÉTODOS PRIVADOS
	 */

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

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

		if (relexpins != null && relexpins.getIdRelExpIns() != null) {
			where.append(" AND t1.ID_REL_EXP_INS = ?");
			params.add(relexpins.getIdRelExpIns());
		}
		if (relexpins != null && relexpins.getIdExpediente() != null) {
			where.append(" AND t1.ID_EXPEDIENTE = ?");
			params.add(relexpins.getIdExpediente());
		}
		if (relexpins != null && relexpins.getInspecciones().getIdInspeccion() != null) {
			where.append(" AND t1.ID_INSPECCION = ?");
			params.add(relexpins.getInspecciones().getIdInspeccion());
		}
		if (relexpins != null && relexpins.getTipoInspeccion() != null) {
			where.append(" AND t1.TIPO_INSPECCION = ?");
			params.add(relexpins.getTipoInspeccion());
		}
		if (relexpins != null && relexpins.getUsuarioRegistro() != null) {
			where.append(" AND t1.USUARIO_REGISTRO = ?");
			params.add(relexpins.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 RelExpIns entity
	 *
	 * @param relexpins
	 *            RelExpIns 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(RelExpIns relexpins, Boolean startsWith) {

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

		if (relexpins != null && relexpins.getIdRelExpIns() != null) {
			where.append(" AND t1.ID_REL_EXP_INS = ?");
			params.add(relexpins.getIdRelExpIns());
		}
		if (relexpins != null && relexpins.getIdExpediente() != null) {
			where.append(" AND t1.ID_EXPEDIENTE = ?");
			params.add(relexpins.getIdExpediente());
		}
		if (relexpins != null && relexpins.getInspecciones().getIdInspeccion() != null) {
			where.append(" AND t1.ID_INSPECCION = ?");
			params.add(relexpins.getInspecciones().getIdInspeccion());
		}
		if (relexpins != null && relexpins.getTipoInspeccion() != null) {
			where.append(" AND t1.TIPO_INSPECCION = ?");
			params.add(relexpins.getTipoInspeccion());
		}
		if (relexpins != null && relexpins.getUsuarioRegistro() != null) {
			where.append(" AND UPPER(t1.USUARIO_REGISTRO) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(relexpins.getUsuarioRegistro().toUpperCase() + "%");
			} else {
				params.add("%" + relexpins.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;
	}

	@Override
	public RelExpIns updateFechaFinInspeccion(RelExpIns relExpIns) {
		String query = "UPDATE REL_EXP_INS SET FECHA_FIN_INSPECCION = ?, FECHA_ULTMODIFICACION = SYSDATE, USUARIO_ULTMODIFICACION = ? WHERE IDENTIFICADOR_INSPECCION = ? ";
		this.jdbcTemplate.update(query, relExpIns.getInspecciones().getFechaFinInspeccion(),
				relExpIns.getUsuarioUltmodificacion(), relExpIns.getInspecciones().getIdentificadorInspeccion());
		return relExpIns;
	}

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

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

}
