package com.ejie.ad74a.dao;

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;

import java.math.BigDecimal;
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.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ad74a.model.Iprem;
import com.ejie.ad74a.util.UtilSQL;

/**
 * IpremDaoImpl generated by UDA, 04-oct-2023 15:51:45.
 * 
 * @author UDA
 */

@Repository
@Transactional
public class IpremDaoImpl implements IpremDao {

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDIPREM79", "IPREM79", "ANIO79", "FALTA79",
			"USUALTA79", "FMODIF79", "USUMODIF79" };

	private JdbcTemplate jdbcTemplate;

	public static final String SEQUENCE_IPREM = "SEQ_IPREM";

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<Iprem> rwMap = new RowMapper<Iprem>() {
		public Iprem mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new Iprem(UtilSQL.getLong(resultSet, "IDIPREM79"), resultSet.getBigDecimal("IPREM79"),
					UtilSQL.getLong(resultSet, "ANIO79"), resultSet.getDate("FALTA79"),
					resultSet.getString("USUALTA79"), resultSet.getDate("FMODIF79"), resultSet.getString("USUMODIF79"));
		}
	};

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

	private RowMapper<Iprem> rwMapAnioIPREM = new RowMapper<Iprem>() {
		public Iprem mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			Iprem iprem = new Iprem();
			iprem.setAnio79(UtilSQL.getLong(resultSet, "ANIO79"));
			return iprem;
		}
	};

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

			Iprem iprem = new Iprem(UtilSQL.getLong(resultSet, "IDIPREM79"), resultSet.getBigDecimal("IPREM79"),
					UtilSQL.getLong(resultSet, "ANIO79"), resultSet.getDate("FALTA79"),
					resultSet.getString("USUALTA79"), resultSet.getDate("FMODIF79"), resultSet.getString("USUMODIF79"));

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

		String query = "INSERT INTO IPREM (ID_IPREM_79, IPREM_79, ANIO_79, F_ALTA_79, USU_ALTA_79, F_MODIF_79, USU_MODIF_79) VALUES ("
				+ SEQUENCE_IPREM + ".NEXTVAL,"
				+ ":iprem, :anio, (select sysdate from dual), :usualta, :fmodif, :usumodif)";

		SqlParameterSource parameters = new MapSqlParameterSource().addValue("iprem", iprem.getIprem79())
				.addValue("anio", iprem.getAnio79()).addValue("usualta", iprem.getUsuAlta79())
				.addValue("fmodif", iprem.getFecModif79()).addValue("usumodif", iprem.getUsuModif79());

		NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
		namedParameterJdbcTemplate.update(query, parameters, generatedKeyHolder, new String[] { "ID_IPREM_79" });
		iprem.setIdIprem79(generatedKeyHolder.getKey().longValue());

		return iprem;
	}

	/**
	 * Updates a single row in the Iprem table.
	 *
	 * @param iprem Iprem
	 * @return Iprem
	 */
	public Iprem update(Iprem iprem) {
		String query = "UPDATE IPREM SET IPREM_79=?, ANIO_79=?, F_MODIF_79=SYSDATE, USU_MODIF_79=? WHERE ID_IPREM_79=?";
		this.jdbcTemplate.update(query, iprem.getIprem79(), iprem.getAnio79(), iprem.getUsuModif79(),
				iprem.getIdIprem79());
		return iprem;
	}

	/**
	 * Finds a single row in the Iprem table.
	 *
	 * @param iprem Iprem
	 * @return Iprem
	 */
	@Transactional(readOnly = true)
	public Iprem find(Iprem iprem) {
		String query = "SELECT t1.ID_IPREM_79 IDIPREM79, t1.IPREM_79 IPREM79, t1.ANIO_79 ANIO79, t1.F_ALTA_79 FALTA79, t1.USU_ALTA_79 USUALTA79, t1.F_MODIF_79 FMODIF79, t1.USU_MODIF_79 USUMODIF79 FROM IPREM t1  WHERE t1.ID_IPREM_79 = ?  ";

		List<Iprem> ipremList = this.jdbcTemplate.query(query, this.rwMap, iprem.getIdIprem79());
		return (Iprem) DataAccessUtils.uniqueResult(ipremList);
	}

	/**
	 * Removes a single row in the Iprem table.
	 *
	 * @param iprem Iprem
	 * @return
	 */
	public void remove(Iprem iprem) {
		String query = "DELETE FROM IPREM WHERE ID_IPREM_79=?";
		this.jdbcTemplate.update(query, iprem.getIdIprem79());
	}

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

		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_IPREM_79 IDIPREM79, t1.IPREM_79 IPREM79, t1.ANIO_79 ANIO79, t1.F_ALTA_79 FALTA79, t1.USU_ALTA_79 USUALTA79, t1.F_MODIF_79 FMODIF79, t1.USU_MODIF_79 USUMODIF79");
		query.append(" FROM IPREM t1  ");
		// Where clause & Params
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterIprem, 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, Iprem.class, params,
				"ID_IPREM_79");
		where.append(sbMultipleSQL);

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

	/**
	 * Finds a list of rows in the Iprem table.
	 *
	 * @param iprem           Iprem
	 * @param tableRequestDto TableRequestDto
	 * @return List<Iprem>
	 */
	@Transactional(readOnly = true)
	public List<Iprem> findAll(Iprem iprem, TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_IPREM_79 IDIPREM79,t1.IPREM_79 IPREM79,t1.ANIO_79 ANIO79,t1.F_ALTA_79 FALTA79,t1.USU_ALTA_79 USUALTA79,t1.F_MODIF_79 FMODIF79,t1.USU_MODIF_79 USUMODIF79 ");
		query.append("FROM IPREM t1 ");

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

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

	/**
	 * Finds rows in the Iprem table using like.
	 *
	 * @param iprem           Iprem
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<Iprem>
	 */
	@Transactional(readOnly = true)
	public List<Iprem> findAllLike(Iprem iprem, TableRequestDto tableRequestDto, Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_IPREM_79 IDIPREM79,t1.IPREM_79 IPREM79,t1.ANIO_79 ANIO79,t1.F_ALTA_79 FALTA79,t1.USU_ALTA_79 USUALTA79,t1.F_MODIF_79 FMODIF79,t1.USU_MODIF_79 USUMODIF79 ");
		query.append("FROM IPREM t1 ");

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(iprem, 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 Iprem selected for rup_table
	 *
	 * @param iprem           Iprem
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<TableRowDto<Iprem>>
	 */
	@Override
	public List<TableRowDto<Iprem>> reorderSelection(Iprem iprem, TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_IPREM_79 IDIPREM79,t1.IPREM_79 IPREM79,t1.ANIO_79 ANIO79,t1.F_ALTA_79 FALTA79,t1.USU_ALTA_79 USUALTA79,t1.F_MODIF_79 FMODIF79,t1.USU_MODIF_79 USUMODIF79 ");
		// FROM
		query.append("FROM IPREM t1 ");
		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(iprem, 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, Iprem.class,
				filterParamList, "IDIPREM79");

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

	/**
	 * Search method for rup_table
	 *
	 * @param filterParams    Iprem
	 * @param searchParams    Iprem
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<TableRowDto<Iprem>>
	 */
	@Override
	public List<TableRowDto<Iprem>> search(Iprem filterParams, Iprem searchParams, TableRequestDto tableRequestDto,
			Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_IPREM_79 IDIPREM79,t1.IPREM_79 IPREM79,t1.ANIO_79 ANIO79,t1.F_ALTA_79 FALTA79,t1.USU_ALTA_79 USUALTA79,t1.F_MODIF_79 FMODIF79,t1.USU_MODIF_79 USUMODIF79 ");
		// FROM
		query.append("FROM IPREM 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, Iprem.class,
				filterParamList, searchSQL, searchParamList, from_alias, "IDIPREM79");

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

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

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

	/**
	 * Filter in the hierarchical Iprem table.
	 *
	 * @param filterIprem     Iprem
	 * @param tableRequestDto TableRequestDto
	 * @return List<JerarquiaDto<Iprem>>
	 */
	@Override
	public List<JerarquiaDto<Iprem>> findAllLikeJerarquia(Iprem filterIprem, TableRequestDto tableRequestDto) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_IPREM_79 IDIPREM79,t1.IPREM_79 IPREM79,t1.ANIO_79 ANIO79,t1.F_ALTA_79 FALTA79,t1.USU_ALTA_79 USUALTA79,t1.F_MODIF_79 FMODIF79,t1.USU_MODIF_79 USUMODIF79 ");
		// FROM
		// StringBuilder from = new StringBuilder("FROM IPREM t1 ");
		List<String> from = new ArrayList<String>();
		from.add("IPREM");
		// 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(filterIprem, false);

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

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

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

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

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

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

	/*
	 * MÉTODOS PRIVADOS
	 */

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

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

		if (iprem != null && iprem.getIdIprem79() != null) {
			where.append(" AND t1.ID_IPREM_79 = ?");
			params.add(iprem.getIdIprem79());
		}
		if (iprem != null && iprem.getIprem79() != null) {
			where.append(" AND t1.IPREM_79 = ?");
			params.add(iprem.getIprem79());
		}
		if (iprem != null && iprem.getAnio79() != null) {
			where.append(" AND t1.ANIO_79 = ?");
			params.add(iprem.getAnio79());
		}
		if (iprem != null && iprem.getFecAlta79() != null) {
			where.append(" AND t1.F_ALTA_79 = ?");
			params.add(iprem.getFecAlta79());
		}
		if (iprem != null && iprem.getUsuAlta79() != null) {
			where.append(" AND t1.USU_ALTA_79 = ?");
			params.add(iprem.getUsuAlta79());
		}
		if (iprem != null && iprem.getFecModif79() != null) {
			where.append(" AND t1.F_MODIF_79 = ?");
			params.add(iprem.getFecModif79());
		}
		if (iprem != null && iprem.getUsuModif79() != null) {
			where.append(" AND t1.USU_MODIF_79 = ?");
			params.add(iprem.getUsuModif79());
		}

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

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

		if (iprem != null && iprem.getIdIprem79() != null) {
			where.append(" AND t1.ID_IPREM_79 = ?");
			params.add(iprem.getIdIprem79());
		}
		if (iprem != null && iprem.getIprem79() != null) {
			where.append(" AND t1.IPREM_79 = ?");
			params.add(iprem.getIprem79());
		}

		if (iprem != null && iprem.getIprem79minimo() != null) {
			where.append(" AND t1.IPREM_79 >= ?");
			params.add(iprem.getIprem79minimo());
		}

		if (iprem != null && iprem.getIprem79maximo() != null) {
			where.append(" AND t1.IPREM_79 <= ?");
			params.add(iprem.getIprem79maximo());
		}
		if (iprem != null && iprem.getAnio79() != null) {
			where.append(" AND t1.ANIO_79 = ?");
			params.add(iprem.getAnio79());
		}
		if (iprem != null && iprem.getFecAlta79() != null) {
			where.append(" AND t1.F_ALTA_79 = ?");
			params.add(iprem.getFecAlta79());
		}
		if (iprem != null && iprem.getUsuAlta79() != null) {
			where.append(" AND UPPER(t1.USU_ALTA_79) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(iprem.getUsuAlta79().toUpperCase() + "%");
			} else {
				params.add("%" + iprem.getUsuAlta79().toUpperCase() + "%");
			}
			where.append(" AND t1.USU_ALTA_79 IS NOT NULL");
		}
		if (iprem != null && iprem.getFecModif79() != null) {
			where.append(" AND t1.F_MODIF_79 = ?");
			params.add(iprem.getFecModif79());
		}
		if (iprem != null && iprem.getUsuModif79() != null) {
			where.append(" AND UPPER(t1.USU_MODIF_79) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(iprem.getUsuModif79().toUpperCase() + "%");
			} else {
				params.add("%" + iprem.getUsuModif79().toUpperCase() + "%");
			}
			where.append(" AND t1.USU_MODIF_79 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 BigDecimal getByAnio79(Iprem iprem) {
		StringBuilder query = new StringBuilder("SELECT IPREM_79 FROM IPREM WHERE ANIO_79 = ?   ");

		ArrayList<Object> params = new ArrayList<Object>();
		params.add(iprem.getAnio79());
		return this.jdbcTemplate.queryForObject(query.toString(), params.toArray(), BigDecimal.class);
	}

	@Override
	public List<Iprem> getAnioIPREMList() {
		StringBuilder query = new StringBuilder(
				"SELECT  DISTINCT t1.ANIO_79 ANIO79 FROM IPREM t1 order by  t1.ANIO_79 desc  ");

		return (List<Iprem>) this.jdbcTemplate.query(query.toString(), this.rwMapAnioIPREM);
	}

	@Override
	public Iprem getMaxIprem() {
		String query = "SELECT t1.ID_IPREM_79 IDIPREM79, t1.IPREM_79 IPREM79, t1.ANIO_79 ANIO79, t1.F_ALTA_79 FALTA79, t1.USU_ALTA_79 USUALTA79, t1.F_MODIF_79 FMODIF79, t1.USU_MODIF_79 USUMODIF79 FROM IPREM t1  WHERE t1.ANIO_79 = (SELECT MAX(ANIO_79) FROM IPREM) ";

		List<Iprem> ipremList = this.jdbcTemplate.query(query, this.rwMap);
		return (Iprem) DataAccessUtils.uniqueResult(ipremList);
	}

	@Override
	public Iprem getIpremActual() {
		// TODO Auto-generated method stub
		String query = "SELECT t1.ID_IPREM_79 IDIPREM79, t1.IPREM_79 IPREM79, t1.ANIO_79 ANIO79, t1.F_ALTA_79 FALTA79, t1.USU_ALTA_79 USUALTA79, t1.F_MODIF_79 FMODIF79, t1.USU_MODIF_79 USUMODIF79 FROM IPREM t1  WHERE t1.ANIO_79 = (select max(t2.anio_79) from iprem t2) ";

		List<Iprem> ipremList = this.jdbcTemplate.query(query, this.rwMap);
		return (Iprem) DataAccessUtils.uniqueResult(ipremList);
	}

	@Override
	public Iprem getIpremAnio(Long anio) {
		// TODO Auto-generated method stub
		String query = "SELECT t1.ID_IPREM_79 IDIPREM79, t1.IPREM_79 IPREM79, t1.ANIO_79 ANIO79, t1.F_ALTA_79 FALTA79, t1.USU_ALTA_79 USUALTA79, t1.F_MODIF_79 FMODIF79, t1.USU_MODIF_79 USUMODIF79 FROM IPREM t1  WHERE t1.ANIO_79 = (?) ";

		List<Iprem> ipremList = this.jdbcTemplate.query(query, this.rwMap, anio);
		return (Iprem) DataAccessUtils.uniqueResult(ipremList);
	}

}
