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.MaestroTiposAviso;
import com.ejie.ad74a.util.UtilSQL;
import com.ejie.ad74a.util.constantes.OrigenAvisosEnum;
import com.ejie.x38.dao.RowNumResultSetExtractor;
import com.ejie.x38.dto.TableManager;
import com.ejie.x38.dto.TableRequestDto;
import com.ejie.x38.dto.TableRowDto;

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

@Repository
@Transactional
public class MaestroTiposAvisoDaoImpl implements MaestroTiposAvisoDao {

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

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDTIPOAVISO40", "DESCES40", "DESCEU40",
			"FBAJA40" };

	private JdbcTemplate jdbcTemplate;

	public static final String SEQUENCE_MAESTRO_TIPOS_AVISO = "SEQ_MAESTRO_TIPOS_AVISO";

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<MaestroTiposAviso> rwMap = new RowMapper<MaestroTiposAviso>() {
		public MaestroTiposAviso mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new MaestroTiposAviso(UtilSQL.getLong(resultSet, "IDTIPOAVISO40"), resultSet.getString("DESCES40"),
					resultSet.getString("DESCEU40"), resultSet.getDate("FBAJA40"));
		}
	};

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

	/**
	 * 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 MaestroTiposAviso table.
	 *
	 * @param maestrotiposaviso MaestroTiposAviso
	 * @return MaestroTiposAviso
	 */
	public MaestroTiposAviso add(MaestroTiposAviso maestrotiposaviso) {
		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();

		String query = "INSERT INTO MAESTRO_TIPOS_AVISO (ID_TIPO_AVISO_40, DESC_ES_40, DESC_EU_40, F_BAJA_40) "
				+ "VALUES (" + SEQUENCE_MAESTRO_TIPOS_AVISO + ".NEXTVAL,:descEs,:descEu,:fBaja)";

		MapSqlParameterSource parameters = new MapSqlParameterSource()
				.addValue("descEs", maestrotiposaviso.getDescEs40()).addValue("descEu", maestrotiposaviso.getDescEu40())
				.addValue("fBaja", maestrotiposaviso.getfBaja40());

		NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
		namedParameterJdbcTemplate.update(query, parameters, generatedKeyHolder, new String[] { "ID_TIPO_AVISO_40" });
		maestrotiposaviso.setIdTipoAviso40(generatedKeyHolder.getKey().longValue());

		return maestrotiposaviso;
	}

	/**
	 * Updates a single row in the MaestroTiposAviso table.
	 *
	 * @param maestrotiposaviso MaestroTiposAviso
	 * @return MaestroTiposAviso
	 */
	public MaestroTiposAviso update(MaestroTiposAviso maestrotiposaviso) {
		String query = "UPDATE MAESTRO_TIPOS_AVISO SET DESC_ES_40=?, DESC_EU_40=?, F_BAJA_40=? WHERE ID_TIPO_AVISO_40=?";
		this.jdbcTemplate.update(query, maestrotiposaviso.getDescEs40(), maestrotiposaviso.getDescEu40(),
				maestrotiposaviso.getfBaja40(), maestrotiposaviso.getIdTipoAviso40());
		return maestrotiposaviso;
	}

	/**
	 * Finds a single row in the MaestroTiposAviso table.
	 *
	 * @param maestrotiposaviso MaestroTiposAviso
	 * @return MaestroTiposAviso
	 */
	@Transactional(readOnly = true)
	public MaestroTiposAviso find(MaestroTiposAviso maestrotiposaviso) {
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_TIPO_AVISO_40 IDTIPOAVISO40, t1.DESC_ES_40 DESCES40, t1.DESC_EU_40 DESCEU40, t1.F_BAJA_40 FBAJA40 ");

		query.append("FROM MAESTRO_TIPOS_AVISO t1 ");
		query.append("WHERE t1.ID_TIPO_AVISO_40 = ? ");

		List<MaestroTiposAviso> maestrotiposavisoList = this.jdbcTemplate.query(query.toString(), this.rwMap,
				maestrotiposaviso.getIdTipoAviso40());
		return DataAccessUtils.uniqueResult(maestrotiposavisoList);
	}

	/**
	 * Removes a single row in the MaestroTiposAviso table.
	 *
	 * @param maestrotiposaviso MaestroTiposAviso
	 * @return
	 */
	public void remove(MaestroTiposAviso maestrotiposaviso) {
		String query = "DELETE FROM MAESTRO_TIPOS_AVISO WHERE ID_TIPO_AVISO_40=?";
		this.jdbcTemplate.update(query, maestrotiposaviso.getIdTipoAviso40());
	}

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

		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_TIPO_AVISO_40 IDTIPOAVISO40, t1.DESC_ES_40 DESCES40, t1.DESC_EU_40 DESCEU40, t1.F_BAJA_40 FBAJA40");
		query.append(" FROM MAESTRO_TIPOS_AVISO t1  ");
		// Where clause & Params
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterMaestroTiposAviso, 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, MaestroTiposAviso.class,
				params, "ID_TIPO_AVISO_40");
		where.append(sbMultipleSQL);

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

	/**
	 * Finds a list of rows in the MaestroTiposAviso table.
	 *
	 * @param maestrotiposaviso MaestroTiposAviso
	 * @param tableRequestDto   TableRequestDto
	 * @return List<MaestroTiposAviso>
	 */
	@Transactional(readOnly = true)
	public List<MaestroTiposAviso> findAll(MaestroTiposAviso maestrotiposaviso, TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_TIPO_AVISO_40 IDTIPOAVISO40,t1.DESC_ES_40 DESCES40,t1.DESC_EU_40 DESCEU40,t1.F_BAJA_40 FBAJA40 ");
		query.append("FROM MAESTRO_TIPOS_AVISO t1 ");

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

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

	@Transactional(readOnly = true)
	public List<MaestroTiposAviso> findPorUsuarios(MaestroTiposAviso maestrotiposaviso, TableRequestDto tableRequestDto,
			String usuario) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_TIPO_AVISO_40 IDTIPOAVISO40,t1.DESC_ES_40 DESCES40,t1.DESC_EU_40 DESCEU40,t1.F_BAJA_40 FBAJA40 ");
		query.append("FROM MAESTRO_TIPOS_AVISO t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereUsuMap(maestrotiposaviso, usuario);
		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,
					MaestroTiposAvisoDaoImpl.ORDER_BY_WHITE_LIST);
		}

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

	@Transactional(readOnly = true)
	public List<MaestroTiposAviso> findManuales(MaestroTiposAviso maestrotiposaviso, TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_TIPO_AVISO_40 IDTIPOAVISO40,t1.DESC_ES_40 DESCES40,t1.DESC_EU_40 DESCEU40,t1.F_BAJA_40 FBAJA40 ");
		query.append("FROM MAESTRO_TIPOS_AVISO t1 ");

		// Where clause & Params
		// Map<String, ?> mapaWhere = this.getWhereManuales(maestrotiposaviso);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		// where.append(" AND t1.ORIGEN_AUTOMATICO_40 = 0");
		query.append(where);

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

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

	/**
	 * Finds rows in the MaestroTiposAviso table using like.
	 *
	 * @param maestrotiposaviso MaestroTiposAviso
	 * @param tableRequestDto   TableRequestDto
	 * @param startsWith        Boolean
	 * @return List<MaestroTiposAviso>
	 */
	@Transactional(readOnly = true)
	public List<MaestroTiposAviso> findAllLike(MaestroTiposAviso maestrotiposaviso, TableRequestDto tableRequestDto,
			Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_TIPO_AVISO_40 IDTIPOAVISO40,t1.DESC_ES_40 DESCES40,t1.DESC_EU_40 DESCEU40,t1.F_BAJA_40 FBAJA40 ");
		query.append("FROM MAESTRO_TIPOS_AVISO t1 ");

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(maestrotiposaviso, 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 MaestroTiposAviso selected for rup_table
	 *
	 * @param maestrotiposaviso MaestroTiposAviso
	 * @param tableRequestDto   TableRequestDto
	 * @param startsWith        Boolean
	 * @return List<TableRowDto<MaestroTiposAviso>>
	 */
	@Override
	public List<TableRowDto<MaestroTiposAviso>> reorderSelection(MaestroTiposAviso maestrotiposaviso,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_TIPO_AVISO_40 IDTIPOAVISO40,t1.DESC_ES_40 DESCES40,t1.DESC_EU_40 DESCEU40,t1.F_BAJA_40 FBAJA40 ");
		// FROM
		query.append("FROM MAESTRO_TIPOS_AVISO t1 ");
		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(maestrotiposaviso, 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,
				MaestroTiposAviso.class, filterParamList, "IDTIPOAVISO40");

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

	/**
	 * Search method for rup_table
	 *
	 * @param filterParams    MaestroTiposAviso
	 * @param searchParams    MaestroTiposAviso
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<TableRowDto<MaestroTiposAviso>>
	 */
	@Override
	public List<TableRowDto<MaestroTiposAviso>> search(MaestroTiposAviso filterParams, MaestroTiposAviso searchParams,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_TIPO_AVISO_40 IDTIPOAVISO40,t1.DESC_ES_40 DESCES40,t1.DESC_EU_40 DESCEU40,t1.F_BAJA_40 FBAJA40 ");
		// FROM
		query.append("FROM MAESTRO_TIPOS_AVISO 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,
				MaestroTiposAviso.class, filterParamList, searchSQL, searchParamList, from_alias, "IDTIPOAVISO40");

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

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

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

	/*
	 * MÉTODOS PRIVADOS
	 */

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

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

		if (maestrotiposaviso != null && maestrotiposaviso.getIdTipoAviso40() != null) {
			where.append(" AND t1.ID_TIPO_AVISO_40 = ?");
			params.add(maestrotiposaviso.getIdTipoAviso40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getDescEs40() != null) {
			where.append(" AND t1.DESC_ES_40 = ?");
			params.add(maestrotiposaviso.getDescEs40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getDescEu40() != null) {
			where.append(" AND t1.DESC_EU_40 = ?");
			params.add(maestrotiposaviso.getDescEu40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getfBaja40() != null) {
			where.append(" AND t1.F_BAJA_40 = ?");
			params.add(maestrotiposaviso.getfBaja40());
		}

		Map<String, Object> mapWhere = new HashMap<String, Object>();
		mapWhere.put("query", where);
		mapWhere.put("params", params);

		return mapWhere;
	}

	private Map<String, ?> getWhereUsuMap(MaestroTiposAviso maestrotiposaviso, String usuario) {

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

		if (maestrotiposaviso != null && maestrotiposaviso.getIdTipoAviso40() != null) {
			where.append(" AND t1.ID_TIPO_AVISO_40 = ?");
			params.add(maestrotiposaviso.getIdTipoAviso40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getDescEs40() != null) {
			where.append(" AND t1.DESC_ES_40 = ?");
			params.add(maestrotiposaviso.getDescEs40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getDescEu40() != null) {
			where.append(" AND t1.DESC_EU_40 = ?");
			params.add(maestrotiposaviso.getDescEu40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getfBaja40() != null) {
			where.append(" AND t1.F_BAJA_40 = ?");
			params.add(maestrotiposaviso.getfBaja40());
		}

		if (usuario.equals("DSS")) {
			where.append(" AND t1.ORIGEN_DSS_40 = 1");
		}
		if (usuario.equals("PS")) {
			where.append(" AND t1.ORIGEN_PS_40 = 1");
		}
		if (usuario.equals("ADMIN")) {
			where.append(" AND t1.ORIGEN_AUTOMATICO_40 = 0");

		}

		Map<String, Object> mapWhere = new HashMap<String, Object>();
		mapWhere.put("query", where);
		mapWhere.put("params", params);

		return mapWhere;
	}

	private Map<String, ?> getWhereDssMap(MaestroTiposAviso maestrotiposaviso) {

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

		if (maestrotiposaviso != null && maestrotiposaviso.getIdTipoAviso40() != null) {
			where.append(" AND t1.ID_TIPO_AVISO_40 = ?");
			params.add(maestrotiposaviso.getIdTipoAviso40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getDescEs40() != null) {
			where.append(" AND t1.DESC_ES_40 = ?");
			params.add(maestrotiposaviso.getDescEs40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getDescEu40() != null) {
			where.append(" AND t1.DESC_EU_40 = ?");
			params.add(maestrotiposaviso.getDescEu40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getfBaja40() != null) {
			where.append(" AND t1.F_BAJA_40 = ?");
			params.add(maestrotiposaviso.getfBaja40());
		}

		where.append(" AND t1.ORIGEN_DSS_40 = 1");

		Map<String, Object> mapWhere = new HashMap<String, Object>();
		mapWhere.put("query", where);
		mapWhere.put("params", params);

		return mapWhere;
	}

	private Map<String, ?> getWherePsMap(MaestroTiposAviso maestrotiposaviso) {

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

		if (maestrotiposaviso != null && maestrotiposaviso.getIdTipoAviso40() != null) {
			where.append(" AND t1.ID_TIPO_AVISO_40 = ?");
			params.add(maestrotiposaviso.getIdTipoAviso40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getDescEs40() != null) {
			where.append(" AND t1.DESC_ES_40 = ?");
			params.add(maestrotiposaviso.getDescEs40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getDescEu40() != null) {
			where.append(" AND t1.DESC_EU_40 = ?");
			params.add(maestrotiposaviso.getDescEu40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getfBaja40() != null) {
			where.append(" AND t1.F_BAJA_40 = ?");
			params.add(maestrotiposaviso.getfBaja40());
		}

		where.append(" AND t1.ORIGEN_PS_40 = 1");

		Map<String, Object> mapWhere = new HashMap<String, Object>();
		mapWhere.put("query", where);
		mapWhere.put("params", params);

		return mapWhere;
	}

	private Map<String, ?> getWhereSsbMap(MaestroTiposAviso maestrotiposaviso) {

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

		if (maestrotiposaviso != null && maestrotiposaviso.getIdTipoAviso40() != null) {
			where.append(" AND t1.ID_TIPO_AVISO_40 = ?");
			params.add(maestrotiposaviso.getIdTipoAviso40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getDescEs40() != null) {
			where.append(" AND t1.DESC_ES_40 = ?");
			params.add(maestrotiposaviso.getDescEs40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getDescEu40() != null) {
			where.append(" AND t1.DESC_EU_40 = ?");
			params.add(maestrotiposaviso.getDescEu40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getfBaja40() != null) {
			where.append(" AND t1.F_BAJA_40 = ?");
			params.add(maestrotiposaviso.getfBaja40());
		}

		where.append(" AND t1.ORIGEN_AUTOMATICO_40 = 0");

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

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

		if (maestrotiposaviso != null && maestrotiposaviso.getIdTipoAviso40() != null) {
			where.append(" AND t1.ID_TIPO_AVISO_40 = ?");
			params.add(maestrotiposaviso.getIdTipoAviso40());
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getDescEs40() != null) {
			where.append(" AND UPPER(t1.DESC_ES_40) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrotiposaviso.getDescEs40().toUpperCase() + "%");
			} else {
				params.add("%" + maestrotiposaviso.getDescEs40().toUpperCase() + "%");
			}
			where.append(" AND t1.DESC_ES_40 IS NOT NULL");
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getDescEu40() != null) {
			where.append(" AND UPPER(t1.DESC_EU_40) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrotiposaviso.getDescEu40().toUpperCase() + "%");
			} else {
				params.add("%" + maestrotiposaviso.getDescEu40().toUpperCase() + "%");
			}
			where.append(" AND t1.DESC_EU_40 IS NOT NULL");
		}
		if (maestrotiposaviso != null && maestrotiposaviso.getfBaja40() != null) {
			where.append(" AND t1.F_BAJA_40 = ?");
			params.add(maestrotiposaviso.getfBaja40());
		}

		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

	@Override
	public List<MaestroTiposAviso> findAvisosDss(MaestroTiposAviso maestrotiposaviso) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_TIPO_AVISO_40 IDTIPOAVISO40,t1.DESC_ES_40 DESCES40,t1.DESC_EU_40 DESCEU40,t1.F_BAJA_40 FBAJA40 ");
		query.append("FROM MAESTRO_TIPOS_AVISO t1 ");

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

		query.append(where);

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

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

	@Override
	public List<MaestroTiposAviso> findAvisosPs(MaestroTiposAviso maestrotiposaviso) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_TIPO_AVISO_40 IDTIPOAVISO40,t1.DESC_ES_40 DESCES40,t1.DESC_EU_40 DESCEU40,t1.F_BAJA_40 FBAJA40 ");
		query.append("FROM MAESTRO_TIPOS_AVISO t1 ");

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

		query.append(where);

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

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

	@Override
	public List<MaestroTiposAviso> findAvisosSsb(MaestroTiposAviso maestrotiposaviso) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_TIPO_AVISO_40 IDTIPOAVISO40,t1.DESC_ES_40 DESCES40,t1.DESC_EU_40 DESCEU40,t1.F_BAJA_40 FBAJA40 ");
		query.append("FROM MAESTRO_TIPOS_AVISO t1 ");

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

		query.append(where);

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

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

}
