package com.ejie.ab61.dao.inicioOficioExp;

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.ab61.model.inicioOficioExp.RelExpOriDes;
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;

/**
 * RelExpOriDesDaoImpl  
 * 
 *  
 */

@Repository
@Transactional
public class RelExpOriDesDaoImpl implements RelExpOriDesDao {

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDRELEXPORIDES", "IDEXPEDIENTEORIGEN",
			"FOLDERIDORIGEN", "FOLDERIDDESTINO", "FECHAREGISTRO", "USUARIOREGISTRO" };

	private JdbcTemplate jdbcTemplate;

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<RelExpOriDes> rwMap = new RowMapper<RelExpOriDes>() {
		public RelExpOriDes mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new RelExpOriDes(resultSet.getInt("IDRELEXPORIDES"), resultSet.getInt("IDEXPEDIENTEORIGEN"),
					resultSet.getString("FOLDERIDORIGEN"), resultSet.getString("FOLDERIDDESTINO"),
					resultSet.getDate("FECHAREGISTRO"), resultSet.getString("USUARIOREGISTRO"));
		}
	};

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

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

			RelExpOriDes relexporides = new RelExpOriDes(resultSet.getInt("IDRELEXPORIDES"),
					resultSet.getInt("IDEXPEDIENTEORIGEN"), resultSet.getString("FOLDERIDORIGEN"),
					resultSet.getString("FOLDERIDDESTINO"), resultSet.getDate("FECHAREGISTRO"),
					resultSet.getString("USUARIOREGISTRO"));

			JerarquiaDto<RelExpOriDes> jerarquia = new JerarquiaDto<RelExpOriDes>();
			jerarquia.setModel(relexporides);
			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 RelExpOriDes table.
	 *
	 * @param relexporides
	 *            RelExpOriDes
	 * @return RelExpOriDes
	 */
	public RelExpOriDes add(RelExpOriDes relexporides) {

		String query = "INSERT INTO REL_EXP_ORI_DES (ID_REL_EXP_ORI_DES, ID_EXPEDIENTE_ORIGEN, FOLDER_ID_ORIGEN, FOLDER_ID_DESTINO, FECHA_REGISTRO, USUARIO_REGISTRO) VALUES (AB61.S_AB6125T00.NEXTVAL,?,?,?,?,?)";
		this.jdbcTemplate.update(query, relexporides.getIdExpedienteOrigen(), relexporides.getFolderIdOrigen(),
				relexporides.getFolderIdDestino(), relexporides.getFechaRegistro(), relexporides.getUsuarioRegistro());
		return relexporides;
	}

	/**
	 * Updates a single row in the RelExpOriDes table.
	 *
	 * @param relexporides
	 *            RelExpOriDes
	 * @return RelExpOriDes
	 */
	public RelExpOriDes update(RelExpOriDes relexporides) {
		String query = "UPDATE REL_EXP_ORI_DES SET ID_EXPEDIENTE_ORIGEN=?, FOLDER_ID_ORIGEN=?, FOLDER_ID_DESTINO=?, FECHA_REGISTRO=?, USUARIO_REGISTRO=? WHERE ID_REL_EXP_ORI_DES=?";
		this.jdbcTemplate.update(query, relexporides.getIdExpedienteOrigen(), relexporides.getFolderIdOrigen(),
				relexporides.getFolderIdDestino(), relexporides.getFechaRegistro(), relexporides.getUsuarioRegistro(),
				relexporides.getIdRelExpOriDes());
		return relexporides;
	}

	/**
	 * Finds a single row in the RelExpOriDes table.
	 *
	 * @param relexporides
	 *            RelExpOriDes
	 * @return RelExpOriDes
	 */
	@Transactional(readOnly = true)
	public RelExpOriDes find(RelExpOriDes relexporides) {
		String query = "SELECT t1.ID_REL_EXP_ORI_DES IDRELEXPORIDES, t1.ID_EXPEDIENTE_ORIGEN IDEXPEDIENTEORIGEN, t1.FOLDER_ID_ORIGEN FOLDERIDORIGEN, t1.FOLDER_ID_DESTINO FOLDERIDDESTINO, t1.FECHA_REGISTRO FECHAREGISTRO, t1.USUARIO_REGISTRO USUARIOREGISTRO FROM REL_EXP_ORI_DES t1  WHERE t1.ID_REL_EXP_ORI_DES = ?  ";

		List<RelExpOriDes> relexporidesList = this.jdbcTemplate.query(query, this.rwMap,
				relexporides.getIdRelExpOriDes());
		return (RelExpOriDes) DataAccessUtils.uniqueResult(relexporidesList);
	}

	/**
	 * Removes a single row in the RelExpOriDes table.
	 *
	 * @param relexporides
	 *            RelExpOriDes
	 * @return
	 */
	public void remove(RelExpOriDes relexporides) {
		String query = "DELETE FROM REL_EXP_ORI_DES WHERE ID_REL_EXP_ORI_DES=?";
		this.jdbcTemplate.update(query, relexporides.getIdRelExpOriDes());
	}

	/**
	 * Finds a list of rows in the RelExpOriDes table.
	 *
	 * @param relexporides
	 *            RelExpOriDes
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<RelExpOriDes>
	 */
	@Transactional(readOnly = true)
	public List<RelExpOriDes> findAll(RelExpOriDes relexporides, JQGridRequestDto jqGridRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_REL_EXP_ORI_DES IDRELEXPORIDES,t1.ID_EXPEDIENTE_ORIGEN IDEXPEDIENTEORIGEN,t1.FOLDER_ID_ORIGEN FOLDERIDORIGEN,t1.FOLDER_ID_DESTINO FOLDERIDDESTINO,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		query.append("FROM REL_EXP_ORI_DES t1 ");

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

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

	/**
	 * Finds rows in the RelExpOriDes table using like.
	 *
	 * @param relexporides
	 *            RelExpOriDes
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<RelExpOriDes>
	 */
	@Transactional(readOnly = true)
	public List<RelExpOriDes> findAllLike(RelExpOriDes relexporides, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_REL_EXP_ORI_DES IDRELEXPORIDES,t1.ID_EXPEDIENTE_ORIGEN IDEXPEDIENTEORIGEN,t1.FOLDER_ID_ORIGEN FOLDERIDORIGEN,t1.FOLDER_ID_DESTINO FOLDERIDDESTINO,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		query.append("FROM REL_EXP_ORI_DES t1 ");

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(relexporides);
		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 RelExpOriDes table using like.
	 *
	 * @param relexporides
	 *            RelExpOriDes
	 * @param startsWith
	 *            Boolean
	 * @return Long
	 */
	@Transactional(readOnly = true)
	public Long findAllLikeCount(RelExpOriDes relexporides, Boolean startsWith) {
		StringBuilder query = new StringBuilder("SELECT COUNT(1) FROM REL_EXP_ORI_DES t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(relexporides, 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 RelExpOriDes selected for rup_table
	 *
	 * @param relexporides
	 *            RelExpOriDes
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelExpOriDes>>
	 */
	@Override
	public List<TableRowDto<RelExpOriDes>> reorderSelection(RelExpOriDes relexporides,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_EXP_ORI_DES IDRELEXPORIDES,t1.ID_EXPEDIENTE_ORIGEN IDEXPEDIENTEORIGEN,t1.FOLDER_ID_ORIGEN FOLDERIDORIGEN,t1.FOLDER_ID_DESTINO FOLDERIDDESTINO,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		query.append("FROM REL_EXP_ORI_DES t1 ");
		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(relexporides, 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, RelExpOriDes.class,
				filterParamList, "ID_REL_EXP_ORI_DES");

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

	/**
	 * Search method for rup_table
	 *
	 * @param filterParams
	 *            RelExpOriDes
	 * @param searchParams
	 *            RelExpOriDes
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelExpOriDes>>
	 */
	@Override
	public List<TableRowDto<RelExpOriDes>> search(RelExpOriDes filterParams, RelExpOriDes searchParams,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_EXP_ORI_DES IDRELEXPORIDES,t1.ID_EXPEDIENTE_ORIGEN IDEXPEDIENTEORIGEN,t1.FOLDER_ID_ORIGEN FOLDERIDORIGEN,t1.FOLDER_ID_DESTINO FOLDERIDDESTINO,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		query.append("FROM REL_EXP_ORI_DES 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, RelExpOriDes.class,
				filterParamList, searchSQL, searchParamList, from_alias, "ID_REL_EXP_ORI_DES");

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

	/**
	 * Remove multiple method for rup_table
	 *
	 * @param filterrelexporides
	 *            RelExpOriDes
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	@Override
	public void removeMultiple(RelExpOriDes filterrelexporides, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		// SELECT
		/** TODO: select por clave */
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_EXP_ORI_DES IDRELEXPORIDES,t1.ID_EXPEDIENTE_ORIGEN IDEXPEDIENTEORIGEN,t1.FOLDER_ID_ORIGEN FOLDERIDORIGEN,t1.FOLDER_ID_DESTINO FOLDERIDDESTINO,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		query.append("FROM REL_EXP_ORI_DES t1 ");

		// WHERE
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterrelexporides, 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, RelExpOriDes.class,
				query, params, "ID_REL_EXP_ORI_DES");
		this.jdbcTemplate.update(sbRemoveMultipleSQL.toString(), params.toArray());
	}

	/**
	 * Filter in the hierarchical RelExpOriDes table.
	 *
	 * @param filterRelExpOriDes
	 *            RelExpOriDes
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<JerarquiaDto<RelExpOriDes>>
	 */
	@Override
	public List<JerarquiaDto<RelExpOriDes>> findAllLikeJerarquia(RelExpOriDes filterRelExpOriDes,
			JQGridRequestDto jqGridRequestDto) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_REL_EXP_ORI_DES IDRELEXPORIDES,t1.ID_EXPEDIENTE_ORIGEN IDEXPEDIENTEORIGEN,t1.FOLDER_ID_ORIGEN FOLDERIDORIGEN,t1.FOLDER_ID_DESTINO FOLDERIDDESTINO,t1.FECHA_REGISTRO FECHAREGISTRO,t1.USUARIO_REGISTRO USUARIOREGISTRO ");
		// FROM
		// StringBuilder from = new StringBuilder("FROM REL_EXP_ORI_DES t1 ");
		List<String> from = new ArrayList<String>();
		from.add("REL_EXP_ORI_DES");
		// 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(filterRelExpOriDes, false);

		// JERARQUIA
		// Especificar la columna padre correspondiente en lugar de la generada
		// por defecto (ID_PADRE)
		query = JQGridManagerJerarquia.getQuery(jqGridRequestDto, query, mapaWhere, "ID_REL_EXP_ORI_DES", "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,
					RelExpOriDesDaoImpl.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 filterRelExpOriDes
	 *            RelExpOriDes
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return Long
	 */
	@Override
	public Long findAllLikeCountJerarquia(RelExpOriDes filterRelExpOriDes, JQGridRequestDto jqGridRequestDto) {
		// FROM
		// StringBuilder from = new StringBuilder("FROM REL_EXP_ORI_DES t1 ");
		List<String> from = new ArrayList<String>();
		from.add("REL_EXP_ORI_DES");

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

		// JERARQUIA
		// Especificar la columna padre correspondiente en lugar de la generada
		// por defecto (ID_PADRE)
		StringBuilder query = JQGridManagerJerarquia.getQueryCount(jqGridRequestDto, mapaWhere, "ID_REL_EXP_ORI_DES",
				"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 filterRelExpOriDes
	 *            RelExpOriDes
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<TableRowDto<RelExpOriDes>>
	 */
	@Override
	public List<TableRowDto<RelExpOriDes>> findAllChild(RelExpOriDes filterRelExpOriDes,
			JQGridRequestDto jqGridRequestDto) {

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

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

		// MULTISELECCION
		// Especificar la columna padre correspondiente en lugar de la generada
		// por defecto (ID_PADRE)
		StringBuilder query = JQGridManagerJerarquia.getQueryChildren(jqGridRequestDto, mapaWhere, "ID_REL_EXP_ORI_DES",
				"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<RelExpOriDes>(this.rwMapPK, "ID_REL_EXP_ORI_DES"), params.toArray());
	}

	/*
	 * MÉTODOS PRIVADOS
	 */

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

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

		if (relexporides != null && relexporides.getIdRelExpOriDes() != null) {
			where.append(" AND t1.ID_REL_EXP_ORI_DES = ?");
			params.add(relexporides.getIdRelExpOriDes());
		}
		if (relexporides != null && relexporides.getIdExpedienteOrigen() != null) {
			where.append(" AND t1.ID_EXPEDIENTE_ORIGEN = ?");
			params.add(relexporides.getIdExpedienteOrigen());
		}
		if (relexporides != null && relexporides.getFolderIdOrigen() != null) {
			where.append(" AND t1.FOLDER_ID_ORIGEN = ?");
			params.add(relexporides.getFolderIdOrigen());
		}
		if (relexporides != null && relexporides.getFolderIdDestino() != null) {
			where.append(" AND t1.FOLDER_ID_DESTINO = ?");
			params.add(relexporides.getFolderIdDestino());
		}
		if (relexporides != null && relexporides.getFechaRegistro() != null) {
			where.append(" AND t1.FECHA_REGISTRO = ?");
			params.add(relexporides.getFechaRegistro());
		}
		if (relexporides != null && relexporides.getUsuarioRegistro() != null) {
			where.append(" AND t1.USUARIO_REGISTRO = ?");
			params.add(relexporides.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 RelExpOriDes entity
	 *
	 * @param relexporides
	 *            RelExpOriDes 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(RelExpOriDes relexporides, Boolean startsWith) {

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

		if (relexporides != null && relexporides.getIdRelExpOriDes() != null) {
			where.append(" AND t1.ID_REL_EXP_ORI_DES = ?");
			params.add(relexporides.getIdRelExpOriDes());
		}
		if (relexporides != null && relexporides.getIdExpedienteOrigen() != null) {
			where.append(" AND t1.ID_EXPEDIENTE_ORIGEN = ?");
			params.add(relexporides.getIdExpedienteOrigen());
		}
		if (relexporides != null && relexporides.getFolderIdOrigen() != null) {
			where.append(" AND UPPER(t1.FOLDER_ID_ORIGEN) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(relexporides.getFolderIdOrigen().toUpperCase() + "%");
			} else {
				params.add("%" + relexporides.getFolderIdOrigen().toUpperCase() + "%");
			}
			where.append(" AND t1.FOLDER_ID_ORIGEN IS NOT NULL");
		}
		if (relexporides != null && relexporides.getFolderIdDestino() != null) {
			where.append(" AND UPPER(t1.FOLDER_ID_DESTINO) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(relexporides.getFolderIdDestino().toUpperCase() + "%");
			} else {
				params.add("%" + relexporides.getFolderIdDestino().toUpperCase() + "%");
			}
			where.append(" AND t1.FOLDER_ID_DESTINO IS NOT NULL");
		}
		if (relexporides != null && relexporides.getFechaRegistro() != null) {
			where.append(" AND t1.FECHA_REGISTRO = ?");
			params.add(relexporides.getFechaRegistro());
		}
		if (relexporides != null && relexporides.getUsuarioRegistro() != null) {
			where.append(" AND UPPER(t1.USUARIO_REGISTRO) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(relexporides.getUsuarioRegistro().toUpperCase() + "%");
			} else {
				params.add("%" + relexporides.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;
}
