/**
* @(#) PersistenciaTamovext.java
*/
package com.tarisan.control;
import com.tarisan.data.*;
import com.tarisan.excepcion.*;
import com.tarisan.log.*;
import com.tarisan.util.Utilidades;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.sql.Date;
import java.util.Calendar;
import java.util.Vector;
/**
* Controla los accesos a la persistencia para la entidad Tamovext.
* @author Dpto. Informática.
* @version 1.0, 24/09/2003
*/
public class PersistenciaTamovext
{
/**
* El objeto Paginacion que controla el número de filas a mostrar por cada página.
*/
private Paginacion objPaginacion = new Paginacion();
/**
* Obtiene el valor del objeto Paginacion para saber que página se está visualizando.
* @return El objeto Paginacion
*/
public Paginacion getPaginacion()
{
return objPaginacion;
}
/**
* Obtiene el último valor del campo NUMSEQ del médico indicado perteneciente a la tabla TAMOVEXT.
* @param medico Código del médico conectado.
* @return El último valor del campo NUMSEQ del médico en cuestión.
* @throws
*
* OBSOLETO
* 25/10/2017
* csm_jjripaper
* Cambiamos en oracle el campo numseq al tipo autoincrement de oracle (trigger berfore insert llamando a una secuencia).
*/
public int obtenerUltimoValorNumSeq2(int medico) throws ExcepcionTarisan
{
StringBuffer strSql = new StringBuffer();
Object aCondiciones[]=null;
int intUltimoValorNumseq = 1;
try
{
/*
strSql.append("SELECT MAX(numseq) as max_numseq");
strSql.append(" FROM tamovext");
strSql.append(" WHERE medico=?");*/
/*
* Cambiamos a una secuencia por que estamos viendo que hay veces que no incrementa bien el contador
* // select NUMSEQ_SEQ.nextval from dual;
*/
/*strSql.append("select NUMSEQ_SEQ.nextval as MAXNUMSEQ");
strSql.append(" FROM dual");*/
String sql = "select NUMSEQ_SEQ.nextval as MAXNUMSEQ FROM dual";
aCondiciones = new Object[1];
aCondiciones[0]=Integer.valueOf(medico);
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, sql/*, aCondiciones*/);
//nos posicionamos en el registro
if (rs.next())
//obtenemos el campo PARAM1
intUltimoValorNumseq = rs.getInt("MAXNUMSEQ");
rs.close();
ParametrosConfiguracion.dataStore.liberarConexion(conexion);
LogTarisan.logger.log(NivelLog.DEBUG, "Seleccionado el valor " + intUltimoValorNumseq + " del campo NUMSEQ de la tabla TAMOVEXT (" + strSql.toString() + ")");
}
catch (ExcepcionTarisan sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la selección de TAMOVEXT: " + sqle + " (" + strSql.toString() + ")");
throw (ExcepcionTarisan)sqle;
}
catch(SQLException sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la selección de TAMOVEXT: " + sqle + " (" + strSql.toString() + ")");
throw new ExcepcionTarisan(sqle.getMessage());
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la selección de TAMOVEXT: " + sqle + " (" + strSql.toString() + ")");
throw new ExcepcionTarisan(sqle.getMessage());
}
catch(Throwable sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la selección de TAMOVEXT: " + sqle + " (" + strSql.toString() + ")");
throw new ExcepcionTarisan(sqle.getMessage());
}
return intUltimoValorNumseq;
}
/**
* Añade a la tabla TAMOVEXT un nuevo registro según la sentencia INSERT indicada.
* @param sqlSelect La sentencia INSERT.
* @param aValores Array que contiene los valores de los parámetros utilizados en la sentencia.
* @throws
*/
public void insertarMovimiento(String sqlSelect, Object[] aValores) throws ExcepcionTarisan
{
try
{
ParametrosConfiguracion.dataStore.insertar(sqlSelect, aValores);
}
catch (ExcepcionTarisan sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la insercion de TAMOVEXT: " + sqle + " (" + sqlSelect + ")");
throw (ExcepcionTarisan)sqle;
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la insercion de TAMOVEXT: " + sqle + " (" + sqlSelect + ")");
throw new ExcepcionTarisan(sqle.getMessage());
}
catch(Throwable sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la insercion de TAMOVEXT: " + sqle + " (" + sqlSelect + ")");
throw new ExcepcionTarisan(sqle.getMessage());
}
}
/**
* Añade a la tabla TAMOVEXT un nuevo registro según la sentencia INSERT indicada.
* @param sqlSelect La sentencia INSERT.
* @param aValores Array que contiene los valores de los parámetros utilizados en la sentencia.
* @param conexion La conexión por la que se ejecuta la sentencia. Utilizado para el uso transacciones.
* @throws
*/
public void insertarMovimiento(String sqlSelect, Object[] aValores, Connection conexion) throws ExcepcionTarisan
{
try
{
ParametrosConfiguracion.dataStore.insertar(sqlSelect, aValores, conexion);
}
catch (ExcepcionTarisan sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la insercion de TAMOVEXT: " + sqle + " (" + sqlSelect + ")");
throw (ExcepcionTarisan)sqle;
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la insercion de TAMOVEXT: " + sqle + " (" + sqlSelect + ")");
throw new ExcepcionTarisan(sqle.getMessage());
}
catch(Throwable sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la insercion de TAMOVEXT: " + sqle + " (" + sqlSelect + ")");
throw new ExcepcionTarisan(sqle.getMessage());
}
}
/**
* Busca un movimiento en tamovex, devuelve cierto si lo encuentra
* @param aCondiciones
* @param conexion
* @return
*/
public boolean buscarDuplicadoTamovext(Object[] aCondiciones, Connection conexion)
{
boolean resultado = false;
StringBuffer strSql = new StringBuffer();
strSql.append("Select * from TAMOVEXT");
strSql.append("where MEDICO = ?");
strSql.append("and ESPECIALIDAD = ?");
strSql.append("and ACTO = ?");
strSql.append("and COLECTIVO = ?");
strSql.append("and POLIZA = ?");
strSql.append("and ORDEN = ?");
strSql.append("and FECHA = ?");
try
{
ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, strSql.toString(), aCondiciones);
if(rs.next())
resultado = true;
}
catch (ExcepcionTarisan sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la insercion de TAMOVEXT: " + sqle + " (" + strSql + ")");
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la insercion de TAMOVEXT: " + sqle + " (" + strSql + ")");
}
return resultado;
}
public boolean insertarTamovext(Object[] aValores, Connection conexion)
{
boolean resultado = false;
StringBuffer strSql = new StringBuffer();
strSql.append("INSERT INTO TAMOVEXT");
strSql.append(" (MEDICO, ESPECIALIDAD, ACTO, COLECTIVO, POLIZA, ORDEN, FECHA, PRECIO, PRESCRIPTOR, ENTIDAD, TARJETA_CHIPCARD, AUTORIZACION, ESPECIALIDAD_CHIPCARD, ACTO_CHIPCARD)");
strSql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
try
{
ParametrosConfiguracion.dataStore.insertar(strSql.toString(), aValores, conexion);
resultado = true;
}
catch (ExcepcionTarisan sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la insercion de TAMOVEXT: " + sqle + " (" + strSql + ")");
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la insercion de TAMOVEXT: " + sqle + " (" + strSql + ")");
}
return resultado;
}
public boolean insertarTamovextDesplazado(Object[] aValores, Connection conexion)
{
boolean resultado = false;
StringBuffer strSql = new StringBuffer();
strSql.append("INSERT INTO TAMOVEXT");
strSql.append(" (MEDICO, ESPECIALIDAD, ACTO, COLECTIVO, POLIZA, ORDEN, FECHA, PRECIO, PRESCRIPTOR, TARJETA_CHIPCARD, ESPECIALIDAD_CHIPCARD, ACTO_CHIPCARD, TALON_CHIPCARD, ENTIDAD, AUTORIZACION)");
strSql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
try
{
resultado = ParametrosConfiguracion.dataStore.insertar(strSql.toString(), aValores, conexion);
}
catch (ExcepcionTarisan sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la insercion de TAMOVEXT: " + sqle + " (" + strSql + ")");
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la insercion de TAMOVEXT: " + sqle + " (" + strSql + ")");
}
return resultado;
}
public boolean tieneRevisionHoy(int medico, String tarjeta, boolean chipcard)
{
boolean resul = false;
StringBuffer strSql = new StringBuffer();
java.sql.Date intUltimoValorNumseq = null;
java.sql.Date dtFecha = null;
Object[] aCondiciones;
try
{
strSql.append("SELECT fecha");
strSql.append(" FROM tamovext");
strSql.append(" WHERE medico="+medico);
strSql.append(" and tarjeta_chipcard='"+tarjeta+"'");
strSql.append(" and acto in (1,2)");
Calendar calFechaActual = Calendar.getInstance();
int mes = calFechaActual.get(Calendar.MONTH)+1;
String strhoy = calFechaActual.get(Calendar.DAY_OF_MONTH)+"/"+mes+"/"+calFechaActual.get(Calendar.YEAR);
strSql.append(" and fecha = to_date('"+strhoy+"', 'dd/mm/yyyy')");
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
LogTarisan.logger.log(NivelLog.DEBUG, "Consulta: "+strSql.toString());
ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, strSql.toString());
//nos posicionamos en el registro
if (rs.next())
//obtenemos el campo PARAM1
resul = true;
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la selección de TAMOVEXT (TIENE REVISION DESPLAZADO): " + sqle + " (" + strSql.toString() + ")");
} catch (ExcepcionTarisan e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resul;
}
public boolean tieneRevisionHoyColPolOrd(int medico, long colectivo, double poliza, int beneficiario, boolean chipcard)
{
boolean resul = false;
StringBuffer strSql = new StringBuffer();
java.sql.Date intUltimoValorNumseq = null;
java.sql.Date dtFecha = null;
Object[] aCondiciones;
try
{
strSql.append("SELECT fecha");
strSql.append(" FROM tamovext");
strSql.append(" WHERE medico="+medico);
strSql.append(" and colectivo="+colectivo);
strSql.append(" and poliza="+poliza);
strSql.append(" and orden="+beneficiario);
strSql.append(" and acto in (1,2)");
Calendar calFechaActual = Calendar.getInstance();
int mes = calFechaActual.get(Calendar.MONTH)+1;
String strhoy = calFechaActual.get(Calendar.DAY_OF_MONTH)+"/"+mes+"/"+calFechaActual.get(Calendar.YEAR);
strSql.append(" and fecha = to_date('"+strhoy+"', 'dd/mm/yyyy')");
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
LogTarisan.logger.log(NivelLog.DEBUG, "Consulta: "+strSql.toString());
ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, strSql.toString());
//nos posicionamos en el registro
if (rs.next())
//obtenemos el campo PARAM1
resul = true;
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la selección de TAMOVEXT (TIENE REVISION DESPLAZADO): " + sqle + " (" + strSql.toString() + ")");
} catch (ExcepcionTarisan e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resul;
}
/**
* Comprueba si un asegurado determinado ya ha tenido consulta de "chipcard" para un médico determinado.
*
* @param medico el médico para el que buscamos
* @param tarjeta la tarjeta chipcard que buscamos
* @return cierto si hay acto "consulta" en tamovext
*/
public boolean tieneConsultaChipcard(int medico, String tarjeta)
{
boolean resul = false;
StringBuffer strSql = new StringBuffer();
Object[] aCondiciones;
try
{
strSql.append("Select count(*) COUNT from tamovext");
strSql.append(" where medico = ?");
strSql.append(" and tarjeta_chipcard=?");
strSql.append(" and acto_chipcard=1");
aCondiciones = new Object[2];
aCondiciones[0] = Integer.valueOf(medico);
aCondiciones[1] = new String(tarjeta);
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, strSql.toString(), aCondiciones);
if(rs.next())
{
if(rs.getInt("COUNT")>0)
resul = true;
}
rs.close();
ParametrosConfiguracion.dataStore.liberarConexion(conexion);
LogTarisan.logger.log(NivelLog.DEBUG, "La tarjeta "+tarjeta+" tiene consulta en tamovext para el medico "+medico+": "+resul);
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la selección de TAMOVEXT (PRIMERA VISITA DESPLAZADO): " + sqle + " (" + strSql.toString() + ")");
} catch (ExcepcionTarisan e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resul;
}
public boolean esPrimeraVisitaDesplazado(int medico, String tarjeta, boolean chipcard)
{
boolean resul = false;
StringBuffer strSql = new StringBuffer();
java.sql.Date intUltimoValorNumseq = null;
java.sql.Date dtFecha = null;
Object[] aCondiciones;
try
{
strSql.append("SELECT fecha");
strSql.append(" FROM tamovext");
strSql.append(" WHERE medico=?");
strSql.append(" and tarjeta_chipcard=?");
strSql.append(" and acto in (1)");
aCondiciones = new Object[2];
aCondiciones[0]=Integer.valueOf(medico);
aCondiciones[1]=new String(tarjeta);
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, strSql.toString(), aCondiciones);
//nos posicionamos en el registro
if (rs.next())
//obtenemos el campo PARAM1
dtFecha = rs.getDate("fecha");
rs.close();
ParametrosConfiguracion.dataStore.liberarConexion(conexion);
//YA TENEMOS LA FECHA DE LA ÚLTIMA VISITA (O NULL SI NO HA VENIDO NUNCA)
if (dtFecha!=null) //existe registro. Se comprueba si es la primera visita
{
Calendar calFechaPrimeraVisita = Calendar.getInstance();
calFechaPrimeraVisita.setTime(dtFecha);
Calendar calFechaActual = Calendar.getInstance();
if (ParametrosConfiguracion.periodoNatural.equalsIgnoreCase("si")) //se trata de un periodo natural
{
if (calFechaActual.get(Calendar.YEAR)>calFechaPrimeraVisita.get(Calendar.YEAR)) { //años diferentes
resul = true;
} else {
if (calFechaActual.get(Calendar.YEAR)==calFechaPrimeraVisita.get(Calendar.YEAR )) //mismo año
{
//le sumamos a la fecha obtenida el periodo de meses para conocer si es primera visita
if(chipcard)
calFechaPrimeraVisita.add(Calendar.MONTH, ParametrosConfiguracion.periodoPrimeraVisitaChipcard);
else
calFechaPrimeraVisita.add(Calendar.MONTH, ParametrosConfiguracion.periodoPrimeraVisita);
if ( calFechaPrimeraVisita.getTime().compareTo( calFechaActual.getTime() ) < 0 ) { //la fecha de la primera visita mas el periodo es inferior a la fecha actual
resul = true;
}
}
}
}
else //se trata de un periodo no natural
{
//le sumamos a la fecha obtenida el periodo de meses para conocer si es primera visita
calFechaPrimeraVisita.add(Calendar.MONTH, ParametrosConfiguracion.periodoPrimeraVisita);
if ( calFechaPrimeraVisita.getTime().compareTo( calFechaActual.getTime() ) < 0 ) //la fecha de la primera visita mas el periodo es inferior a la fecha actual
resul = true;
}
} else { //No existe registro. Es primera visita
resul = true;
}
LogTarisan.logger.log(NivelLog.DEBUG, "Es primera visita DESPLAZADO: " + resul +" (" + strSql.toString() + ")");
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la selección de TAMOVEXT (PRIMERA VISITA DESPLAZADO): " + sqle + " (" + strSql.toString() + ")");
} catch (ExcepcionTarisan e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resul;
}
public Vector ObtenerMovimientosMismaPolizaYMedico(int medico, int espe, int acto, int entidad, long col, long pol, int ord)
{
Vector resul = new Vector();;
StringBuffer sqlSelect = new StringBuffer();
Object[] aCondiciones = new Object[7];
try
{
sqlSelect.append("Select * from tamovext_espia ");
sqlSelect.append("where medico = ? ");
sqlSelect.append("and especialidad = ? ");
sqlSelect.append("and acto = ? ");
sqlSelect.append("and entidad = ? ");
sqlSelect.append("and colectivo = ? ");
sqlSelect.append("and poliza = ? ");
sqlSelect.append("and orden = ? ");
sqlSelect.append("order by fecha desc ");
aCondiciones[0] = Integer.valueOf(medico);
aCondiciones[1] = Integer.valueOf(espe);
aCondiciones[2] = Integer.valueOf(acto);
aCondiciones[3] = Integer.valueOf(entidad);
aCondiciones[4] = Long.valueOf(col);
aCondiciones[5] = Long.valueOf(pol);
aCondiciones[6] = Integer.valueOf(ord);
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, sqlSelect.toString(), aCondiciones);
while (rs.next())
{
Tamovext ta = new Tamovext();
ta.setMedico(rs.getInt("MEDICO"));
ta.setEspecialidad(rs.getInt("ESPECIALIDAD"));
ta.setActo(rs.getInt("ACTO"));
ta.setNumseq(rs.getInt("NUMSEQ"));
ta.setEntidad(rs.getInt("ENTIDAD"));
ta.setColec(rs.getInt("COLECTIVO"));
ta.setPoliza(rs.getDouble("POLIZA"));
ta.setOrden(rs.getInt("ORDEN"));
ta.setFecha(rs.getDate("FECHA"));
ta.setPrecio(rs.getDouble("PRECIO"));
ta.setPrescriptor(rs.getInt("PRESCRIPTOR"));
ta.setAutorizacion(rs.getLong("AUTORIZACION"));
/*
* TODO faltan los de chipcard
*/
resul.add(ta);
}
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la selección de TAMOVEXT obteniendo movimientos previos de la misma poliza y medico");
} catch (ExcepcionTarisan e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resul;
}
public Vector ObtenerMovimientosAutorizacion(long autorizacion)
{
Vector resul = new Vector();;
StringBuffer sqlSelect = new StringBuffer();
Object[] aCondiciones = new Object[1];
try
{
sqlSelect.append("Select * from tamovext_espia ");
sqlSelect.append("where autorizacion = ? ");
sqlSelect.append("order by fecha desc ");
aCondiciones[0] = Long.valueOf(autorizacion);
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, sqlSelect.toString(), aCondiciones);
while (rs.next())
{
Tamovext ta = new Tamovext();
ta.setMedico(rs.getInt("MEDICO"));
ta.setEspecialidad(rs.getInt("ESPECIALIDAD"));
ta.setActo(rs.getInt("ACTO"));
ta.setNumseq(rs.getInt("NUMSEQ"));
ta.setEntidad(rs.getInt("ENTIDAD"));
ta.setColec(rs.getInt("COLECTIVO"));
ta.setPoliza(rs.getDouble("POLIZA"));
ta.setOrden(rs.getInt("ORDEN"));
ta.setFecha(rs.getDate("FECHA"));
ta.setPrecio(rs.getDouble("PRECIO"));
ta.setPrescriptor(rs.getInt("PRESCRIPTOR"));
ta.setAutorizacion(rs.getLong("AUTORIZACION"));
/*
* TODO faltan los de chipcard
*/
resul.add(ta);
}
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "Error en la selección de TAMOVEXT obteniendo movimientos previos de la misma poliza y medico");
} catch (ExcepcionTarisan e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resul;
}
public Vector AnaliticasCapturadasTotal(int medico)
{
return this.AnaliticasCapturadas(medico, null, null);
}
public Vector AnaliticasCapturadasDesde(int medico, java.sql.Date fecini)
{
return this.AnaliticasCapturadas(medico, fecini, null);
}
public Vector AnaliticasCapturadasHasta(int medico, java.sql.Date fecfin)
{
return this.AnaliticasCapturadas(medico, null, fecfin);
}
public Vector