package com.ejie.m62fb.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
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.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.m62fb.model.Adjudicatario;
import com.ejie.m62fb.model.M62fbr45s0s00;
import com.ejie.m62fb.util.UdaUtils;
import com.ejie.x38.dto.Pagination;

/**
 * M62fbr45s0s00DaoImpl generated by UDA, 17-oct-2012 15:03:38.
 * 
XXXX
 */

@Repository
@Transactional
public class M62fbr45s0s00DaoImpl implements M62fbr45s0s00Dao {
	private JdbcTemplate jdbcTemplate;
	private RowMapper<M62fbr45s0s00> rwMap = new RowMapper<M62fbr45s0s00>() {
		public M62fbr45s0s00 mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
			Adjudicatario adjudicatario = new Adjudicatario();
			adjudicatario.setIdPersona(resultSet.getBigDecimal("IDPERSONA"));

			return new M62fbr45s0s00(resultSet.getBigDecimal("IDM62R45T0"),
					resultSet.getBigDecimal("IDPERSONA"),
					resultSet.getString("NIFADJUDICATARIO"),
					resultSet.getString("CODSITOFERTA"),
					resultSet.getDate("FECOFERTA"),
					resultSet.getDate("FECFINALIZACION"),
					resultSet.getDate("FECDECLINACION"),
					resultSet.getDate("FECACEPTACION"),
					resultSet.getString("TEXOBSERVACIONES"),
					resultSet.getDate("FEHULTMODIF"),
					resultSet.getString("CODUSUULTMODIF"),
					resultSet.getInt("IDEREGISTRO"),
					resultSet.getDate("FECALTA"),
					resultSet.getString("NOMADJUDICATARIO"),
					resultSet.getString("DESSITOFERTA"), adjudicatario);
		}
	};

	/**
	 * Method use to set the datasource.
	 * 
	 * @param dataSource
	 *            DataSource
	 * @return
	 */
	@Resource
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	/**
	 * Inserts a single row in the M62fbr45s0s00 table.
	 * 
	 * @param m62fbr45s0s00
	 *            Pagination
	 * @return M62fbr45s0s00
	 */
	public M62fbr45s0s00 add(final M62fbr45s0s00 m62fbr45s0s00) {
		final String query = "INSERT INTO M62R45T0S00 "
				+ "(ID_PERSONA, COD_SIT_OFERTA, FEC_OFERTA, FEC_FINALIZACION, "
				+ "FEC_DECLINACION, FEC_ACEPTACION, TEX_OBSERVACIONES ) "
				+ "VALUES (?,'P',?,?,?,?,?)";
		KeyHolder keyHolder = new GeneratedKeyHolder();
		this.jdbcTemplate.update(new PreparedStatementCreator() {
			@Override
			public PreparedStatement createPreparedStatement(
					Connection connection) throws SQLException {
				// Sentencia SQL que realiza la INSERT y array con ids que son
				// primary key
				PreparedStatement ps = connection.prepareStatement(query,
						new String[] { "ID_M62R45T0" });
				ps.setBigDecimal(
						1, /* ID_PERSONA */
						m62fbr45s0s00.getNueadjudicatario().getIdPersona() != null ? m62fbr45s0s00
								.getNueadjudicatario().getIdPersona() : null);
				/*
				 * ps.setString( 2, /* COD_SIT_OFERTA
				 */
				/*
				 * m62fbr45s0s00.getCodSitOferta() != null ? m62fbr45s0s00
				 * .getCodSitOferta() : null);
				 */
				ps.setObject(
						2, /* FEC_OFERTA */
						m62fbr45s0s00.getFecOferta() != null ? m62fbr45s0s00
								.getFecOferta() : null);
				ps.setObject(
						3, /* FEC_FINALIZACION */
						m62fbr45s0s00.getFecFinalizacion() != null ? m62fbr45s0s00
								.getFecFinalizacion() : null);
				ps.setObject(
						4, /* FEC_DECLINACION */
						m62fbr45s0s00.getFecDeclinacion() != null ? m62fbr45s0s00
								.getFecDeclinacion() : null);
				ps.setObject(
						5, /* FEC_ACEPTACION */
						m62fbr45s0s00.getFecAceptacion() != null ? m62fbr45s0s00
								.getFecAceptacion() : null);
				ps.setString(
						6, /* TEX_OBSERVACIONES */
						m62fbr45s0s00.getTexObservaciones() != null ? m62fbr45s0s00
								.getTexObservaciones() : null);
				return ps;

			}

		}, keyHolder);
		// obtener ID_EXPEDIENTE generado por el trigger
		m62fbr45s0s00.setIdM62r45t0(new BigDecimal(keyHolder.getKeys()
				.get("ID_M62R45T0").toString()));

		m62fbr45s0s00.setDesSitOferta(getDescSitOferta(m62fbr45s0s00
				.getCodSitOferta()));
		m62fbr45s0s00.setNomAdjudicatario(getDescAdjudicatario(m62fbr45s0s00
				.getNueadjudicatario().getIdPersona()));

		/*
		 * this.jdbcTemplate.update(query, m62fbr45s0s00.getIdPersona(),
		 * m62fbr45s0s00.getCodSitOferta(), m62fbr45s0s00.getFecOferta(),
		 * m62fbr45s0s00.getFecFinalizacion(),
		 * m62fbr45s0s00.getFecDeclinacion(), m62fbr45s0s00.getFecAceptacion(),
		 * m62fbr45s0s00.getTexObservaciones());
		 */
		return m62fbr45s0s00;
	}

	/**
	 * Updates a single row in the M62fbr45s0s00 table.
	 * 
	 * @param m62fbr45s0s00
	 *            Pagination
	 * @return M62fbr45s0s00
	 */
	public M62fbr45s0s00 update(M62fbr45s0s00 m62fbr45s0s00) {
		String query;
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFecFinalizacion() != null) {
			query = "UPDATE M62R44T0S00 SET COD_SITUACION='D', FEH_ULT_MODIF=SYSDATE WHERE NUM_VIVIENDA IN ( SELECT NUM_VIVIENDA FROM M62R46T0S00 WHERE ID_M62R45T0=?)";
			this.jdbcTemplate.update(query, m62fbr45s0s00.getIdM62r45t0());
		}

		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFecDeclinacion() != null) {
			query = "UPDATE M62R44T0S00 SET COD_SITUACION='D', FEH_ULT_MODIF=SYSDATE WHERE NUM_VIVIENDA IN ( SELECT NUM_VIVIENDA FROM M62R46T0S00 WHERE ID_M62R45T0=?)";
			this.jdbcTemplate.update(query, m62fbr45s0s00.getIdM62r45t0());
		}

		query = "UPDATE M62R45T0S00 SET ID_PERSONA=? , COD_SIT_OFERTA=? , FEC_OFERTA=? , FEC_FINALIZACION=? , FEC_DECLINACION=? , FEC_ACEPTACION=? , TEX_OBSERVACIONES=? , FEH_ULT_MODIF=SYSDATE WHERE ID_M62R45T0=?";
		this.jdbcTemplate.update(query, m62fbr45s0s00.getNueadjudicatario()
				.getIdPersona(), m62fbr45s0s00.getCodSitOferta(), m62fbr45s0s00
				.getFecOferta(), m62fbr45s0s00.getFecFinalizacion(),
				m62fbr45s0s00.getFecDeclinacion(), m62fbr45s0s00
						.getFecAceptacion(), m62fbr45s0s00
						.getTexObservaciones(), m62fbr45s0s00.getIdM62r45t0());

		m62fbr45s0s00.setDesSitOferta(getDescSitOferta(m62fbr45s0s00
				.getCodSitOferta()));
		m62fbr45s0s00.setNomAdjudicatario(getDescAdjudicatario(m62fbr45s0s00
				.getNueadjudicatario().getIdPersona()));
		return m62fbr45s0s00;
	}

	/**
	 * Finds a single row in the M62fbr45s0s00 table.
	 * 
	 * @param m62fbr45s0s00
	 *            Pagination
	 * @return M62fbr45s0s00
	 */
	@Transactional(readOnly = true, timeout = 500)
	public M62fbr45s0s00 find(M62fbr45s0s00 m62fbr45s0s00) {
		String query = "SELECT t1.ID_M62R45T0 IDM62R45T0, t1.ID_PERSONA IDPERSONA, t2.NIF_TITULAR NIFADJUDICATARIO, t1.COD_SIT_OFERTA CODSITOFERTA, t1.FEC_OFERTA FECOFERTA, t1.FEC_FINALIZACION FECFINALIZACION, t1.FEC_DECLINACION FECDECLINACION, t1.FEC_ACEPTACION FECACEPTACION, t1.TEX_OBSERVACIONES TEXOBSERVACIONES, t1.FEH_ULT_MODIF FEHULTMODIF, t1.COD_USU_ULT_MODIF CODUSUULTMODIF, t1.IDE_REGISTRO IDEREGISTRO, t1.FEC_ALTA FECALTA, M62G0000K000S00.FS_APELLIDOS_NOMBRE(t1.ID_PERSONA) NOMADJUDICATARIO, t3.DES_TABLA_CAS DESSITOFERTA FROM M62R45T0S00 t1, M62P20T0S00 t2, M62P28V0S00_SITUACION_OFE t3  WHERE t1.ID_M62R45T0 = ? AND t1.COD_SIT_OFERTA = t3.COD_TABLA(+) AND t1.ID_PERSONA = t2.ID_PERSONA ";

		List<M62fbr45s0s00> m62fbr45s0s00List = this.jdbcTemplate.query(query,
				this.rwMap, m62fbr45s0s00.getIdM62r45t0());
		return (M62fbr45s0s00) DataAccessUtils.uniqueResult(m62fbr45s0s00List);
	}

	/**
	 * Removes a single row in the M62fbr45s0s00 table.
	 * 
	 * @param m62fbr45s0s00
	 *            Pagination
	 * @return
	 */
	public void remove(M62fbr45s0s00 m62fbr45s0s00) {
		// Ponemos disponibles las viviendas en el catálogo
		//M62fbr45s0s00 ofertaCompleta = new M62fbr45s0s00();

		//ofertaCompleta = this.find(m62fbr45s0s00);
		String query = "UPDATE M62R44T0S00 SET COD_SITUACION='D' , FEH_ULT_MODIF=SYSDATE WHERE NUM_VIVIENDA IN ( SELECT NUM_VIVIENDA FROM M62R46T0S00 WHERE ID_M62R45T0=?)";
		this.jdbcTemplate.update(query, m62fbr45s0s00.getIdM62r45t0());

		// Borramos las viviendas asociadas a la oferta de la r46
		query = "DELETE FROM M62R46T0S00 WHERE ID_M62R45T0=?";
		this.jdbcTemplate.update(query, m62fbr45s0s00.getIdM62r45t0());
		// Borramos la oferta
		query = "DELETE FROM M62R45T0S00 WHERE ID_M62R45T0=?";
		this.jdbcTemplate.update(query, m62fbr45s0s00.getIdM62r45t0());
	}

	/**
	 * Finds a List of rows in the M62fbr45s0s00 table.
	 * 
	 * @param m62fbr45s0s00
	 *            M62fbr45s0s00
	 * @param pagination
	 *            Pagination
	 * @return List
	 */
	@Transactional(readOnly = true, timeout = 500)
	public List<M62fbr45s0s00> findAll(M62fbr45s0s00 m62fbr45s0s00,
			Pagination pagination) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_M62R45T0 IDM62R45T0,t1.ID_PERSONA IDPERSONA, t2.NIF_TITULAR NIFADJUDICATARIO, t1.COD_SIT_OFERTA CODSITOFERTA,t1.FEC_OFERTA FECOFERTA,t1.FEC_FINALIZACION FECFINALIZACION,t1.FEC_DECLINACION FECDECLINACION,t1.FEC_ACEPTACION FECACEPTACION,t1.TEX_OBSERVACIONES TEXOBSERVACIONES,t1.FEH_ULT_MODIF FEHULTMODIF,t1.COD_USU_ULT_MODIF CODUSUULTMODIF,t1.IDE_REGISTRO IDEREGISTRO,t1.FEC_ALTA FECALTA, M62G0000K000S00.FS_APELLIDOS_NOMBRE(t1.ID_PERSONA) NOMADJUDICATARIO, t3.DES_TABLA_CAS DESSITOFERTA ");
		query.append("FROM M62R45T0S00 t1, M62P20T0S00 t2, M62P28V0S00_SITUACION_OFE t3 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(m62fbr45s0s00);
		StringBuilder where = new StringBuilder(
				" WHERE 1=1 AND t1.ID_PERSONA = t2.ID_PERSONA(+) AND t1.COD_SIT_OFERTA = t3.COD_TABLA(+) ");
		where.append(mapaWhere.get("query"));
		query.append(where);

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

		if (pagination != null) {
			query = pagination.getPaginationQuery(query);
		}

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

	/**
	 * Counts rows in the M62fbr45s0s00 table.
	 * 
	 * @param m62fbr45s0s00
	 *            M62fbr45s0s00
	 * @return Long
	 */
	@Transactional(readOnly = true, timeout = 500)
	public Long findAllCount(M62fbr45s0s00 m62fbr45s0s00) {
		StringBuilder query = new StringBuilder(
				"SELECT COUNT(1) FROM M62R45T0S00 t1, M62P20T0S00 t2, M62P28V0S00_SITUACION_OFE t3 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(m62fbr45s0s00);
		StringBuilder where = new StringBuilder(
				" WHERE 1=1 AND t1.ID_PERSONA = t2.ID_PERSONA(+) AND t1.COD_SIT_OFERTA = t3.COD_TABLA(+) ");
		where.append(mapaWhere.get("query"));
		query.append(where);

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

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

	/**
	 * Finds rows in the M62fbr45s0s00 table using like.
	 * 
	 * @param m62fbr45s0s00
	 *            M62fbr45s0s00
	 * @param pagination
	 *            Pagination
	 * @param startsWith
	 *            Boolean
	 * @return List
	 */
	@Transactional(readOnly = true, timeout = 500)
	public List<M62fbr45s0s00> findAllLike(M62fbr45s0s00 m62fbr45s0s00,
			Pagination pagination, Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_M62R45T0 IDM62R45T0,t1.ID_PERSONA IDPERSONA, t2.NIF_TITULAR NIFADJUDICATARIO, t1.COD_SIT_OFERTA CODSITOFERTA,t1.FEC_OFERTA FECOFERTA,t1.FEC_FINALIZACION FECFINALIZACION,t1.FEC_DECLINACION FECDECLINACION,t1.FEC_ACEPTACION FECACEPTACION,t1.TEX_OBSERVACIONES TEXOBSERVACIONES,t1.FEH_ULT_MODIF FEHULTMODIF,t1.COD_USU_ULT_MODIF CODUSUULTMODIF,t1.IDE_REGISTRO IDEREGISTRO,t1.FEC_ALTA FECALTA, M62G0000K000S00.FS_APELLIDOS_NOMBRE(t1.ID_PERSONA) NOMADJUDICATARIO, t3.DES_TABLA_CAS DESSITOFERTA ");
		query.append("FROM M62R45T0S00 t1, M62P20T0S00 t2, M62P28V0S00_SITUACION_OFE t3 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(m62fbr45s0s00,
				startsWith);
		StringBuilder where = new StringBuilder(
				" WHERE 1=1 AND t1.ID_PERSONA = t2.ID_PERSONA(+) AND t1.COD_SIT_OFERTA = t3.COD_TABLA(+) ");
		where.append(mapaWhere.get("query"));
		query.append(where);

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

		if (pagination != null) {
			query = pagination.getPaginationQuery(query);
		}

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

	/**
	 * Counts rows in the M62fbr45s0s00 table using like.
	 * 
	 * @param m62fbr45s0s00
	 *            M62fbr45s0s00
	 * @param startsWith
	 *            Boolean
	 * @return Long
	 */
	@Transactional(readOnly = true, timeout = 500)
	public Long findAllLikeCount(M62fbr45s0s00 m62fbr45s0s00, Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT COUNT(1) FROM M62R45T0S00 t1, M62P20T0S00 t2, M62P28V0S00_SITUACION_OFE t3 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(m62fbr45s0s00,
				startsWith);
		StringBuilder where = new StringBuilder(
				" WHERE 1=1 AND t1.ID_PERSONA = t2.ID_PERSONA(+) AND t1.COD_SIT_OFERTA = t3.COD_TABLA(+) ");
		where.append(mapaWhere.get("query"));
		query.append(where);

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

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

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

		StringBuffer where = new StringBuffer(
				M62fbr45s0s00DaoImpl.STRING_BUILDER_INIT);
		List<Object> params = new ArrayList<Object>();

		if (m62fbr45s0s00 != null && m62fbr45s0s00.getIdM62r45t0() != null) {
			where.append(" AND t1.ID_M62R45T0 = ?");
			params.add(m62fbr45s0s00.getIdM62r45t0());
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getIdPersona() != null) {
			where.append(" AND t1.ID_PERSONA = ?");
			params.add(m62fbr45s0s00.getIdPersona());
		}
		if (m62fbr45s0s00 != null
				&& m62fbr45s0s00.getNifAdjudicatario() != null) {
			where.append(" AND t2.NIF_TITULAR = ?");
			params.add(m62fbr45s0s00.getNifAdjudicatario());
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getCodSitOferta() != null) {
			where.append(" AND t1.COD_SIT_OFERTA = ?");
			params.add(m62fbr45s0s00.getCodSitOferta());
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFecOferta() != null) {
			where.append(" AND t1.FEC_OFERTA = ?");
			params.add(m62fbr45s0s00.getFecOferta());
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFecFinalizacion() != null) {
			where.append(" AND t1.FEC_FINALIZACION = ?");
			params.add(m62fbr45s0s00.getFecFinalizacion());
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFecDeclinacion() != null) {
			where.append(" AND t1.FEC_DECLINACION = ?");
			params.add(m62fbr45s0s00.getFecDeclinacion());
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFecAceptacion() != null) {
			where.append(" AND t1.FEC_ACEPTACION = ?");
			params.add(m62fbr45s0s00.getFecAceptacion());
		}
		if (m62fbr45s0s00 != null
				&& m62fbr45s0s00.getTexObservaciones() != null) {
			where.append(" AND t1.TEX_OBSERVACIONES = ?");
			params.add(m62fbr45s0s00.getTexObservaciones());
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFehUltModif() != null) {
			where.append(" AND t1.FEH_ULT_MODIF = ?");
			params.add(m62fbr45s0s00.getFehUltModif());
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getCodUsuUltModif() != null) {
			where.append(" AND t1.COD_USU_ULT_MODIF = ?");
			params.add(m62fbr45s0s00.getCodUsuUltModif());
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getIdeRegistro() != null) {
			where.append(" AND t1.IDE_REGISTRO = ?");
			params.add(m62fbr45s0s00.getIdeRegistro());
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFecAlta() != null) {
			where.append(" AND t1.FEC_ALTA = ?");
			params.add(m62fbr45s0s00.getFecAlta());
		}

		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 M62fbr45s0s00 entity
	 * 
	 * @param m62fbr45s0s00
	 *            M62fbr45s0s00 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, ?> getWhereLikeMap(M62fbr45s0s00 m62fbr45s0s00,
			Boolean startsWith) {

		StringBuffer where = new StringBuffer(
				M62fbr45s0s00DaoImpl.STRING_BUILDER_INIT);
		List<Object> params = new ArrayList<Object>();

		if (m62fbr45s0s00 != null && m62fbr45s0s00.getIdM62r45t0() != null) {
			where.append(" AND t1.ID_M62R45T0 = ?");
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getIdPersona() != null) {
			where.append(" AND t1.ID_PERSONA = ?");
		}
		if (m62fbr45s0s00 != null
				&& m62fbr45s0s00.getNifAdjudicatario() != null) {
			where.append(" AND t2.NIF_TITULAR = ?");
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getCodSitOferta() != null) {
			where.append(" AND UPPER(t1.COD_SIT_OFERTA) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(m62fbr45s0s00.getCodSitOferta().toUpperCase() + "%");
			} else {
				params.add("%" + m62fbr45s0s00.getCodSitOferta().toUpperCase()
						+ "%");
			}
			where.append(" AND t1.COD_SIT_OFERTA IS NOT NULL");
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFecOferta() != null) {
			where.append(" AND t1.FEC_OFERTA = ?");
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFecFinalizacion() != null) {
			where.append(" AND t1.FEC_FINALIZACION = ?");
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFecDeclinacion() != null) {
			where.append(" AND t1.FEC_DECLINACION = ?");
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFecAceptacion() != null) {
			where.append(" AND t1.FEC_ACEPTACION = ?");
		}
		if (m62fbr45s0s00 != null
				&& m62fbr45s0s00.getTexObservaciones() != null) {
			where.append(" AND UPPER(t1.TEX_OBSERVACIONES) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(m62fbr45s0s00.getTexObservaciones().toUpperCase()
						+ "%");
			} else {
				params.add("%"
						+ m62fbr45s0s00.getTexObservaciones().toUpperCase()
						+ "%");
			}
			where.append(" AND t1.TEX_OBSERVACIONES IS NOT NULL");
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFehUltModif() != null) {
			where.append(" AND t1.FEH_ULT_MODIF = ?");
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getCodUsuUltModif() != null) {
			where.append(" AND UPPER(t1.COD_USU_ULT_MODIF) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(m62fbr45s0s00.getCodUsuUltModif().toUpperCase()
						+ "%");
			} else {
				params.add("%"
						+ m62fbr45s0s00.getCodUsuUltModif().toUpperCase() + "%");
			}
			where.append(" AND t1.COD_USU_ULT_MODIF IS NOT NULL");
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getIdeRegistro() != null) {
			where.append(" AND t1.IDE_REGISTRO = ?");
		}
		if (m62fbr45s0s00 != null && m62fbr45s0s00.getFecAlta() != null) {
			where.append(" AND t1.FEC_ALTA = ?");
		}

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

		return mapWhere;
	}

	/**
	 * Finds rows in the r45t0 table using like.
	 * 
	 * @param idM62r45t0_search
	 *            BigDecimal
	 * @param nifAdjudicatario_search
	 *            String
	 * @param codSitOferta_search
	 *            String
	 * @param fecOfertaDesde_search
	 *            Date
	 * @param fecOfertaHasta_search
	 *            Date
	 * @param fecFinalizacionDesde_search
	 *            Date
	 * @param fecFInalizacionHasta_search
	 *            Date
	 * @param fecDeclinacionDesde_search
	 *            Date
	 * @param fecDeclinacionHasta_search
	 *            Date
	 * @param fecAceptacionDesde_search
	 *            Date
	 * @param fecAceptacionHasta_search
	 *            Date
	 * @param pagination
	 *            Pagination
	 * @return List
	 */
	@Transactional(readOnly = true, timeout = 500)
	public List<M62fbr45s0s00> findAllPersonalizado(BigDecimal idR45,
			BigDecimal idPersona, String codSitOferta, Date fecOfertaDesde,
			Date fecOfertaHasta, Date fecFinalizacionDesde,
			Date fecFinalizacionHasta, Date fecDeclinacionDesde,
			Date fecDeclinacionHasta, Date fecAceptacionDesde,
			Date fecAceptacionHasta, Pagination pagination) {
		StringBuffer where = new StringBuffer(3000);
		where.append(" WHERE 1=1 AND t1.ID_PERSONA = t2.ID_PERSONA(+) AND t1.COD_SIT_OFERTA = t3.COD_TABLA(+) ");

		StringBuffer query = new StringBuffer(
				"SELECT  t1.ID_M62R45T0 IDM62R45T0,t1.ID_PERSONA IDPERSONA, t2.NIF_TITULAR NIFADJUDICATARIO, t1.COD_SIT_OFERTA CODSITOFERTA,t1.FEC_OFERTA FECOFERTA,t1.FEC_FINALIZACION FECFINALIZACION,t1.FEC_DECLINACION FECDECLINACION,t1.FEC_ACEPTACION FECACEPTACION,t1.TEX_OBSERVACIONES TEXOBSERVACIONES,t1.FEH_ULT_MODIF FEHULTMODIF,t1.COD_USU_ULT_MODIF CODUSUULTMODIF,t1.IDE_REGISTRO IDEREGISTRO,t1.FEC_ALTA FECALTA, M62G0000K000S00.FS_APELLIDOS_NOMBRE(t1.ID_PERSONA) NOMADJUDICATARIO, t3.DES_TABLA_CAS DESSITOFERTA ");
		query.append("FROM M62R45T0S00 t1, M62P20T0S00 t2, M62P28V0S00_SITUACION_OFE t3 ");

		HashMap<String, Object> hm = new HashMap<String, Object>(
				getWhereFiltro(idR45, idPersona, codSitOferta, fecOfertaDesde,
						fecOfertaHasta, fecFinalizacionDesde,
						fecFinalizacionHasta, fecDeclinacionDesde,
						fecDeclinacionHasta, fecAceptacionDesde,
						fecAceptacionHasta));
		where.append(hm.get("where").toString());
		query.append(where.toString());
		List<Object> params = (List<Object>) hm.get("params");
		// order
		StringBuffer order = new StringBuffer(3000);
		if (pagination != null) {
			if (pagination.getSort() != null) {
				order.append(" ORDER BY " + pagination.getSort() + " "
						+ pagination.getAscDsc());
				query.append(order);
			}
			query = new StringBuffer(UdaUtils.getQueryLimits(
					pagination, query.toString()));
		}
		return (List<M62fbr45s0s00>) this.jdbcTemplate.query(query.toString(),
				rwMap, params.toArray());
	}

	/**
	 * Counts rows in the r45t0 .
	 * 
	 * @param idM62r45t0_search
	 *            BigDecimal
	 * @param idPersona_search
	 *            BigDecimal
	 * @param codSitOferta_search
	 *            String
	 * @param fecOfertaDesde_search
	 *            Date
	 * @param fecOfertaHasta_search
	 *            Date
	 * @param fecFinalizacionDesde_search
	 *            Date
	 * @param fecFinalizacionHasta_search
	 *            Date
	 * @param fecDeclinacionDesde_search
	 *            Date
	 * @param fecDeclinacionHasta_search
	 *            Date
	 * @param fecAceptacionDesde_search
	 *            Date
	 * @param fecAceptacionHasta_search
	 *            Date
	 * @return Long
	 */
	@Transactional(readOnly = true, timeout = 500)
	public Long findAllPersonalizadoCount(BigDecimal idR45,
			BigDecimal idPersona, String codSitOferta, Date fecOfertaDesde,
			Date fecOfertaHasta, Date fecFinalizacionDesde,
			Date fecFinalizacionHasta, Date fecDeclinacionDesde,
			Date fecDeclinacionHasta, Date fecAceptacionDesde,
			Date fecAceptacionHasta) {
		StringBuffer where = new StringBuffer(3000);
		where.append(" WHERE 1=1 AND t1.ID_PERSONA = t2.ID_PERSONA(+) AND t1.COD_SIT_OFERTA = t3.COD_TABLA(+) ");

		StringBuffer query = new StringBuffer("SELECT COUNT(1) ");
		query.append("FROM M62R45T0S00 t1, M62P20T0S00 t2, M62P28V0S00_SITUACION_OFE t3 ");

		HashMap<String, Object> hm = new HashMap<String, Object>(
				getWhereFiltro(idR45, idPersona, codSitOferta, fecOfertaDesde,
						fecOfertaHasta, fecFinalizacionDesde,
						fecFinalizacionHasta, fecDeclinacionDesde,
						fecDeclinacionHasta, fecAceptacionDesde,
						fecAceptacionHasta));
		where.append(hm.get("where").toString());
		query.append(where.toString());
		List<Object> params = (List<Object>) hm.get("params");
		return this.jdbcTemplate.queryForLong(query.toString(),
				params.toArray());
	}

	/**
	 * Crea la where para el filtro personalizado
	 * 
	 * @param idM62r45t0_search
	 *            BigDecimal
	 * @param idPersona_search
	 *            BigDecimal
	 * @param codSitOferta_search
	 *            String
	 * @param fecOfertaDesde_search
	 *            Date
	 * @param fecOfertaHasta_search
	 *            Date
	 * @param fecFinalizacionDesde_search
	 *            Date
	 * @param fecFinalizacionHasta_search
	 *            Date
	 * @param fecDeclinacionDesde_search
	 *            Date
	 * @param fecDeclinacionHasta_search
	 *            Date
	 * @param fecAceptacionDesde_search
	 *            Date
	 * @param fecAceptacionHasta_search
	 *            Date
	 * @return HashMap
	 */
	private HashMap<String, Object> getWhereFiltro(BigDecimal idR45,
			BigDecimal idPersona, String codSitOferta, Date fecOfertaDesde,
			Date fecOfertaHasta, Date fecFinalizacionDesde,
			Date fecFinalizacionHasta, Date fecDeclinacionDesde,
			Date fecDeclinacionHasta, Date fecAceptacionDesde,
			Date fecAceptacionHasta) {
		StringBuffer where = new StringBuffer(3000);
		List<Object> params = new ArrayList<Object>();
		HashMap<String, Object> hm = new HashMap<String, Object>();

		if (idR45 != null) {
			where.append(" AND t1.ID_M62R45T0 = ?");
			params.add(idR45);
		}
		if (idPersona != null) {
			where.append(" AND t1.ID_PERSONA = ?");
			params.add(idPersona);
		}
		if (codSitOferta != null && codSitOferta != "") {
			where.append(" AND t1.COD_SIT_OFERTA = ?");
			params.add(codSitOferta);
		}
		if (fecOfertaDesde != null) {
			where.append(" AND t1.FEC_OFERTA >= ?");
			params.add(fecOfertaDesde);
		}
		if (fecOfertaHasta != null) {
			where.append(" AND t1.FEC_OFERTA <= ?");
			params.add(fecOfertaHasta);
		}
		if (fecFinalizacionDesde != null) {
			where.append(" AND t1.FEC_FINALIZACION >= ?");
			params.add(fecFinalizacionDesde);
		}
		if (fecFinalizacionHasta != null) {
			where.append(" AND t1.FEC_FINALIZACION <= ?");
			params.add(fecFinalizacionHasta);
		}
		if (fecDeclinacionDesde != null) {
			where.append(" AND t1.FEC_DECLINACION >= ?");
			params.add(fecDeclinacionDesde);
		}
		if (fecDeclinacionHasta != null) {
			where.append(" AND t1.FEC_DECLINACION <= ?");
			params.add(fecDeclinacionHasta);
		}
		if (fecAceptacionDesde != null) {
			where.append(" AND t1.FEC_ACEPTACION >= ?");
			params.add(fecAceptacionDesde);
		}
		if (fecAceptacionHasta != null) {
			where.append(" AND t1.FEC_ACEPTACION <= ?");
			params.add(fecAceptacionHasta);
		}
		hm.put("where", where.toString());
		hm.put("params", params);
		return hm;
	}

	/**
	 * Devuelve la descripción del adjudicatario.
	 * 
	 * @param idPersona
	 *            String
	 * @return
	 */
	@Transactional(readOnly = true, timeout = 500)
	public String getDescAdjudicatario(BigDecimal idPersona) {
		List<Object> params = new ArrayList<Object>();
		if (idPersona != null) {
			StringBuffer query = new StringBuffer(
					"SELECT COD_TIPO_CLAVE || '.' || NIF_TITULAR || '.' || NOM_NOMBRE || ' ' || NOM_APELLIDO_1 || ' ' || NOM_APELLIDO_2 FROM M62P20T0S00 WHERE ID_PERSONA = ? ");
			params.add(idPersona.toString());
			// query.append(where);
			return (String) this.jdbcTemplate.queryForObject(query.toString(),
					String.class, params.toArray());
		} else {
			return "";
		}
	}

	/**
	 * Devuelve la descripción del adjudicatario.
	 * 
	 * @param idPersona
	 *            String
	 * @return
	 */
	@Transactional(readOnly = true, timeout = 500)
	private String getDescSitOferta(String codSitOferta) {
		List<Object> params = new ArrayList<Object>();
		if (codSitOferta != null) {
			StringBuffer query = new StringBuffer(
					"SELECT DES_TABLA_CAS FROM M62P28V0S00_SITUACION_OFE WHERE COD_TABLA = ? ");
			params.add(codSitOferta.toString());
			// query.append(where);
			return (String) this.jdbcTemplate.queryForObject(query.toString(),
					String.class, params.toArray());
		} else {
			return "";
		}
	}

	// CHECKSTYLE:ON CyclomaticComplexity - Generación de código de UDA

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