|
| 1 | +package giis.demo.tkrun; |
| 2 | + |
| 3 | +import java.time.LocalDateTime; |
| 4 | +import java.time.format.DateTimeFormatter; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import javax.swing.JOptionPane; |
| 9 | +import javax.swing.table.TableModel; |
| 10 | + |
| 11 | +import giis.demo.util.SwingUtil; |
| 12 | + |
| 13 | +public class EntregaController { |
| 14 | + private EntregaModel model; |
| 15 | + private EntregaView view; |
| 16 | + |
| 17 | + public EntregaController(EntregaModel entregaModel, EntregaView entregaView) { |
| 18 | + this.model = entregaModel; |
| 19 | + this.view = entregaView; |
| 20 | + //no hay inicializacion especifica del modelo, solo de la vista |
| 21 | + this.initView(); |
| 22 | + } |
| 23 | + |
| 24 | + public void initController() { |
| 25 | + view.getBtCancelar().addActionListener(e -> SwingUtil.exceptionWrapper(() -> salir())); |
| 26 | + view.getBtRegistrar().addActionListener(e -> realizarEntrega()); |
| 27 | + view.getBtInfo().addActionListener(e -> verInfo()); |
| 28 | + } |
| 29 | + |
| 30 | + private void verInfo() { |
| 31 | + String nref = view.getTfNRef().getText(); |
| 32 | + if(comprobarNRef(nref)) { |
| 33 | + int nrefnum = Integer.parseInt(nref); |
| 34 | + EnvioDisplayDTO envio = model.getEnvio(nrefnum); |
| 35 | + if(validarEnvio(envio,nrefnum)) { |
| 36 | + List<EnvioDisplayDTO> pedidos = new ArrayList<>(); |
| 37 | + pedidos.add(envio); |
| 38 | + TableModel tmodel = SwingUtil.getTableModelFromPojos(pedidos, new String[] {"nombreReceptor", "correoReceptor","direccionReceptor"}); |
| 39 | + view.getTbInfo().setModel(tmodel); |
| 40 | + SwingUtil.autoAdjustColumns(view.getTbInfo()); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private boolean validarEnvio(EnvioDisplayDTO envio, int nrefnum) { |
| 46 | + if (envio==null) { |
| 47 | + JOptionPane.showMessageDialog(null, "No existe un envio \ncon el NRef: " + nrefnum); |
| 48 | + return false; |
| 49 | + } else return true; |
| 50 | + } |
| 51 | + |
| 52 | + private boolean comprobarNRef(String nref) { |
| 53 | + try { |
| 54 | + if(nref.isBlank() || Integer.parseInt(nref) <= 0) { |
| 55 | + JOptionPane.showMessageDialog(null, "Formato de NRef inválido"); |
| 56 | + return false; |
| 57 | + } else return true; |
| 58 | + } catch (NumberFormatException e) { |
| 59 | + JOptionPane.showMessageDialog(null, "Formato de NRef inválido"); |
| 60 | + return false; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private void realizarEntrega() { |
| 65 | + if (comprobarCampos()) { |
| 66 | + int id = Integer.parseInt(view.getTfID().getText()); |
| 67 | + int nref = Integer.parseInt(view.getTfNRef().getText()); |
| 68 | + if (comprobarEnvio(id, nref)) { |
| 69 | + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| 70 | + LocalDateTime now = LocalDateTime.now(); |
| 71 | + String fechaHoraActual = dtf.format(now); |
| 72 | + model.registrarEntrega(id, nref, "Entrega", view.getTfUbicacion().getText(), fechaHoraActual); |
| 73 | + JOptionPane.showMessageDialog(null, "REGISTRO DE ENTREGA REALIZADO CORRECTAMENTE"); |
| 74 | + view.reset(); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private boolean comprobarEnvio(int id, int nref) { |
| 80 | + PedidosTransportistaDisplayDTO envio = model.getEnvio(id, nref); |
| 81 | + if (envio==null) { |
| 82 | + JOptionPane.showMessageDialog(null, "No existe un pedido con el número: " + nref + "\nasignado al repartidor con ID: " + id); |
| 83 | + return false; |
| 84 | + } else { |
| 85 | + MovimientosDisplayDTO movimiento = model.getLastMov(id, nref); |
| 86 | + if (movimiento != null) { |
| 87 | + if (movimiento.getMovimiento().equals("Descarga")) { |
| 88 | + JOptionPane.showMessageDialog(null, "No puede haber un movimiento de entrega seguido de uno de descarga. \nEl último movimiento debe ser de carga"); |
| 89 | + return false; |
| 90 | + } else if (!envio.getDireccionReceptor().equals(view.getTfUbicacion().getText())) { |
| 91 | + JOptionPane.showMessageDialog(null, "La ubicación de la entrega no coincide con la de la entrega asignada"); |
| 92 | + return false; |
| 93 | + } |
| 94 | + } else { |
| 95 | + JOptionPane.showMessageDialog(null, "El envío aún no ha sido recogido"); |
| 96 | + return false; |
| 97 | + } |
| 98 | + return true; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private boolean comprobarCampos() { |
| 103 | + try { |
| 104 | + if(view.getTfID().getText().isBlank() || view.getTfNRef().getText().isBlank() || view.getTfUbicacion().getText().isBlank()) { |
| 105 | + JOptionPane.showMessageDialog(null, "Rellena todos los campos"); |
| 106 | + return false; |
| 107 | + } else if(Integer.parseInt(view.getTfID().getText()) <= 0 || Integer.parseInt(view.getTfNRef().getText()) <= 0) { |
| 108 | + JOptionPane.showMessageDialog(null, "Formato de ID o Número de referencia inválido"); |
| 109 | + return false; |
| 110 | + } else return true; |
| 111 | + } catch (NumberFormatException e) { |
| 112 | + JOptionPane.showMessageDialog(null, "Formato de ID o Número de referencia inválido"); |
| 113 | + return false; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private void salir() { |
| 118 | + int eleccion=JOptionPane.showConfirmDialog(null, "¿Está segur@ de cancelar la entrega del envío?"); |
| 119 | + if(eleccion==JOptionPane.YES_OPTION) |
| 120 | + view.reset(); |
| 121 | + } |
| 122 | + |
| 123 | + public void initView() { |
| 124 | + view.getFrame().setVisible(true); |
| 125 | + } |
| 126 | +} |
0 commit comments