Datasets:

ArXiv:
License:
File size: 7,999 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
/*
 * 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.InfoVehiculo;
import beans.Respuesta;
import beans.Vehiculo;
import beans.VehiculoAnonimo;
import beans.VehiculoData;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import model.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

/**
 *
 * @author Juan Carlos
 */
public class VehiculoDAO {

    public static int registrarVehiculo(Vehiculo vehiculo, Date fechaVencimiento, int idConductor) {
        int fa = 0;
        SqlSession conn = null;
        fa = validarVehiculo(vehiculo);
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        if (fa != 0) {
            return fa;
        }
        if (buscarVehiculo(vehiculo)) {
            return -2;
        }
        try {
            
            VehiculoData data = new VehiculoData();
            data.setNumPlacas(vehiculo.getNumPlacas());
            data.setIdMarca(vehiculo.getIdMarca());
            data.setModelo(vehiculo.getModelo());
            data.setColor(vehiculo.getColor());
            data.setYear(vehiculo.getYear());
            data.setNumPoliza(vehiculo.getNumPoliza());
            data.setIdAseguradora(vehiculo.getIdAseguradora());
            data.setFechaVencimiento(fechaVencimiento);
            data.setFa(0);
            map.put("numPlacas", data.getNumPlacas().toString());
            map.put("idMarca", data.getIdMarca());
            map.put("modelo", data.getModelo());
            map.put("color", data.getColor());
            map.put("year", data.getYear());
            map.put("numPoliza", data.getNumPoliza());
            map.put("idAseguradora", data.getIdAseguradora());
            map.put("fechaVencimiento", data.getFechaVencimiento());
            map.put("idConductor", idConductor);
            map.put("fa", data.getFa());
            conn = MyBatisUtils.getSession();
            fa = conn.update("Vehiculo.registrar", map);
            conn.commit();
            if (fa < 0) {
                return -3;
            }
        } catch (Exception ex) {
            System.out.println(map);
            System.out.println(ex.getLocalizedMessage());
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        return fa;
    }
    
    public static int registrarVehiculoAnonimo(VehiculoAnonimo vehiculo) {
        int res = 1;
        SqlSession conn = null;
        try {
            conn = MyBatisUtils.getSession();
            res = conn.insert("Vehiculo.registrarAnonimo", vehiculo);
            conn.commit();
            if (res <= 0) {
                return -3;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        return res;
    }

    public static List<InfoVehiculo> getVehiculos(Integer idConductor) {
        List<InfoVehiculo> list = new ArrayList<>();
        SqlSession conn = null;
        try {
            conn = MyBatisUtils.getSession();
            list = conn.selectList("Vehiculo.getVehiculosConductor", idConductor);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        return list;
    }

    public static int editarVehiculo(Vehiculo vehiculo) {
        int res = 0;
        SqlSession conn = null;

        if (validarVehiculo(vehiculo) == 0) {
            try {
                conn = MyBatisUtils.getSession();
                res = conn.update("Vehiculo.editarVehiculo", vehiculo);
                conn.commit();
                if (res <= 0) {
                    return -3;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (conn != null) {
                    conn.close();
                }
            }
        }
        return res;
    }

    public static Respuesta eliminarVehiculo(String numPlacas) {
        int res = 0;
        Respuesta respuesta = new Respuesta();
        respuesta.setError(false);
        respuesta.setErrorcode(0);
        SqlSession conn = null;

        if (numPlacas == null || numPlacas.trim().isEmpty()) {
            respuesta.setError(true);
            respuesta.setErrorcode(2);
            respuesta.setMensaje("Numplacas vacias");
        } else {
            try {
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            map.put("numPlacas", numPlacas);
            map.put("resultado", 0);
            conn = MyBatisUtils.getSession();
            res = conn.update("Vehiculo.eliminarVehiculo",map);
            conn.commit();
            System.out.println(res);
                if (res < 0) {
                    respuesta.setError(true);
                    respuesta.setErrorcode(3);
                    respuesta.setMensaje("Error al eliminar vehiculo de la BD");
                } else {
                    respuesta.setError(false);
                    respuesta.setErrorcode(200);
                    respuesta.setMensaje("Vehiculo eliminado exitosamente");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (conn != null) {
                    conn.close();
                }
            }
        }
        return respuesta;
    }

    public static List<Vehiculo> getVehiculosReporte(Integer idReporte) {
        List<Vehiculo> list = new ArrayList<>();
        SqlSession conn = null;
        try {
            conn = MyBatisUtils.getSession();
            list = conn.selectList("Vehiculo.getVehiculosReporte", idReporte);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        return list;
    }
    
    private static int validarVehiculo(Vehiculo vehiculo) {
        if (vehiculo.getIdMarca() == 0) {
            return -1;
        }
        if (vehiculo.getModelo() == null
                || vehiculo.getModelo().toString().trim().isEmpty()) {
            return -1;
        }
        if (vehiculo.getColor() == null
                || vehiculo.getColor().toString().trim().isEmpty()) {
            return -1;
        }
        if (vehiculo.getNumPlacas() == null
                || vehiculo.getNumPlacas().toString().trim().isEmpty()) {
            return -1;
        }
        if (vehiculo.getYear() == null
                || vehiculo.getYear().toString().trim().isEmpty()) {
            return -1;
        }
        return 0;
    }

    private static boolean buscarVehiculo(Vehiculo vehiculo) {
        String numPlacas = vehiculo.getNumPlacas();
        boolean respuesta = false;
        List<Vehiculo> list = new ArrayList<Vehiculo>();
        if (numPlacas != null && !numPlacas.isEmpty()) {
            SqlSession conn = null;
            try {
                conn = MyBatisUtils.getSession();
                list = conn.selectList("Vehiculo.buscarVehiculo", numPlacas);
                if (!list.isEmpty()) {
                    String idVehiculo = list.get(0).getNumPlacas();
                    String idVehiculoAux = vehiculo.getNumPlacas();
                    if (!Objects.equals(idVehiculo, idVehiculoAux)) {
                        respuesta = true;
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (conn != null) {
                    conn.close();
                }
            }
        }
        return respuesta;
    }
}