Skip to content

Commit fc418e2

Browse files
authored
Merge pull request #4 from uo287568/HU06-CargaDeEnvíos
Hu06 carga de envíos
2 parents 2138a30 + 39eda23 commit fc418e2

9 files changed

+365
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package giis.demo.tkrun;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.format.DateTimeFormatter;
5+
6+
import javax.swing.JOptionPane;
7+
8+
import giis.demo.util.SwingUtil;
9+
10+
public class CargaController {
11+
private CargaModel model;
12+
private CargaView view;
13+
14+
public CargaController(CargaModel cargaModel, CargaView cargaView) {
15+
this.model = cargaModel;
16+
this.view = cargaView;
17+
//no hay inicializacion especifica del modelo, solo de la vista
18+
this.initView();
19+
}
20+
21+
public void initController() {
22+
view.getBtCancelar().addActionListener(e -> SwingUtil.exceptionWrapper(() -> salir()));
23+
view.getBtRegistrar().addActionListener(e -> realizarCarga());
24+
}
25+
26+
private void realizarCarga() {
27+
if (comprobarCampos()) {
28+
int id = Integer.parseInt(view.getTfID().getText());
29+
int nref = Integer.parseInt(view.getTfNRef().getText());
30+
if (comprobarEnvio(id, nref)) {
31+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
32+
LocalDateTime now = LocalDateTime.now();
33+
String fechaHoraActual = dtf.format(now);
34+
model.registrarCarga(id, nref, "Carga", view.getTfUbicacion().getText(), fechaHoraActual);
35+
JOptionPane.showMessageDialog(null, "REGISTRO DE CARGA REALIZADO CORRECTAMENTE");
36+
view.reset();
37+
}
38+
}
39+
}
40+
41+
private boolean comprobarEnvio(int id, int nref) {
42+
PedidosTransportistaDisplayDTO envio = model.getEnvio(id, nref);
43+
if (envio==null) {
44+
JOptionPane.showMessageDialog(null, "No existe un pedido con el número: " + nref + "\nasignado al repartidor con ID: " + id);
45+
return false;
46+
} else {
47+
MovimientosDisplayDTO movimiento = model.getLastMov(id, nref);
48+
if (movimiento != null) {
49+
if (movimiento.getMovimiento().equals("Carga")) {
50+
JOptionPane.showMessageDialog(null, "No puede haber un movimiento de carga seguido de otro de carga. \nEl último movimiento debe ser de descarga");
51+
return false;
52+
} else if (!movimiento.getUbicacion().equals(view.getTfUbicacion().getText())) {
53+
JOptionPane.showMessageDialog(null, "La ubicación de la carga no coincide con la de la última descarga");
54+
return false;
55+
}
56+
}
57+
return true;
58+
}
59+
}
60+
61+
private boolean comprobarCampos() {
62+
try {
63+
if(view.getTfID().getText().isBlank() || view.getTfNRef().getText().isBlank() || view.getTfUbicacion().getText().isBlank()) {
64+
JOptionPane.showMessageDialog(null, "Rellena todos los campos");
65+
return false;
66+
} else if(Integer.parseInt(view.getTfID().getText()) <= 0 || Integer.parseInt(view.getTfNRef().getText()) <= 0) {
67+
JOptionPane.showMessageDialog(null, "Formato de ID o Número de referencia inválido");
68+
return false;
69+
} else return true;
70+
} catch (NumberFormatException e) {
71+
JOptionPane.showMessageDialog(null, "Formato de ID o Número de referencia inválido");
72+
return false;
73+
}
74+
}
75+
76+
private void salir() {
77+
int eleccion=JOptionPane.showConfirmDialog(null, "¿Está segur@ de cancelar la carga del envío?");
78+
if(eleccion==JOptionPane.YES_OPTION)
79+
view.reset();
80+
}
81+
82+
public void initView() {
83+
view.getFrame().setVisible(true);
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package giis.demo.tkrun;
2+
3+
import java.util.List;
4+
5+
import giis.demo.util.Database;
6+
/**
7+
* Acceso a los datos de carreras e inscripciones,
8+
* utilizado como modelo para el ejemplo de swing y para las pruebas unitarias y de interfaz de usuario.
9+
*
10+
* <br/>En los metodos de este ejemplo toda la logica de negocio se realiza mediante una unica query sql por lo que siempre
11+
* se utilizan los metodos de utilidad en la clase Database que usan apache commons-dbutils y controlan la conexion.
12+
* En caso de que en un mismo metodo se realicen diferentes queries se deberia controlar la conexion desde esta clase
13+
* (ver como ejemplo la implementacion en Database).
14+
*
15+
* <br/>Si utilizase algún otro framework para manejar la persistencia, la funcionalidad proporcionada por esta clase sería la asignada
16+
* a los Servicios, Repositorios y DAOs.
17+
*/
18+
public class CargaModel {
19+
20+
private Database db=new Database();
21+
22+
public PedidosTransportistaDisplayDTO getEnvio(int id, int nref) {
23+
String sql = "select * from pedidosTransportista where id=? and nref=?";
24+
List<PedidosTransportistaDisplayDTO> envios = db.executeQueryPojo(PedidosTransportistaDisplayDTO.class, sql, id, nref);
25+
if(envios.isEmpty()) {
26+
return null;
27+
}
28+
return envios.get(0);
29+
}
30+
31+
public MovimientosDisplayDTO getLastMov(int id, int nref) {
32+
String sql = "select * from movimientos where id=? and nref=? order by fechamov desc";
33+
List<MovimientosDisplayDTO> movimientos = db.executeQueryPojo(MovimientosDisplayDTO.class, sql, id, nref);
34+
if(movimientos.isEmpty()) {
35+
return null;
36+
}
37+
return movimientos.get(0);
38+
}
39+
40+
public void registrarCarga(int id, int nref, String mov, String ubi, String fechaHoraActual) {
41+
String sql="insert into movimientos (id,nref,movimiento,ubicacion,fechaMov) values (?,?,?,?,?)";
42+
db.executeUpdate(sql,id,nref,mov,ubi,fechaHoraActual);
43+
}
44+
45+
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package giis.demo.tkrun;
2+
3+
import javax.swing.JFrame;
4+
import net.miginfocom.swing.MigLayout;
5+
import javax.swing.JLabel;
6+
import javax.swing.JOptionPane;
7+
import javax.swing.JTextField;
8+
import javax.swing.JButton;
9+
10+
import javax.swing.JPanel;
11+
import java.awt.Font;
12+
import java.awt.FlowLayout;
13+
import java.awt.GridLayout;
14+
import javax.swing.JTextArea;
15+
import java.awt.Color;
16+
17+
public class CargaView {
18+
19+
private JFrame frmCargaEnvios;
20+
private JLabel lbTítulo;
21+
private JButton btnCancelar;
22+
private JButton btRegistro;
23+
private JLabel lbID;
24+
private JTextField tfID;
25+
private JLabel lbNRef;
26+
private JTextField tfNRef;
27+
private JLabel lbUbicacion;
28+
private JTextField tfUbicacion;
29+
private JLabel lbFecha;
30+
31+
/**
32+
* Create the application.
33+
*/
34+
public CargaView() {
35+
initialize();
36+
}
37+
38+
/**
39+
* Initialize the contents of the frame.
40+
*/
41+
private void initialize() {
42+
frmCargaEnvios = new JFrame();
43+
frmCargaEnvios.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 13));
44+
frmCargaEnvios.setTitle("CargaEnvíos");
45+
frmCargaEnvios.setName("CargaEnvíos");
46+
frmCargaEnvios.setBounds(0, 0, 366, 231);
47+
frmCargaEnvios.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
48+
frmCargaEnvios.getContentPane().setLayout(new MigLayout("", "[grow]", "[][10.00][][][][][16.00][]"));
49+
50+
lbTítulo = new JLabel("Registro de carga de envíos:");
51+
lbTítulo.setFont(new Font("Tahoma", Font.BOLD, 15));
52+
frmCargaEnvios.getContentPane().add(lbTítulo, "cell 0 0");
53+
54+
lbID = new JLabel("ID del transportista: ");
55+
lbID.setFont(new Font("Tahoma", Font.PLAIN, 13));
56+
frmCargaEnvios.getContentPane().add(lbID, "flowx,cell 0 2");
57+
58+
lbNRef = new JLabel("Número de referencia del envío: ");
59+
lbNRef.setFont(new Font("Tahoma", Font.PLAIN, 13));
60+
frmCargaEnvios.getContentPane().add(lbNRef, "flowx,cell 0 3");
61+
62+
lbUbicacion = new JLabel("Ubicación de la carga:");
63+
lbUbicacion.setFont(new Font("Tahoma", Font.PLAIN, 13));
64+
frmCargaEnvios.getContentPane().add(lbUbicacion, "flowx,cell 0 4");
65+
66+
lbFecha = new JLabel("Se registrará la fecha y hora de la carga (hora actual)");
67+
lbFecha.setFont(new Font("Tahoma", Font.PLAIN, 13));
68+
frmCargaEnvios.getContentPane().add(lbFecha, "cell 0 5");
69+
70+
tfID = new JTextField();
71+
frmCargaEnvios.getContentPane().add(tfID, "cell 0 2");
72+
tfID.setColumns(10);
73+
74+
tfNRef = new JTextField();
75+
frmCargaEnvios.getContentPane().add(tfNRef, "cell 0 3");
76+
tfNRef.setColumns(10);
77+
78+
tfUbicacion = new JTextField();
79+
frmCargaEnvios.getContentPane().add(tfUbicacion, "cell 0 4");
80+
tfUbicacion.setColumns(10);
81+
82+
JPanel pnBotones = new JPanel();
83+
FlowLayout fl_pnBotones = (FlowLayout) pnBotones.getLayout();
84+
fl_pnBotones.setAlignment(FlowLayout.RIGHT);
85+
frmCargaEnvios.getContentPane().add(pnBotones, "cell 0 7,grow");
86+
87+
btnCancelar = new JButton("Cancelar");
88+
btnCancelar.setForeground(Color.WHITE);
89+
btnCancelar.setBackground(Color.RED);
90+
btnCancelar.setFont(new Font("Tahoma", Font.BOLD, 12));
91+
pnBotones.add(btnCancelar);
92+
93+
btRegistro = new JButton("Registrar carga");
94+
btRegistro.setBackground(Color.GREEN);
95+
btRegistro.setFont(new Font("Tahoma", Font.BOLD, 12));
96+
pnBotones.add(btRegistro);
97+
}
98+
99+
public void reset() {
100+
this.tfID.setText("");
101+
this.tfNRef.setText("");
102+
this.tfUbicacion.setText("");
103+
this.getFrame().setVisible(false);
104+
}
105+
106+
//Getters y Setters anyadidos para acceso desde el controlador (repersentacion compacta)
107+
public JFrame getFrame() { return this.frmCargaEnvios; }
108+
public JButton getBtCancelar() { return this.btnCancelar; }
109+
public JButton getBtRegistrar() { return this.btRegistro; }
110+
public JTextField getTfID() { return tfID; }
111+
public JTextField getTfNRef() { return tfNRef; }
112+
public JTextField getTfUbicacion() { return tfUbicacion; }
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package giis.demo.tkrun;
2+
3+
public class MovimientosDisplayDTO {
4+
private int id;
5+
private int nref;
6+
private String movimiento;
7+
private String ubicacion;
8+
private String fechaMov;
9+
public MovimientosDisplayDTO() {}
10+
public MovimientosDisplayDTO(int id, int nref, String movimiento, String ubicacion, String fechaMov) {
11+
super();
12+
this.id = id;
13+
this.nref = nref;
14+
this.movimiento = movimiento;
15+
this.ubicacion = ubicacion;
16+
this.fechaMov = fechaMov;
17+
}
18+
public int getId() {
19+
return id;
20+
}
21+
public int getNref() {
22+
return nref;
23+
}
24+
public String getMovimiento() {
25+
return movimiento;
26+
}
27+
public String getUbicacion() {
28+
return ubicacion;
29+
}
30+
public String getFechaMov() {
31+
return fechaMov;
32+
}
33+
public void setId(int id) {
34+
this.id = id;
35+
}
36+
public void setNref(int nref) {
37+
this.nref = nref;
38+
}
39+
public void setMovimiento(String movimiento) {
40+
this.movimiento = movimiento;
41+
}
42+
public void setUbicacion(String ubicacion) {
43+
this.ubicacion = ubicacion;
44+
}
45+
public void setFechaMov(String fechaMov) {
46+
this.fechaMov = fechaMov;
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package giis.demo.tkrun;
2+
3+
public class MovimientosEntity {
4+
private int id;
5+
private int nref;
6+
private String movimiento;
7+
private String ubicacion;
8+
private String fechaMov;
9+
10+
public int getId() {
11+
return id;
12+
}
13+
public int getNref() {
14+
return nref;
15+
}
16+
public String getMovimiento() {
17+
return movimiento;
18+
}
19+
public String getUbicacion() {
20+
return ubicacion;
21+
}
22+
public String getFechaMov() {
23+
return fechaMov;
24+
}
25+
public void setId(int id) {
26+
this.id = id;
27+
}
28+
public void setNref(int nref) {
29+
this.nref = nref;
30+
}
31+
public void setMovimiento(String movimiento) {
32+
this.movimiento = movimiento;
33+
}
34+
public void setUbicacion(String ubicacion) {
35+
this.ubicacion = ubicacion;
36+
}
37+
public void setFechaMov(String fechaMov) {
38+
this.fechaMov = fechaMov;
39+
}
40+
}

0 commit comments

Comments
 (0)