|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
|
|
public class ConductorDAO { |
|
|
|
private static final Integer TOKEN_LENGTH = 4; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String generarParteToken() { |
|
Integer parte = 0; |
|
Random rg = new Random(); |
|
parte = rg.nextInt(9); |
|
return parte.toString(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
} |
|
} |
|
|