package com.ejie.ad74a.dao;

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.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ad74a.model.DocumentosInforme;
import com.ejie.ad74a.util.UtilSQL;
import com.ejie.ad74a.util.comun.UtilSeguridad;
import com.ejie.ad74a.util.constantes.EstadoInformesEnum;
import com.ejie.x38.dao.RowNumResultSetExtractor;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableManager;
import com.ejie.x38.dto.TableManagerJerarquia;
import com.ejie.x38.dto.TableRequestDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * DocumentosInformeDaoImpl generated by UDA, 19-abr-2023 16:38:34.
 * 
 * @author UDA
 */

@Repository
@Transactional
public class DocumentosInformeDaoImpl implements DocumentosInformeDao {

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDDOCINFORME17", "IDTIPODOC17", "OID17",
			"OIDDOKUSI17", "URLRUTA17", "USUALTA17", "USUMODIF17", "FALTA17", "FMODIF17" };

	private JdbcTemplate jdbcTemplate;

	public static final String SEQUENCE_DOC_INFORME = "SEQ_DOC_INFORME";

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<DocumentosInforme> rwMap = new RowMapper<DocumentosInforme>() {
		public DocumentosInforme mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new DocumentosInforme(UtilSQL.getLong(resultSet, "IDDOCINFORME17"),
					UtilSQL.getLong(resultSet, "IDTIPODOC17"), resultSet.getString("OID17"),
					resultSet.getString("OIDDOKUSI17"), resultSet.getString("URLRUTA17"),
					resultSet.getString("USUALTA17"), resultSet.getString("USUMODIF17"), resultSet.getDate("FALTA17"),
					resultSet.getDate("FMODIF17"), resultSet.getString("NOMBREDOC17"));
		}
	};

	private RowMapper<DocumentosInforme> rwMapPK = new RowMapper<DocumentosInforme>() {
		public DocumentosInforme mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new DocumentosInforme(UtilSQL.getLong(resultSet, "IDDOCINFORME17"));
		}
	};

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

			DocumentosInforme documentosinforme = new DocumentosInforme(UtilSQL.getLong(resultSet, "IDDOCINFORME17"),
					UtilSQL.getLong(resultSet, "IDTIPODOC17"), resultSet.getString("OID17"),
					resultSet.getString("OIDDOKUSI17"), resultSet.getString("URLRUTA17"),
					resultSet.getString("USUALTA17"), resultSet.getString("USUMODIF17"), resultSet.getDate("FALTA17"),
					resultSet.getDate("FMODIF17"), resultSet.getString("NOMBREDOC17"));

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

		String usuario = null;
		if (documentosinforme.getUsuModif17() == null) {
			try {
				usuario = UtilSeguridad.getUsuarioConectado().getUserName();

			} catch (Exception e) {
				usuario = com.ejie.ad74a.constantes.Constantes.COD_APLICACION;

			}
		} else {
			usuario = documentosinforme.getUsuModif17();
		}

		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();

		String query = "INSERT INTO DOCUMENTOS_INFORME (ID_DOC_INFORME_17, ID_TIPO_DOC_17, OID_17, OID_DOKUSI_17, URL_RUTA_17, USU_ALTA_17,  F_ALTA_17,  NOMBRE_DOC_17) "
				+ "VALUES (" + SEQUENCE_DOC_INFORME
				+ ".NEXTVAL,:idTipoDoc,:oid,:oidDokusi,:urlRuta,:usuAlta,sysdate,:nombreDoc17)";

		MapSqlParameterSource parameters = new MapSqlParameterSource()
				.addValue("idTipoDoc", documentosinforme.getIdTipoDoc17()).addValue("oid", documentosinforme.getOid17())
				.addValue("oidDokusi", documentosinforme.getOidDokusi17())
				.addValue("urlRuta", documentosinforme.getUrlRuta17()).addValue("usuAlta", usuario)
				.addValue("nombreDoc17", documentosinforme.getNombreDoc17());

		NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
		namedParameterJdbcTemplate.update(query, parameters, generatedKeyHolder, new String[] { "ID_DOC_INFORME_17" });
		documentosinforme.setIdDocInforme17(generatedKeyHolder.getKey().longValue());

		return documentosinforme;
	}

	/**
	 * Updates a single row in the DocumentosInforme table.
	 *
	 * @param documentosinforme DocumentosInforme
	 * @return DocumentosInforme
	 */
	public DocumentosInforme update(DocumentosInforme documentosinforme) {
		String usuario = null;
		if (documentosinforme.getUsuModif17() == null) {
			try {
				usuario = UtilSeguridad.getUsuarioConectado().getUserName();

			} catch (Exception e) {
				usuario = com.ejie.ad74a.constantes.Constantes.COD_APLICACION;

			}
		} else {
			usuario = documentosinforme.getUsuModif17();
		}
		String query = "UPDATE DOCUMENTOS_INFORME SET ID_TIPO_DOC_17=?, OID_17=?, OID_DOKUSI_17 = ?, URL_RUTA_17=?, USU_MODIF_17=?, F_MODIF_17=sysdate, NOMBRE_DOC_17=? WHERE ID_DOC_INFORME_17=?";
		this.jdbcTemplate.update(query, documentosinforme.getIdTipoDoc17(), documentosinforme.getOid17(),
				documentosinforme.getOidDokusi17(), documentosinforme.getUrlRuta17(), usuario,
				documentosinforme.getfModif17(), documentosinforme.getNombreDoc17(),
				documentosinforme.getIdDocInforme17());
		return documentosinforme;
	}

	/**
	 * Finds a single row in the DocumentosInforme table.
	 *
	 * @param documentosinforme DocumentosInforme
	 * @return DocumentosInforme
	 */
	@Transactional(readOnly = true)
	public DocumentosInforme find(DocumentosInforme documentosinforme) {
		String query = "SELECT t1.ID_DOC_INFORME_17 IDDOCINFORME17, t1.ID_TIPO_DOC_17 IDTIPODOC17, t1.OID_17 OID17, t1.OID_DOKUSI_17 OIDDOKUSI17, t1.URL_RUTA_17 URLRUTA17, t1.USU_ALTA_17 USUALTA17, t1.USU_MODIF_17 USUMODIF17, t1.F_ALTA_17 FALTA17, t1.F_MODIF_17 FMODIF17, t1.NOMBRE_DOC_17 NOMBREDOC17 FROM DOCUMENTOS_INFORME t1  WHERE t1.ID_DOC_INFORME_17 = ?  ";

		List<DocumentosInforme> documentosinformeList = this.jdbcTemplate.query(query, this.rwMap,
				documentosinforme.getIdDocInforme17());
		return (DocumentosInforme) DataAccessUtils.uniqueResult(documentosinformeList);
	}

	/**
	 * Removes a single row in the DocumentosInforme table.
	 *
	 * @param documentosinforme DocumentosInforme
	 * @return
	 */
	public void remove(DocumentosInforme documentosinforme) {
		String query = "DELETE FROM DOCUMENTOS_INFORME WHERE ID_DOC_INFORME_17=?";
		this.jdbcTemplate.update(query, documentosinforme.getIdDocInforme17());
	}

	/**
	 * Exporta Datos al clipBoard
	 *
	 */
	public List<DocumentosInforme> getMultiple(DocumentosInforme filterDocumentosInforme,
			TableRequestDto tableRequestDto, Boolean startsWith) {

		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_DOC_INFORME_17 IDDOCINFORME17, t1.ID_TIPO_DOC_17 IDTIPODOC17, t1.OID_17 OID17, t1.OID_DOKUSI_17 OIDDOKUSI17, t1.URL_RUTA_17 URLRUTA17, t1.USU_ALTA_17 USUALTA17, t1.USU_MODIF_17 USUMODIF17, t1.F_ALTA_17 FALTA17, t1.F_MODIF_17 FMODIF17, t1.NOMBRE_DOC_17 NOMBREDOC17");
		query.append(" FROM DOCUMENTOS_INFORME t1  ");
		// Where clause & Params
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterDocumentosInforme, startsWith);
		StringBuilder where = new StringBuilder(query + " WHERE 1=1 ");
		where.append(mapaWhere.get("query"));

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

		StringBuilder sbMultipleSQL = TableManager.getSelectMultipleQuery(tableRequestDto, DocumentosInforme.class,
				params, "ID_DOC_INFORME_17");
		where.append(sbMultipleSQL);

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

	/**
	 * Finds a list of rows in the DocumentosInforme table.
	 *
	 * @param documentosinforme DocumentosInforme
	 * @param tableRequestDto   TableRequestDto
	 * @return List<DocumentosInforme>
	 */
	@Transactional(readOnly = true)
	public List<DocumentosInforme> findAll(DocumentosInforme documentosinforme, TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_DOC_INFORME_17 IDDOCINFORME17,t1.ID_TIPO_DOC_17 IDTIPODOC17,t1.OID_17 OID17,t1.OID_DOKUSI_17 OIDDOKUSI17, t1.URL_RUTA_17 URLRUTA17,t1.USU_ALTA_17 USUALTA17,t1.USU_MODIF_17 USUMODIF17,t1.F_ALTA_17 FALTA17,t1.F_MODIF_17 FMODIF17, t1.NOMBRE_DOC_17 NOMBREDOC17 ");
		query.append("FROM DOCUMENTOS_INFORME t1 ");

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

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

		if (tableRequestDto != null) {
			query = TableManager.getPaginationQuery(tableRequestDto, query,
					DocumentosInformeDaoImpl.ORDER_BY_WHITE_LIST);
		}

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

	/**
	 * Finds rows in the DocumentosInforme table using like.
	 *
	 * @param documentosinforme DocumentosInforme
	 * @param tableRequestDto   TableRequestDto
	 * @param startsWith        Boolean
	 * @return List<DocumentosInforme>
	 */
	@Transactional(readOnly = true)
	public List<DocumentosInforme> findAllLike(DocumentosInforme documentosinforme, TableRequestDto tableRequestDto,
			Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_DOC_INFORME_17 IDDOCINFORME17,t1.ID_TIPO_DOC_17 IDTIPODOC17,t1.OID_17 OID17,t1.OID_DOKUSI_17 OIDDOKUSI17, t1.URL_RUTA_17 URLRUTA17,t1.USU_ALTA_17 USUALTA17,t1.USU_MODIF_17 USUMODIF17,t1.F_ALTA_17 FALTA17,t1.F_MODIF_17 FMODIF17, t1.NOMBRE_DOC_17 NOMBREDOC17 ");
		query.append("FROM DOCUMENTOS_INFORME t1 ");

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

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

		if (tableRequestDto != null) {
			query = TableManager.getPaginationQuery(tableRequestDto, query,
					DocumentosInformeDaoImpl.ORDER_BY_WHITE_LIST);
		}

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(documentosinforme, 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.queryForObject(query.toString(), params.toArray(), Long.class);
	}

	/**
	 * Reorder the data list of DocumentosInforme selected for rup_table
	 *
	 * @param documentosinforme DocumentosInforme
	 * @param tableRequestDto   TableRequestDto
	 * @param startsWith        Boolean
	 * @return List<TableRowDto<DocumentosInforme>>
	 */
	@Override
	public List<TableRowDto<DocumentosInforme>> reorderSelection(DocumentosInforme documentosinforme,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_DOC_INFORME_17 IDDOCINFORME17,t1.ID_TIPO_DOC_17 IDTIPODOC17,t1.OID_17 OID17,t1.OID_DOKUSI_17 OIDDOKUSI17, t1.URL_RUTA_17 URLRUTA17,t1.USU_ALTA_17 USUALTA17,t1.USU_MODIF_17 USUMODIF17,t1.F_ALTA_17 FALTA17,t1.F_MODIF_17 FMODIF17 ");
		// FROM
		query.append("FROM DOCUMENTOS_INFORME t1 ");
		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(documentosinforme, 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 = TableManager.getReorderQuery(query, tableRequestDto,
				DocumentosInforme.class, filterParamList, "IDDOCINFORME17");

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

	/**
	 * Search method for rup_table
	 *
	 * @param filterParams    DocumentosInforme
	 * @param searchParams    DocumentosInforme
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<TableRowDto<DocumentosInforme>>
	 */
	@Override
	public List<TableRowDto<DocumentosInforme>> search(DocumentosInforme filterParams, DocumentosInforme searchParams,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_DOC_INFORME_17 IDDOCINFORME17,t1.ID_TIPO_DOC_17 IDTIPODOC17,t1.OID_17 OID17,t1.OID_DOKUSI_17 OIDDOKUSI17, t1.URL_RUTA_17 URLRUTA17,t1.USU_ALTA_17 USUALTA17,t1.USU_MODIF_17 USUMODIF17,t1.F_ALTA_17 FALTA17,t1.F_MODIF_17 FMODIF17 ");
		// FROM
		query.append("FROM DOCUMENTOS_INFORME 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 = TableManager.getSearchQuery(query, tableRequestDto,
				DocumentosInforme.class, filterParamList, searchSQL, searchParamList, from_alias, "IDDOCINFORME17");

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

	/**
	 * Remove multiple method for rup_table
	 *
	 * @param tableRequestDto TableRequestDto
	 */
	@Override
	public void removeMultiple(TableRequestDto tableRequestDto) {
		StringBuilder sbRemoveMultipleSQL = TableManager.getRemoveMultipleQuery(tableRequestDto,
				DocumentosInforme.class, "DOCUMENTOS_INFORME", new String[] { "IDDOCINFORME17" });

		List<String> params = tableRequestDto.getMultiselection().getSelectedIds();

		this.jdbcTemplate.update(sbRemoveMultipleSQL.toString(), params.toArray());
	}

	/**
	 * Filter in the hierarchical DocumentosInforme table.
	 *
	 * @param filterDocumentosInforme DocumentosInforme
	 * @param tableRequestDto         TableRequestDto
	 * @return List<JerarquiaDto<DocumentosInforme>>
	 */
	@Override
	public List<JerarquiaDto<DocumentosInforme>> findAllLikeJerarquia(DocumentosInforme filterDocumentosInforme,
			TableRequestDto tableRequestDto) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_DOC_INFORME_17 IDDOCINFORME17,t1.ID_TIPO_DOC_17 IDTIPODOC17,t1.OID_17 OID17,t1.OID_DOKUSI_17 OIDDOKUSI17, t1.URL_RUTA_17 URLRUTA17,t1.USU_ALTA_17 USUALTA17,t1.USU_MODIF_17 USUMODIF17,t1.F_ALTA_17 FALTA17,t1.F_MODIF_17 FMODIF17, t1.NOMBRE_DOC_17 NOMBREDOC17 ");
		// FROM
		// StringBuilder from = new StringBuilder("FROM DOCUMENTOS_INFORME t1 ");
		List<String> from = new ArrayList<String>();
		from.add("DOCUMENTOS_INFORME");
		// 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(filterDocumentosInforme, false);

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

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

		// PAGINACION
		if (tableRequestDto != null) {
			query = TableManagerJerarquia.getPaginationQuery(tableRequestDto, query,
					DocumentosInformeDaoImpl.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 filterDocumentosInforme DocumentosInforme
	 * @param tableRequestDto         TableRequestDto
	 * @return Long
	 */
	@Override
	public Long findAllLikeCountJerarquia(DocumentosInforme filterDocumentosInforme, TableRequestDto tableRequestDto) {
		// FROM
		// StringBuilder from = new StringBuilder("FROM DOCUMENTOS_INFORME t1 ");
		List<String> from = new ArrayList<String>();
		from.add("DOCUMENTOS_INFORME");

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

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

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

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

	/**
	 * Find dependent hierarchical rows
	 *
	 * @param filterDocumentosInforme DocumentosInforme
	 * @param tableRequestDto         TableRequestDto
	 * @return List<TableRowDto<DocumentosInforme>>
	 */
	@Override
	public List<TableRowDto<DocumentosInforme>> findAllChild(DocumentosInforme filterDocumentosInforme,
			TableRequestDto tableRequestDto) {

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

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

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

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

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

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

	/*
	 * MÉTODOS PRIVADOS
	 */

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

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

		if (documentosinforme != null && documentosinforme.getIdDocInforme17() != null) {
			where.append(" AND t1.ID_DOC_INFORME_17 = ?");
			params.add(documentosinforme.getIdDocInforme17());
		}
		if (documentosinforme != null && documentosinforme.getIdTipoDoc17() != null) {
			where.append(" AND t1.ID_TIPO_DOC_17 = ?");
			params.add(documentosinforme.getIdTipoDoc17());
		}
		if (documentosinforme != null && documentosinforme.getOid17() != null) {
			where.append(" AND t1.OID_17 = ?");
			params.add(documentosinforme.getOid17());
		}
		if (documentosinforme != null && documentosinforme.getOidDokusi17() != null) {
			where.append(" AND t1.OID_DOKUSI_17 = ?");
			params.add(documentosinforme.getOidDokusi17());
		}
		if (documentosinforme != null && documentosinforme.getUrlRuta17() != null) {
			where.append(" AND t1.URL_RUTA_17 = ?");
			params.add(documentosinforme.getUrlRuta17());
		}
		if (documentosinforme != null && documentosinforme.getUsuAlta17() != null) {
			where.append(" AND t1.USU_ALTA_17 = ?");
			params.add(documentosinforme.getUsuAlta17());
		}
		if (documentosinforme != null && documentosinforme.getUsuModif17() != null) {
			where.append(" AND t1.USU_MODIF_17 = ?");
			params.add(documentosinforme.getUsuModif17());
		}
		if (documentosinforme != null && documentosinforme.getfAlta17() != null) {
			where.append(" AND t1.F_ALTA_17 = ?");
			params.add(documentosinforme.getfAlta17());
		}
		if (documentosinforme != null && documentosinforme.getfModif17() != null) {
			where.append(" AND t1.F_MODIF_17 = ?");
			params.add(documentosinforme.getfModif17());
		}

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

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

		if (documentosinforme != null && documentosinforme.getIdDocInforme17() != null) {
			where.append(" AND t1.ID_DOC_INFORME_17 = ?");
			params.add(documentosinforme.getIdDocInforme17());
		}
		if (documentosinforme != null && documentosinforme.getIdTipoDoc17() != null) {
			where.append(" AND t1.ID_TIPO_DOC_17 = ?");
			params.add(documentosinforme.getIdTipoDoc17());
		}
		if (documentosinforme != null && documentosinforme.getOid17() != null) {
			where.append(" AND UPPER(t1.OID_17) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(documentosinforme.getOid17().toUpperCase() + "%");
			} else {
				params.add("%" + documentosinforme.getOid17().toUpperCase() + "%");
			}
			where.append(" AND t1.OID_17 IS NOT NULL");
		}
		if (documentosinforme != null && documentosinforme.getOidDokusi17() != null) {
			where.append(" AND UPPER(t1.OID_DOKUSI_17) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(documentosinforme.getOidDokusi17().toUpperCase() + "%");
			} else {
				params.add("%" + documentosinforme.getOidDokusi17().toUpperCase() + "%");
			}
			where.append(" AND t1.OID_DOKUSI_17 IS NOT NULL");
		}
		if (documentosinforme != null && documentosinforme.getUrlRuta17() != null) {
			where.append(" AND UPPER(t1.URL_RUTA_17) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(documentosinforme.getUrlRuta17().toUpperCase() + "%");
			} else {
				params.add("%" + documentosinforme.getUrlRuta17().toUpperCase() + "%");
			}
			where.append(" AND t1.URL_RUTA_17 IS NOT NULL");
		}
		if (documentosinforme != null && documentosinforme.getUsuAlta17() != null) {
			where.append(" AND UPPER(t1.USU_ALTA_17) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(documentosinforme.getUsuAlta17().toUpperCase() + "%");
			} else {
				params.add("%" + documentosinforme.getUsuAlta17().toUpperCase() + "%");
			}
			where.append(" AND t1.USU_ALTA_17 IS NOT NULL");
		}
		if (documentosinforme != null && documentosinforme.getUsuModif17() != null) {
			where.append(" AND UPPER(t1.USU_MODIF_17) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(documentosinforme.getUsuModif17().toUpperCase() + "%");
			} else {
				params.add("%" + documentosinforme.getUsuModif17().toUpperCase() + "%");
			}
			where.append(" AND t1.USU_MODIF_17 IS NOT NULL");
		}
		if (documentosinforme != null && documentosinforme.getfAlta17() != null) {
			where.append(" AND t1.F_ALTA_17 = ?");
			params.add(documentosinforme.getfAlta17());
		}
		if (documentosinforme != null && documentosinforme.getfModif17() != null) {
			where.append(" AND t1.F_MODIF_17 = ?");
			params.add(documentosinforme.getfModif17());
		}

		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

	public void updateOidDokusiByFolderId(String folderId, String oidPid, String oidDokusi, String user) {
		String query = "UPDATE DOCUMENTOS_INFORME SET OID_17 = ?, OID_DOKUSI_17=?, USU_MODIF_17=?, F_MODIF_17=sysdate WHERE ID_DOC_INFORME_17= ( SELECT ID_DOC_INFORME_17 FROM DOCUMENTOS_INFORME, EXPEDIENTES, INFORMES WHERE ID_EXPEDIENTE_00 = ID_EXPEDIENTE_16 AND ID_DOC_INFORME_16  = ID_DOC_INFORME_17 AND ESTADO_16 = ? AND FOLDER_ID_00 = ?)";
		this.jdbcTemplate.update(query, oidPid, oidDokusi, user, EstadoInformesEnum.ENVIADO.getId(), folderId);
	}

	/**
	 * StringBuilder initilization value
	 */
	public static final int STRING_BUILDER_INIT = 4096;
}
