package m05bDaos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import n05bModelosVistas.m05bOption;

import m05bClasses.m05bConexion;

public class m05bPaginator {
	public static final int PAGESIZE = 30;
	public static final int INITPAGE = -2;
	public static final int NO = -1;

	public StringBuffer paginar(StringBuffer strsqin, int pageNumber) {
		StringBuffer strsql = new StringBuffer();
		strsql.append("SELECT * FROM 	(   SELECT a.*, rownum r__     FROM  (   ");
		strsql.append(strsqin);
		strsql.append(") a    WHERE rownum < ((");
		strsql.append(pageNumber);
		strsql.append(" * ");
		strsql.append(PAGESIZE);
		strsql.append(" ) + 1 )		)	WHERE r__ >= (((");
		strsql.append(pageNumber);
		strsql.append(" -1) *  ");
		strsql.append(PAGESIZE);
		strsql.append(" ) + 1)");
		return strsql;
	}

	public StringBuffer contar(StringBuffer strsqin) {
		StringBuffer strsql = new StringBuffer();
		strsql.append("SELECT count (*)  as num FROM 	(  ");
		strsql.append(strsqin);
		strsql.append(")");
		return strsql;
	}
	public int contarRegistros(StringBuffer sql) throws Exception {

		return contarRegistros(sql,-1);

	}
	public int contarRegistros(StringBuffer sql, int anio) throws Exception {

		PreparedStatement stmt = null;
		ResultSet rs = null;

		Connection m_Connection = null;
		int salida = 0;
		StringBuffer consulta = contar(sql);

		try {
			m05bConexion miConex = new m05bConexion();
			m_Connection = miConex.getConnection();
			
			stmt = m_Connection.prepareStatement(consulta.toString());
			if(anio!=-1)
				stmt.setInt(1, anio);
			rs = stmt.executeQuery();

			if (rs.next()) {
				salida = rs.getInt("num");
			}
			stmt.close();
		} catch (Exception e) {

		} finally {
			try {
				m_Connection.close();

			} catch (Exception e) {

			}
		}
		return salida;

	}
	public int contarRegistros(StringBuffer sql, String anio) throws Exception {

		PreparedStatement stmt = null;
		ResultSet rs = null;

		Connection m_Connection = null;
		int salida = 0;
		StringBuffer consulta = contar(sql);

		try {
			m05bConexion miConex = new m05bConexion();
			m_Connection = miConex.getConnection();
			
			stmt = m_Connection.prepareStatement(consulta.toString());
			if(anio!="-1")
				stmt.setString(1, anio);
			rs = stmt.executeQuery();

			if (rs.next()) {
				salida = rs.getInt("num");
			}
			stmt.close();
		} catch (Exception e) {

		} finally {
			try {
				m_Connection.close();

			} catch (Exception e) {

			}
		}
		return salida;

	}
	public int contarRegistros(StringBuffer sql, String fecha1,String fecha2) throws Exception {

		PreparedStatement stmt = null;
		ResultSet rs = null;

		Connection m_Connection = null;
		int salida = 0;
		StringBuffer consulta = contar(sql);

		try {
			m05bConexion miConex = new m05bConexion();
			m_Connection = miConex.getConnection();
			
			stmt = m_Connection.prepareStatement(consulta.toString());
			stmt.setString(1, fecha1);
			stmt.setString(2, fecha2);
			stmt.setString(3, fecha1);
			stmt.setString(4, fecha2);
			stmt.setString(5, fecha1);
			stmt.setString(6, fecha2);
			stmt.setString(7, fecha1);
			stmt.setString(8, fecha2);
			stmt.setString(9, fecha2);
			rs = stmt.executeQuery();

			if (rs.next()) {
				salida = rs.getInt("num");
			}
			stmt.close();
		} catch (Exception e) {

		} finally {
			try {
				m_Connection.close();

			} catch (Exception e) {

			}
		}
		return salida;

	}

}
