File size: 10,557 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package model.dao;
import beans.Conductor;
import beans.Respuesta;
import beans.RespuestaValidacion;
import gateway.sms.JaxSms;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import model.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
/**
*
* @author Juan Carlos
*/
public class ConductorDAO {
private static final Integer TOKEN_LENGTH = 4;
/**
* Método para registrar el conductor en la base de datos
*
* @param conductor el conductor a registrar
* @return Integer: el numero de filas afectadas o un código de error
*/
public static int registrarConductor(Conductor conductor) {
JaxSms jax = new JaxSms();
int res = 0;
SqlSession conn = null;
if (conductor.getNombre() == null
|| conductor.getNombre().toString().trim().isEmpty()) {
return -1;
}
if (conductor.getTelefono().length() > 10) {
return -1;
}
if (conductor.getFechaNacimiento() == null
|| conductor.getFechaNacimiento().toString()
.trim().isEmpty()) {
return -1;
}
if (conductor.getNumLicencia() == null
|| conductor.getNumLicencia().toString()
.trim().isEmpty()) {
return -1;
}
if (buscarTelefono(conductor)) {
return -2;
}
String token = "";
for (int x = 0; x < TOKEN_LENGTH; x++) {
token += generarParteToken();
}
conductor.setTokenAcceso(token);
conductor.setEstatus(1);
try {
conn = MyBatisUtils.getSession();
res = conn.insert("Conductor.registrar", conductor);
conn.commit();
if(res > 0){
String mensaje = "Tu token para Crashify es: " + conductor.getTokenAcceso();
jax.enviar(conductor.getTelefono(), mensaje);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
return res;
}
/**
* Método para validar un conductor asociado a un teléfono con un token de
* acceso especifico
*
* @param telefono le telefono a validar
* @param token el token de acceso ingresado por el conductor
* @return Respuesta Validación: Error si no se ha podido validar o el
* conductor validado
*/
public static RespuestaValidacion autenticarConductor(String telefono, String token) {
RespuestaValidacion respuestaValidacion = new RespuestaValidacion();
Respuesta error = new Respuesta();
Conductor conductor;
int filas = 0;
if (telefono == null || telefono.isEmpty()) {
error.setError(true);
error.setErrorcode(1);
error.setMensaje("Ingrese telefono");
respuestaValidacion.setError(error);
} else if (token == null || token.isEmpty()) {
error.setError(true);
error.setErrorcode(2);
error.setMensaje("Ingrese token");
respuestaValidacion.setError(error);
} else if (!verificarTelefono(telefono)) {
error.setError(true);
error.setErrorcode(3);
error.setMensaje("Error de registro, telefono ya validado");
respuestaValidacion.setError(error);
} else {
conductor = buscarConductor(telefono);
if (conductor.getIdConductor() != null) {
SqlSession conn = null;
try {
conn = MyBatisUtils.getSession();
filas = conn.update("Conductor.validarConductor", conductor);
conn.commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
if (filas > 0) {
conductor = ConductorDAO.buscarConductor(telefono);
error.setError(false);
error.setErrorcode(200);
error.setMensaje("Conductor validado");
respuestaValidacion.setConductor(conductor);
} else {
error.setError(true);
error.setErrorcode(4);
error.setMensaje("Error al validar conductor");
}
}
return respuestaValidacion;
}
/**
* Método para que el conductor pueda iniciar autenticarse en el sistema
* tomando como referencia la información almacenada en la base de datos
*
* @param telefono
* @param password
* @return
*/
public static RespuestaValidacion iniciarSesion(String telefono, String password) {
RespuestaValidacion respuesta = new RespuestaValidacion();
Respuesta error = new Respuesta();
Conductor conductor = new Conductor();
int filas = 0;
if (telefono == null || telefono.isEmpty()) {
error.setError(true);
error.setErrorcode(1);
error.setMensaje("Ingrese telefono");
respuesta.setError(error);
} else if (password == null || password.isEmpty()) {
error.setError(true);
error.setErrorcode(2);
error.setMensaje("Ingrese token");
respuesta.setError(error);
} else if (buscarConductor(telefono).getIdConductor() == null) {
error.setError(true);
error.setErrorcode(3);
error.setMensaje("Error de registro, no existe un conductor con ese telefono");
respuesta.setError(error);
} else {
SqlSession conn = null;
try {
HashMap<String, Object> param = new HashMap<String, Object>();
param.put("telefono", telefono);
param.put("password", password);
conn = MyBatisUtils.getSession();
conductor = conn.selectOne("Conductor.iniciarSesion", param);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
if (conductor.getIdConductor()!=null) {
error.setError(false);
error.setErrorcode(201);
error.setMensaje("Conductor registrado");
respuesta.setError(error);
respuesta.setConductor(conductor);
} else {
error.setError(true);
error.setErrorcode(5);
error.setMensaje("Error al cargar conductor");
respuesta.setError(error);
}
}
return respuesta;
}
/**
* Método para buscar si el telefono de un conductor ya fue registrado
*
* @param conductor el conductor dueño del telefono
* @return Boolean: true si ya esta registrado, false en caso contrario
*/
private static boolean buscarTelefono(Conductor conductor) {
String telefono = conductor.getTelefono();
boolean respuesta = false;
List<Conductor> list = new ArrayList<Conductor>();
if (telefono != null && !telefono.isEmpty()) {
SqlSession conn = null;
try {
conn = MyBatisUtils.getSession();
list = conn.selectList("Conductor.buscarTelefono", telefono);
if (!list.isEmpty()) {
Integer idUsuario = list.get(0).getIdConductor();
Integer idUsuarioAux = conductor.getIdConductor();
if (!Objects.equals(idUsuario, idUsuarioAux)) {
respuesta = true;
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
return respuesta;
}
/**
* Método para generar de manera aleatoria una parte del token de acceso
*
* @return String: Una parte del token de acceso.
*/
private static String generarParteToken() {
Integer parte = 0;
Random rg = new Random();
parte = rg.nextInt(9);
return parte.toString();
}
/**
* Método para verificar si un telefono ya ha sido validado antes
*
* @param telefono el telefono a validar
* @return Boolean: true si aun no ha sido registrado
*/
private static boolean verificarTelefono(String telefono) {
List<Conductor> list = new ArrayList<>();
if (telefono != null && !telefono.isEmpty()) {
SqlSession conn = null;
try {
conn = MyBatisUtils.getSession();
list = conn.selectList("Conductor.buscarTelefonoNoValidado", telefono);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
return !list.isEmpty();
}
/**
* Método para obtener el conductor asociado al teléfono registrado
*
* @param telefono el teléfono a comparar
* @return Conductor: el conductor encontrado, null en caso contrario
*/
private static Conductor buscarConductor(String telefono) {
Conductor conductor = new Conductor();
if (telefono != null && !telefono.isEmpty()) {
SqlSession conn = null;
try {
Conductor conductorAux = new Conductor();
conn = MyBatisUtils.getSession();
conductorAux = conn.selectOne("Conductor.buscarTelefono", telefono);
if(conductorAux.getIdConductor()!=null){
conductor = conductorAux;
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
return conductor;
}
}
|