Files
2025-06-09 13:37:06 +02:00

477 lines
18 KiB
Java

package com.tarisan.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Vector;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import com.tarisan.control.Constantes;
import com.tarisan.control.Paginacion;
import com.tarisan.control.ParametrosConfiguracion;
import com.tarisan.control.PersistenciaPaciente;
import com.tarisan.control.PersistenciaTamedico;
import com.tarisan.control.PersistenciaTapolfac;
import com.tarisan.data.Paciente;
import com.tarisan.data.Tamedico;
import com.tarisan.data.Tarjeta;
import com.tarisan.data.Ttactmed;
import com.tarisan.data.Usuario;
import com.tarisan.excepcion.ExcepcionTarisan;
import com.tarisan.log.LogTarisan;
import com.tarisan.log.NivelLog;
import com.tarisan.util.Utilidades;
public class WebServiceEntidad extends HttpServlet
{
public void dogET(HttpServletRequest req, HttpServletResponse res) throws IOException
{
this.doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException
{
Integer terminal = -1;
Integer acto = -1;
Integer especialidad = -1;
String tarjeta = null;
String xml = "";
if (req.getParameter("medico") != null) {
terminal = Integer.parseInt(req.getParameter("medico"));
}
if (req.getParameter("acto") != null) {
acto = Integer.parseInt(req.getParameter("acto"));
}
if (req.getParameter("tarjeta") != null) {
tarjeta = req.getParameter("tarjeta");
}
if (req.getParameter("especialidad") != null) {
especialidad = Integer.parseInt(req.getParameter("especialidad"));
}
xml = this.responder(req, res, tarjeta, terminal, acto, especialidad);
res.setContentType("text/xml");
PrintWriter out = res.getWriter();
out.println(xml);
out.close();
}
public String responder(HttpServletRequest req, HttpServletResponse res, String valorTarjeta, Integer terminal, Integer acto, Integer especialidad) throws IOException
{
/*
* TARJETA_VALIDA = 0;
* TARJETA_CADUCADA = 1;
* TARJETA_BAJA = 2;
* TARJETA_SUSPENSO = 3;
* ACTO NO PRESCRIBIBLE = 4;
* ESPECIALIDAD NO PERMITIDA = 5;
* ACTO PRESCRIBILBLE = 6;
*/
String xmlRespuesta = "";
String mensajeRespuesta = "DENEGADA";
long codigoAut = 0;
String descripcionCodigoRespuesta = "";
Integer codigoRespuesta = -1; // ERROR la tarjeta no es nuestra
Calendar fec = Calendar.getInstance();
Timestamp fecha = new Timestamp(fec.getTimeInMillis());
boolean esNuestra = false;
Paciente paciente = new Paciente();
Tarjeta tarjeta = new Tarjeta();
try {
esNuestra = this.tarjetaNuestra(valorTarjeta);
if(esNuestra){
LogTarisan.logger.log(NivelLog.DEBUG, "WebServiceEntidad - La tarjeta es nuestra. valorTarjeta.substring(2,8)->"+valorTarjeta.substring(2,8));
String sMensaje="";
tarjeta = GestorPacientes.parsearTarjeta_pistas1_2(valorTarjeta);
LogTarisan.logger.log(NivelLog.DEBUG, "WebServiceEntidad - Se han cargado los datos bien. tarjeta: "+tarjeta.getTarjeta()+", contrato: "+tarjeta.getContrato()+", colectivo: "+tarjeta.getColectivo()+", poliza: "+tarjeta.getPoliza()+", orden"+tarjeta.getBeneficiario()+", entidad_chipcard: "+tarjeta.getEntidadChipcard()+"...");
if(tarjeta.getValida() && tarjeta.getEntidadChipcard().compareTo(ParametrosConfiguracion.bin_chipcard_propios)==0)
{
PersistenciaPaciente per = new PersistenciaPaciente();
Object objetoSeleccionado = null;
try
{
objetoSeleccionado = per.seleccionarColPolOrd(tarjeta, terminal);
paciente = (Paciente)objetoSeleccionado;
LogTarisan.logger.log(NivelLog.INFO, "WebServiceEntidad - Tarjeta de paciente válida: " + tarjeta);
LogTarisan.logger.log(NivelLog.INFO, "WebServiceEntidad - Paciente: " + paciente.getNombre());
paciente.setDesplazado(ParametrosConfiguracion.bin_chipcard_propios);
Integer contrato = paciente.getTarjeta().getContrato();
Long colectivo = paciente.getTarjeta().getColectivo();
Long poliza = paciente.getTarjeta().getPoliza();
Integer tarifa = 0;
if (this.especialidadPermitida(especialidad)){
if (this.ActoPrescribible(especialidad, contrato, colectivo, poliza, tarifa, acto)){
codigoRespuesta = 6; // OK acto prescribilbe
}else{
codigoRespuesta = 4; // EROR acto no cubierto
}
}else{
codigoRespuesta = 5; // ERROR especialidad no permitida
}
}
catch(ClassCastException ex)
{
Integer nMensaje = (Integer)objetoSeleccionado;
sMensaje = GestorPacientes.obtenerMensaje(nMensaje);
codigoRespuesta = nMensaje;
LogTarisan.logger.log(NivelLog.INFO, "WebServiceEntidad - ERROR - "+sMensaje);
//res.sendRedirect("../jsp/error.jsp" );
}
}
}else{
LogTarisan.logger.log(NivelLog.DEBUG, "WebServiceEntidad - ERROR - La tarjeta NO es nuestra. valorTarjeta.substring(2,8)->"+valorTarjeta.substring(2,8));
}
} catch (ExcepcionTarisan e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//GENERAMOS EN XML COMO STRING
if (codigoRespuesta==6){
mensajeRespuesta = "AUTORIZADA";
}
codigoAut = this.obtenerCodigoAut();
descripcionCodigoRespuesta =this.obtenerMensajeRespuesta(codigoRespuesta);
xmlRespuesta = "<xml version=\"1.0\" encoding=\"UTF-8\">"+
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"+
"<soapenv:Body>"+
"<requestService xmlns=\"https://tarisan.imqnavarra.com/tarisan/servlet/WebServiceEntidad\">"+
"<ObjetoRespuesta>"+
"<Respuesta>"+
"<CodigoRespuesta>"+
codigoRespuesta+
"</CodigoRespuesta>"+
"<MensajeRespuesta>"+
mensajeRespuesta+
"</MensajeRespuesta>"+
"<DescripcionRespuesta>"+
descripcionCodigoRespuesta+
"</DescripcionRespuesta>"+
"<Asegurado>"+
"<Nombre>"+
paciente.getNombre()+
"</Nombre>"+
"<LecturaTarjeta>"+
valorTarjeta+
"</LecturaTarjeta>"+
"</Asegurado>"+
"<Autorizacion>"+
"<CodAut>"+
codigoAut+
"</CodAut>"+
"<EspeAut>"+
especialidad+
"</EspeAut>"+
"<ActoAut>"+
acto+
"</ActoAut>"+
"<FechaAut>"+
fecha+
"</FechaAut>"+
"</Autorizacion>"+
"<Terminal>"+
terminal+
"</Terminal>"+
"</Respuesta>"+
"</ObjetoRespuesta>"+
"</requestService>"+
"</soapenv:Body>"+
"</soapenv:Envelope> "+
"</xml>";
insertarPeticion(codigoAut, terminal, especialidad, acto, tarjeta.getTarjetaDesplazado());
insertarMovimiento(codigoAut, tarjeta.getTarjetaDesplazado(), mensajeRespuesta, descripcionCodigoRespuesta, terminal, '"'+xmlRespuesta+'"', especialidad, acto);
return xmlRespuesta;
}
private boolean tarjetaNuestra(String valorTarjeta) throws ExcepcionTarisan, IOException{
boolean resultado = false;
try{
if(valorTarjeta.contains(ParametrosConfiguracion.bin_chipcard_propios)){
resultado = true;
}
}
catch(Exception ex){
throw new ExcepcionTarisan(ex.toString());
}
return resultado;
}
private boolean especialidadPermitida(Integer especialidad){
boolean resp = false;
String descripcion = "";
try
{
String sqlSelect = "select * from wsespecialidad where especialidad="+especialidad;
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, sqlSelect);
if(rs.next())
{
resp = true;
descripcion = rs.getString("DESCRIPCION");
LogTarisan.logger.log(NivelLog.DEBUG, "WebServiceEntidad - OK - La especialidad seleccionada es: "+descripcion);
}else{
LogTarisan.logger.log(NivelLog.DEBUG, "WebServiceEntidad - ERROR - Especialidad("+especialidad+") no permitida");
}
rs.close();
ParametrosConfiguracion.dataStore.liberarConexion(conexion);
}
catch (ExcepcionTarisan et) {
et.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return resp;
}
public boolean ActoPrescribible(int especialidad, int contratoPoliza, long colectivo, long poliza, int tarifa, Integer acto) throws ExcepcionTarisan
{
boolean resultado = false;
StringBuffer strSql = new StringBuffer();
Object[] aCondiciones = null;
try
{
//Preparar SELECT
strSql.append("SELECT TTACTMED.DESCRIPCION, TTACTMED.ACTO, TATARIFA.IMPORTE AS PRECIO");
strSql.append(" FROM TTACTMED, TATARIFA");
strSql.append(" WHERE TTACTMED.ACTO = TATARIFA.ACTO");
strSql.append(" AND TTACTMED.ESPECIALIDAD = TATARIFA.ESPECIALIDAD");
strSql.append(" AND TATARIFA.TARIFA = ?");
strSql.append(" AND TATARIFA.IMPORTE > 0");
strSql.append(" AND TTACTMED.ESPECIALIDAD = ?");
strSql.append(" AND TTACTMED.AUTOPRESCRIPCION = 'S'");
strSql.append(" AND TTACTMED.ACTO <> ?");
strSql.append(" AND TTACTMED.ACTO = ?");
strSql.append(" AND TTACTMED.ACTO IN");
//SUBSELECT
//INCLUIDOS EN TADERPOL
strSql.append(" (");
strSql.append(" SELECT ACTO FROM TADERPOL WHERE TADERPOL.COLECTIVO = ? AND TADERPOL.POLIZA = ? AND TADERPOL.ESPECIALIDAD = ? AND EXCLUIDO = 0");
strSql.append(" UNION");
//INCLUIDOS EN TADERCOL NO EXCLUIDOS EN TADERPOL
strSql.append(" SELECT ACTO FROM TADERCOL WHERE TADERCOL.COLECTIVO = ? AND TADERCOL.ESPECIALIDAD = ? AND EXCLUIDO = 0 AND ACTO NOT IN");
//EXCLUIDOS EN TADERPOL
strSql.append(" (SELECT ACTO FROM TADERPOL WHERE TADERPOL.COLECTIVO = ? AND TADERPOL.POLIZA = ? AND TADERPOL.ESPECIALIDAD = ? AND EXCLUIDO = 1)");
strSql.append(" UNION");
//INCLUIDOS EN TADERCAC Y NO EXCLUIDOS EN TADERCOL Y TADERPOL
strSql.append(" SELECT ACTO FROM TADERCAC WHERE TADERCAC.CONTRATO = ? AND TADERCAC.ESPECIALIDAD = ? AND EXCLUIDO = 0 AND ACTO NOT IN");
//EXCLUIDOS DE TADERCOL Y TADERPOL
//EXCLUIDOS EN TADERPOL
strSql.append(" (SELECT ACTO FROM TADERPOL WHERE TADERPOL.COLECTIVO = ? AND TADERPOL.POLIZA = ? AND TADERPOL.ESPECIALIDAD = ? AND EXCLUIDO = 1");
strSql.append(" UNION");
//EXCLUIDOS EN TADERCOL NO INCLUIDOS EN TADERPOL
strSql.append(" SELECT ACTO FROM TADERCOL WHERE TADERCOL.COLECTIVO = ? AND TADERCOL.ESPECIALIDAD = ? AND EXCLUIDO = 1 AND ACTO NOT IN");
//EXCLUIDOS EN TADERPOL
strSql.append(" (SELECT ACTO FROM TADERPOL WHERE TADERPOL.COLECTIVO = ? AND TADERPOL.POLIZA = ? AND TADERPOL.ESPECIALIDAD = ? AND EXCLUIDO = 0)");
strSql.append(" )");
strSql.append(" )");
aCondiciones = new Object[22];
//Preparar condiciones
aCondiciones[0] = Integer.valueOf(tarifa);
aCondiciones[1] = Integer.valueOf(especialidad);
aCondiciones[2] = Integer.valueOf(993999);
aCondiciones[3] = Integer.valueOf(acto);
aCondiciones[4] = Long.valueOf(colectivo);
aCondiciones[5] = Double.valueOf(poliza);
aCondiciones[6] = Integer.valueOf(especialidad);
aCondiciones[7] = Long.valueOf(colectivo);
aCondiciones[8] = Integer.valueOf(especialidad);
aCondiciones[9] = Long.valueOf(colectivo);
aCondiciones[10] = Double.valueOf(poliza);
aCondiciones[11] = Integer.valueOf(especialidad);
aCondiciones[12] = Integer.valueOf(contratoPoliza);
aCondiciones[13] = Integer.valueOf(especialidad);
aCondiciones[14] = Long.valueOf(colectivo);
aCondiciones[15] = Double.valueOf(poliza);
aCondiciones[16] = Integer.valueOf(especialidad);
aCondiciones[17] = Long.valueOf(colectivo);
aCondiciones[18] = Integer.valueOf(especialidad);
aCondiciones[19] = Long.valueOf(colectivo);
aCondiciones[20] = Double.valueOf(poliza);
aCondiciones[21] = Integer.valueOf(especialidad);
//Conexión
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
strSql.append(" order by ttactmed.acto");
ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, strSql.toString(), aCondiciones);
//Rellenamos en vector
if(rs.next())
{
resultado = true;
LogTarisan.logger.log(NivelLog.DEBUG, "WebServiceEntidad - OK - Acto("+acto+") para la especialidad("+especialidad+") permitido");
}else{
LogTarisan.logger.log(NivelLog.DEBUG, "WebServiceEntidad - ERROR - El acto("+acto+") para la especialidad("+especialidad+") NO está permitido");
}
//Cerrar ResultSet
rs.close();
//Liberar Conexión
ParametrosConfiguracion.dataStore.liberarConexion(conexion);
}
catch (ExcepcionTarisan sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "WebServiceEntidad - ActoPrescribible - Error en la selección de TTACTMED: " + Utilidades.obtenerSentenciaSQL(strSql, null, aCondiciones) );
throw (ExcepcionTarisan)sqle;
}
catch(SQLException sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "WebServiceEntidad - ActoPrescribible - Error en la selección de TTACTMED: " + Utilidades.obtenerSentenciaSQL(strSql, null, aCondiciones) );
throw new ExcepcionTarisan(sqle.getMessage());
}
catch(Exception sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "WebServiceEntidad - ActoPrescribible - Error en la selección de TTACTMED: " + Utilidades.obtenerSentenciaSQL(strSql, null, aCondiciones) );
throw new ExcepcionTarisan(sqle.getMessage());
}
catch(Throwable sqle)
{
LogTarisan.logger.log(NivelLog.ERROR, "WebServiceEntidad - ActoPrescribible - Error en la selección de TTACTMED: " + sqle + " (" + strSql.toString() + ")");
throw new ExcepcionTarisan(sqle.getMessage());
}
return resultado;
}
private Long obtenerCodigoAut(){
long codigoAut = 0;
try
{
String sqlSelect = "SELECT autorizacion_ws.nextval as auto FROM DUAL";
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, sqlSelect);
if(rs.next())
{
codigoAut = rs.getLong("auto");
LogTarisan.logger.log(NivelLog.DEBUG, "WebServiceEntidad - OK - El código del movimiento es: "+codigoAut);
}else{
LogTarisan.logger.log(NivelLog.DEBUG, "WebServiceEntidad - ERROR - Erro al obtener el código del movimiento");
}
rs.close();
ParametrosConfiguracion.dataStore.liberarConexion(conexion);
}
catch (ExcepcionTarisan et) {
et.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return codigoAut;
}
private String obtenerMensajeRespuesta(Integer codigoRespuesta){
String mensaje = "";
try
{
String sqlSelect = "select DESCRIPCION from wsmensajes where codigo="+codigoRespuesta;
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, sqlSelect);
if(rs.next())
{
mensaje = rs.getString("DESCRIPCION");
LogTarisan.logger.log(NivelLog.DEBUG, "WebServiceEntidad - OK - El mensaje para el codigo("+codigoRespuesta+") es: "+mensaje);
}else{
LogTarisan.logger.log(NivelLog.DEBUG, "WebServiceEntidad - ERROR - Error al obtener el mensaje de respuesta: " +sqlSelect);
}
rs.close();
ParametrosConfiguracion.dataStore.liberarConexion(conexion);
}
catch (ExcepcionTarisan et) {
et.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return mensaje;
}
private void insertarPeticion(Long autorizacion, Integer medico, Integer especialidad, Integer acto, String tarjeta_chipcard)
{
String sqlInsert = "";
Object[] aValores = new Object[5];
try {
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
sqlInsert = "Insert into wspeticiones (autorizacion, medico, especialidad, acto, tarjeta_chipcard, fecha_hora) values (?, ?, ?, ?, ?, sysdate)";
aValores[0]=Long.valueOf(autorizacion);
aValores[1]=Integer.valueOf(medico);
aValores[2]=Integer.valueOf(especialidad);
aValores[3]=Integer.valueOf(acto);
aValores[4]=new String(tarjeta_chipcard);
ParametrosConfiguracion.dataStore.insertar(sqlInsert, aValores, conexion);
LogTarisan.logger.log(NivelLog.DEBUG, "WebService - OK - Peticion insertada correctamente: "+sqlInsert);
} catch (ExcepcionTarisan e) {
LogTarisan.logger.log(NivelLog.ERROR, "WebService - ERROR - Error al insertar el registro de wspeticiones: " +sqlInsert);
}
}
private void insertarMovimiento(Long autorizacion, String tarjeta, String respuesta, String mensaje, Integer terminal, String xml, Integer especialidad, Integer acto)
{
String sqlInsert = "";
Object[] aValores = new Object[8];
try {
Connection conexion = ParametrosConfiguracion.dataStore.obtenerConexion();
sqlInsert = "Insert into wsmovimientos (autorizacion, tarjeta, respuesta, mensaje, terminal, xml, especialidad, acto, fecha_hora) values (?, ?, ?, ?, ?, ?, ?, ?, SYSDATE)";
aValores[0]=Long.valueOf(autorizacion);
aValores[1]=new String(tarjeta);
aValores[2]=new String(respuesta);
aValores[3]=new String(mensaje);
aValores[4]=Integer.valueOf(terminal);
aValores[5]=new String(xml);
aValores[6]=Integer.valueOf(especialidad);
aValores[7]=Integer.valueOf(acto);
ParametrosConfiguracion.dataStore.insertar(sqlInsert, aValores, conexion);
LogTarisan.logger.log(NivelLog.DEBUG, "WebService - OK - Movimiento insertado correctamente: "+sqlInsert);
} catch (ExcepcionTarisan e) {
LogTarisan.logger.log(NivelLog.ERROR, "WebService - ERROR - Error al insertar el registro de wsmovimientos: " +sqlInsert);
}
}
}