package com.ejie.ad74a.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
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.ad74a.model.MaestroActores;
import com.ejie.ad74a.model.MaestroSubtiposAviso;
import com.ejie.ad74a.model.MaestroSubtiposAvisoActor;
import com.ejie.ad74a.model.MaestroTiposAviso;
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;

/**
 * MaestroSubtiposAvisoActorDaoImpl generated by UDA, 12-may-2023 11:16:38.
 * 
 * @author UDA
 */

@Repository
@Transactional
public class MaestroSubtiposAvisoActorDaoImpl implements MaestroSubtiposAvisoActorDao {

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDSUBTIPOAVISO41", "IDACTOR41", "FALTA04",
			"USUALTA04" };

	private JdbcTemplate jdbcTemplate;

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<MaestroSubtiposAvisoActor> rwMap = new RowMapper<MaestroSubtiposAvisoActor>() {
		public MaestroSubtiposAvisoActor mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new MaestroSubtiposAvisoActor(
					new MaestroSubtiposAviso(UtilSQL.getLong(resultSet, "IDSUBTIPOAVISO41"), null, null, null, null, "",
							""),
					new MaestroActores(UtilSQL.getLong(resultSet, "IDACTOR42"), null, null, null, null, null, null),
					resultSet.getDate("FALTA04"), resultSet.getString("USUALTA04"));
		}
	};

	private RowMapper<MaestroSubtiposAvisoActor> rwMapPK = new RowMapper<MaestroSubtiposAvisoActor>() {
		public MaestroSubtiposAvisoActor mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new MaestroSubtiposAvisoActor(
					new MaestroSubtiposAviso(UtilSQL.getLong(resultSet, "IDSUBTIPOAVISO41")),
					new MaestroActores(UtilSQL.getLong(resultSet, "IDACTOR42")));
		}
	};

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

			MaestroSubtiposAvisoActor maestrosubtiposavisoactor = new MaestroSubtiposAvisoActor(
					new MaestroSubtiposAviso(UtilSQL.getLong(resultSet, "IDSUBTIPOAVISO41"), null, null, null, null, "",
							""),
					new MaestroActores(UtilSQL.getLong(resultSet, "IDACTOR42"), null, null, null, null, null, null),
					resultSet.getDate("FALTA04"), resultSet.getString("USUALTA04"));

			JerarquiaDto<MaestroSubtiposAvisoActor> jerarquia = new JerarquiaDto<MaestroSubtiposAvisoActor>();
			jerarquia.setModel(maestrosubtiposavisoactor);
			jerarquia.setLevel((int) UtilSQL.getInt(resultSet, "LEVEL"));
			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 MaestroSubtiposAvisoActor table.
	 *
	 * @param maestrosubtiposavisoactor MaestroSubtiposAvisoActor
	 * @return MaestroSubtiposAvisoActor
	 */
	public MaestroSubtiposAvisoActor add(MaestroSubtiposAvisoActor maestrosubtiposavisoactor) {
		String query = "INSERT INTO MAESTRO_SUBTIPOS_AVISO_ACTOR (ID_SUB_TIPO_AVISO_41, ID_ACTOR_41, F_ALTA_04, USU_ALTA_04) VALUES (?,?,?,?)";
		Object MaestroSubtiposAvisoIdSubTipoAviso41Aux = null;
		if (maestrosubtiposavisoactor.getMaestroSubtiposAviso() != null
				&& maestrosubtiposavisoactor.getMaestroSubtiposAviso().getIdSubTipoAviso41() != null) {
			MaestroSubtiposAvisoIdSubTipoAviso41Aux = maestrosubtiposavisoactor.getMaestroSubtiposAviso()
					.getIdSubTipoAviso41();
		}
		Object MaestroActoresIdActor42Aux = null;
		if (maestrosubtiposavisoactor.getMaestroActores() != null
				&& maestrosubtiposavisoactor.getMaestroActores().getIdActor42() != null) {
			MaestroActoresIdActor42Aux = maestrosubtiposavisoactor.getMaestroActores().getIdActor42();
		}
		this.jdbcTemplate.update(query, MaestroSubtiposAvisoIdSubTipoAviso41Aux, MaestroActoresIdActor42Aux,
				maestrosubtiposavisoactor.getFAlta04(), maestrosubtiposavisoactor.getUsuAlta04());
		return maestrosubtiposavisoactor;
	}

	/**
	 * Updates a single row in the MaestroSubtiposAvisoActor table.
	 *
	 * @param maestrosubtiposavisoactor MaestroSubtiposAvisoActor
	 * @return MaestroSubtiposAvisoActor
	 */
	public MaestroSubtiposAvisoActor update(MaestroSubtiposAvisoActor maestrosubtiposavisoactor) {
		String query = "UPDATE MAESTRO_SUBTIPOS_AVISO_ACTOR SET F_ALTA_04=?, USU_ALTA_04=? WHERE ID_SUB_TIPO_AVISO_41=? AND ID_ACTOR_41=?";
		Object MaestroSubtiposAvisoIdSubTipoAviso41Aux = null;
		if (maestrosubtiposavisoactor.getMaestroSubtiposAviso() != null
				&& maestrosubtiposavisoactor.getMaestroSubtiposAviso().getIdSubTipoAviso41() != null) {
			MaestroSubtiposAvisoIdSubTipoAviso41Aux = maestrosubtiposavisoactor.getMaestroSubtiposAviso()
					.getIdSubTipoAviso41();
		}
		Object MaestroActoresIdActor42Aux = null;
		if (maestrosubtiposavisoactor.getMaestroActores() != null
				&& maestrosubtiposavisoactor.getMaestroActores().getIdActor42() != null) {
			MaestroActoresIdActor42Aux = maestrosubtiposavisoactor.getMaestroActores().getIdActor42();
		}
		this.jdbcTemplate.update(query, maestrosubtiposavisoactor.getFAlta04(),
				maestrosubtiposavisoactor.getUsuAlta04(), MaestroSubtiposAvisoIdSubTipoAviso41Aux,
				MaestroActoresIdActor42Aux);
		return maestrosubtiposavisoactor;
	}

	/**
	 * Finds a single row in the MaestroSubtiposAvisoActor table.
	 *
	 * @param maestrosubtiposavisoactor MaestroSubtiposAvisoActor
	 * @return MaestroSubtiposAvisoActor
	 */
	@Transactional(readOnly = true)
	public MaestroSubtiposAvisoActor find(MaestroSubtiposAvisoActor maestrosubtiposavisoactor) {
		String query = "SELECT t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41, t1.ID_ACTOR_41 IDACTOR41, t1.F_ALTA_04 FALTA04, t1.USU_ALTA_04 USUALTA04 FROM MAESTRO_SUBTIPOS_AVISO_ACTOR t1  WHERE t1.ID_SUB_TIPO_AVISO_41 = ?   AND t1.ID_ACTOR_41 = ?  ";

		List<MaestroSubtiposAvisoActor> maestrosubtiposavisoactorList = this.jdbcTemplate.query(query, this.rwMap,
				maestrosubtiposavisoactor.getMaestroSubtiposAviso().getIdSubTipoAviso41(),
				maestrosubtiposavisoactor.getMaestroActores().getIdActor42());
		return (MaestroSubtiposAvisoActor) DataAccessUtils.uniqueResult(maestrosubtiposavisoactorList);
	}

	/**
	 * Removes a single row in the MaestroSubtiposAvisoActor table.
	 *
	 * @param maestrosubtiposavisoactor MaestroSubtiposAvisoActor
	 * @return
	 */
	public void remove(MaestroSubtiposAvisoActor maestrosubtiposavisoactor) {
		String query = "DELETE FROM MAESTRO_SUBTIPOS_AVISO_ACTOR WHERE ID_SUB_TIPO_AVISO_41=? AND ID_ACTOR_41=?";
		this.jdbcTemplate.update(query, maestrosubtiposavisoactor.getMaestroSubtiposAviso().getIdSubTipoAviso41(),
				maestrosubtiposavisoactor.getMaestroActores().getIdActor42());
	}

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

		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41, t1.ID_ACTOR_41 IDACTOR41, t1.F_ALTA_04 FALTA04, t1.USU_ALTA_04 USUALTA04");
		query.append(" FROM MAESTRO_SUBTIPOS_AVISO_ACTOR t1  ");
		// Where clause & Params
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterMaestroSubtiposAvisoActor, 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,
				MaestroSubtiposAvisoActor.class, params, "ID_SUB_TIPO_AVISO_41 AND ID_ACTOR_41");
		where.append(sbMultipleSQL);

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

	/**
	 * Finds a list of rows in the MaestroSubtiposAvisoActor table.
	 *
	 * @param maestrosubtiposavisoactor MaestroSubtiposAvisoActor
	 * @param tableRequestDto           TableRequestDto
	 * @return List<MaestroSubtiposAvisoActor>
	 */
	@Transactional(readOnly = true)
	public List<MaestroSubtiposAvisoActor> findAll(MaestroSubtiposAvisoActor maestrosubtiposavisoactor,
			TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_ACTOR_41 IDACTOR41,t1.F_ALTA_04 FALTA04,t1.USU_ALTA_04 USUALTA04 ");
		query.append("FROM MAESTRO_SUBTIPOS_AVISO_ACTOR t1 ");

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

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

	/**
	 * Finds rows in the MaestroSubtiposAvisoActor table using like.
	 *
	 * @param maestrosubtiposavisoactor MaestroSubtiposAvisoActor
	 * @param tableRequestDto           TableRequestDto
	 * @param startsWith                Boolean
	 * @return List<MaestroSubtiposAvisoActor>
	 */
	@Transactional(readOnly = true)
	public List<MaestroSubtiposAvisoActor> findAllLike(MaestroSubtiposAvisoActor maestrosubtiposavisoactor,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_ACTOR_41 IDACTOR41,t1.F_ALTA_04 FALTA04,t1.USU_ALTA_04 USUALTA04 ");
		query.append("FROM MAESTRO_SUBTIPOS_AVISO_ACTOR t1 ");

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(maestrosubtiposavisoactor, 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 MaestroSubtiposAvisoActor selected for rup_table
	 *
	 * @param maestrosubtiposavisoactor MaestroSubtiposAvisoActor
	 * @param tableRequestDto           TableRequestDto
	 * @param startsWith                Boolean
	 * @return List<TableRowDto<MaestroSubtiposAvisoActor>>
	 */
	@Override
	public List<TableRowDto<MaestroSubtiposAvisoActor>> reorderSelection(
			MaestroSubtiposAvisoActor maestrosubtiposavisoactor, TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_ACTOR_41 IDACTOR41,t1.F_ALTA_04 FALTA04,t1.USU_ALTA_04 USUALTA04 ");
		// FROM
		query.append("FROM MAESTRO_SUBTIPOS_AVISO_ACTOR t1 ");
		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(maestrosubtiposavisoactor, 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,
				MaestroSubtiposAvisoActor.class, filterParamList, "IDSUBTIPOAVISO41,IDACTOR41");

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

	/**
	 * Search method for rup_table
	 *
	 * @param filterParams    MaestroSubtiposAvisoActor
	 * @param searchParams    MaestroSubtiposAvisoActor
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<TableRowDto<MaestroSubtiposAvisoActor>>
	 */
	@Override
	public List<TableRowDto<MaestroSubtiposAvisoActor>> search(MaestroSubtiposAvisoActor filterParams,
			MaestroSubtiposAvisoActor searchParams, TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_ACTOR_41 IDACTOR41,t1.F_ALTA_04 FALTA04,t1.USU_ALTA_04 USUALTA04 ");
		// FROM
		query.append("FROM MAESTRO_SUBTIPOS_AVISO_ACTOR 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,
				MaestroSubtiposAvisoActor.class, filterParamList, searchSQL, searchParamList, from_alias,
				"IDSUBTIPOAVISO41,IDACTOR41");

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

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

		for (String row : selectedIds) {
			String[] parts = row.split(tableRequestDto.getCore().getPkToken());
			for (String param : parts) {
				params.add(param);
			}
		}

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

	/**
	 * Filter in the hierarchical MaestroSubtiposAvisoActor table.
	 *
	 * @param filterMaestroSubtiposAvisoActor MaestroSubtiposAvisoActor
	 * @param tableRequestDto                 TableRequestDto
	 * @return List<JerarquiaDto<MaestroSubtiposAvisoActor>>
	 */
	@Override
	public List<JerarquiaDto<MaestroSubtiposAvisoActor>> findAllLikeJerarquia(
			MaestroSubtiposAvisoActor filterMaestroSubtiposAvisoActor, TableRequestDto tableRequestDto) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_ACTOR_41 IDACTOR41,t1.F_ALTA_04 FALTA04,t1.USU_ALTA_04 USUALTA04 ");
		// FROM
		// StringBuilder from = new StringBuilder("FROM MAESTRO_SUBTIPOS_AVISO_ACTOR t1
		// ");
		List<String> from = new ArrayList<String>();
		from.add("MAESTRO_SUBTIPOS_AVISO_ACTOR");
		// 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(filterMaestroSubtiposAvisoActor, false);

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

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

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

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

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

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

	/*
	 * MÉTODOS PRIVADOS
	 */

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

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

		if (maestrosubtiposavisoactor != null
				&& maestrosubtiposavisoactor.getMaestroSubtiposAviso().getIdSubTipoAviso41() != null) {
			where.append(" AND t1.ID_SUB_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposavisoactor.getMaestroSubtiposAviso().getIdSubTipoAviso41());
		}
		if (maestrosubtiposavisoactor != null && maestrosubtiposavisoactor.getMaestroActores().getIdActor42() != null) {
			where.append(" AND t1.ID_ACTOR_42 = ?");
			params.add(maestrosubtiposavisoactor.getMaestroActores().getIdActor42());
		}
		if (maestrosubtiposavisoactor != null && maestrosubtiposavisoactor.getFAlta04() != null) {
			where.append(" AND t1.F_ALTA_04 = ?");
			params.add(maestrosubtiposavisoactor.getFAlta04());
		}
		if (maestrosubtiposavisoactor != null && maestrosubtiposavisoactor.getUsuAlta04() != null) {
			where.append(" AND t1.USU_ALTA_04 = ?");
			params.add(maestrosubtiposavisoactor.getUsuAlta04());
		}

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

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

		if (maestrosubtiposavisoactor != null
				&& maestrosubtiposavisoactor.getMaestroSubtiposAviso().getIdSubTipoAviso41() != null) {
			where.append(" AND t1.ID_SUB_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposavisoactor.getMaestroSubtiposAviso().getIdSubTipoAviso41());
		}
		if (maestrosubtiposavisoactor != null && maestrosubtiposavisoactor.getMaestroActores().getIdActor42() != null) {
			where.append(" AND t1.ID_ACTOR_42 = ?");
			params.add(maestrosubtiposavisoactor.getMaestroActores().getIdActor42());
		}
		if (maestrosubtiposavisoactor != null && maestrosubtiposavisoactor.getFAlta04() != null) {
			where.append(" AND t1.F_ALTA_04 = ?");
			params.add(maestrosubtiposavisoactor.getFAlta04());
		}
		if (maestrosubtiposavisoactor != null && maestrosubtiposavisoactor.getUsuAlta04() != null) {
			where.append(" AND UPPER(t1.USU_ALTA_04) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrosubtiposavisoactor.getUsuAlta04().toUpperCase() + "%");
			} else {
				params.add("%" + maestrosubtiposavisoactor.getUsuAlta04().toUpperCase() + "%");
			}
			where.append(" AND t1.USU_ALTA_04 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;
}
