package com.ejie.ad74a.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.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ad74a.model.Riesgos;
import com.ejie.ad74a.model.Solicitantes;
import com.ejie.ad74a.model.Soporte;
import com.ejie.ad74a.util.UtilSQL;
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;

/**
 * RiesgosDaoImpl generated by UDA, 22-may-2023 13:11:28.
 * 
 * @author UDA
 */

@Repository
@Transactional
public class RiesgosDaoImpl implements RiesgosDao {

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDRIESGO21", "ALTORIESGO21", "NIVELMALTRATO21",
			"NIVELSUICIDIO21", "IDVALORACION21", "IDSOLICITANTE21", "USUHISTORICO21", "FHISTORICO21", "FALTA21",
			"USUALTA21" };

	private JdbcTemplate jdbcTemplate;

	public static final String SEQUENCE_RIESGOS = "SEQ_RIESGOS";

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<Riesgos> rwMap = new RowMapper<Riesgos>() {
		public Riesgos mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new Riesgos(UtilSQL.getLong(resultSet, "IDRIESGO21"), UtilSQL.getInt(resultSet, "ALTORIESGO21"),
					UtilSQL.getInt(resultSet, "NIVELMALTRATO21"), UtilSQL.getInt(resultSet, "NIVELSUICIDIO21"),
					UtilSQL.getLong(resultSet, "IDVALORACION21"), UtilSQL.getLong(resultSet, "IDSOLICITANTE21"),
					resultSet.getString("USUHISTORICO21"), resultSet.getDate("FHISTORICO21"),
					resultSet.getDate("FALTA21"), resultSet.getString("USUALTA21"));
		}
	};

	private RowMapper<Riesgos> rwMapHistorico = new RowMapper<Riesgos>() {
		public Riesgos mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new Riesgos(UtilSQL.getLong(resultSet, "IDRIESGO21"), UtilSQL.getInt(resultSet, "ALTORIESGO21"),
					UtilSQL.getInt(resultSet, "NIVELMALTRATO21"), UtilSQL.getInt(resultSet, "NIVELSUICIDIO21"),
					resultSet.getString("USUHISTORICO21"), resultSet.getDate("FHISTORICO21"));
		}
	};

	private RowMapper<Riesgos> rwMapPK = new RowMapper<Riesgos>() {
		public Riesgos mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new Riesgos(UtilSQL.getLong(resultSet, "IDRIESGO21"));
		}
	};

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

			Riesgos riesgos = new Riesgos(UtilSQL.getLong(resultSet, "IDRIESGO21"),
					UtilSQL.getInt(resultSet, "ALTORIESGO21"), UtilSQL.getInt(resultSet, "NIVELMALTRATO21"),
					UtilSQL.getInt(resultSet, "NIVELSUICIDIO21"), UtilSQL.getLong(resultSet, "IDVALORACION21"),
					UtilSQL.getLong(resultSet, "IDSOLICITANTE21"), resultSet.getString("USUHISTORICO21"),
					resultSet.getDate("FHISTORICO21"), resultSet.getDate("FALTA21"), resultSet.getString("USUALTA21"));

			JerarquiaDto<Riesgos> jerarquia = new JerarquiaDto<Riesgos>();
			jerarquia.setModel(riesgos);
			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 Riesgos table.
	 *
	 * @param riesgos Riesgos
	 * @return Riesgos
	 */
	public Riesgos add(Riesgos riesgos) {

		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();

		String query = "INSERT INTO RIESGOS (ID_RIESGO_21, ALTO_RIESGO_21, NIVEL_MALTRATO_21, NIVEL_SUICIDIO_21, ID_VALORACION_21, ID_SOLICITANTE_21, USU_HISTORICO_21, F_HISTORICO_21, F_ALTA_21, USU_ALTA_21) "
				+ "VALUES (" + SEQUENCE_RIESGOS
				+ ".NEXTVAL,:altoRiesgo,:nivelMaltrato,:nivelSuicidio,:idValoracion,:idSolicitante,:usuHistorico,:fHistorico,sysdate,:usuAlta)";

		MapSqlParameterSource parameters = new MapSqlParameterSource().addValue("altoRiesgo", riesgos.getAltoRiesgo21())
				.addValue("nivelMaltrato", riesgos.getNivelMaltrato21())
				.addValue("nivelSuicidio", riesgos.getNivelSuicidio21())
				.addValue("idValoracion", riesgos.getIdValoracion21())
				.addValue("idSolicitante", riesgos.getIdSolicitante21())
				.addValue("usuHistorico", riesgos.getUsuHistorico21()).addValue("usuAlta", riesgos.getUsuAlta21())
				.addValue("fHistorico", riesgos.getfHistorico21()).addValue("usuAlta", riesgos.getUsuAlta21());

		NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
		namedParameterJdbcTemplate.update(query, parameters, generatedKeyHolder, new String[] { "ID_RIESGO_21" });

		riesgos.setIdRiesgo21(generatedKeyHolder.getKey().longValue());

		return riesgos;
	}

	/**
	 * Updates a single row in the Riesgos table.
	 *
	 * @param riesgos Riesgos
	 * @return Riesgos
	 */
	public Riesgos update(Riesgos riesgos) {
		String query = "UPDATE RIESGOS SET ALTO_RIESGO_21=?, NIVEL_MALTRATO_21=?, NIVEL_SUICIDIO_21=?, ID_VALORACION_21=?, ID_SOLICITANTE_21=?, USU_HISTORICO_21=?, F_HISTORICO_21=?, F_ALTA_21=?, USU_ALTA_21=? WHERE ID_RIESGO_21=?";
		this.jdbcTemplate.update(query, riesgos.getAltoRiesgo21(), riesgos.getNivelMaltrato21(),
				riesgos.getNivelSuicidio21(), riesgos.getIdValoracion21(), riesgos.getIdSolicitante21(),
				riesgos.getUsuHistorico21(), riesgos.getfHistorico21(), riesgos.getfAlta21(), riesgos.getUsuAlta21(),
				riesgos.getIdRiesgo21());
		return riesgos;
	}

	/**
	 * Updates a single row in the Riesgos table as historico.
	 *
	 * @param riesgos Riesgos
	 * @return Riesgos
	 */
	public Riesgos updateHistorico(Riesgos riesgos) {
		String query = "UPDATE RIESGOS SET USU_HISTORICO_21=?, F_HISTORICO_21=SYSDATE WHERE ID_RIESGO_21=?";
		this.jdbcTemplate.update(query, riesgos.getUsuHistorico21(), riesgos.getIdRiesgo21());
		return riesgos;
	}

	/**
	 * Finds a single row in the Riesgos table.
	 *
	 * @param riesgos Riesgos
	 * @return Riesgos
	 */
	@Transactional(readOnly = true)
	public Riesgos find(Riesgos riesgos) {
		String query = "SELECT t1.ID_RIESGO_21 IDRIESGO21, t1.ALTO_RIESGO_21 ALTORIESGO21, t1.NIVEL_MALTRATO_21 NIVELMALTRATO21, t1.NIVEL_SUICIDIO_21 NIVELSUICIDIO21, t1.ID_VALORACION_21 IDVALORACION21, t1.ID_SOLICITANTE_21 IDSOLICITANTE21, t1.USU_HISTORICO_21 USUHISTORICO21, t1.F_HISTORICO_21 FHISTORICO21, t1.F_ALTA_21 FALTA21, t1.USU_ALTA_21 USUALTA21 FROM RIESGOS t1  WHERE t1.ID_RIESGO_21 = ?  ";

		List<Riesgos> riesgosList = this.jdbcTemplate.query(query, this.rwMap, riesgos.getIdRiesgo21());
		return (Riesgos) DataAccessUtils.uniqueResult(riesgosList);
	}

	/**
	 * Removes a single row in the Riesgos table.
	 *
	 * @param riesgos Riesgos
	 * @return
	 */
	public void remove(Riesgos riesgos) {
		String query = "DELETE FROM RIESGOS WHERE ID_RIESGO_21=?";
		this.jdbcTemplate.update(query, riesgos.getIdRiesgo21());
	}

	/**
	 * Exporta Datos al clipBoard
	 *
	 */
	public List<Riesgos> getMultiple(Riesgos filterRiesgos, TableRequestDto tableRequestDto, Boolean startsWith) {

		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_RIESGO_21 IDRIESGO21, t1.ALTO_RIESGO_21 ALTORIESGO21, t1.NIVEL_MALTRATO_21 NIVELMALTRATO21, t1.NIVEL_SUICIDIO_21 NIVELSUICIDIO21, t1.ID_VALORACION_21 IDVALORACION21, t1.ID_SOLICITANTE_21 IDSOLICITANTE21, t1.USU_HISTORICO_21 USUHISTORICO21, t1.F_HISTORICO_21 FHISTORICO21, t1.F_ALTA_21 FALTA21, t1.USU_ALTA_21 USUALTA21");
		query.append(" FROM RIESGOS t1  ");
		// Where clause & Params
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterRiesgos, startsWith);
		StringBuilder where = new StringBuilder(query + " WHERE 1=1 ");
		where.append(mapaWhere.get("query"));

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

		StringBuilder sbMultipleSQL = TableManager.getSelectMultipleQuery(tableRequestDto, Riesgos.class, params,
				"ID_RIESGO_21");
		where.append(sbMultipleSQL);

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

	/**
	 * Finds a list of rows in the Riesgos table.
	 *
	 * @param riesgos         Riesgos
	 * @param tableRequestDto TableRequestDto
	 * @return List<Riesgos>
	 */
	@Transactional(readOnly = true)
	public List<Riesgos> findAll(Riesgos riesgos, TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_RIESGO_21 IDRIESGO21,t1.ALTO_RIESGO_21 ALTORIESGO21,t1.NIVEL_MALTRATO_21 NIVELMALTRATO21,t1.NIVEL_SUICIDIO_21 NIVELSUICIDIO21,t1.ID_VALORACION_21 IDVALORACION21,t1.ID_SOLICITANTE_21 IDSOLICITANTE21,t1.USU_HISTORICO_21 USUHISTORICO21,t1.F_HISTORICO_21 FHISTORICO21,t1.F_ALTA_21 FALTA21,t1.USU_ALTA_21 USUALTA21 ");
		query.append("FROM RIESGOS t1 ");

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

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

	/**
	 * Finds rows in the Riesgos table using like.
	 *
	 * @param riesgos         Riesgos
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<Riesgos>
	 */
	@Transactional(readOnly = true)
	public List<Riesgos> findAllLike(Riesgos riesgos, TableRequestDto tableRequestDto, Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_RIESGO_21 IDRIESGO21,t1.ALTO_RIESGO_21 ALTORIESGO21,t1.NIVEL_MALTRATO_21 NIVELMALTRATO21,t1.NIVEL_SUICIDIO_21 NIVELSUICIDIO21,t1.ID_VALORACION_21 IDVALORACION21,t1.ID_SOLICITANTE_21 IDSOLICITANTE21,t1.USU_HISTORICO_21 USUHISTORICO21,t1.F_HISTORICO_21 FHISTORICO21,t1.F_ALTA_21 FALTA21,t1.USU_ALTA_21 USUALTA21 ");
		query.append("FROM RIESGOS t1 ");

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

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

	/**
	 * Finds rows in the Riesgos table using like.
	 *
	 * @param riesgos         Riesgos
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<Riesgos>
	 */
	@Transactional(readOnly = true)
	public List<Riesgos> findAllLikeHistorico(Riesgos riesgos, TableRequestDto tableRequestDto, Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_RIESGO_21 IDRIESGO21,t1.ALTO_RIESGO_21 ALTORIESGO21,t1.NIVEL_MALTRATO_21 NIVELMALTRATO21,t1.NIVEL_SUICIDIO_21 NIVELSUICIDIO21,t1.USU_HISTORICO_21 USUHISTORICO21,t1.F_HISTORICO_21 FHISTORICO21 ");
		query.append("FROM RIESGOS t1 ");

		// Where clause & Params
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(" AND t1.F_HISTORICO_21 IS NOT NULL AND t1.ID_SOLICITANTE_21 = ?");
		query.append(where);

		List<Object> params = new ArrayList<Object>();
		params.add(riesgos.getIdSolicitante21());

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(riesgos);
		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 Riesgos table using like.
	 *
	 * @param riesgos    Riesgos
	 * @param startsWith Boolean
	 * @return Long
	 */
	@Transactional(readOnly = true)
	public Long findAllLikeCount(Riesgos riesgos, Boolean startsWith) {
		StringBuilder query = new StringBuilder("SELECT COUNT(1) FROM RIESGOS t1 ");

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

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

		// Where clause & Params
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append("AND t1.F_HISTORICO_21 IS NOT NULL AND t1.ID_SOLICITANTE_21 = ?");
		query.append(where);

		List<Object> params = new ArrayList<Object>();
		params.add(riesgos.getIdSolicitante21());

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

	/**
	 * Reorder the data list of Riesgos selected for rup_table
	 *
	 * @param riesgos         Riesgos
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<TableRowDto<Riesgos>>
	 */
	@Override
	public List<TableRowDto<Riesgos>> reorderSelection(Riesgos riesgos, TableRequestDto tableRequestDto,
			Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_RIESGO_21 IDRIESGO21,t1.ALTO_RIESGO_21 ALTORIESGO21,t1.NIVEL_MALTRATO_21 NIVELMALTRATO21,t1.NIVEL_SUICIDIO_21 NIVELSUICIDIO21,t1.ID_VALORACION_21 IDVALORACION21,t1.ID_SOLICITANTE_21 IDSOLICITANTE21,t1.USU_HISTORICO_21 USUHISTORICO21,t1.F_HISTORICO_21 FHISTORICO21,t1.F_ALTA_21 FALTA21,t1.USU_ALTA_21 USUALTA21 ");
		// FROM
		query.append("FROM RIESGOS t1 ");
		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(riesgos, 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, Riesgos.class,
				filterParamList, "IDRIESGO21");

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

	/**
	 * Reorder the data list of Riesgos selected for rup_table
	 *
	 * @param riesgos         Riesgos
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<TableRowDto<Riesgos>>
	 */
	@Override
	public List<TableRowDto<Riesgos>> reorderSelectionHistorico(Riesgos riesgos, TableRequestDto tableRequestDto,
			Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_RIESGO_21 IDRIESGO21,t1.ALTO_RIESGO_21 ALTORIESGO21,t1.NIVEL_MALTRATO_21 NIVELMALTRATO21,t1.NIVEL_SUICIDIO_21 NIVELSUICIDIO21,t1.USU_HISTORICO_21 USUHISTORICO21,t1.F_HISTORICO_21 FHISTORICO21 ");
		// FROM
		query.append("FROM RIESGOS t1 ");
		// FILTRADO
		// Where clause & Params
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append("AND t1.F_HISTORICO_21 IS NOT NULL AND t1.ID_SOLICITANTE_21 = ?");
		query.append(where);

		// Parámetros de filtrado
		List<Object> filterParamList = new ArrayList<Object>();
		filterParamList.add(riesgos.getIdSolicitante21());

		// SQL para la reordenación
		StringBuilder sbReorderSelectionSQL = TableManager.getReorderQuery(query, tableRequestDto, Riesgos.class,
				filterParamList, "IDRIESGO21");

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

	/**
	 * Search method for rup_table
	 *
	 * @param filterParams    Riesgos
	 * @param searchParams    Riesgos
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<TableRowDto<Riesgos>>
	 */
	@Override
	public List<TableRowDto<Riesgos>> search(Riesgos filterParams, Riesgos searchParams,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_RIESGO_21 IDRIESGO21,t1.ALTO_RIESGO_21 ALTORIESGO21,t1.NIVEL_MALTRATO_21 NIVELMALTRATO21,t1.NIVEL_SUICIDIO_21 NIVELSUICIDIO21,t1.ID_VALORACION_21 IDVALORACION21,t1.ID_SOLICITANTE_21 IDSOLICITANTE21,t1.USU_HISTORICO_21 USUHISTORICO21,t1.F_HISTORICO_21 FHISTORICO21,t1.F_ALTA_21 FALTA21,t1.USU_ALTA_21 USUALTA21 ");
		// FROM
		query.append("FROM RIESGOS 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, Riesgos.class,
				filterParamList, searchSQL, searchParamList, from_alias, "IDRIESGO21");

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

	/**
	 * Remove multiple method for rup_table
	 *
	 * @param tableRequestDto TableRequestDto
	 */
	@Override
	public void removeMultiple(TableRequestDto tableRequestDto) {
		StringBuilder sbRemoveMultipleSQL = TableManager.getRemoveMultipleQuery(tableRequestDto, Riesgos.class,
				"RIESGOS", new String[] { "IDRIESGO21" });

		List<String> params = tableRequestDto.getMultiselection().getSelectedIds();

		this.jdbcTemplate.update(sbRemoveMultipleSQL.toString(), params.toArray());
	}

	/**
	 * Filter in the hierarchical Riesgos table.
	 *
	 * @param filterRiesgos   Riesgos
	 * @param tableRequestDto TableRequestDto
	 * @return List<JerarquiaDto<Riesgos>>
	 */
	@Override
	public List<JerarquiaDto<Riesgos>> findAllLikeJerarquia(Riesgos filterRiesgos, TableRequestDto tableRequestDto) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_RIESGO_21 IDRIESGO21,t1.ALTO_RIESGO_21 ALTORIESGO21,t1.NIVEL_MALTRATO_21 NIVELMALTRATO21,t1.NIVEL_SUICIDIO_21 NIVELSUICIDIO21,t1.ID_VALORACION_21 IDVALORACION21,t1.ID_SOLICITANTE_21 IDSOLICITANTE21,t1.USU_HISTORICO_21 USUHISTORICO21,t1.F_HISTORICO_21 FHISTORICO21,t1.F_ALTA_21 FALTA21,t1.USU_ALTA_21 USUALTA21 ");
		// FROM
		// StringBuilder from = new StringBuilder("FROM RIESGOS t1 ");
		List<String> from = new ArrayList<String>();
		from.add("RIESGOS");
		// 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(filterRiesgos, false);

		// JERARQUIA
		// Especificar la columna padre correspondiente en lugar de la generada por
		// defecto (ID_PADRE)
		query = TableManagerJerarquia.getQuery(tableRequestDto, query, mapaWhere, "IDRIESGO21", "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,
					RiesgosDaoImpl.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 filterRiesgos   Riesgos
	 * @param tableRequestDto TableRequestDto
	 * @return Long
	 */
	@Override
	public Long findAllLikeCountJerarquia(Riesgos filterRiesgos, TableRequestDto tableRequestDto) {
		// FROM
		// StringBuilder from = new StringBuilder("FROM RIESGOS t1 ");
		List<String> from = new ArrayList<String>();
		from.add("RIESGOS");

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

		// JERARQUIA
		// Especificar la columna padre correspondiente en lugar de la generada por
		// defecto (ID_PADRE)
		StringBuilder query = TableManagerJerarquia.getQueryCount(tableRequestDto, mapaWhere, "IDRIESGO21", "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 filterRiesgos   Riesgos
	 * @param tableRequestDto TableRequestDto
	 * @return List<TableRowDto<Riesgos>>
	 */
	@Override
	public List<TableRowDto<Riesgos>> findAllChild(Riesgos filterRiesgos, TableRequestDto tableRequestDto) {

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

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

		// MULTISELECCION
		// Especificar la columna padre correspondiente en lugar de la generada por
		// defecto (ID_PADRE)
		StringBuilder query = TableManagerJerarquia.getQueryChildren(tableRequestDto, mapaWhere, "IDRIESGO21",
				"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<Riesgos>(this.rwMapPK, "IDRIESGO21"), params.toArray());
	}

	/*
	 * MÉTODOS PRIVADOS
	 */

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

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

		if (riesgos != null && riesgos.getIdRiesgo21() != null) {
			where.append(" AND t1.ID_RIESGO_21 = ?");
			params.add(riesgos.getIdRiesgo21());
		}
		if (riesgos != null && riesgos.getAltoRiesgo21() != null) {
			where.append(" AND t1.ALTO_RIESGO_21 = ?");
			params.add(riesgos.getAltoRiesgo21());
		}
		if (riesgos != null && riesgos.getNivelMaltrato21() != null) {
			where.append(" AND t1.NIVEL_MALTRATO_21 = ?");
			params.add(riesgos.getNivelMaltrato21());
		}
		if (riesgos != null && riesgos.getNivelSuicidio21() != null) {
			where.append(" AND t1.NIVEL_SUICIDIO_21 = ?");
			params.add(riesgos.getNivelSuicidio21());
		}
		if (riesgos != null && riesgos.getIdValoracion21() != null) {
			where.append(" AND t1.ID_VALORACION_21 = ?");
			params.add(riesgos.getIdValoracion21());
		}
		if (riesgos != null && riesgos.getIdSolicitante21() != null) {
			where.append(" AND t1.ID_SOLICITANTE_21 = ?");
			params.add(riesgos.getIdSolicitante21());
		}
		if (riesgos != null && riesgos.getUsuHistorico21() != null) {
			where.append(" AND t1.USU_HISTORICO_21 = ?");
			params.add(riesgos.getUsuHistorico21());
		}
		if (riesgos != null && riesgos.getfHistorico21() != null) {
			where.append(" AND t1.F_HISTORICO_21 = ?");
			params.add(riesgos.getfHistorico21());
		}
		if (riesgos != null && riesgos.getfAlta21() != null) {
			where.append(" AND t1.F_ALTA_21 = ?");
			params.add(riesgos.getfAlta21());
		}
		if (riesgos != null && riesgos.getUsuAlta21() != null) {
			where.append(" AND t1.USU_ALTA_21 = ?");
			params.add(riesgos.getUsuAlta21());
		}

		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
	 * Riesgos entity
	 *
	 * @param riesgos    Riesgos 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(Riesgos riesgos, Boolean startsWith) {

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

		if (riesgos != null && riesgos.getIdRiesgo21() != null) {
			where.append(" AND t1.ID_RIESGO_21 = ?");
			params.add(riesgos.getIdRiesgo21());
		}
		if (riesgos != null && riesgos.getAltoRiesgo21() != null) {
			where.append(" AND t1.ALTO_RIESGO_21 = ?");
			params.add(riesgos.getAltoRiesgo21());
		}
		if (riesgos != null && riesgos.getNivelMaltrato21() != null) {
			where.append(" AND t1.NIVEL_MALTRATO_21 = ?");
			params.add(riesgos.getNivelMaltrato21());
		}
		if (riesgos != null && riesgos.getNivelSuicidio21() != null) {
			where.append(" AND t1.NIVEL_SUICIDIO_21 = ?");
			params.add(riesgos.getNivelSuicidio21());
		}
		if (riesgos != null && riesgos.getIdValoracion21() != null) {
			where.append(" AND t1.ID_VALORACION_21 = ?");
			params.add(riesgos.getIdValoracion21());
		}
		if (riesgos != null && riesgos.getIdSolicitante21() != null) {
			where.append(" AND t1.ID_SOLICITANTE_21 = ?");
			params.add(riesgos.getIdSolicitante21());
		}
		if (riesgos != null && riesgos.getUsuHistorico21() != null) {
			where.append(" AND UPPER(t1.USU_HISTORICO_21) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(riesgos.getUsuHistorico21().toUpperCase() + "%");
			} else {
				params.add("%" + riesgos.getUsuHistorico21().toUpperCase() + "%");
			}
			where.append(" AND t1.USU_HISTORICO_21 IS NOT NULL");
		}
		if (riesgos != null && riesgos.getfHistorico21() != null) {
			where.append(" AND t1.F_HISTORICO_21 = ?");
			params.add(riesgos.getfHistorico21());
		}
		if (riesgos != null && riesgos.getfAlta21() != null) {
			where.append(" AND t1.F_ALTA_21 = ?");
			params.add(riesgos.getfAlta21());
		}
		if (riesgos != null && riesgos.getUsuAlta21() != null) {
			where.append(" AND UPPER(t1.USU_ALTA_21) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(riesgos.getUsuAlta21().toUpperCase() + "%");
			} else {
				params.add("%" + riesgos.getUsuAlta21().toUpperCase() + "%");
			}
			where.append(" AND t1.USU_ALTA_21 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;

	@Override
	public Riesgos findActivoBySolicitante(Solicitantes sol) {
		String query = "SELECT t1.ID_RIESGO_21 IDRIESGO21, t1.ALTO_RIESGO_21 ALTORIESGO21, t1.NIVEL_MALTRATO_21 NIVELMALTRATO21, "
				+ "t1.NIVEL_SUICIDIO_21 NIVELSUICIDIO21, t1.ID_VALORACION_21 IDVALORACION21, t1.ID_SOLICITANTE_21 IDSOLICITANTE21, "
				+ "t1.USU_HISTORICO_21 USUHISTORICO21, t1.F_HISTORICO_21 FHISTORICO21, t1.F_ALTA_21 FALTA21, t1.USU_ALTA_21 USUALTA21 "
				+ "FROM RIESGOS t1 WHERE t1.ID_SOLICITANTE_21 = ? AND  F_HISTORICO_21 IS NULL ";

		List<Riesgos> soporteList = this.jdbcTemplate.query(query, this.rwMap, sol.getIdSolicitante01());
		return (Riesgos) DataAccessUtils.uniqueResult(soporteList);
	}
}
