/** * @(#) */ package com.tarisan.control; import java.sql.Connection; import java.sql.Date; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Calendar; import com.tarisan.data.RespuestaChipcard; import com.tarisan.excepcion.ExcepcionTarisan; import com.tarisan.log.LogTarisan; import com.tarisan.log.NivelLog; import com.tarisan.util.Utilidades; public class PersistenciaRespuestaChipcard { public RespuestaChipcard parsearRespuesta(String response) { RespuestaChipcard resul = new RespuestaChipcard(); if(response != null) { LogTarisan.logger.log(NivelLog.DEBUG, "Llega 14: "+response); String campo = ""; if(response.contains("")+1, response.indexOf("")); resul.set_mensaje(campo); } else { resul.set_error(false); campo = response.substring(response.indexOf(""), response.indexOf("")); LogTarisan.logger.log(NivelLog.DEBUG, "Llega 18"); resul.set_tarjeta(campo); campo = ""; campo = response.substring(response.indexOf(""), response.indexOf("")); LogTarisan.logger.log(NivelLog.DEBUG, "Llega 22"); resul.set_respuesta(campo); campo = ""; campo = response.substring(response.indexOf(""), response.indexOf("")); LogTarisan.logger.log(NivelLog.DEBUG, "Llega 26"); resul.set_mensaje(campo); campo = ""; resul.set_talon_chipcard(Integer.parseInt(response.substring(response.indexOf("")+8, response.indexOf("")))); LogTarisan.logger.log(NivelLog.DEBUG, "Llega 30"); campo = ""; resul.set_terminal(Integer.parseInt(response.substring(response.indexOf("")+6, response.indexOf("")))); LogTarisan.logger.log(NivelLog.DEBUG, "Llega 33"); campo = ""; resul.set_especialidad(Integer.parseInt(response.substring(response.indexOf("")+7, response.indexOf("")))); LogTarisan.logger.log(NivelLog.DEBUG, "Llega 53"); campo = ""; resul.set_acto(Integer.parseInt(response.substring(response.indexOf("")+6, response.indexOf("")))); LogTarisan.logger.log(NivelLog.DEBUG, "Llega 56"); campo = ""; // 2013-11-27-09.14.45.100121 String tiempo = response.substring(response.indexOf("")+11, response.indexOf("")); LogTarisan.logger.log(NivelLog.DEBUG, "Llega 61. tiempo: "+tiempo); Calendar cal = Calendar.getInstance(); int anio = 0; int mes = 0; int dia = 0; int hora=0; int min=0; int sec=0; //cal.set(year, month, date, hourOfDay, minute, second); anio = Integer.valueOf(tiempo.substring(0, 4)); LogTarisan.logger.log(NivelLog.DEBUG, "año "+anio); mes = Integer.valueOf(tiempo.substring(5,7)); LogTarisan.logger.log(NivelLog.DEBUG, "Mes "+mes); dia = Integer.valueOf(tiempo.substring(8,10)); LogTarisan.logger.log(NivelLog.DEBUG, "Dia "+dia); hora = Integer.valueOf(tiempo.substring(11, 13)); LogTarisan.logger.log(NivelLog.DEBUG, "hora "+hora); min = Integer.valueOf(tiempo.substring(14, 16)); LogTarisan.logger.log(NivelLog.DEBUG, "min "+min); sec = Integer.valueOf(tiempo.substring(17, 19)); LogTarisan.logger.log(NivelLog.DEBUG, "sec "+sec); cal.clear(); cal.set(Calendar.DAY_OF_MONTH, dia); cal.set(Calendar.MONTH, mes); cal.set(Calendar.YEAR, anio); cal.set(Calendar.HOUR_OF_DAY, hora); cal.set(Calendar.MINUTE, min); cal.set(Calendar.SECOND, sec); LogTarisan.logger.log(NivelLog.DEBUG, "Llega 83"); resul.set_fecha_hora(new Date(cal.getTimeInMillis())); LogTarisan.logger.log(NivelLog.DEBUG, "Llega 85"); resul.set_xml(response); } } return resul; } public Integer obtenerSecuencia() { Integer sec = 0; String consulta = "select PETICION_CHIPCARD.NEXTVAL from dual"; Connection conexion = null; try { conexion = ParametrosConfiguracion.dataStore.obtenerConexion(); ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, consulta); if(rs.next()) sec = rs.getInt("NEXTVAL"); } catch (ExcepcionTarisan e) { LogTarisan.logger.log(NivelLog.DEBUG, "Excepcion TARISAN en obtenerSecuencia"); } catch (SQLException e) { LogTarisan.logger.log(NivelLog.DEBUG, "Excepcion SQL en obtenerSecuencia"); } try { conexion.close(); } catch (SQLException e) { LogTarisan.logger.log(NivelLog.ERROR, "Error al cerrar la conexion con oracle"); } return sec; } public boolean insertar_peticiones(int medico, int especialidad, int acto, String tarjeta, Calendar cal, Integer secuencia) { boolean resul = false; StringBuffer strSql = new StringBuffer(); Object[] aValores = new Object[6]; strSql.append("INSERT INTO CHIPCARD_PETICIONES (MEDICO, ESPECIALIDAD, ACTO, TARJETA_CHIPCARD, FECHA_HORA, SECUENCIA) VALUES (?, ?, ?, '?', TO_DATE('?', 'dd/mm/yyyy hh24:mi:ss'), ?)"); aValores[0] = Integer.valueOf(medico); aValores[1] = Integer.valueOf(especialidad); aValores[2] = Integer.valueOf(acto); aValores[3] = new String(tarjeta); aValores[5] = Integer.valueOf(secuencia); String segundos = ""; if(cal.get(Calendar.SECOND)<10) segundos = "0"+cal.get(Calendar.SECOND); else segundos = Integer.toString(cal.get(Calendar.SECOND)); aValores[4] = new String(cal.get(Calendar.DAY_OF_MONTH)+"/"+(cal.get(Calendar.MONTH)+1)+"/"+cal.get(Calendar.YEAR)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+segundos); Connection conexion = null; try { conexion = ParametrosConfiguracion.dataStore.obtenerConexion(); LogTarisan.logger.log(NivelLog.DEBUG, Utilidades.obtenerSentenciaSQL(strSql, aValores, null)); resul = ParametrosConfiguracion.dataStore.insertar(Utilidades.obtenerSentenciaSQL(strSql, aValores, null), conexion); }catch (ExcepcionTarisan e1) { LogTarisan.logger.log(NivelLog.DEBUG, "Excepcion tarisan al generar la sentencia sql. "+strSql.toString()+aValores); } try { conexion.close(); } catch (SQLException e) { LogTarisan.logger.log(NivelLog.ERROR, "Error al cerrar la conexion con oracle"); } return resul; } /** * Recibe el objeto respuesta chipcard y lo inserta en la tabla Chipcard_movimientos. * @param res * @return */ public boolean insertar_movimientos(RespuestaChipcard res, Integer secuencia) { boolean resul = false; //INSERTAR EN CHIPCARD_MOVIMIENTOS PARA TENER EN BBDD LOS MOVIMIENTOS DE CHIPCARD /* * TARJETA VARCHAR2(24 BYTE), RESPUESTA VARCHAR2(255 BYTE), MENSAJE VARCHAR2(255 BYTE), AUTORIZACION NUMBER(10) NOT NULL, TERMINAL VARCHAR2(10 BYTE) */ StringBuffer strSql = new StringBuffer(); Object[] aValores = new Object[10]; strSql.append("INSERT INTO CHIPCARD_MOVIMIENTOS (TARJETA, RESPUESTA, MENSAJE, AUTORIZACION, TERMINAL, ESPECIALIDAD, ACTO, FECHA_HORA, XMLCOMPLETO, SECUENCIA) VALUES ('?', '?', '?', ?, '?', ?, ?, to_date('?', 'dd/mm/yyyy hh24:mi:ss'), '?', ?)"); aValores[0] = new String(res.get_tarjeta().substring(14)); aValores[1] = new String(res.get_respuesta().substring(15)); aValores[2] = new String(res.get_mensaje().substring(17)); aValores[3] = Integer.valueOf(res.get_talon_chipcard()); aValores[4] = Integer.valueOf(res.get_terminal()); aValores[5] = Integer.valueOf(res.get_especialidad()); aValores[6] = Integer.valueOf(res.get_acto()); aValores[9] = Integer.valueOf(secuencia); Calendar cal = Calendar.getInstance(); cal.setTime(res.get_fecha_hora()); String segundos = ""; if(cal.get(Calendar.SECOND)<10) segundos = "0"+cal.get(Calendar.SECOND); else segundos = Integer.toString(cal.get(Calendar.SECOND)); aValores[7] = new String(cal.get(Calendar.DAY_OF_MONTH)+"/"+(cal.get(Calendar.MONTH)+1)+"/"+cal.get(Calendar.YEAR)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+segundos); aValores[8] = new String(res.get_xml()); try { LogTarisan.logger.log(NivelLog.DEBUG, Utilidades.obtenerSentenciaSQL(strSql, aValores, null)); } catch (ExcepcionTarisan e1) { LogTarisan.logger.log(NivelLog.DEBUG, "Excepcion tarisan al generar la sentencia sql. "+strSql.toString()+aValores); } Connection conexion = null; try { conexion = ParametrosConfiguracion.dataStore.obtenerConexion(); //resul = ParametrosConfiguracion.dataStore.insertar(strSql.toString(), aValores, conexion); resul = ParametrosConfiguracion.dataStore.insertar(Utilidades.obtenerSentenciaSQL(strSql, aValores, null), conexion); } catch (ExcepcionTarisan e) { LogTarisan.logger.log(NivelLog.ERROR, "Excepcion en chipcard_movimientos la respuesta: "+res.get_talon_chipcard()+", el error: "+e.toString()); } try { conexion.close(); } catch (SQLException e) { LogTarisan.logger.log(NivelLog.ERROR, "Error al cerrar la conexion con oracle"); } return resul; } public boolean talon_duplicado(Integer autoriz, int medico) { boolean resultado = false; StringBuffer strSql = new StringBuffer(); Object[] aCondiciones = new Object[2]; strSql.append("select count(talon_chipcard) cuantos from tamovext where talon_chipcard = ? and medico <> ?"); aCondiciones[0]=autoriz; aCondiciones[0]=medico; Connection conexion = null; try{ conexion = ParametrosConfiguracion.dataStore.obtenerConexion(); ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, strSql.toString(), aCondiciones); if(rs.next()) { int cuanto = rs.getInt("CUANTOS"); if(cuanto > 0) resultado = true; } } catch (ExcepcionTarisan e) { LogTarisan.logger.log(NivelLog.DEBUG, "Excepcion en talon_duplicado"); } catch (SQLException e) { LogTarisan.logger.log(NivelLog.DEBUG, "Excepcion en talon_duplicado"); } try { conexion.close(); } catch (SQLException e) { LogTarisan.logger.log(NivelLog.ERROR, "Error al cerrar la conexion con oracle"); } return resultado; } /** * Hemos detectado que si hay dos peticiones exáctamente al mismo tiempo las respuestas se alternan y da un error a pesar de que la respuesta haya sido autorizada. * para asegurarnos buscamos la secuencia de la petición en la tabla de respuestas y "recomprobamos" el resultado * @param secuencia * @return */ public boolean obtener_resultado_secuencia(Integer secuencia) { boolean resul = false; String sql = "SELECT RESPUESTA FROM CHIPCARD_MOVIMIENTOS WHERE SECUENCIA = "+secuencia; Connection conexion = null; try { conexion = ParametrosConfiguracion.dataStore.obtenerConexion(); ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, sql); if(rs.next()) { String respuesta = rs.getString("RESPUESTA"); if(respuesta.contains(ParametrosConfiguracion.chipcard_oper_auto)) resul = true; } }catch (ExcepcionTarisan e) { LogTarisan.logger.log(NivelLog.DEBUG, "Excepcion tarisan al obtener resultado por secuencia"); } catch (SQLException e) { LogTarisan.logger.log(NivelLog.DEBUG, "Excepcion sql al obtener resultado por secuencia"); } try { conexion.close(); } catch (SQLException e) { LogTarisan.logger.log(NivelLog.ERROR, "Error al cerrar la conexion con oracle"); } LogTarisan.logger.log(NivelLog.DEBUG, "Se encuentra la secuencia "+secuencia+"? "+resul); return resul; } public String obtener_respuesta_secuencia(Integer secuencia) { String resul = ""; String sql = "SELECT XMLCOMPLETO FROM CHIPCARD_MOVIMIENTOS WHERE SECUENCIA = "+secuencia; Connection conexion = null; try { conexion = ParametrosConfiguracion.dataStore.obtenerConexion(); ResultSet rs = ParametrosConfiguracion.dataStore.seleccionar(conexion, sql); if(rs.next()) { resul = rs.getString("XMLCOMPLETO"); } }catch (ExcepcionTarisan e) { LogTarisan.logger.log(NivelLog.DEBUG, "Excepcion tarisan al obtener resultado por secuencia"); } catch (SQLException e) { LogTarisan.logger.log(NivelLog.DEBUG, "Excepcion sql al obtener resultado por secuencia"); } try { conexion.close(); } catch (SQLException e) { LogTarisan.logger.log(NivelLog.ERROR, "Error al cerrar la conexion con oracle"); } LogTarisan.logger.log(NivelLog.DEBUG, "Se encuentra la secuencia "+secuencia+"? "+resul); return resul; } }