Skip to content

Commit 8c63be0

Browse files
authored
Merge pull request #6 from uo287568/HU08-DescargaDeEnvios
HU de descarga de envíos realizada
2 parents 99cd995 + 347ea1b commit 8c63be0

File tree

5 files changed

+268
-17
lines changed

5 files changed

+268
-17
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 DescargaController {
11+
private DescargaModel model;
12+
private DescargaView view;
13+
14+
public DescargaController(DescargaModel descargaModel, DescargaView descargaView) {
15+
this.model = descargaModel;
16+
this.view = descargaView;
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 -> realizarDescarga());
24+
}
25+
26+
private void realizarDescarga() {
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.registrarDescarga(id, nref, "Descarga", view.getTfUbicacion().getText(), fechaHoraActual);
35+
JOptionPane.showMessageDialog(null, "REGISTRO DE DESCARGA 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("Descarga")) {
50+
JOptionPane.showMessageDialog(null, "No puede haber un movimiento de descarga seguido de otro de descarga. \nEl último movimiento debe ser de carga");
51+
return false;
52+
} else if (movimiento.getUbicacion().equals(view.getTfUbicacion().getText())) {
53+
JOptionPane.showMessageDialog(null, "La ubicación de la descarga coincide con la de la última carga");
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 descarga 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 DescargaModel {
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 registrarDescarga(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,110 @@
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.JTextField;
7+
import javax.swing.JButton;
8+
9+
import javax.swing.JPanel;
10+
import java.awt.Font;
11+
import java.awt.FlowLayout;
12+
import java.awt.Color;
13+
14+
public class DescargaView {
15+
16+
private JFrame frmDescargaEnvios;
17+
private JLabel lbTítulo;
18+
private JButton btnCancelar;
19+
private JButton btRegistro;
20+
private JLabel lbID;
21+
private JTextField tfID;
22+
private JLabel lbNRef;
23+
private JTextField tfNRef;
24+
private JLabel lbUbicacion;
25+
private JTextField tfUbicacion;
26+
private JLabel lbFecha;
27+
28+
/**
29+
* Create the application.
30+
*/
31+
public DescargaView() {
32+
initialize();
33+
}
34+
35+
/**
36+
* Initialize the contents of the frame.
37+
*/
38+
private void initialize() {
39+
frmDescargaEnvios = new JFrame();
40+
frmDescargaEnvios.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 13));
41+
frmDescargaEnvios.setTitle("DescargaEnvíos");
42+
frmDescargaEnvios.setName("DescargaEnvíos");
43+
frmDescargaEnvios.setBounds(0, 0, 374, 231);
44+
frmDescargaEnvios.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
45+
frmDescargaEnvios.getContentPane().setLayout(new MigLayout("", "[grow]", "[][10.00][][][][][16.00][]"));
46+
47+
lbTítulo = new JLabel("Registro de descarga de envíos:");
48+
lbTítulo.setFont(new Font("Tahoma", Font.BOLD, 15));
49+
frmDescargaEnvios.getContentPane().add(lbTítulo, "cell 0 0");
50+
51+
lbID = new JLabel("ID del transportista: ");
52+
lbID.setFont(new Font("Tahoma", Font.PLAIN, 13));
53+
frmDescargaEnvios.getContentPane().add(lbID, "flowx,cell 0 2");
54+
55+
lbNRef = new JLabel("Número de referencia del envío: ");
56+
lbNRef.setFont(new Font("Tahoma", Font.PLAIN, 13));
57+
frmDescargaEnvios.getContentPane().add(lbNRef, "flowx,cell 0 3");
58+
59+
lbUbicacion = new JLabel("Ubicación de la descarga:");
60+
lbUbicacion.setFont(new Font("Tahoma", Font.PLAIN, 13));
61+
frmDescargaEnvios.getContentPane().add(lbUbicacion, "flowx,cell 0 4");
62+
63+
lbFecha = new JLabel("Se registrará la fecha y hora de la descarga (hora actual)");
64+
lbFecha.setFont(new Font("Tahoma", Font.PLAIN, 13));
65+
frmDescargaEnvios.getContentPane().add(lbFecha, "cell 0 5");
66+
67+
tfID = new JTextField();
68+
frmDescargaEnvios.getContentPane().add(tfID, "cell 0 2");
69+
tfID.setColumns(10);
70+
71+
tfNRef = new JTextField();
72+
frmDescargaEnvios.getContentPane().add(tfNRef, "cell 0 3");
73+
tfNRef.setColumns(10);
74+
75+
tfUbicacion = new JTextField();
76+
frmDescargaEnvios.getContentPane().add(tfUbicacion, "cell 0 4");
77+
tfUbicacion.setColumns(10);
78+
79+
JPanel pnBotones = new JPanel();
80+
FlowLayout fl_pnBotones = (FlowLayout) pnBotones.getLayout();
81+
fl_pnBotones.setAlignment(FlowLayout.RIGHT);
82+
frmDescargaEnvios.getContentPane().add(pnBotones, "cell 0 7,grow");
83+
84+
btnCancelar = new JButton("Cancelar");
85+
btnCancelar.setForeground(Color.WHITE);
86+
btnCancelar.setBackground(Color.RED);
87+
btnCancelar.setFont(new Font("Tahoma", Font.BOLD, 12));
88+
pnBotones.add(btnCancelar);
89+
90+
btRegistro = new JButton("Registrar descarga");
91+
btRegistro.setBackground(Color.GREEN);
92+
btRegistro.setFont(new Font("Tahoma", Font.BOLD, 12));
93+
pnBotones.add(btRegistro);
94+
}
95+
96+
public void reset() {
97+
this.tfID.setText("");
98+
this.tfNRef.setText("");
99+
this.tfUbicacion.setText("");
100+
this.getFrame().setVisible(false);
101+
}
102+
103+
//Getters y Setters anyadidos para acceso desde el controlador (repersentacion compacta)
104+
public JFrame getFrame() { return this.frmDescargaEnvios; }
105+
public JButton getBtCancelar() { return this.btnCancelar; }
106+
public JButton getBtRegistrar() { return this.btRegistro; }
107+
public JTextField getTfID() { return tfID; }
108+
public JTextField getTfNRef() { return tfNRef; }
109+
public JTextField getTfUbicacion() { return tfUbicacion; }
110+
}

src/main/java/giis/demo/tkrun/LocalizacionEnvioView.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
public class LocalizacionEnvioView {
2626

27-
private JFrame frmTransportista;
27+
private JFrame frmLocalizacion;
2828
private JLabel lbTítulo;
2929
private JTextField tfNRef;
3030
private JPanel pnEnvio;
@@ -51,21 +51,21 @@ public LocalizacionEnvioView() {
5151
* Initialize the contents of the frame.
5252
*/
5353
private void initialize() {
54-
frmTransportista = new JFrame();
55-
frmTransportista.setTitle("EnvíosTransportista");
56-
frmTransportista.setName("EnvíosTransportista");
57-
frmTransportista.setBounds(0, 0, 577, 438);
58-
frmTransportista.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
59-
frmTransportista.getContentPane().setLayout(new MigLayout("", "[grow]", "[][][][][][grow][][grow][][]"));
54+
frmLocalizacion = new JFrame();
55+
frmLocalizacion.setTitle("LocalizaciónEnvíos");
56+
frmLocalizacion.setName("LocalizaciónEnvíos");
57+
frmLocalizacion.setBounds(0, 0, 577, 438);
58+
frmLocalizacion.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
59+
frmLocalizacion.getContentPane().setLayout(new MigLayout("", "[grow]", "[][][][][][grow][][grow][][]"));
6060

6161
lbTítulo = new JLabel("Localización de un envío:");
6262
lbTítulo.setFont(new Font("Tahoma", Font.BOLD, 15));
63-
frmTransportista.getContentPane().add(lbTítulo, "cell 0 0");
63+
frmLocalizacion.getContentPane().add(lbTítulo, "cell 0 0");
6464

6565
pnEnvio = new JPanel();
6666
FlowLayout fl_pnEnvio = (FlowLayout) pnEnvio.getLayout();
6767
fl_pnEnvio.setAlignment(FlowLayout.LEFT);
68-
frmTransportista.getContentPane().add(pnEnvio, "cell 0 2,grow");
68+
frmLocalizacion.getContentPane().add(pnEnvio, "cell 0 2,grow");
6969

7070
lbNRef = new JLabel("Número de referencia del envío: ");
7171
lbNRef.setFont(new Font("Tahoma", Font.PLAIN, 13));
@@ -82,25 +82,25 @@ private void initialize() {
8282

8383
lbEstado = new JLabel("El envío se encuentra en el estado: ");
8484
lbEstado.setFont(new Font("Tahoma", Font.PLAIN, 13));
85-
frmTransportista.getContentPane().add(lbEstado, "flowx,cell 0 3");
85+
frmLocalizacion.getContentPane().add(lbEstado, "flowx,cell 0 3");
8686

8787
lbInfo = new JLabel("Información general del envío:");
8888
lbInfo.setFont(new Font("Tahoma", Font.PLAIN, 13));
89-
frmTransportista.getContentPane().add(lbInfo, "cell 0 4");
89+
frmLocalizacion.getContentPane().add(lbInfo, "cell 0 4");
9090

9191
pnInfo = new JScrollPane();
92-
frmTransportista.getContentPane().add(pnInfo, "cell 0 5,grow");
92+
frmLocalizacion.getContentPane().add(pnInfo, "cell 0 5,grow");
9393

9494
tbInfo = new JTable();
9595
tbInfo.setDefaultEditor(Object.class, null); //readonly
9696
pnInfo.setViewportView(tbInfo);
9797

9898
lbMovimientos = new JLabel("Movimientos del envío:");
9999
lbMovimientos.setFont(new Font("Tahoma", Font.PLAIN, 13));
100-
frmTransportista.getContentPane().add(lbMovimientos, "cell 0 6");
100+
frmLocalizacion.getContentPane().add(lbMovimientos, "cell 0 6");
101101

102102
pnMovimientos = new JScrollPane();
103-
frmTransportista.getContentPane().add(pnMovimientos, "cell 0 7,grow");
103+
frmLocalizacion.getContentPane().add(pnMovimientos, "cell 0 7,grow");
104104

105105
tbMovimientos = new JTable();
106106
tbMovimientos.setDefaultEditor(Object.class, null); //readonly
@@ -109,7 +109,7 @@ private void initialize() {
109109
JPanel pnSalir = new JPanel();
110110
FlowLayout fl_pnSalir = (FlowLayout) pnSalir.getLayout();
111111
fl_pnSalir.setAlignment(FlowLayout.RIGHT);
112-
frmTransportista.getContentPane().add(pnSalir, "cell 0 9,grow");
112+
frmLocalizacion.getContentPane().add(pnSalir, "cell 0 9,grow");
113113

114114
btnSalir = new JButton("Salir");
115115
btnSalir.setForeground(Color.WHITE);
@@ -120,7 +120,7 @@ private void initialize() {
120120
tfEstado = new JTextField();
121121
tfEstado.setFont(new Font("Tahoma", Font.BOLD, 13));
122122
tfEstado.setEditable(false);
123-
frmTransportista.getContentPane().add(tfEstado, "cell 0 3");
123+
frmLocalizacion.getContentPane().add(tfEstado, "cell 0 3");
124124
tfEstado.setColumns(10);
125125
}
126126

@@ -131,7 +131,7 @@ public void reset() {
131131
}
132132

133133
//Getters y Setters anyadidos para acceso desde el controlador (repersentacion compacta)
134-
public JFrame getFrame() { return this.frmTransportista; }
134+
public JFrame getFrame() { return this.frmLocalizacion; }
135135
public JButton getBtSalir() { return this.btnSalir; }
136136
public JButton getBtBuscar() { return this.btBuscar; }
137137
public JTextField getTfNRef() { return tfNRef; }

src/main/java/giis/demo/util/SwingMain.java

+9
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ public void actionPerformed(ActionEvent e) {
106106
}
107107
});
108108
frame.getContentPane().add(btnCarga);
109+
110+
JButton btnDescarga = new JButton("Descarga de envíos");
111+
btnDescarga.addActionListener(new ActionListener() { //NOSONAR codigo autogenerado
112+
public void actionPerformed(ActionEvent e) {
113+
DescargaController controller=new DescargaController(new DescargaModel(), new DescargaView());
114+
controller.initController();
115+
}
116+
});
117+
frame.getContentPane().add(btnDescarga);
109118
}
110119

111120
public JFrame getFrame() { return this.frame; }

0 commit comments

Comments
 (0)