package com.ejie.ab63.dao.plantillas;

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.ab63.model.plantillas.RelPlaCam;
import com.ejie.x38.dao.RowNumResultSetExtractor;
import com.ejie.x38.dto.JQGridManager;
import com.ejie.x38.dto.JQGridManagerJerarquia;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * RelPlaCamDaoImpl  
 * 
 *  
 */

@Repository
@Transactional
public class RelPlaCamDaoImpl implements RelPlaCamDao {

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDRELPLACAM", "IDPLANTILLA", "IDCAMPO",
			"FECHAREGISTRO", "USUARIOREGISTRO" };

	private JdbcTemplate jdbcTemplate;

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<RelPlaCam> rwMap = new RowMapper<RelPlaCam>() {
		public RelPlaCam mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new RelPlaCam(resultSet.getInt("IDRELPLACAM"), resultSet.getInt("IDPLANTILLA"),
					resultSet.getInt("IDCAMPO"), resultSet.getDate("FECHAREGISTRO"),
					resultSet.getString("USUARIOREGISTRO"));
		}
	};

	private RowMapper<RelPlaCam> rwMapPK = new RowMapper<RelPlaCam>() {
		public RelPlaCam mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new RelPlaCam(resultSet.getInt("IDRELPLACAM"));
		}
	};

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

			RelPlaCam relplacam = new RelPlaCam(resultSet.getInt("IDRELPLACAM"), resultSet.getInt("IDPLANTILLA"),
					resultSet.getInt("IDCAMPO"), resultSet.getDate("FECHAREGISTRO"),
					resultSet.getString("USUARIOREGISTRO"));

			JerarquiaDto<RelPlaCam> jerarquia = new JerarquiaDto<RelPlaCam>();
			jerarquia.setModel(relplacam);
			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 RelPlaCam table.
	 *
	 * @param relplacam
	 *            RelPlaCam
	 * @return RelPlaCam
	 */
	public RelPlaCam add(RelPlaCam relplacam) {
		String query = "INSERT INTO REL_PLA_CAM (ID_REL_PLA_CAM, ID_PLANTILLA, ID_CAMPO, FECHA_REGISTRO, USUARIO_REGISTRO) VALUES (?,?,?,?,?)";
		this.jdbcTemplate.update(query, relplacam.getIdRelPlaCam(), relplacam.getIdPlantilla(), relplacam.getIdCampo(),
				relplacam.getFechaRegistro(), relplacam.getUsuarioRegistro());
		return relplacam;
	}

	/**
	 * Updates a single row in the RelPlaCam table.
	 *
	 * @param relplacam
	 *            RelPlaCam
	 * @return RelPlaCam
	 */
	public RelPlaCam update(RelPlaCam relplacam) {
		String query = "UPDATE REL_PLA_CAM SET ID_PLANTILLA=?, ID_CAMPO=?, FECHA_REGISTRO=?, USUARIO_REGISTRO=? WHERE ID_REL_PLA_CAM=?";
		this.jdbcTemplate.update(query, relplacam.getIdPlantilla(), relplacam.getIdCampo(),
				relplacam.getFechaRegistro(), relplacam.getUsuarioRegistro(), relplacam.getIdRelPlaCam());
		return relplacam;
	}

	/**
	 * Finds a single row in the RelPlaCam table.
	 *
	 * @param relplacam
	 *            RelPlaCam
	 * @return RelPlaCam
	 */
	@Transactional(readOnly = true)
	public RelPlaCam find(RelPlaCam relplacam) {
		String query = "SELECT t1.ID_REL_PLA_CAM IDRELPLACAM, t1.ID_PLANTILLA IDPLANTILLA, t1.ID_CAMPO IDCAMPO, t1.FECHA_REGISTRO FECHAREGISTRO, t1.USUARIO_REGISTRO USUARIOREGISTRO FROM REL_PLA_CAM t1  WHERE t1.ID_REL_PLA_CAM = ?  ";

		List<RelPlaCam> relplacamList = this.jdbcTemplate.query(query, this.rwMap, relplacam.getIdRelPlaCam());
		return (RelPlaCam) DataAccessUtils.uniqueResult(relplacamList);
	}

	/**
	 * Removes a single row in the RelPlaCam table.
	 *
	 * @param relplacam
	 *            RelPlaCam
	 * @return
	 */
	public void remove(RelPlaCam relplacam) {
		String query = "DELETE FROM REL_PLA_CAM WHERE ID_REL_PLA_CAM=?";
		this.jdbcTemplate.update(query, relplacam.getIdRelPlaCam());
	}

	/**
	 * Finds a list of rows in the RelPlaCam table.
	 *
	 * @param relplacam
	 *            RelPlaCam
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<RelPlaCam>
	 */
	@Transactional(readOnly = true)
	public List<RelPlaCam> findAll(RelPlaCam relplacam, JQGridRequestDto jqGridRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_REL_PLA_CAM IDRELPLACAM,t1.ID_PLANTILLA IDPLANTILLA,t1.ID_CAMPO IDCAMPO,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		query.append("FROM REL_PLA_CAM t1 ");

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

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

		if (jqGridRequestDto != null) {
			query = JQGridManager.getPaginationQuery(jqGridRequestDto, query, RelPlaCamDaoImpl.ORDER_BY_WHITE_LIST);
		}

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

	/**
	 * Finds rows in the RelPlaCam table using like.
	 *
	 * @param relplacam
	 *            RelPlaCam
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<RelPlaCam>
	 */
	@Transactional(readOnly = true)
	public List<RelPlaCam> findAllLike(RelPlaCam relplacam, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_REL_PLA_CAM IDRELPLACAM,t1.ID_PLANTILLA IDPLANTILLA,t1.ID_CAMPO IDCAMPO,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		query.append("FROM REL_PLA_CAM t1 ");

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

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

		if (jqGridRequestDto != null) {
			query = JQGridManager.getPaginationQuery(jqGridRequestDto, query, RelPlaCamDaoImpl.ORDER_BY_WHITE_LIST);
		}

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

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

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

	/**
	 * Reorder the data list of RelPlaCam selected for rup_table
	 *
	 * @param relplacam
	 *            RelPlaCam
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelPlaCam>>
	 */
	@Override
	public List<TableRowDto<RelPlaCam>> reorderSelection(RelPlaCam relplacam, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_PLA_CAM IDRELPLACAM,t1.ID_PLANTILLA IDPLANTILLA,t1.ID_CAMPO IDCAMPO,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		query.append("FROM REL_PLA_CAM t1 ");
		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(relplacam, 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 = JQGridManager.getReorderQuery(query, jqGridRequestDto, RelPlaCam.class,
				filterParamList, "ID_REL_PLA_CAM");

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

	/**
	 * Search method for rup_table
	 *
	 * @param filterParams
	 *            RelPlaCam
	 * @param searchParams
	 *            RelPlaCam
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelPlaCam>>
	 */
	@Override
	public List<TableRowDto<RelPlaCam>> search(RelPlaCam filterParams, RelPlaCam searchParams,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_PLA_CAM IDRELPLACAM,t1.ID_PLANTILLA IDPLANTILLA,t1.ID_CAMPO IDCAMPO,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		query.append("FROM REL_PLA_CAM 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 = JQGridManager.getSearchQuery(query, jqGridRequestDto, RelPlaCam.class,
				filterParamList, searchSQL, searchParamList, from_alias, "ID_REL_PLA_CAM");

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

	/**
	 * Remove multiple method for rup_table
	 *
	 * @param filterrelplacam
	 *            RelPlaCam
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	@Override
	public void removeMultiple(RelPlaCam filterrelplacam, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		// SELECT
		/** TODO: select por clave */
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_PLA_CAM IDRELPLACAM,t1.ID_PLANTILLA IDPLANTILLA,t1.ID_CAMPO IDCAMPO,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		query.append("FROM REL_PLA_CAM t1 ");

		// WHERE
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterrelplacam, startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		@SuppressWarnings("unchecked")
		List<Object> params = (List<Object>) mapaWhere.get("params");

		StringBuilder sbRemoveMultipleSQL = JQGridManager.getRemoveMultipleQuery(jqGridRequestDto, RelPlaCam.class,
				query, params, "ID_REL_PLA_CAM");
		this.jdbcTemplate.update(sbRemoveMultipleSQL.toString(), params.toArray());
	}

	/**
	 * Filter in the hierarchical RelPlaCam table.
	 *
	 * @param filterRelPlaCam
	 *            RelPlaCam
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<JerarquiaDto<RelPlaCam>>
	 */
	@Override
	public List<JerarquiaDto<RelPlaCam>> findAllLikeJerarquia(RelPlaCam filterRelPlaCam,
			JQGridRequestDto jqGridRequestDto) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_PLA_CAM IDRELPLACAM,t1.ID_PLANTILLA IDPLANTILLA,t1.ID_CAMPO IDCAMPO,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		// StringBuilder from = new StringBuilder("FROM REL_PLA_CAM t1 ");
		List<String> from = new ArrayList<String>();
		from.add("REL_PLA_CAM");
		// 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(filterRelPlaCam, false);

		// JERARQUIA
		// Especificar la columna padre correspondiente en lugar de la generada
		// por defecto (ID_PADRE)
		query = JQGridManagerJerarquia.getQuery(jqGridRequestDto, query, mapaWhere, "ID_REL_PLA_CAM", "ID_PADRE",
				"NOMBRE", from, from_alias);

		/*
		 * Ejemplo de llamada en caso de indicar joins entre tablas y
		 * condiciones de negocio
		 */
		// query = JQGridManagerJerarquia.getQuery(jqGridRequestDto, query,
		// mapaWhere, "ID", "ID_PADRE", "NOMBRE", from, from_alias, joins,
		// businessFilters, businessParams);

		// PAGINACION
		if (jqGridRequestDto != null) {
			query = JQGridManagerJerarquia.getPaginationQuery(jqGridRequestDto, query,
					RelPlaCamDaoImpl.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 filterRelPlaCam
	 *            RelPlaCam
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return Long
	 */
	@Override
	public Long findAllLikeCountJerarquia(RelPlaCam filterRelPlaCam, JQGridRequestDto jqGridRequestDto) {
		// FROM
		// StringBuilder from = new StringBuilder("FROM REL_PLA_CAM t1 ");
		List<String> from = new ArrayList<String>();
		from.add("REL_PLA_CAM");

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

		// JERARQUIA
		// Especificar la columna padre correspondiente en lugar de la generada
		// por defecto (ID_PADRE)
		StringBuilder query = JQGridManagerJerarquia.getQueryCount(jqGridRequestDto, mapaWhere, "ID_REL_PLA_CAM",
				"ID_PADRE", from, from_alias);

		/*
		 * Ejemplo de llamada en caso de indicar joins entre tablas y
		 * condiciones de negocio
		 */
		// StringBuilder query =
		// JQGridManagerJerarquia.getQueryCount(jqGridRequestDto, mapaWhere,
		// "ID", "ID_PADRE", from, from_alias, joins, businessFilters,
		// businessParams);

		List<?> params = (List<?>) mapaWhere.get("params");
		return this.jdbcTemplate.queryForLong(query.toString(), params.toArray());
	}

	/**
	 * Find dependent hierarchical rows
	 *
	 * @param filterRelPlaCam
	 *            RelPlaCam
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<TableRowDto<RelPlaCam>>
	 */
	@Override
	public List<TableRowDto<RelPlaCam>> findAllChild(RelPlaCam filterRelPlaCam, JQGridRequestDto jqGridRequestDto) {

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

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

		// MULTISELECCION
		// Especificar la columna padre correspondiente en lugar de la generada
		// por defecto (ID_PADRE)
		StringBuilder query = JQGridManagerJerarquia.getQueryChildren(jqGridRequestDto, mapaWhere, "ID_REL_PLA_CAM",
				"ID_PADRE", from, from_alias);

		/*
		 * Ejemplo de llamada en caso de indicar joins entre tablas y
		 * condiciones de negocio
		 */
		// StringBuilder query =
		// JQGridManagerJerarquia.getQueryChildren(jqGridRequestDto, mapaWhere,
		// "ID", "ID_PADRE", from, from_alias, joins, businessFilters,
		// businessParams);

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

		return this.jdbcTemplate.query(query.toString(),
				new RowNumResultSetExtractor<RelPlaCam>(this.rwMapPK, "ID_REL_PLA_CAM"), params.toArray());
	}

	/*
	 * MÉTODOS PRIVADOS
	 */

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

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

		if (relplacam != null && relplacam.getIdRelPlaCam() != null) {
			where.append(" AND t1.ID_REL_PLA_CAM = ?");
			params.add(relplacam.getIdRelPlaCam());
		}
		if (relplacam != null && relplacam.getIdPlantilla() != null) {
			where.append(" AND t1.ID_PLANTILLA = ?");
			params.add(relplacam.getIdPlantilla());
		}
		if (relplacam != null && relplacam.getIdCampo() != null) {
			where.append(" AND t1.ID_CAMPO = ?");
			params.add(relplacam.getIdCampo());
		}
		if (relplacam != null && relplacam.getFechaRegistro() != null) {
			where.append(" AND t1.FECHA_REGISTRO = ?");
			params.add(relplacam.getFechaRegistro());
		}
		if (relplacam != null && relplacam.getUsuarioRegistro() != null) {
			where.append(" AND t1.USUARIO_REGISTRO = ?");
			params.add(relplacam.getUsuarioRegistro());
		}

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

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

		if (relplacam != null && relplacam.getIdRelPlaCam() != null) {
			where.append(" AND t1.ID_REL_PLA_CAM = ?");
			params.add(relplacam.getIdRelPlaCam());
		}
		if (relplacam != null && relplacam.getIdPlantilla() != null) {
			where.append(" AND t1.ID_PLANTILLA = ?");
			params.add(relplacam.getIdPlantilla());
		}
		if (relplacam != null && relplacam.getIdCampo() != null) {
			where.append(" AND t1.ID_CAMPO = ?");
			params.add(relplacam.getIdCampo());
		}
		if (relplacam != null && relplacam.getFechaRegistro() != null) {
			where.append(" AND t1.FECHA_REGISTRO = ?");
			params.add(relplacam.getFechaRegistro());
		}
		if (relplacam != null && relplacam.getUsuarioRegistro() != null) {
			where.append(" AND UPPER(t1.USUARIO_REGISTRO) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(relplacam.getUsuarioRegistro().toUpperCase() + "%");
			} else {
				params.add("%" + relplacam.getUsuarioRegistro().toUpperCase() + "%");
			}
			where.append(" AND t1.USUARIO_REGISTRO 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;
}
