package com.ejie.ab39a.dao;

import com.ejie.x38.dao.RowNumResultSetExtractor;
import com.ejie.x38.dto.JQGridManager;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.TableRowDto;
import com.gfi.constants.Constants;
import com.gfi.utils.UtilSQL;
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.ab39a.model.ElementosPe;

/**
 * ElementosPeDaoImpl.
 * 
 *  
 */

@Repository
@Transactional
public class ElementosPeDaoImpl implements ElementosPeDao {

	private static final String[] ORDER_BY_WHITE_LIST = new String[] { "CODELEMENTOPE", "CODELEMENTOPEPADRE",
			"CODPLANE", "ORDEN", "NIVEL", "NOMBRE", "NOMBRECAS", "IDELEMENTO", "HELBURU", "HAS_EUS", "HAS_ELEBI",
			"AURRE_EUS", "AURRE_ELEBI", "AMAI_EUS", "AMAI_ELEBI", "OHARRAK" };

	private JdbcTemplate jdbcTemplate;

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<ElementosPe> rwMap = new RowMapper<ElementosPe>() {
		@Override
		public ElementosPe mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new ElementosPe(resultSet.getInt("CODELEMENTOPE"), resultSet.getInt("CODPLANE"),
					resultSet.getLong("ORDEN"), resultSet.getLong("NIVEL"), resultSet.getString("NOMBRE"),
					resultSet.getString("NOMBRECAS"), resultSet.getLong("IDELEMENTO"), resultSet.getString("HELBURU"),
					resultSet.getBigDecimal("HASEUS"), resultSet.getBigDecimal("HASELEBI"),
					resultSet.getBigDecimal("AURREEUS"), resultSet.getBigDecimal("AURREELEBI"),
					resultSet.getBigDecimal("AMAIEUS"), resultSet.getBigDecimal("AMAIELEBI"),
					resultSet.getString("OHARRAK"),
					new ElementosPe(resultSet.getInt("ElementosPeCODELEMENTOPE"),
							resultSet.getInt("ElementosPeCODPLANE"), resultSet.getLong("ElementosPeORDEN"),
							resultSet.getLong("ElementosPeNIVEL"), resultSet.getString("ElementosPeNOMBRE"),
							resultSet.getString("ElementosPeNOMBRECAS"), resultSet.getLong("ElementosPeIDELEMENTO"),
							resultSet.getString("ElementosPeHELBURU"), resultSet.getBigDecimal("ElementosPeHASEUS"),
							resultSet.getBigDecimal("ElementosPeHASELEBI"),
							resultSet.getBigDecimal("ElementosPeAURREEUS"),
							resultSet.getBigDecimal("ElementosPeAURREELEBI"),
							resultSet.getBigDecimal("ElementosPeAMAIEUS"),
							resultSet.getBigDecimal("ElementosPeAMAIELEBI"), resultSet.getString("ElementosPeOHARRAK"),
							new ElementosPe(resultSet.getInt("ElementosPeCODELEMENTOPEPADRE"), null, null, null, null,
									null, null, null, null, null, null, null, null, null, null, null, null, null)));
		}
	};

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

	/**
	 * 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 ElementosPe table.
	 *
	 * @param elementospe
	 *            ElementosPe elementospe
	 * @return ElementosPe ElementosPe
	 */
	@Override
	public ElementosPe add(ElementosPe elementospe) {
		StringBuilder query = new StringBuilder(
				"INSERT INTO ELEMENTOS_PE (CODELEMENTOPE, CODELEMENTOPEPADRE, CODPLANE, ORDEN, NIVEL, NOMBRE, NOMBRECAS, IDELEMENTO, HELBURU, HAS_EUS, HAS_ELEBI, AURRE_EUS, AURRE_ELEBI, AMAI_EUS, AMAI_ELEBI, OHARRAK) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		Object getElementosPeCodelementopeAux = null;
		if (elementospe.getElementosPe() != null && elementospe.getElementosPe().getCodelementope() != null) {
			getElementosPeCodelementopeAux = elementospe.getElementosPe().getCodelementope();
		}
		this.jdbcTemplate.update(query.toString(), elementospe.getCodelementope(), getElementosPeCodelementopeAux,
				elementospe.getCodplane(), elementospe.getOrden(), elementospe.getNivel(), elementospe.getNombre(),
				elementospe.getNombrecas(), elementospe.getIdelemento(), elementospe.getHelburu(),
				elementospe.getHasEus(), elementospe.getHasElebi(), elementospe.getAurreEus(),
				elementospe.getAurreElebi(), elementospe.getAmaiEus(), elementospe.getAmaiElebi(),
				elementospe.getOharrak());
		return elementospe;
	}

	/**
	 * Updates a single row in the ElementosPe table.
	 *
	 * @param elementospe
	 *            ElementosPe elementospe
	 * @return ElementosPe ElementosPe
	 */
	@Override
	public ElementosPe update(ElementosPe elementospe) {
		StringBuilder query = new StringBuilder(
				"UPDATE ELEMENTOS_PE SET CODELEMENTOPEPADRE=?, CODPLANE=?, ORDEN=?, NIVEL=?, NOMBRE=?, NOMBRECAS=?, IDELEMENTO=?, HELBURU=?, HAS_EUS=?, HAS_ELEBI=?, AURRE_EUS=?, AURRE_ELEBI=?, AMAI_EUS=?, AMAI_ELEBI=?, OHARRAK=?");
		query.append(" WHERE CODELEMENTOPE=?");
		Object getElementosPeCodelementopeAux = null;
		if (elementospe.getElementosPe() != null && elementospe.getElementosPe().getCodelementope() != null) {
			getElementosPeCodelementopeAux = elementospe.getElementosPe().getCodelementope();
		}
		this.jdbcTemplate.update(query.toString(), getElementosPeCodelementopeAux, elementospe.getCodplane(),
				elementospe.getOrden(), elementospe.getNivel(), elementospe.getNombre(), elementospe.getNombrecas(),
				elementospe.getIdelemento(), elementospe.getHelburu(), elementospe.getHasEus(),
				elementospe.getHasElebi(), elementospe.getAurreEus(), elementospe.getAurreElebi(),
				elementospe.getAmaiEus(), elementospe.getAmaiElebi(), elementospe.getOharrak(),
				elementospe.getCodelementope());
		return elementospe;
	}

	/**
	 * Finds a single row in the ElementosPe table.
	 *
	 * @param elementospe
	 *            ElementosPe elementospe
	 * @return ElementosPe ElementosPe
	 */
	@Override
	@Transactional(readOnly = true)
	public ElementosPe find(ElementosPe elementospe) {
		// SELECT - FROM
		StringBuilder query = new StringBuilder(this.getSelectFromQuery(true));
		// WHERE
		query.append(" WHERE t1.CODELEMENTOPE = ?   AND t1.CODELEMENTOPEPADRE= t2.CODELEMENTOPE(+)");

		List<ElementosPe> elementospeList = this.jdbcTemplate.query(query.toString(), this.rwMap,
				elementospe.getCodelementope());
		return DataAccessUtils.uniqueResult(elementospeList);
	}

	/**
	 * Removes a single row in the ElementosPe table.
	 *
	 * @param elementospe
	 *            ElementosPe elementospe
	 */
	@Override
	public void remove(ElementosPe elementospe) {
		String query = "DELETE FROM ELEMENTOS_PE WHERE CODELEMENTOPE=?";
		this.jdbcTemplate.update(query, elementospe.getCodelementope());
	}

	/**
	 * Finds a list of rows in the ElementosPe table.
	 * 
	 * @param elementosPe
	 *            ElementosPe elementosPe
	 * @param jqGridRequestDto
	 *            JQGridRequestDto jqGridRequestDto
	 * @return List<ElementosPe> lista
	 */
	@Override
	@Transactional(readOnly = true)
	public List<ElementosPe> findAll(ElementosPe elementosPe, JQGridRequestDto jqGridRequestDto) {
		// SELECT - FROM
		StringBuilder query = new StringBuilder(this.getSelectFromQuery(true));

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

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

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

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

	/**
	 * Finds rows in the ElementosPe table using like.
	 * 
	 * @param elementosPe
	 *            ElementosPe elementosPe
	 * @param jqGridRequestDto
	 *            JQGridRequestDto jqGridRequestDto
	 * @param startsWith
	 *            Boolean startsWith
	 * @return List<ElementosPe> lista
	 */
	@Override
	@Transactional(readOnly = true)
	public List<ElementosPe> findAllLike(ElementosPe elementosPe, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		// SELECT - FROM
		StringBuilder query = new StringBuilder(this.getSelectFromQuery(true));

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

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

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Counts rows in the ElementosPe table.
	 * 
	 * @param elementosPe
	 *            ElementosPe elementosPe
	 * @return Long Long
	 */
	@Override
	@Transactional(readOnly = true)
	public Long findAllCount(ElementosPe elementosPe) {
		StringBuilder query = new StringBuilder("SELECT COUNT(1) FROM ELEMENTOS_PE t1 , ELEMENTOS_PE t2 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(elementosPe);
		StringBuilder where = new StringBuilder(" WHERE 1=1 AND t1.CODELEMENTOPEPADRE= t2.CODELEMENTOPE(+) ");
		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 ElementosPe table using like.
	 * 
	 * @param elementosPe
	 *            ElementosPe elementosPe
	 * @param startsWith
	 *            Boolean startsWith
	 * @return Long Long
	 */
	@Override
	@Transactional(readOnly = true)
	public Long findAllLikeCount(ElementosPe elementosPe, Boolean startsWith) {
		StringBuilder query = new StringBuilder("SELECT COUNT(1) FROM ELEMENTOS_PE t1 ,ELEMENTOS_PE t2 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(elementosPe, startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 AND t1.CODELEMENTOPEPADRE= t2.CODELEMENTOPE(+) ");
		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 ElementosPe selected for rup_table
	 * 
	 * @param elementosPe
	 *            ElementosPe elementosPe
	 * @param jqGridRequestDto
	 *            JQGridRequestDto jqGridRequestDto
	 * @param startsWith
	 *            Boolean startsWith
	 * @return List<TableRowDto<ElementosPe>> lista
	 */
	@Override
	public List<TableRowDto<ElementosPe>> reorderSelection(ElementosPe elementosPe, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		// SELECT - FROM
		StringBuilder query = new StringBuilder(this.getSelectFromQuery(true));

		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(elementosPe, startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 AND t1.CODELEMENTOPEPADRE= t2.CODELEMENTOPE(+) ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		// Parámetros de filtrado
		@SuppressWarnings(value = "unchecked")
		List<Object> filterParamList = (List<Object>) mapaWhere.get("params");

		// SQL para la reordenación
		StringBuilder sbReorderSelectionSQL = JQGridManager.getReorderQuery(query, jqGridRequestDto, ElementosPe.class,
				filterParamList, "CODELEMENTOPE");

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

	/**
	 * Remove multiple method for rup_table
	 * 
	 * @param filterElementosPe
	 *            ElementosPe filterElementosPe
	 * @param jqGridRequestDto
	 *            JQGridRequestDto jqGridRequestDto
	 * @param startsWith
	 *            Boolean startsWith
	 */
	@Override
	public void removeMultiple(ElementosPe filterElementosPe, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		// SELECT - FROM
		/** TODO: select por clave */
		StringBuilder query = new StringBuilder(this.getSelectFromQuery(true));

		// WHERE
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterElementosPe, startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 AND t1.CODELEMENTOPEPADRE= t2.CODELEMENTOPE(+) ");
		where.append(mapaWhere.get("query"));
		query.append(where);

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

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

	/*
	 * MÉTODOS PRIVADOS
	 */

	/**
	 * Devuelve la Select con todos los campos. Incluye el From si se indica.
	 * 
	 * @param includeFrom
	 *            boolean includeFrom
	 * @return String String
	 */
	private String getSelectFromQuery(boolean includeFrom) {
		StringBuilder query = new StringBuilder(
				"SELECT t1.CODELEMENTOPE CODELEMENTOPE, t1.CODPLANE CODPLANE, t1.ORDEN ORDEN, t1.NIVEL NIVEL, t1.NOMBRE NOMBRE, t1.NOMBRECAS NOMBRECAS, t1.IDELEMENTO IDELEMENTO, t1.HELBURU HELBURU, t1.HAS_EUS HASEUS, t1.HAS_ELEBI HASELEBI, t1.AURRE_EUS AURREEUS, t1.AURRE_ELEBI AURREELEBI, t1.AMAI_EUS AMAIEUS, t1.AMAI_ELEBI AMAIELEBI, t1.OHARRAK OHARRAK, t2.CODELEMENTOPE ELEMENTOSPECODELEMENTOPE, t2.CODPLANE ELEMENTOSPECODPLANE, t2.ORDEN ELEMENTOSPEORDEN, t2.NIVEL ELEMENTOSPENIVEL, t2.NOMBRE ELEMENTOSPENOMBRE, t2.NOMBRECAS ELEMENTOSPENOMBRECAS, t2.IDELEMENTO ELEMENTOSPEIDELEMENTO, t2.HELBURU ELEMENTOSPEHELBURU, t2.HAS_EUS ELEMENTOSPEHASEUS, t2.HAS_ELEBI ELEMENTOSPEHASELEBI, t2.AURRE_EUS ELEMENTOSPEAURREEUS, t2.AURRE_ELEBI ELEMENTOSPEAURREELEBI, t2.AMAI_EUS ELEMENTOSPEAMAIEUS, t2.AMAI_ELEBI ELEMENTOSPEAMAIELEBI, t2.OHARRAK ELEMENTOSPEOHARRAK, t2.CODELEMENTOPEPADRE ELEMENTOSPECODELEMENTOPEPADRE ");
		if (includeFrom) {
			query.append(" FROM ELEMENTOS_PE t1 , ELEMENTOS_PE t2 ");
		}
		return query.toString();
	}

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

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

		if (elementosPe != null) {
			if (elementosPe.getCodelementope() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.CODELEMENTOPE", elementosPe.getCodelementope());
			}
			if (elementosPe.getElementosPe() != null) {
				if (elementosPe.getElementosPe().getCodelementope() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.CODELEMENTOPE",
							elementosPe.getElementosPe().getCodelementope());
				}
				if (elementosPe.getElementosPe().getElementosPe() != null
						&& elementosPe.getElementosPe().getElementosPe().getCodelementope() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.CODELEMENTOPEPADRE",
							elementosPe.getElementosPe().getElementosPe().getCodelementope());
				}
				if (elementosPe.getElementosPe().getCodplane() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.CODPLANE", elementosPe.getElementosPe().getCodplane());
				}
				if (elementosPe.getElementosPe().getOrden() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.ORDEN", elementosPe.getElementosPe().getOrden());
				}
				if (elementosPe.getElementosPe().getNivel() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.NIVEL", elementosPe.getElementosPe().getNivel());
				}
				if (elementosPe.getElementosPe().getNombre() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.NOMBRE", elementosPe.getElementosPe().getNombre());
				}
				if (elementosPe.getElementosPe().getNombrecas() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.NOMBRECAS", elementosPe.getElementosPe().getNombrecas());
				}
				if (elementosPe.getElementosPe().getIdelemento() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.IDELEMENTO",
							elementosPe.getElementosPe().getIdelemento());
				}
				if (elementosPe.getElementosPe().getHelburu() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.HELBURU", elementosPe.getElementosPe().getHelburu());
				}
				if (elementosPe.getElementosPe().getHasEus() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.HAS_EUS", elementosPe.getElementosPe().getHasEus());
				}
				if (elementosPe.getElementosPe().getHasElebi() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.HAS_ELEBI", elementosPe.getElementosPe().getHasElebi());
				}
				if (elementosPe.getElementosPe().getAurreEus() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.AURRE_EUS", elementosPe.getElementosPe().getAurreEus());
				}
				if (elementosPe.getElementosPe().getAurreElebi() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.AURRE_ELEBI",
							elementosPe.getElementosPe().getAurreElebi());
				}
				if (elementosPe.getElementosPe().getAmaiEus() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.AMAI_EUS", elementosPe.getElementosPe().getAmaiEus());
				}
				if (elementosPe.getElementosPe().getAmaiElebi() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.AMAI_ELEBI",
							elementosPe.getElementosPe().getAmaiElebi());
				}
				if (elementosPe.getElementosPe().getOharrak() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.OHARRAK", elementosPe.getElementosPe().getOharrak());
				}
			}
			if (elementosPe.getCodplane() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.CODPLANE", elementosPe.getCodplane());
			}
			if (elementosPe.getOrden() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.ORDEN", elementosPe.getOrden());
			}
			if (elementosPe.getNivel() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.NIVEL", elementosPe.getNivel());
			}
			if (elementosPe.getNombre() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.NOMBRE", elementosPe.getNombre());
			}
			if (elementosPe.getNombrecas() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.NOMBRECAS", elementosPe.getNombrecas());
			}
			if (elementosPe.getIdelemento() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.IDELEMENTO", elementosPe.getIdelemento());
			}
			if (elementosPe.getHelburu() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.HELBURU", elementosPe.getHelburu());
			}
			if (elementosPe.getHasEus() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.HAS_EUS", elementosPe.getHasEus());
			}
			if (elementosPe.getHasElebi() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.HAS_ELEBI", elementosPe.getHasElebi());
			}
			if (elementosPe.getAurreEus() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.AURRE_EUS", elementosPe.getAurreEus());
			}
			if (elementosPe.getAurreElebi() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.AURRE_ELEBI", elementosPe.getAurreElebi());
			}
			if (elementosPe.getAmaiEus() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.AMAI_EUS", elementosPe.getAmaiEus());
			}
			if (elementosPe.getAmaiElebi() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.AMAI_ELEBI", elementosPe.getAmaiElebi());
			}
			if (elementosPe.getOharrak() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.OHARRAK", elementosPe.getOharrak());
			}
		}

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

		return mapWhere;
	}

	/**
	 * Returns a map with the needed value to create the conditions to filter by
	 * the ElementosPe entity
	 * 
	 * @param elementosPe
	 *            ElementosPe 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.
	 */
	private Map<String, Object> getWhereLikeMap(ElementosPe elementosPe, Boolean startsWith) {

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

		if (elementosPe != null) {
			if (elementosPe.getCodelementope() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.CODELEMENTOPE", elementosPe.getCodelementope(),
						startsWith);
			}
			if (elementosPe.getElementosPe() != null) {
				if (elementosPe.getElementosPe().getCodelementope() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.CODELEMENTOPE",
							elementosPe.getElementosPe().getCodelementope(), startsWith);
				}
				if (elementosPe.getElementosPe().getElementosPe() != null
						&& elementosPe.getElementosPe().getElementosPe().getCodelementope() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.CODELEMENTOPEPADRE",
							elementosPe.getElementosPe().getElementosPe().getCodelementope(), startsWith);
				}
				if (elementosPe.getElementosPe().getCodplane() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.CODPLANE",
							elementosPe.getElementosPe().getCodplane(), startsWith);
				}
				if (elementosPe.getElementosPe().getOrden() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.ORDEN", elementosPe.getElementosPe().getOrden(),
							startsWith);
				}
				if (elementosPe.getElementosPe().getNivel() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.NIVEL", elementosPe.getElementosPe().getNivel(),
							startsWith);
				}
				if (elementosPe.getElementosPe().getNombre() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.NOMBRE", elementosPe.getElementosPe().getNombre(),
							startsWith);
				}
				if (elementosPe.getElementosPe().getNombrecas() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.NOMBRECAS",
							elementosPe.getElementosPe().getNombrecas(), startsWith);
				}
				if (elementosPe.getElementosPe().getIdelemento() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.IDELEMENTO",
							elementosPe.getElementosPe().getIdelemento(), startsWith);
				}
				if (elementosPe.getElementosPe().getHelburu() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.HELBURU", elementosPe.getElementosPe().getHelburu(),
							startsWith);
				}
				if (elementosPe.getElementosPe().getHasEus() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.HAS_EUS", elementosPe.getElementosPe().getHasEus(),
							startsWith);
				}
				if (elementosPe.getElementosPe().getHasElebi() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.HAS_ELEBI",
							elementosPe.getElementosPe().getHasElebi(), startsWith);
				}
				if (elementosPe.getElementosPe().getAurreEus() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.AURRE_EUS",
							elementosPe.getElementosPe().getAurreEus(), startsWith);
				}
				if (elementosPe.getElementosPe().getAurreElebi() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.AURRE_ELEBI",
							elementosPe.getElementosPe().getAurreElebi(), startsWith);
				}
				if (elementosPe.getElementosPe().getAmaiEus() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.AMAI_EUS", elementosPe.getElementosPe().getAmaiEus(),
							startsWith);
				}
				if (elementosPe.getElementosPe().getAmaiElebi() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.AMAI_ELEBI",
							elementosPe.getElementosPe().getAmaiElebi(), startsWith);
				}
				if (elementosPe.getElementosPe().getOharrak() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.OHARRAK", elementosPe.getElementosPe().getOharrak(),
							startsWith);
				}
			}
			if (elementosPe.getCodplane() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.CODPLANE", elementosPe.getCodplane(), startsWith);
			}
			if (elementosPe.getOrden() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.ORDEN", elementosPe.getOrden(), startsWith);
			}
			if (elementosPe.getNivel() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.NIVEL", elementosPe.getNivel(), startsWith);
			}
			if (elementosPe.getNombre() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.NOMBRE", elementosPe.getNombre(), startsWith);
			}
			if (elementosPe.getNombrecas() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.NOMBRECAS", elementosPe.getNombrecas(), startsWith);
			}
			if (elementosPe.getIdelemento() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.IDELEMENTO", elementosPe.getIdelemento(), startsWith);
			}
			if (elementosPe.getHelburu() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.HELBURU", elementosPe.getHelburu(), startsWith);
			}
			if (elementosPe.getHasEus() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.HAS_EUS", elementosPe.getHasEus(), startsWith);
			}
			if (elementosPe.getHasElebi() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.HAS_ELEBI", elementosPe.getHasElebi(), startsWith);
			}
			if (elementosPe.getAurreEus() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.AURRE_EUS", elementosPe.getAurreEus(), startsWith);
			}
			if (elementosPe.getAurreElebi() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.AURRE_ELEBI", elementosPe.getAurreElebi(), startsWith);
			}
			if (elementosPe.getAmaiEus() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.AMAI_EUS", elementosPe.getAmaiEus(), startsWith);
			}
			if (elementosPe.getAmaiElebi() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.AMAI_ELEBI", elementosPe.getAmaiElebi(), startsWith);
			}
			if (elementosPe.getOharrak() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.OHARRAK", elementosPe.getOharrak(), startsWith);
			}
		}

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

		return mapWhere;
	}
}
