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.Ayuntamientos;
import com.ejie.ad74a.model.MaestroTrSociales;
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;

/**
 * MaestroTrSocialesDaoImpl generated by UDA, 19-abr-2023 16:38:34.
 * 
 * @author UDA
 */

@Repository
@Transactional
public class MaestroTrSocialesDaoImpl implements MaestroTrSocialesDao {

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDTRSOCIAL39", "NOMBRE39", "APE139", "APE239",
			"IDENTIFICADOR39", "IDAYUNTAMIENTO39", "FALTA39", "USUALTA39", "FMODIF39", "USUMODIF39" };

	private JdbcTemplate jdbcTemplate;

	public static final String SEQUENCE_TR_SOCIALES = "SEQ_MAESTRO_TR_SOCIALES";

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<MaestroTrSociales> rwMap = new RowMapper<MaestroTrSociales>() {
		public MaestroTrSociales mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			MaestroTrSociales maestroTrSociales = new MaestroTrSociales();
			maestroTrSociales.setIdTrSocial39(UtilSQL.getLong(resultSet, "IDTRSOCIAL39"));
			maestroTrSociales.setNombre39(resultSet.getString("NOMBRE39"));
			maestroTrSociales.setApe139(resultSet.getString("APE139"));
			maestroTrSociales.setApe239(resultSet.getString("APE239"));
			maestroTrSociales.setIdentificador39(resultSet.getString("IDENTIFICADOR39"));
			maestroTrSociales.setfAlta39(resultSet.getDate("FALTA39"));
			maestroTrSociales.setUsuAlta39(resultSet.getString("USUALTA39"));
			maestroTrSociales.setfModif39(resultSet.getDate("FMODIF39"));
			maestroTrSociales.setUsuModif39(resultSet.getString("USUMODIF39"));
			maestroTrSociales.setEmail39(resultSet.getString("EMAIL39"));
			maestroTrSociales.setTelefono39(resultSet.getLong("TELEFONO39"));

			Ayuntamientos ayuntamientos = new Ayuntamientos();
			ayuntamientos.setDescEs31(resultSet.getString("DESCES31"));
			ayuntamientos.setDescEu31(resultSet.getString("DESCEU31"));

			maestroTrSociales.setAyuntamientos(ayuntamientos);
			return maestroTrSociales;
		}
	};

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

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

			MaestroTrSociales maestrotrsociales = new MaestroTrSociales(UtilSQL.getLong(resultSet, "IDTRSOCIAL39"),
					resultSet.getString("NOMBRE39"), resultSet.getString("APE139"), resultSet.getString("APE239"),
					resultSet.getString("IDENTIFICADOR39"), UtilSQL.getLong(resultSet, "IDAYUNTAMIENTO39"),
					resultSet.getDate("FALTA39"), resultSet.getString("USUALTA39"), resultSet.getDate("FMODIF39"),
					resultSet.getString("USUMODIF39"));

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

		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();

		String query = "INSERT INTO MAESTRO_TR_SOCIALES (ID_TR_SOCIAL_39, NOMBRE_39, APE1_39, APE2_39, IDENTIFICADOR_39, ID_AYUNTAMIENTO_39, F_ALTA_39, USU_ALTA_39, F_MODIF_39, USU_MODIF_39) "
				+ "VALUES (" + SEQUENCE_TR_SOCIALES
				+ ".NEXTVAL,:nombre,:ape1,:ape2, UPPER(:identif),:idAyunta,sysdate,:usuAlta,:fModif,:usuModif)";

		MapSqlParameterSource parameters = new MapSqlParameterSource()
				.addValue("nombre", maestrotrsociales.getNombre39()).addValue("ape1", maestrotrsociales.getApe139())
				.addValue("ape2", maestrotrsociales.getApe239())
				.addValue("identif", maestrotrsociales.getIdentificador39())
				.addValue("idAyunta", maestrotrsociales.getIdAyuntamiento39())
				.addValue("usuAlta", maestrotrsociales.getUsuAlta39())
				.addValue("fModif", maestrotrsociales.getfModif39())
				.addValue("usuModif", maestrotrsociales.getUsuModif39());

		NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
		namedParameterJdbcTemplate.update(query, parameters, generatedKeyHolder, new String[] { "ID_TR_SOCIAL_39" });
		maestrotrsociales.setIdTrSocial39(generatedKeyHolder.getKey().longValue());

		return maestrotrsociales;
	}

	/**
	 * Updates a single row in the MaestroTrSociales table.
	 *
	 * @param maestrotrsociales MaestroTrSociales
	 * @return MaestroTrSociales
	 */
	public MaestroTrSociales update(MaestroTrSociales maestrotrsociales) {
		String query = "UPDATE MAESTRO_TR_SOCIALES SET NOMBRE_39=?, APE1_39=?, APE2_39=?, IDENTIFICADOR_39=UPPER(?), ID_AYUNTAMIENTO_39=?, F_ALTA_39=?, USU_ALTA_39=?, F_MODIF_39=?, USU_MODIF_39=? WHERE ID_TR_SOCIAL_39=?";
		this.jdbcTemplate.update(query, maestrotrsociales.getNombre39(), maestrotrsociales.getApe139(),
				maestrotrsociales.getApe239(), maestrotrsociales.getIdentificador39(),
				maestrotrsociales.getIdAyuntamiento39(), maestrotrsociales.getfAlta39(),
				maestrotrsociales.getUsuAlta39(), maestrotrsociales.getfModif39(), maestrotrsociales.getUsuModif39(),
				maestrotrsociales.getIdTrSocial39());
		return maestrotrsociales;
	}

	/**
	 * Finds a single row in the MaestroTrSociales table.
	 *
	 * @param maestrotrsociales MaestroTrSociales
	 * @return MaestroTrSociales
	 */
	@Transactional(readOnly = true)
	public MaestroTrSociales find(MaestroTrSociales maestrotrsociales) {
		String query = "SELECT t1.ID_TR_SOCIAL_39 IDTRSOCIAL39, t1.NOMBRE_39 NOMBRE39, t1.APE1_39 APE139, t1.APE2_39 APE239, t1.IDENTIFICADOR_39 IDENTIFICADOR39, t1.ID_AYUNTAMIENTO_39 IDAYUNTAMIENTO39, t1.F_ALTA_39 FALTA39, t1.USU_ALTA_39 USUALTA39, t1.F_MODIF_39 FMODIF39, t1.USU_MODIF_39 USUMODIF39 FROM MAESTRO_TR_SOCIALES t1  WHERE t1.ID_TR_SOCIAL_39 = ?  ";

		List<MaestroTrSociales> maestrotrsocialesList = this.jdbcTemplate.query(query, this.rwMap,
				maestrotrsociales.getIdTrSocial39());
		return (MaestroTrSociales) DataAccessUtils.uniqueResult(maestrotrsocialesList);
	}

	/**
	 * Removes a single row in the MaestroTrSociales table.
	 *
	 * @param maestrotrsociales MaestroTrSociales
	 * @return
	 */
	public void remove(MaestroTrSociales maestrotrsociales) {
		String query = "DELETE FROM MAESTRO_TR_SOCIALES WHERE ID_TR_SOCIAL_39=?";
		this.jdbcTemplate.update(query, maestrotrsociales.getIdTrSocial39());
	}

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

		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_TR_SOCIAL_39 IDTRSOCIAL39, t1.NOMBRE_39 NOMBRE39, t1.APE1_39 APE139, t1.APE2_39 APE239, t1.IDENTIFICADOR_39 IDENTIFICADOR39, t1.ID_AYUNTAMIENTO_39 IDAYUNTAMIENTO39, t1.F_ALTA_39 FALTA39, t1.USU_ALTA_39 USUALTA39, t1.F_MODIF_39 FMODIF39, t1.USU_MODIF_39 USUMODIF39");
		query.append(" FROM MAESTRO_TR_SOCIALES t1  ");
		// Where clause & Params
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterMaestroTrSociales, 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, MaestroTrSociales.class,
				params, "ID_TR_SOCIAL_39");
		where.append(sbMultipleSQL);

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

	/**
	 * Finds a list of rows in the MaestroTrSociales table.
	 *
	 * @param maestrotrsociales MaestroTrSociales
	 * @param tableRequestDto   TableRequestDto
	 * @return List<MaestroTrSociales>
	 */
	@Transactional(readOnly = true)
	public List<MaestroTrSociales> findAll(MaestroTrSociales maestrotrsociales, TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_TR_SOCIAL_39 IDTRSOCIAL39,t1.NOMBRE_39 NOMBRE39,t1.APE1_39 APE139,t1.APE2_39 APE239,t1.IDENTIFICADOR_39 IDENTIFICADOR39,t1.ID_AYUNTAMIENTO_39 IDAYUNTAMIENTO39,t1.F_ALTA_39 FALTA39,t1.USU_ALTA_39 USUALTA39,t1.F_MODIF_39 FMODIF39,t1.USU_MODIF_39 USUMODIF39 ");
		query.append("FROM MAESTRO_TR_SOCIALES t1 ");

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

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

	/**
	 * Finds rows in the MaestroTrSociales table using like.
	 *
	 * @param maestrotrsociales MaestroTrSociales
	 * @param tableRequestDto   TableRequestDto
	 * @param startsWith        Boolean
	 * @return List<MaestroTrSociales>
	 */
	@Transactional(readOnly = true)
	public List<MaestroTrSociales> findAllLike(MaestroTrSociales maestrotrsociales, TableRequestDto tableRequestDto,
			Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_TR_SOCIAL_39 IDTRSOCIAL39,t1.NOMBRE_39 NOMBRE39,t1.APE1_39 APE139,t1.APE2_39 APE239,t1.IDENTIFICADOR_39 IDENTIFICADOR39,t1.ID_AYUNTAMIENTO_39 IDAYUNTAMIENTO39,t1.F_ALTA_39 FALTA39,t1.USU_ALTA_39 USUALTA39,t1.F_MODIF_39 FMODIF39,t1.USU_MODIF_39 USUMODIF39, EMAIL_39 EMAIL39, TELEFONO_39 TELEFONO39 "
						+ ",t2.DESC_ES_31 DESCES31, t2.DESC_EU_31 DESCEU31 ");
		query.append("FROM MAESTRO_TR_SOCIALES t1, AYUNTAMIENTOS t2");

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

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

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(maestrotrsociales, 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 MaestroTrSociales selected for rup_table
	 *
	 * @param maestrotrsociales MaestroTrSociales
	 * @param tableRequestDto   TableRequestDto
	 * @param startsWith        Boolean
	 * @return List<TableRowDto<MaestroTrSociales>>
	 */
	@Override
	public List<TableRowDto<MaestroTrSociales>> reorderSelection(MaestroTrSociales maestrotrsociales,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_TR_SOCIAL_39 IDTRSOCIAL39,t1.NOMBRE_39 NOMBRE39,t1.APE1_39 APE139,t1.APE2_39 APE239,t1.IDENTIFICADOR_39 IDENTIFICADOR39,t1.ID_AYUNTAMIENTO_39 IDAYUNTAMIENTO39,t1.F_ALTA_39 FALTA39,t1.USU_ALTA_39 USUALTA39,t1.F_MODIF_39 FMODIF39,t1.USU_MODIF_39 USUMODIF39 ");
		// FROM
		query.append("FROM MAESTRO_TR_SOCIALES t1 ");
		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(maestrotrsociales, 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,
				MaestroTrSociales.class, filterParamList, "IDTRSOCIAL39");

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

	/**
	 * Search method for rup_table
	 *
	 * @param filterParams    MaestroTrSociales
	 * @param searchParams    MaestroTrSociales
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<TableRowDto<MaestroTrSociales>>
	 */
	@Override
	public List<TableRowDto<MaestroTrSociales>> search(MaestroTrSociales filterParams, MaestroTrSociales searchParams,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_TR_SOCIAL_39 IDTRSOCIAL39,t1.NOMBRE_39 NOMBRE39,t1.APE1_39 APE139,t1.APE2_39 APE239,t1.IDENTIFICADOR_39 IDENTIFICADOR39,t1.ID_AYUNTAMIENTO_39 IDAYUNTAMIENTO39,t1.F_ALTA_39 FALTA39,t1.USU_ALTA_39 USUALTA39,t1.F_MODIF_39 FMODIF39,t1.USU_MODIF_39 USUMODIF39 ");
		// FROM
		query.append("FROM MAESTRO_TR_SOCIALES 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,
				MaestroTrSociales.class, filterParamList, searchSQL, searchParamList, from_alias, "IDTRSOCIAL39");

		return this.jdbcTemplate.query(sbReorderSelectionSQL.toString(),
				new RowNumResultSetExtractor<MaestroTrSociales>(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,
				MaestroTrSociales.class, "MAESTRO_TR_SOCIALES", new String[] { "IDTRSOCIAL39" });

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

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

	/**
	 * Filter in the hierarchical MaestroTrSociales table.
	 *
	 * @param filterMaestroTrSociales MaestroTrSociales
	 * @param tableRequestDto         TableRequestDto
	 * @return List<JerarquiaDto<MaestroTrSociales>>
	 */
	@Override
	public List<JerarquiaDto<MaestroTrSociales>> findAllLikeJerarquia(MaestroTrSociales filterMaestroTrSociales,
			TableRequestDto tableRequestDto) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_TR_SOCIAL_39 IDTRSOCIAL39,t1.NOMBRE_39 NOMBRE39,t1.APE1_39 APE139,t1.APE2_39 APE239,t1.IDENTIFICADOR_39 IDENTIFICADOR39,t1.ID_AYUNTAMIENTO_39 IDAYUNTAMIENTO39,t1.F_ALTA_39 FALTA39,t1.USU_ALTA_39 USUALTA39,t1.F_MODIF_39 FMODIF39,t1.USU_MODIF_39 USUMODIF39 ");
		// FROM
		// StringBuilder from = new StringBuilder("FROM MAESTRO_TR_SOCIALES t1 ");
		List<String> from = new ArrayList<String>();
		from.add("MAESTRO_TR_SOCIALES");
		// 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(filterMaestroTrSociales, false);

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

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

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

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

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

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

	/*
	 * MÉTODOS PRIVADOS
	 */

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

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

		if (maestrotrsociales != null && maestrotrsociales.getIdTrSocial39() != null) {
			where.append(" AND t1.ID_TR_SOCIAL_39 = ?");
			params.add(maestrotrsociales.getIdTrSocial39());
		}
		if (maestrotrsociales != null && maestrotrsociales.getNombre39() != null) {
			where.append(" AND t1.NOMBRE_39 = ?");
			params.add(maestrotrsociales.getNombre39());
		}
		if (maestrotrsociales != null && maestrotrsociales.getApe139() != null) {
			where.append(" AND t1.APE1_39 = ?");
			params.add(maestrotrsociales.getApe139());
		}
		if (maestrotrsociales != null && maestrotrsociales.getApe239() != null) {
			where.append(" AND t1.APE2_39 = ?");
			params.add(maestrotrsociales.getApe239());
		}
		if (maestrotrsociales != null && maestrotrsociales.getIdentificador39() != null) {
			where.append(" AND UPPER(t1.IDENTIFICADOR_39) = UPPER(?)");
			params.add(maestrotrsociales.getIdentificador39());
		}
		if (maestrotrsociales != null && maestrotrsociales.getIdAyuntamiento39() != null) {
			where.append(" AND t1.ID_AYUNTAMIENTO_39 = ?");
			params.add(maestrotrsociales.getIdAyuntamiento39());
		}
		if (maestrotrsociales != null && maestrotrsociales.getfAlta39() != null) {
			where.append(" AND t1.F_ALTA_39 = ?");
			params.add(maestrotrsociales.getfAlta39());
		}
		if (maestrotrsociales != null && maestrotrsociales.getUsuAlta39() != null) {
			where.append(" AND t1.USU_ALTA_39 = ?");
			params.add(maestrotrsociales.getUsuAlta39());
		}
		if (maestrotrsociales != null && maestrotrsociales.getfModif39() != null) {
			where.append(" AND t1.F_MODIF_39 = ?");
			params.add(maestrotrsociales.getfModif39());
		}
		if (maestrotrsociales != null && maestrotrsociales.getUsuModif39() != null) {
			where.append(" AND t1.USU_MODIF_39 = ?");
			params.add(maestrotrsociales.getUsuModif39());
		}

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

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

		if (maestrotrsociales != null && maestrotrsociales.getIdTrSocial39() != null) {
			where.append(" AND t1.ID_TR_SOCIAL_39 = ?");
			params.add(maestrotrsociales.getIdTrSocial39());
		}
		if (maestrotrsociales != null && maestrotrsociales.getNombre39() != null) {
			where.append(" AND UPPER(t1.NOMBRE_39) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrotrsociales.getNombre39().toUpperCase() + "%");
			} else {
				params.add("%" + maestrotrsociales.getNombre39().toUpperCase() + "%");
			}
			where.append(" AND t1.NOMBRE_39 IS NOT NULL");
		}
		if (maestrotrsociales != null && maestrotrsociales.getApe139() != null) {
			where.append(" AND UPPER(t1.APE1_39) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrotrsociales.getApe139().toUpperCase() + "%");
			} else {
				params.add("%" + maestrotrsociales.getApe139().toUpperCase() + "%");
			}
			where.append(" AND t1.APE1_39 IS NOT NULL");
		}
		if (maestrotrsociales != null && maestrotrsociales.getApe239() != null) {
			where.append(" AND UPPER(t1.APE2_39) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrotrsociales.getApe239().toUpperCase() + "%");
			} else {
				params.add("%" + maestrotrsociales.getApe239().toUpperCase() + "%");
			}
			where.append(" AND t1.APE2_39 IS NOT NULL");
		}
		if (maestrotrsociales != null && maestrotrsociales.getIdentificador39() != null) {
			where.append(" AND UPPER(t1.IDENTIFICADOR_39) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrotrsociales.getIdentificador39().toUpperCase() + "%");
			} else {
				params.add("%" + maestrotrsociales.getIdentificador39().toUpperCase() + "%");
			}
			where.append(" AND t1.IDENTIFICADOR_39 IS NOT NULL");
		}
		if (maestrotrsociales != null && maestrotrsociales.getIdAyuntamiento39() != null) {
			where.append(" AND t1.ID_AYUNTAMIENTO_39 = ?");
			params.add(maestrotrsociales.getIdAyuntamiento39());
		}
		if (maestrotrsociales != null && maestrotrsociales.getfAlta39() != null) {
			where.append(" AND t1.F_ALTA_39 = ?");
			params.add(maestrotrsociales.getfAlta39());
		}
		if (maestrotrsociales != null && maestrotrsociales.getUsuAlta39() != null) {
			where.append(" AND UPPER(t1.USU_ALTA_39) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrotrsociales.getUsuAlta39().toUpperCase() + "%");
			} else {
				params.add("%" + maestrotrsociales.getUsuAlta39().toUpperCase() + "%");
			}
			where.append(" AND t1.USU_ALTA_39 IS NOT NULL");
		}
		if (maestrotrsociales != null && maestrotrsociales.getfModif39() != null) {
			where.append(" AND t1.F_MODIF_39 = ?");
			params.add(maestrotrsociales.getfModif39());
		}
		if (maestrotrsociales != null && maestrotrsociales.getUsuModif39() != null) {
			where.append(" AND UPPER(t1.USU_MODIF_39) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrotrsociales.getUsuModif39().toUpperCase() + "%");
			} else {
				params.add("%" + maestrotrsociales.getUsuModif39().toUpperCase() + "%");
			}
			where.append(" AND t1.USU_MODIF_39 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;
}
