|
| 1 | +package dominio; |
| 2 | + |
| 3 | +import java.sql.ResultSet; |
| 4 | +import java.sql.SQLException; |
| 5 | +import java.util.ArrayList; |
| 6 | + |
| 7 | +import persistencia.Agente; |
| 8 | + |
| 9 | +public class Enfermedad { |
| 10 | + private int id; |
| 11 | + private String nombre; |
| 12 | + private String descripcion; |
| 13 | + private int temporalidad; |
| 14 | + |
| 15 | + public Enfermedad(int id, String nombre, String descripcion, int temporalidad) { |
| 16 | + this.id = id; |
| 17 | + this.nombre = nombre; |
| 18 | + this.descripcion = descripcion; |
| 19 | + this.temporalidad = temporalidad; |
| 20 | + } |
| 21 | + |
| 22 | + public Enfermedad(String nombre, String descripcion, int temporalidad) { |
| 23 | + this.nombre = nombre; |
| 24 | + this.descripcion = descripcion; |
| 25 | + this.temporalidad = temporalidad; |
| 26 | + } |
| 27 | + |
| 28 | + public int getId() { |
| 29 | + return this.id; |
| 30 | + } |
| 31 | + |
| 32 | + public String getNombre() { |
| 33 | + return this.nombre; |
| 34 | + } |
| 35 | + |
| 36 | + public String getDescripcion() { |
| 37 | + return this.descripcion; |
| 38 | + } |
| 39 | + |
| 40 | + public int getTemporalidad() { |
| 41 | + return this.temporalidad; |
| 42 | + } |
| 43 | + |
| 44 | + public int insert() throws Exception { |
| 45 | + int salida = 0; |
| 46 | + |
| 47 | + salida = Agente.getAgente().insert("insert into ENFERMEDADES (NOMBRE, DESCRIPCION, TEMPORALIDAD) values('"+this.nombre+"','"+this.descripcion+"',"+this.temporalidad+")"); |
| 48 | + System.out.println(salida); |
| 49 | + return salida; |
| 50 | + } |
| 51 | + |
| 52 | + public static ArrayList<Enfermedad> readEnfermedad(String nombreEnfermedad) { |
| 53 | + ArrayList<String> list = new ArrayList<String>(); |
| 54 | + ResultSet resultSet; |
| 55 | + String str = new String(nombreEnfermedad); |
| 56 | + ArrayList<Enfermedad> enfermedadesEncontradas = new ArrayList<Enfermedad>(); |
| 57 | + |
| 58 | + int id; |
| 59 | + String nombre; |
| 60 | + String descripcion; |
| 61 | + int temporalidad; |
| 62 | + |
| 63 | + try { |
| 64 | + int i = 0; |
| 65 | + int size = 0; |
| 66 | + Enfermedad enfermedad; |
| 67 | + resultSet = Agente.getAgente().select("select ID_ENFERMEDAD, NOMBRE, DESCRIPCION, TEMPORALIDAD from ENFERMEDADES where lower(NOMBRE) like '%" + str.toLowerCase() + "%'"); |
| 68 | + |
| 69 | + while (resultSet.next()) { |
| 70 | + list.add(resultSet.getString("ID_ENFERMEDAD")); |
| 71 | + list.add(resultSet.getString("NOMBRE")); |
| 72 | + list.add(resultSet.getString("DESCRIPCION")); |
| 73 | + list.add(resultSet.getString("TEMPORALIDAD")); |
| 74 | + } |
| 75 | + |
| 76 | + while (list.size()/4 > i) { |
| 77 | + |
| 78 | + id = Integer.parseInt(list.get(0+size)); |
| 79 | + nombre = list.get(1+size); |
| 80 | + descripcion = list.get(2+size); |
| 81 | + if (list.get(3+size) == null) { |
| 82 | + temporalidad = -1; |
| 83 | + } else { |
| 84 | + temporalidad = Integer.parseInt(list.get(3+size)); |
| 85 | + } |
| 86 | + |
| 87 | + i++; |
| 88 | + size +=4; |
| 89 | + |
| 90 | + enfermedad = new Enfermedad(id, nombre, descripcion, temporalidad); |
| 91 | + enfermedadesEncontradas.add(enfermedad); |
| 92 | + } |
| 93 | + |
| 94 | + } catch (SQLException e) { |
| 95 | + // TODO Auto-generated catch block |
| 96 | + e.printStackTrace(); |
| 97 | + } catch (Exception e) { |
| 98 | + // TODO Auto-generated catch block |
| 99 | + e.printStackTrace(); |
| 100 | + } |
| 101 | + return enfermedadesEncontradas; |
| 102 | + } |
| 103 | + |
| 104 | + public static ArrayList<Vacuna> readVacunas(String nombreEnfermedad) { |
| 105 | + String str = new String(nombreEnfermedad); |
| 106 | + ArrayList<String> list = new ArrayList<String>(); |
| 107 | + ArrayList<Vacuna> vacunasEncontradas = new ArrayList<Vacuna>(); |
| 108 | + |
| 109 | + int id; |
| 110 | + String enfermedad = nombreEnfermedad; |
| 111 | + String nombre; |
| 112 | + int num_dosis; |
| 113 | + |
| 114 | + try { |
| 115 | + int i = 0; |
| 116 | + int size = 0; |
| 117 | + Vacuna vacuna; |
| 118 | + |
| 119 | + ResultSet resultSet = Agente.getAgente().select("select * from VACUNAS where ID_ENFERMEDAD = (SELECT ID_ENFERMEDAD FROM ENFERMEDADES WHERE NOMBRE='" + str + "')"); |
| 120 | + |
| 121 | + while (resultSet.next()) { |
| 122 | + list.add(resultSet.getString("ID")); |
| 123 | + list.add(resultSet.getString("NOMBRE")); |
| 124 | + list.add(resultSet.getString("NUM_DOSIS")); |
| 125 | + } |
| 126 | + |
| 127 | + while (list.size()/3 > i) { |
| 128 | + |
| 129 | + id = Integer.parseInt(list.get(0+size)); |
| 130 | + nombre = list.get(1+size); |
| 131 | + num_dosis = Integer.parseInt(list.get(2+size)); |
| 132 | + i++; |
| 133 | + size +=3; |
| 134 | + |
| 135 | + vacuna = new Vacuna(id, nombre, enfermedad, num_dosis); |
| 136 | + vacunasEncontradas.add(vacuna); |
| 137 | + } |
| 138 | + |
| 139 | + } catch (SQLException e) { |
| 140 | + // TODO Auto-generated catch block |
| 141 | + e.printStackTrace(); |
| 142 | + } |
| 143 | + |
| 144 | + return vacunasEncontradas; |
| 145 | + } |
| 146 | + |
| 147 | + public static ArrayList<Medicina> readMedicinas(String nombreEnfermedad) { |
| 148 | + String str = new String(nombreEnfermedad); |
| 149 | + ArrayList<String> list = new ArrayList<String>(); |
| 150 | + ArrayList<Medicina> medicinasEncontradas = new ArrayList<Medicina>(); |
| 151 | + |
| 152 | + int id; |
| 153 | + String enfermedad = nombreEnfermedad; |
| 154 | + String nombre; |
| 155 | + String organismo; |
| 156 | + |
| 157 | + try { |
| 158 | + int i = 0; |
| 159 | + int size = 0; |
| 160 | + Medicina medicina; |
| 161 | + |
| 162 | + //ResultSet resultSet = Agente.getAgente().select("select * from VACUNAS where ID_ENFERMEDAD = (SELECT ID_ENFERMEDAD FROM ENFERMEDADES WHERE NOMBRE='" + str + "')"); |
| 163 | + ResultSet resultSet = Agente.getAgente().select("select m.ID_MEDICINA, m.NOMBRE, o.NOMBRE FROM MEDICINAS m join ENFERMEDAD_MEDICINA em on m.ID_MEDICINA=em.ID_MEDICINA join ORGANISMOS o on m.O_APROBADOR=o.ID_ORGANISMO join ENFERMEDADES e on em.ID_ENFERMEDAD=e.ID_ENFERMEDAD WHERE e.NOMBRE='"+str+"'"); |
| 164 | + |
| 165 | + while (resultSet.next()) { |
| 166 | + list.add(resultSet.getString("m.ID_MEDICINA")); |
| 167 | + list.add(resultSet.getString("m.NOMBRE")); |
| 168 | + list.add(resultSet.getString("o.NOMBRE")); |
| 169 | + } |
| 170 | + |
| 171 | + while (list.size()/3 > i) { |
| 172 | + |
| 173 | + id = Integer.parseInt(list.get(0+size)); |
| 174 | + nombre = list.get(1+size); |
| 175 | + organismo = list.get(2+size); |
| 176 | + i++; |
| 177 | + size +=3; |
| 178 | + |
| 179 | + medicina = new Medicina(id, nombre, organismo, enfermedad); |
| 180 | + medicinasEncontradas.add(medicina); |
| 181 | + } |
| 182 | + |
| 183 | + } catch (SQLException e) { |
| 184 | + // TODO Auto-generated catch block |
| 185 | + e.printStackTrace(); |
| 186 | + } |
| 187 | + |
| 188 | + return medicinasEncontradas; |
| 189 | + } |
| 190 | + |
| 191 | + public static ArrayList<Sintoma> readSintomas(String nombreEnfermedad) { |
| 192 | + String str = new String(nombreEnfermedad); |
| 193 | + ArrayList<String> list = new ArrayList<String>(); |
| 194 | + ArrayList<Sintoma> sintomasEncontradas = new ArrayList<Sintoma>(); |
| 195 | + |
| 196 | + int id; |
| 197 | + String nombre; |
| 198 | + String descripcion_s; |
| 199 | + |
| 200 | + try { |
| 201 | + int i = 0; |
| 202 | + int size = 0; |
| 203 | + Sintoma sintoma; |
| 204 | + |
| 205 | + //ResultSet resultSet = Agente.getAgente().select("select * from VACUNAS where ID_ENFERMEDAD = (SELECT ID_ENFERMEDAD FROM ENFERMEDADES WHERE NOMBRE='" + str + "')"); |
| 206 | + ResultSet resultSet = Agente.getAgente().select("select s.ID_SINTOMA, s.NOMBRE, s.DESCRIPCION FROM SINTOMAS s join ENFERMEDAD_SINTOMAS es on s.ID_SINTOMA=es.ID_SINTOMA join ENFERMEDADES e on es.ID_ENFERMEDAD=e.ID_ENFERMEDAD where e.NOMBRE='"+str+"'"); |
| 207 | + |
| 208 | + while (resultSet.next()) { |
| 209 | + list.add(resultSet.getString("s.ID_SINTOMA")); |
| 210 | + list.add(resultSet.getString("s.NOMBRE")); |
| 211 | + list.add(resultSet.getString("s.DESCRIPCION")); |
| 212 | + } |
| 213 | + |
| 214 | + while (list.size()/3 > i) { |
| 215 | + |
| 216 | + id = Integer.parseInt(list.get(0+size)); |
| 217 | + nombre = list.get(1+size); |
| 218 | + descripcion_s = list.get(2+size); |
| 219 | + i++; |
| 220 | + size +=3; |
| 221 | + |
| 222 | + sintoma = new Sintoma(id, nombre, descripcion_s); |
| 223 | + sintomasEncontradas.add(sintoma); |
| 224 | + } |
| 225 | + |
| 226 | + } catch (SQLException e) { |
| 227 | + // TODO Auto-generated catch block |
| 228 | + e.printStackTrace(); |
| 229 | + } |
| 230 | + |
| 231 | + return sintomasEncontradas; |
| 232 | + } |
| 233 | + |
| 234 | + public static int deleteEnfermedad(int idEnfermedad) { |
| 235 | + int result = -1; |
| 236 | + |
| 237 | + try { |
| 238 | + result = Agente.getAgente().delete("delete from ENFERMEDADES where ID_ENFERMEDAD = '"+idEnfermedad+"';"); |
| 239 | + } catch (Exception e) { |
| 240 | + // TODO: handle exception |
| 241 | + e.printStackTrace(); |
| 242 | + } |
| 243 | + |
| 244 | + return result; |
| 245 | + } |
| 246 | + |
| 247 | + public static int updateEnfermedad(int idEnfermedad, String nombre, String descripcion, int temporalidad) { |
| 248 | + int result = -1; |
| 249 | + |
| 250 | + try { |
| 251 | + result = Agente.getAgente().update("update ENFERMEDADES set NOMBRE = '"+nombre+"', DESCRIPCION = '"+descripcion+"', TEMPORALIDAD = "+temporalidad+" where ID_ENFERMEDAD = '"+idEnfermedad+"';"); |
| 252 | + } catch (Exception e) { |
| 253 | + // TODO: handle exception |
| 254 | + e.printStackTrace(); |
| 255 | + } |
| 256 | + |
| 257 | + return result; |
| 258 | + } |
| 259 | + |
| 260 | + public static int updateEnfermedad(int idEnfermedad, String campo, int nuevoValor) { |
| 261 | + int result = -1; |
| 262 | + |
| 263 | + try { |
| 264 | + result = Agente.getAgente().update("update ENFERMEDADES set "+campo+" = '"+nuevoValor+"' where ID_ENFERMEDAD = '"+idEnfermedad+"';"); |
| 265 | + } catch (Exception e) { |
| 266 | + // TODO: handle exception |
| 267 | + e.printStackTrace(); |
| 268 | + } |
| 269 | + |
| 270 | + return result; |
| 271 | + } |
| 272 | +} |
0 commit comments