package com.ejie.ab39a.dao;

import com.ejie.ab39a.model.ElementosPe;
import com.ejie.ab39a.model.PlanesGestion;
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.ElementosPg;
import com.ejie.ab39a.model.PlanesEstrategicos;

/**
 * ElementosPgDaoImpl.
 * 
 *  
 */

@Repository
@Transactional
public class ElementosPgDaoImpl implements ElementosPgDao {

	private static final String[] ORDER_BY_WHITE_LIST = new String[] { "CODELEMPG", "CODELEMPGPADRE", "CODELEMENTOPE",
			"CODPLANG", "ORDEN", "NOMBRE", "NOMBRECAS", "NIVEL" };

	private JdbcTemplate jdbcTemplate;

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<ElementosPg> rwMap = new RowMapper<ElementosPg>() {
		@Override
		public ElementosPg mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new ElementosPg(resultSet.getInt("CODELEMPG"), resultSet.getLong("ORDEN"),
					resultSet.getString("NOMBRE"), resultSet.getString("NOMBRECAS"), resultSet.getLong("NIVEL"),
					new ElementosPg(resultSet.getInt("ElementosPgCODELEMPG"), resultSet.getLong("ElementosPgORDEN"),
							resultSet.getString("ElementosPgNOMBRE"), resultSet.getString("ElementosPgNOMBRECAS"),
							resultSet.getLong("ElementosPgNIVEL"),
							new ElementosPg(resultSet.getInt("ElementosPgCODELEMPGPADRE"), null, null, null, null, null,
									null, null, null),
							new ElementosPe(resultSet.getInt("ElementosPgCODELEMENTOPE"), null, null, null, null, null,
									null, null, null, null, null, null, null, null, null, null, null, null),
							new PlanesGestion(resultSet.getInt("ElementosPgCODPLANG"), null, null, null)),
					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)),
					new PlanesGestion(resultSet.getInt("PlanesGestionCODPLANG"),
							new PlanesEstrategicos(resultSet.getInt("PlanesGestionCODPLANE")),
							resultSet.getLong("PlanesGestionANYO")));
		}
	};

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

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

	/*
	 * OPERACIONES CRUD
	 */

	/**
	 * Inserts a single row in the ElementosPg table.
	 *
	 * @param elementospg
	 *            ElementosPg elementospg
	 * @return ElementosPg ElementosPg
	 */
	@Override
	public ElementosPg add(ElementosPg elementospg) {
		StringBuilder query = new StringBuilder(
				"INSERT INTO ELEMENTOS_PG (CODELEMPG, CODELEMPGPADRE, CODELEMENTOPE, CODPLANG, ORDEN, NOMBRE, NOMBRECAS, NIVEL) VALUES (?,?,?,?,?,?,?,?)");
		Object getElementosPgCodelempgAux = null;
		if (elementospg.getElementosPg() != null && elementospg.getElementosPg().getCodelempg() != null) {
			getElementosPgCodelempgAux = elementospg.getElementosPg().getCodelempg();
		}
		Object getElementosPeCodelementopeAux = null;
		if (elementospg.getElementosPe() != null && elementospg.getElementosPe().getCodelementope() != null) {
			getElementosPeCodelementopeAux = elementospg.getElementosPe().getCodelementope();
		}
		Object getPlanesGestionCodplangAux = null;
		if (elementospg.getPlanesGestion() != null && elementospg.getPlanesGestion().getCodplang() != null) {
			getPlanesGestionCodplangAux = elementospg.getPlanesGestion().getCodplang();
		}
		this.jdbcTemplate.update(query.toString(), elementospg.getCodelempg(), getElementosPgCodelempgAux,
				getElementosPeCodelementopeAux, getPlanesGestionCodplangAux, elementospg.getOrden(),
				elementospg.getNombre(), elementospg.getNombrecas(), elementospg.getNivel());
		return elementospg;
	}

	/**
	 * Updates a single row in the ElementosPg table.
	 *
	 * @param elementospg
	 *            ElementosPg elementospg
	 * @return ElementosPg ElementosPg
	 */
	@Override
	public ElementosPg update(ElementosPg elementospg) {
		StringBuilder query = new StringBuilder(
				"UPDATE ELEMENTOS_PG SET CODELEMPGPADRE=?, CODELEMENTOPE=?, CODPLANG=?, ORDEN=?, NOMBRE=?, NOMBRECAS=?, NIVEL=?");
		query.append(" WHERE CODELEMPG=?");
		Object getElementosPgCodelempgAux = null;
		if (elementospg.getElementosPg() != null && elementospg.getElementosPg().getCodelempg() != null) {
			getElementosPgCodelempgAux = elementospg.getElementosPg().getCodelempg();
		}
		Object getElementosPeCodelementopeAux = null;
		if (elementospg.getElementosPe() != null && elementospg.getElementosPe().getCodelementope() != null) {
			getElementosPeCodelementopeAux = elementospg.getElementosPe().getCodelementope();
		}
		Object getPlanesGestionCodplangAux = null;
		if (elementospg.getPlanesGestion() != null && elementospg.getPlanesGestion().getCodplang() != null) {
			getPlanesGestionCodplangAux = elementospg.getPlanesGestion().getCodplang();
		}
		this.jdbcTemplate.update(query.toString(), getElementosPgCodelempgAux, getElementosPeCodelementopeAux,
				getPlanesGestionCodplangAux, elementospg.getOrden(), elementospg.getNombre(),
				elementospg.getNombrecas(), elementospg.getNivel(), elementospg.getCodelempg());
		return elementospg;
	}

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

		List<ElementosPg> elementospgList = this.jdbcTemplate.query(query.toString(), this.rwMap,
				elementospg.getCodelempg());
		return DataAccessUtils.uniqueResult(elementospgList);
	}

	/**
	 * Removes a single row in the ElementosPg table.
	 *
	 * @param elementospg
	 *            ElementosPg elementospg
	 */
	@Override
	public void remove(ElementosPg elementospg) {
		String query = "DELETE FROM ELEMENTOS_PG WHERE CODELEMPG=?";
		this.jdbcTemplate.update(query, elementospg.getCodelempg());
	}

	/**
	 * Removes a single row in the ElementosPg table.
	 *
	 * @param elementospg
	 *            ElementosPg elementospg
	 */
	@Override
	public void removeHijos(ElementosPg elementospg) {
		String query = "DELETE FROM ELEMENTOS_PG WHERE CODELEMPGPADRE IN(SELECT CODELEMPG FROM ELEMENTOS_PG WHERE CODELEMENTOPE =?)";
		this.jdbcTemplate.update(query, elementospg.getCodelempg());
	}

	/**
	 * Removes a single row in the ElementosPg table.
	 *
	 * @param elementospg
	 *            ElementosPg elementospg
	 */
	@Override
	public void removeConIdElemento(ElementosPg elementospg) {
		String query = "DELETE FROM ELEMENTOS_PG WHERE CODELEMENTOPE =?";
		this.jdbcTemplate.update(query, elementospg.getCodelempg());
	}

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

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

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

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

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

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

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

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

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

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

		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(elementosPg, startsWith);
		StringBuilder where = new StringBuilder(
				" WHERE 1=1 AND t1.CODELEMPGPADRE= t2.CODELEMPG(+) AND t1.CODELEMENTOPE= t3.CODELEMENTOPE(+) AND t1.CODPLANG= t4.CODPLANG(+) ");
		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, ElementosPg.class,
				filterParamList, "CODELEMPG");

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

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

		// WHERE
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterElementosPg, startsWith);
		StringBuilder where = new StringBuilder(
				" WHERE 1=1 AND t1.CODELEMPGPADRE= t2.CODELEMPG(+) AND t1.CODELEMENTOPE= t3.CODELEMENTOPE(+) AND t1.CODPLANG= t4.CODPLANG(+) ");
		where.append(mapaWhere.get("query"));
		query.append(where);

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

		StringBuilder sbRemoveMultipleSQL = JQGridManager.getRemoveMultipleQuery(jqGridRequestDto, ElementosPg.class,
				query, params, "CODELEMPG");
		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.CODELEMPG CODELEMPG, t1.ORDEN ORDEN, t1.NOMBRE NOMBRE, t1.NOMBRECAS NOMBRECAS, t1.NIVEL NIVEL, t2.CODELEMPG ELEMENTOSPGCODELEMPG, t2.ORDEN ELEMENTOSPGORDEN, t2.NOMBRE ELEMENTOSPGNOMBRE, t2.NOMBRECAS ELEMENTOSPGNOMBRECAS, t2.NIVEL ELEMENTOSPGNIVEL, t3.CODELEMENTOPE ELEMENTOSPECODELEMENTOPE, t3.CODPLANE ELEMENTOSPECODPLANE, t3.ORDEN ELEMENTOSPEORDEN, t3.NIVEL ELEMENTOSPENIVEL, t3.NOMBRE ELEMENTOSPENOMBRE, t3.NOMBRECAS ELEMENTOSPENOMBRECAS, t3.IDELEMENTO ELEMENTOSPEIDELEMENTO, t3.HELBURU ELEMENTOSPEHELBURU, t3.HAS_EUS ELEMENTOSPEHASEUS, t3.HAS_ELEBI ELEMENTOSPEHASELEBI, t3.AURRE_EUS ELEMENTOSPEAURREEUS, t3.AURRE_ELEBI ELEMENTOSPEAURREELEBI, t3.AMAI_EUS ELEMENTOSPEAMAIEUS, t3.AMAI_ELEBI ELEMENTOSPEAMAIELEBI, t3.OHARRAK ELEMENTOSPEOHARRAK, t4.CODPLANG PLANESGESTIONCODPLANG, t4.CODPLANE PLANESGESTIONCODPLANE, t4.ANYO PLANESGESTIONANYO, t2.CODELEMPGPADRE ELEMENTOSPGCODELEMPGPADRE, t2.CODELEMENTOPE ELEMENTOSPGCODELEMENTOPE, t2.CODPLANG ELEMENTOSPGCODPLANG, t3.CODELEMENTOPEPADRE ELEMENTOSPECODELEMENTOPEPADRE ");
		if (includeFrom) {
			query.append(" FROM ELEMENTOS_PG t1 , ELEMENTOS_PG t2 , ELEMENTOS_PE t3 , PLANES_GESTION t4 ");
		}
		return query.toString();
	}

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

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

		if (elementosPg != null) {
			if (elementosPg.getCodelempg() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.CODELEMPG", elementosPg.getCodelempg());
			}
			if (elementosPg.getElementosPg() != null) {
				if (elementosPg.getElementosPg().getCodelempg() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.CODELEMPG", elementosPg.getElementosPg().getCodelempg());
				}
				if (elementosPg.getElementosPg().getElementosPg() != null
						&& elementosPg.getElementosPg().getElementosPg().getCodelempg() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.CODELEMPGPADRE",
							elementosPg.getElementosPg().getElementosPg().getCodelempg());
				}
				if (elementosPg.getElementosPg().getElementosPe() != null
						&& elementosPg.getElementosPg().getElementosPe().getCodelementope() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.CODELEMENTOPE",
							elementosPg.getElementosPg().getElementosPe().getCodelementope());
				}
				if (elementosPg.getElementosPg().getPlanesGestion() != null
						&& elementosPg.getElementosPg().getPlanesGestion().getCodplang() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.CODPLANG",
							elementosPg.getElementosPg().getPlanesGestion().getCodplang());
				}
				if (elementosPg.getElementosPg().getOrden() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.ORDEN", elementosPg.getElementosPg().getOrden());
				}
				if (elementosPg.getElementosPg().getNombre() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.NOMBRE", elementosPg.getElementosPg().getNombre());
				}
				if (elementosPg.getElementosPg().getNombrecas() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.NOMBRECAS", elementosPg.getElementosPg().getNombrecas());
				}
				if (elementosPg.getElementosPg().getNivel() != null) {
					UtilSQL.createFilterSQL(where, params, "t2.NIVEL", elementosPg.getElementosPg().getNivel());
				}
			}
			if (elementosPg.getElementosPe() != null) {
				if (elementosPg.getElementosPe().getCodelementope() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.CODELEMENTOPE",
							elementosPg.getElementosPe().getCodelementope());
				}
				if (elementosPg.getElementosPe().getElementosPe() != null
						&& elementosPg.getElementosPe().getElementosPe().getCodelementope() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.CODELEMENTOPEPADRE",
							elementosPg.getElementosPe().getElementosPe().getCodelementope());
				}
				if (elementosPg.getElementosPe().getCodplane() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.CODPLANE", elementosPg.getElementosPe().getCodplane());
				}
				if (elementosPg.getElementosPe().getOrden() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.ORDEN", elementosPg.getElementosPe().getOrden());
				}
				if (elementosPg.getElementosPe().getNivel() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.NIVEL", elementosPg.getElementosPe().getNivel());
				}
				if (elementosPg.getElementosPe().getNombre() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.NOMBRE", elementosPg.getElementosPe().getNombre());
				}
				if (elementosPg.getElementosPe().getNombrecas() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.NOMBRECAS", elementosPg.getElementosPe().getNombrecas());
				}
				if (elementosPg.getElementosPe().getIdelemento() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.IDELEMENTO",
							elementosPg.getElementosPe().getIdelemento());
				}
				if (elementosPg.getElementosPe().getHelburu() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.HELBURU", elementosPg.getElementosPe().getHelburu());
				}
				if (elementosPg.getElementosPe().getHasEus() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.HAS_EUS", elementosPg.getElementosPe().getHasEus());
				}
				if (elementosPg.getElementosPe().getHasElebi() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.HAS_ELEBI", elementosPg.getElementosPe().getHasElebi());
				}
				if (elementosPg.getElementosPe().getAurreEus() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.AURRE_EUS", elementosPg.getElementosPe().getAurreEus());
				}
				if (elementosPg.getElementosPe().getAurreElebi() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.AURRE_ELEBI",
							elementosPg.getElementosPe().getAurreElebi());
				}
				if (elementosPg.getElementosPe().getAmaiEus() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.AMAI_EUS", elementosPg.getElementosPe().getAmaiEus());
				}
				if (elementosPg.getElementosPe().getAmaiElebi() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.AMAI_ELEBI",
							elementosPg.getElementosPe().getAmaiElebi());
				}
				if (elementosPg.getElementosPe().getOharrak() != null) {
					UtilSQL.createFilterSQL(where, params, "t3.OHARRAK", elementosPg.getElementosPe().getOharrak());
				}
			}
			if (elementosPg.getPlanesGestion() != null) {
				if (elementosPg.getPlanesGestion().getCodplang() != null) {
					UtilSQL.createFilterSQL(where, params, "t4.CODPLANG", elementosPg.getPlanesGestion().getCodplang());
				}
				if (elementosPg.getPlanesGestion().getPlanesEstrategicos() != null
						&& elementosPg.getPlanesGestion().getPlanesEstrategicos().getCodplane() != null) {
					UtilSQL.createFilterSQL(where, params, "t4.CODPLANE",
							elementosPg.getPlanesGestion().getPlanesEstrategicos().getCodplane());
				}
				if (elementosPg.getPlanesGestion().getAnyo() != null) {
					UtilSQL.createFilterSQL(where, params, "t4.ANYO", elementosPg.getPlanesGestion().getAnyo());
				}
			}
			if (elementosPg.getOrden() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.ORDEN", elementosPg.getOrden());
			}
			if (elementosPg.getNombre() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.NOMBRE", elementosPg.getNombre());
			}
			if (elementosPg.getNombrecas() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.NOMBRECAS", elementosPg.getNombrecas());
			}
			if (elementosPg.getNivel() != null) {
				UtilSQL.createFilterSQL(where, params, "t1.NIVEL", elementosPg.getNivel());
			}
		}

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

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

		if (elementosPg != null) {
			if (elementosPg.getCodelempg() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.CODELEMPG", elementosPg.getCodelempg(), startsWith);
			}
			if (elementosPg.getElementosPg() != null) {
				if (elementosPg.getElementosPg().getCodelempg() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.CODELEMPG",
							elementosPg.getElementosPg().getCodelempg(), startsWith);
				}
				if (elementosPg.getElementosPg().getElementosPg() != null
						&& elementosPg.getElementosPg().getElementosPg().getCodelempg() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.CODELEMPGPADRE",
							elementosPg.getElementosPg().getElementosPg().getCodelempg(), startsWith);
				}
				if (elementosPg.getElementosPg().getElementosPe() != null
						&& elementosPg.getElementosPg().getElementosPe().getCodelementope() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.CODELEMENTOPE",
							elementosPg.getElementosPg().getElementosPe().getCodelementope(), startsWith);
				}
				if (elementosPg.getElementosPg().getPlanesGestion() != null
						&& elementosPg.getElementosPg().getPlanesGestion().getCodplang() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.CODPLANG",
							elementosPg.getElementosPg().getPlanesGestion().getCodplang(), startsWith);
				}
				if (elementosPg.getElementosPg().getOrden() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.ORDEN", elementosPg.getElementosPg().getOrden(),
							startsWith);
				}
				if (elementosPg.getElementosPg().getNombre() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.NOMBRE", elementosPg.getElementosPg().getNombre(),
							startsWith);
				}
				if (elementosPg.getElementosPg().getNombrecas() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.NOMBRECAS",
							elementosPg.getElementosPg().getNombrecas(), startsWith);
				}
				if (elementosPg.getElementosPg().getNivel() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t2.NIVEL", elementosPg.getElementosPg().getNivel(),
							startsWith);
				}
			}
			if (elementosPg.getElementosPe() != null) {
				if (elementosPg.getElementosPe().getCodelementope() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.CODELEMENTOPE",
							elementosPg.getElementosPe().getCodelementope(), startsWith);
				}
				if (elementosPg.getElementosPe().getElementosPe() != null
						&& elementosPg.getElementosPe().getElementosPe().getCodelementope() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.CODELEMENTOPEPADRE",
							elementosPg.getElementosPe().getElementosPe().getCodelementope(), startsWith);
				}
				if (elementosPg.getElementosPe().getCodplane() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.CODPLANE",
							elementosPg.getElementosPe().getCodplane(), startsWith);
				}
				if (elementosPg.getElementosPe().getOrden() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.ORDEN", elementosPg.getElementosPe().getOrden(),
							startsWith);
				}
				if (elementosPg.getElementosPe().getNivel() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.NIVEL", elementosPg.getElementosPe().getNivel(),
							startsWith);
				}
				if (elementosPg.getElementosPe().getNombre() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.NOMBRE", elementosPg.getElementosPe().getNombre(),
							startsWith);
				}
				if (elementosPg.getElementosPe().getNombrecas() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.NOMBRECAS",
							elementosPg.getElementosPe().getNombrecas(), startsWith);
				}
				if (elementosPg.getElementosPe().getIdelemento() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.IDELEMENTO",
							elementosPg.getElementosPe().getIdelemento(), startsWith);
				}
				if (elementosPg.getElementosPe().getHelburu() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.HELBURU", elementosPg.getElementosPe().getHelburu(),
							startsWith);
				}
				if (elementosPg.getElementosPe().getHasEus() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.HAS_EUS", elementosPg.getElementosPe().getHasEus(),
							startsWith);
				}
				if (elementosPg.getElementosPe().getHasElebi() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.HAS_ELEBI",
							elementosPg.getElementosPe().getHasElebi(), startsWith);
				}
				if (elementosPg.getElementosPe().getAurreEus() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.AURRE_EUS",
							elementosPg.getElementosPe().getAurreEus(), startsWith);
				}
				if (elementosPg.getElementosPe().getAurreElebi() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.AURRE_ELEBI",
							elementosPg.getElementosPe().getAurreElebi(), startsWith);
				}
				if (elementosPg.getElementosPe().getAmaiEus() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.AMAI_EUS", elementosPg.getElementosPe().getAmaiEus(),
							startsWith);
				}
				if (elementosPg.getElementosPe().getAmaiElebi() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.AMAI_ELEBI",
							elementosPg.getElementosPe().getAmaiElebi(), startsWith);
				}
				if (elementosPg.getElementosPe().getOharrak() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t3.OHARRAK", elementosPg.getElementosPe().getOharrak(),
							startsWith);
				}
			}
			if (elementosPg.getPlanesGestion() != null) {
				if (elementosPg.getPlanesGestion().getCodplang() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t4.CODPLANG",
							elementosPg.getPlanesGestion().getCodplang(), startsWith);
				}
				if (elementosPg.getPlanesGestion().getPlanesEstrategicos() != null
						&& elementosPg.getPlanesGestion().getPlanesEstrategicos().getCodplane() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t4.CODPLANE",
							elementosPg.getPlanesGestion().getPlanesEstrategicos().getCodplane(), startsWith);
				}
				if (elementosPg.getPlanesGestion().getAnyo() != null) {
					UtilSQL.createFilterLikeSQL(where, params, "t4.ANYO", elementosPg.getPlanesGestion().getAnyo(),
							startsWith);
				}
			}
			if (elementosPg.getOrden() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.ORDEN", elementosPg.getOrden(), startsWith);
			}
			if (elementosPg.getNombre() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.NOMBRE", elementosPg.getNombre(), startsWith);
			}
			if (elementosPg.getNombrecas() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.NOMBRECAS", elementosPg.getNombrecas(), startsWith);
			}
			if (elementosPg.getNivel() != null) {
				UtilSQL.createFilterLikeSQL(where, params, "t1.NIVEL", elementosPg.getNivel(), startsWith);
			}
		}

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

		return mapWhere;
	}
}
