package com.ejie.m62fb.dao;

import com.ejie.x38.dto.Pagination;
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.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.m62fb.i18n.I18nString;
import com.ejie.m62fb.model.M62fbSituacionOfe;

/**
 * M62fbSituacionOfeDaoImpl generated by UDA, 26-oct-2012 12:29:43.
 * 
XXXX
 */

@Repository
@Transactional
public class M62fbSituacionOfeDaoImpl implements M62fbSituacionOfeDao {
	private JdbcTemplate jdbcTemplate;
	private RowMapper<M62fbSituacionOfe> rwMap = new RowMapper<M62fbSituacionOfe>() {
		public M62fbSituacionOfe mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
			// Valor por defecto???
			HashMap<String, String> mp = new HashMap<String, String>();
			mp.put("es", resultSet.getString("DESTABLACAS"));
			mp.put("eu", resultSet.getString("DESTABLAEUS"));

			return new M62fbSituacionOfe(resultSet.getString("CODTABLA"),
					new I18nString(resultSet.getString("DESTABLACAS"), mp));
		}
	};

	/**
	 * 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 M62fbSituacionOfe table.
	 * 
	 * @param m62fbsituacionofe
	 *            Pagination
	 * @return M62fbSituacionOfe
	 */
	public M62fbSituacionOfe add(M62fbSituacionOfe m62fbsituacionofe) {
		String query = "INSERT INTO M62P28V0S00_SITUACION_OFE (COD_TABLA, DES_TABLA_CAS) VALUES (?,?)";
		this.jdbcTemplate.update(query, m62fbsituacionofe.getCodTabla(),
				m62fbsituacionofe.getDesTabla());
		return m62fbsituacionofe;
	}

	/**
	 * Updates a single row in the M62fbSituacionOfe table.
	 * 
	 * @param m62fbsituacionofe
	 *            Pagination
	 * @return M62fbSituacionOfe
	 */
	public M62fbSituacionOfe update(M62fbSituacionOfe m62fbsituacionofe) {
		String query = "UPDATE M62P28V0S00_SITUACION_OFE SET  WHERE COD_TABLA=? AND DES_TABLA_CAS=?";
		this.jdbcTemplate.update(query, m62fbsituacionofe.getCodTabla(),
				m62fbsituacionofe.getDesTabla());
		return m62fbsituacionofe;
	}

	/**
	 * Finds a single row in the M62fbSituacionOfe table.
	 * 
	 * @param m62fbsituacionofe
	 *            Pagination
	 * @return M62fbSituacionOfe
	 */
	@Transactional(readOnly = true, timeout = 500)
	public M62fbSituacionOfe find(M62fbSituacionOfe m62fbsituacionofe) {
		String query = "SELECT t1.COD_TABLA CODTABLA, t1.DES_TABLA_CAS DESTABLACAS, t1.DES_TABLA_EUS DESTABLAEUS FROM M62P28V0S00_SITUACION_OFE t1  WHERE t1.COD_TABLA = ?   AND t1.DES_TABLA_CAS = ?  ";

		List<M62fbSituacionOfe> m62fbsituacionofeList = this.jdbcTemplate
				.query(query, this.rwMap, m62fbsituacionofe.getCodTabla(),
						m62fbsituacionofe.getDesTabla());
		return (M62fbSituacionOfe) DataAccessUtils
				.uniqueResult(m62fbsituacionofeList);
	}

	/**
	 * Removes a single row in the M62fbSituacionOfe table.
	 * 
	 * @param m62fbsituacionofe
	 *            Pagination
	 * @return
	 */
	public void remove(M62fbSituacionOfe m62fbsituacionofe) {
		String query = "DELETE FROM M62P28V0S00_SITUACION_OFE WHERE COD_TABLA=? AND DES_TABLA_CAS=?";
		this.jdbcTemplate.update(query, m62fbsituacionofe.getCodTabla(),
				m62fbsituacionofe.getDesTabla());
	}

	/**
	 * Finds a List of rows in the M62fbSituacionOfe table.
	 * 
	 * @param m62fbsituacionofe
	 *            M62fbSituacionOfe
	 * @param pagination
	 *            Pagination
	 * @return List
	 */
	@Transactional(readOnly = true, timeout = 500)
	public List<M62fbSituacionOfe> findAll(M62fbSituacionOfe m62fbsituacionofe,
			Pagination pagination) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.COD_TABLA CODTABLA,t1.DES_TABLA_CAS DESTABLACAS, t1.DES_TABLA_EUS DESTABLAEUS ");
		query.append("FROM M62P28V0S00_SITUACION_OFE t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(m62fbsituacionofe);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);
		query.append(" ORDER BY t1.DES_TABLA_CAS ASC");

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

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

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

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

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

	/**
	 * Finds rows in the M62fbSituacionOfe table using like.
	 * 
	 * @param m62fbsituacionofe
	 *            M62fbSituacionOfe
	 * @param pagination
	 *            Pagination
	 * @param startsWith
	 *            Boolean
	 * @return List
	 */
	@Transactional(readOnly = true, timeout = 500)
	public List<M62fbSituacionOfe> findAllLike(
			M62fbSituacionOfe m62fbsituacionofe, Pagination pagination,
			Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.COD_TABLA CODTABLA,t1.DES_TABLA_CAS DESTABLACAS, t1.DES_TABLA_EUS DESTABLAEUS ");
		query.append("FROM M62P28V0S00_SITUACION_OFE t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(m62fbsituacionofe,
				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<M62fbSituacionOfe>) this.jdbcTemplate.query(
				query.toString(), this.rwMap, params.toArray());
	}

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

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

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

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

		if (m62fbsituacionofe != null
				&& m62fbsituacionofe.getCodTabla() != null) {
			where.append(" AND t1.COD_TABLA = ?");
			params.add(m62fbsituacionofe.getCodTabla());
		}
		if (m62fbsituacionofe != null
				&& m62fbsituacionofe.getDesTabla() != null) {
			where.append(" AND t1.DES_TABLA_CAS = ?");
			params.add(m62fbsituacionofe.getDesTabla());
		}

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

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

		if (m62fbsituacionofe != null
				&& m62fbsituacionofe.getCodTabla() != null) {
			where.append(" AND UPPER(t1.COD_TABLA) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(m62fbsituacionofe.getCodTabla().toUpperCase() + "%");
			} else {
				params.add("%" + m62fbsituacionofe.getCodTabla().toUpperCase()
						+ "%");
			}
			where.append(" AND t1.COD_TABLA IS NOT NULL");
		}
		/*
		 * if (m62fbsituacionofe != null && m62fbsituacionofe.getDesTablaCas()
		 * != null) {
		 * where.append(" AND UPPER(t1.DES_TABLA_CAS) like ? ESCAPE  '\\'"); if
		 * (startsWith) {
		 * params.add(m62fbsituacionofe.getDesTabla().toUpperCase() + "%"); }
		 * else { params.add("%" + m62fbsituacionofe.getDesTabla().toUpperCase()
		 * + "%"); } where.append(" AND t1.DES_TABLA_CAS 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;
}
