package com.ejie.m62fb.dao.registroVPP;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

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.M62fbr49s0s00;
import com.ejie.x38.dto.Pagination;

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

@Repository
@Transactional
public class M62fbr49s0s00DaoImpl implements M62fbr49s0s00Dao {
	private JdbcTemplate jdbcTemplate;
	private RowMapper<M62fbr49s0s00> rwMap = new RowMapper<M62fbr49s0s00>() {
		public M62fbr49s0s00 mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {

			return new M62fbr49s0s00(resultSet.getBigDecimal("ID_M62R49T0"),
					resultSet.getString("TIPO_FICHERO"),
					resultSet.getString("NOM_FICHERO"),
					resultSet.getString("ESTADO"),
					resultSet.getDate("FEH_ULT_MODIF"),
					resultSet.getDate("FEC_ALTA"),
					resultSet.getString("ERRORES"));
		}
	};

	/**
	 * 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 M62fbr46s0s00 table.
	 * 
	 * @param m62fbr46s0s00
	 *            Pagination
	 * @return M62fbr46s0s00
	 */
	public M62fbr49s0s00 add(final M62fbr49s0s00 m62fbr49s0s00) {
		final String query = "INSERT INTO M62R49T0S00 "
				+ "( TIPO_FICHERO, NOM_FICHERO, ESTADO, FEH_ULT_MODIF, FEC_ALTA, ERRORES ) "
				+ "VALUES (?,?,?,SYSDATE,TRUNC(SYSDATE),? )";
		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_M62R49T0" });
				ps.setString(
						1, /* TIPO_FICHERO */
						m62fbr49s0s00.getTIPO_FICHERO() != null ? m62fbr49s0s00
								.getTIPO_FICHERO() : null);

				ps.setString(
						2, /* NOM_FICHERO */
						m62fbr49s0s00.getNOM_FICHERO() != null ? m62fbr49s0s00
								.getNOM_FICHERO() : null);
				ps.setString(3, /* ESTADO */
				m62fbr49s0s00.getESTADO() != null ? m62fbr49s0s00.getESTADO()
						: null);
				/*
				 * ps.setObject( 4, // FEH_ULT_MODIF
				 * m62fbr49s0s00.getFEH_ULT_MODIF() != null ? m62fbr49s0s00
				 * .getFEH_ULT_MODIF() : null);
				 */
				/*
				 * ps.setObject( 5, // FEC_ALTA m62fbr49s0s00.getFEC_ALTA() !=
				 * null ? m62fbr49s0s00 .getFEC_ALTA() : null);
				 */
				ps.setString(4, /* ERRORES */
				m62fbr49s0s00.getERRORES() != null ? m62fbr49s0s00.getERRORES()
						: null);
				return ps;

			}

		}, keyHolder);
		// obtener ID_EXPEDIENTE generado por el trigger
		m62fbr49s0s00.setID_M62R49T0(new BigDecimal(keyHolder.getKeys()
				.get("ID_M62R49T0").toString()));

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

	/**
	 * Updates a single row in the M62fbr46s0s00 table.
	 * 
	 * @param m62fbr46s0s00
	 *            Pagination
	 * @return M62fbr46s0s00
	 */
	public M62fbr49s0s00 update(M62fbr49s0s00 m62fbr49s0s00) {
		return m62fbr49s0s00;
	}

	/**
	 * Updates a single row in the M62fbr46s0s00 table.
	 * 
	 * @param m62fbr46s0s00
	 *            Pagination
	 * @return M62fbr46s0s00
	 */
	public M62fbr49s0s00 updateEstado(M62fbr49s0s00 m62fbr49s0s00) {
		String query = "UPDATE M62R49T0S00 SET ESTADO=? , ERRORES=? , FEH_ULT_MODIF=SYSDATE WHERE ID_M62R49T0=?";
		this.jdbcTemplate.update(query, m62fbr49s0s00.getESTADO(),
				m62fbr49s0s00.getERRORES(), m62fbr49s0s00.getID_M62R49T0());
		return m62fbr49s0s00;
	}

	/**
	 * Finds a single row in the M62fbr46s0s00 table.
	 * 
	 * @param m62fbr46s0s00
	 *            Pagination
	 * @return M62fbr46s0s00
	 */
	@Transactional(readOnly = true, timeout = 500)
	public M62fbr49s0s00 find(M62fbr49s0s00 m62fbr49s0s00) {
		/*
		 * String query =
		 * "SELECT t1.ID_M62R46T0 IDM62R46T0, t1.ID_M62R45T0 IDM62R45T0, t1.NUM_VIVIENDA NUMVIVIENDA, t1.FEH_ULT_MODIF FEHULTMODIF, t1.COD_USU_ULT_MODIF CODUSUULTMODIF, t1.IDE_REGISTRO IDEREGISTRO, t1.FEC_ALTA FECALTA FROM M62R46T0S00 t1  WHERE t1.ID_M62R46T0 = ?   AND t1.ID_M62R45T0 = ?   AND t1.NUM_VIVIENDA = ?   AND t1.FEH_ULT_MODIF = ?   AND t1.COD_USU_ULT_MODIF = ?   AND t1.IDE_REGISTRO = ?   AND t1.FEC_ALTA = ?  "
		 * ;
		 * 
		 * List<M62fbr46s0s00> m62fbr46s0s00List =
		 * this.jdbcTemplate.query(query, this.rwMap,
		 * m62fbr46s0s00.getIdM62r46t0(), m62fbr46s0s00.getIdM62r45t0(),
		 * m62fbr46s0s00.getNumVivienda(), m62fbr46s0s00.getFehUltModif(),
		 * m62fbr46s0s00.getCodUsuUltModif(), m62fbr46s0s00.getIdeRegistro(),
		 * m62fbr46s0s00.getFecAlta()); return (M62fbr46s0s00)
		 * DataAccessUtils.uniqueResult(m62fbr46s0s00List);
		 */
		return m62fbr49s0s00;
	}

	/**
	 * Removes a single row in the M62fbr46s0s00 table.
	 * 
	 * @param m62fbr46s0s00
	 *            Pagination
	 * @return
	 */
	public void remove(M62fbr49s0s00 m62fbr49s0s00) {
		/*
		 * String query =
		 * "UPDATE M62R44T0S00 SET COD_SITUACION = 'D', FEC_INI_SITUACION = TRUNC(SYSDATE),  FEC_FIN_SITUACION = NULL, FEH_ULT_MODIF = SYSDATE WHERE NUM_VIVIENDA=? "
		 * ; this.jdbcTemplate.update(query, m62fbr46s0s00.getNumVivienda());
		 * 
		 * query =
		 * "DELETE FROM M62R46T0S00 WHERE ID_M62R45T0=? AND NUM_VIVIENDA=? ";
		 * this.jdbcTemplate.update(query, m62fbr46s0s00.getIdM62r45t0(),
		 * m62fbr46s0s00.getNumVivienda());
		 */

	}

	/**
	 * Finds a List of rows in the M62fbr46s0s00 table.
	 * 
	 * @param m62fbr46s0s00
	 *            M62fbr46s0s00
	 * @param pagination
	 *            Pagination
	 * @return List
	 */
	@Transactional(readOnly = true, timeout = 500)
	public List<M62fbr49s0s00> findAll(M62fbr49s0s00 m62fbr49s0s00,
			Pagination pagination) {

		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_M62R49T0 ID_M62R49T0,t1.TIPO_FICHERO TIPO_FICHERO,t1.NOM_FICHERO NOM_FICHERO,t1.ESTADO ESTADO,t1.FEH_ULT_MODIF FEH_ULT_MODIF,t1.FEC_ALTA FEC_ALTA,t1.ERRORES ERRORES ");
		query.append("FROM M62R49T0S00 t1 ");

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

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

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

		return (List<M62fbr49s0s00>) this.jdbcTemplate.query(query.toString(),
				this.rwMap, null);

	}

	/**
	 * Counts rows in the M62fbr46s0s00 table.
	 * 
	 * @param m62fbr46s0s00
	 *            M62fbr46s0s00
	 * @return Long
	 */
	@Transactional(readOnly = true, timeout = 500)
	public Long findAllCount(M62fbr49s0s00 m62fbr49s0s00) {

		StringBuilder query = new StringBuilder(
				"SELECT COUNT(1) FROM M62R49T0S00 t1 ");

		// Where clause & Params Map<String, ?> mapaWhere =
		/*
		 * this.getWhereMap(m62fbr46s0s00); StringBuilder where = new
		 * StringBuilder(" WHERE 1=1 "); where.append(mapaWhere.get("query"));
		 * query.append(where);
		 * 
		 * List<?> params = (List<?>) mapaWhere.get("params");
		 */

		return this.jdbcTemplate.queryForLong(query.toString(), null);

	}

	/**
	 * Finds rows in the M62fbr46s0s00 table using like.
	 * 
	 * @param m62fbr46s0s00
	 *            M62fbr46s0s00
	 * @param pagination
	 *            Pagination
	 * @param startsWith
	 *            Boolean
	 * @return List
	 */
	@Transactional(readOnly = true, timeout = 500)
	public List<M62fbr49s0s00> findAllLike(M62fbr49s0s00 m62fbr49s0s00,
			Pagination pagination, Boolean startsWith) {
		/*
		 * StringBuilder query = new StringBuilder(
		 * "SELECT  t1.ID_M62R46T0 IDM62R46T0,t1.ID_M62R45T0 IDM62R45T0,t1.NUM_VIVIENDA NUMVIVIENDA,t1.FEH_ULT_MODIF FEHULTMODIF,t1.COD_USU_ULT_MODIF CODUSUULTMODIF,t1.IDE_REGISTRO IDEREGISTRO,t1.FEC_ALTA FECALTA "
		 * ); query.append("FROM M62R46T0S00 t1 ");
		 * 
		 * // Where clause & Params Map<String, ?> mapaWhere =
		 * this.getWhereLikeMap(m62fbr46s0s00, startsWith); StringBuilder where
		 * = new StringBuilder(" WHERE 1=1 ");
		 * where.append(mapaWhere.get("query")); query.append(where);
		 * 
		 * List<?> params = (List<?>) mapaWhere.get("params");
		 * 
		 * if (pagination != null) { query =
		 * pagination.getPaginationQuery(query); }
		 * 
		 * return (List<M62fbr46s0s00>)
		 * this.jdbcTemplate.query(query.toString(), this.rwMap,
		 * params.toArray());
		 */
		return null;
	}

	/**
	 * Counts rows in the M62fbr46s0s00 table using like.
	 * 
	 * @param m62fbr46s0s00
	 *            M62fbr46s0s00
	 * @param startsWith
	 *            Boolean
	 * @return Long
	 */
	@Transactional(readOnly = true, timeout = 500)
	public Long findAllLikeCount(M62fbr49s0s00 m62fbr49s0s00, Boolean startsWith) {
		/*
		 * StringBuilder query = new StringBuilder(
		 * "SELECT COUNT(1) FROM M62R46T0S00 t1 ");
		 * 
		 * // Where clause & Params Map<String, ?> mapaWhere =
		 * this.getWhereLikeMap(m62fbr46s0s00, 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.queryForLong(query.toString(),
		 * params.toArray());
		 */
		return null;
	}

	/**
	 * Returns a map with the needed value to create the conditions to filter by
	 * the M62fbr46s0s00 entity
	 * 
	 * @param m62fbr46s0s00
	 *            M62fbr46s0s00 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(M62fbr46s0s00 m62fbr46s0s00) {
	 * 
	 * StringBuffer where = new StringBuffer(
	 * M62fbr49s0s00DaoImpl.STRING_BUILDER_INIT); List<Object> params = new
	 * ArrayList<Object>();
	 * 
	 * if (m62fbr46s0s00 != null && m62fbr46s0s00.getIdM62r46t0() != null) {
	 * where.append(" AND t1.ID_M62R46T0 = ?");
	 * params.add(m62fbr46s0s00.getIdM62r46t0()); } if (m62fbr46s0s00 != null &&
	 * m62fbr46s0s00.getIdM62r45t0() != null) {
	 * where.append(" AND t1.ID_M62R45T0 = ?");
	 * params.add(m62fbr46s0s00.getIdM62r45t0()); } if (m62fbr46s0s00 != null &&
	 * m62fbr46s0s00.getNumVivienda() != null) {
	 * where.append(" AND t1.NUM_VIVIENDA = ?");
	 * params.add(m62fbr46s0s00.getNumVivienda()); } if (m62fbr46s0s00 != null
	 * && m62fbr46s0s00.getFehUltModif() != null) {
	 * where.append(" AND t1.FEH_ULT_MODIF = ?");
	 * params.add(m62fbr46s0s00.getFehUltModif()); } if (m62fbr46s0s00 != null
	 * && m62fbr46s0s00.getCodUsuUltModif() != null) {
	 * where.append(" AND t1.COD_USU_ULT_MODIF = ?");
	 * params.add(m62fbr46s0s00.getCodUsuUltModif()); } if (m62fbr46s0s00 !=
	 * null && m62fbr46s0s00.getIdeRegistro() != null) {
	 * where.append(" AND t1.IDE_REGISTRO = ?");
	 * params.add(m62fbr46s0s00.getIdeRegistro()); } if (m62fbr46s0s00 != null
	 * && m62fbr46s0s00.getFecAlta() != null) {
	 * where.append(" AND t1.FEC_ALTA = ?");
	 * params.add(m62fbr46s0s00.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 M62fbr46s0s00 entity
	 * 
	 * @param m62fbr46s0s00
	 *            M62fbr46s0s00 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(M62fbr46s0s00 m62fbr46s0s00,
	 * Boolean startsWith) {
	 * 
	 * StringBuffer where = new StringBuffer(
	 * M62fbr48s0s00DaoImpl.STRING_BUILDER_INIT); List<Object> params = new
	 * ArrayList<Object>();
	 * 
	 * if (m62fbr46s0s00 != null && m62fbr46s0s00.getIdM62r46t0() != null) {
	 * where.append(" AND t1.ID_M62R46T0 = ?"); } if (m62fbr46s0s00 != null &&
	 * m62fbr46s0s00.getIdM62r45t0() != null) {
	 * where.append(" AND t1.ID_M62R45T0 = ?"); } if (m62fbr46s0s00 != null &&
	 * m62fbr46s0s00.getNumVivienda() != null) {
	 * where.append(" AND t1.NUM_VIVIENDA = ?"); } if (m62fbr46s0s00 != null &&
	 * m62fbr46s0s00.getFehUltModif() != null) {
	 * where.append(" AND t1.FEH_ULT_MODIF = ?"); } if (m62fbr46s0s00 != null &&
	 * m62fbr46s0s00.getCodUsuUltModif() != null) {
	 * where.append(" AND UPPER(t1.COD_USU_ULT_MODIF) like ? ESCAPE  '\\'"); if
	 * (startsWith) { params.add(m62fbr46s0s00.getCodUsuUltModif().toUpperCase()
	 * + "%"); } else { params.add("%" +
	 * m62fbr46s0s00.getCodUsuUltModif().toUpperCase() + "%"); }
	 * where.append(" AND t1.COD_USU_ULT_MODIF IS NOT NULL"); } if
	 * (m62fbr46s0s00 != null && m62fbr46s0s00.getIdeRegistro() != null) {
	 * where.append(" AND t1.IDE_REGISTRO = ?"); } if (m62fbr46s0s00 != null &&
	 * m62fbr46s0s00.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; }
	 */

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

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