Browse Source

ya se insertan los tiposd de hardware con sus respectivos campos de numero de serie, modelo y placa

master
mramirezg 9 months ago
parent
commit
83306d0c21
2 changed files with 155 additions and 18 deletions
  1. +80
    -10
      src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java
  2. +75
    -8
      src/main/java/mx/gob/jumapacelaya/ui/MantenimientoView.java

+ 80
- 10
src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java View File

@ -1,6 +1,7 @@
package mx.gob.jumapacelaya.services;
import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@ -18,14 +19,14 @@ public class DatabaseService {
// Método para obtener los tipos de mantenimientos
public List<String> getTiposDeMantenimientos() {
List<String> tiposDeMantenimientos = new ArrayList<>();
String query = "SELECT * FROM TIPOMANT";
String query = "SELECT tipomantid FROM TIPOMANT";
try (Connection connection = DriverManager.getConnection("jdbc:mysql://mhdb.jumapacelaya.gob.mx:33006/Mantenimientos", "root", "mantenimientos");
try (Connection connection = getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)) {
while (resultSet.next()) {
tiposDeMantenimientos.add(resultSet.getString("NOMBRE"));
tiposDeMantenimientos.add(resultSet.getString("TIPOMANTID"));
}
} catch (SQLException e) {
e.printStackTrace();
@ -36,7 +37,7 @@ public class DatabaseService {
// Método para obtener las nomenclaturas
public String getNomenclatura(String tipoMantenimiento ) {
String nomenclatura = "";
String query = "SELECT nomenclatura FROM TIPOMANT where NOMBRE = ?";
String query = "SELECT nomenclatura FROM TIPOMANT where TIPOMANTID = ?";
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
@ -58,14 +59,14 @@ public class DatabaseService {
/* -------------- Metodo para obtener a los usuarios ---------------- */
public List<String> getUsuarios() {
List<String> usuarios = new ArrayList<>();
String query = "select USUARIOID from USUARIOSFINAN";
String query = "select EMPLEADOID from USUARIOSFINAN";
try (Connection connection = getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)) {
while (resultSet.next()) {
usuarios.add(resultSet.getString("usuarioid"));
usuarios.add(resultSet.getString("EMPLEADOID"));
}
} catch (SQLException e) {
e.printStackTrace();
@ -77,14 +78,14 @@ public class DatabaseService {
/* -------------- Metodo para obtener los departamentos ---------------- */
public List<String> getDepartamentos() {
List<String> departamentos = new ArrayList<>();
String query = "select DESCRIPCION from DEPARTAMENTOSFINAN";
String query = "select DEPARTAMENTOID from DEPARTAMENTOSFINAN";
try (Connection connection = getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)) {
while (resultSet.next()) {
departamentos.add(resultSet.getString("descripcion"));
departamentos.add(resultSet.getString("DEPARTAMENTOID"));
}
} catch (SQLException e) {
e.printStackTrace();
@ -97,14 +98,14 @@ public class DatabaseService {
/* -------------- Metodo para obtener los tipos de hardware ---------------- */
public List<String> getTiposHardware() {
List<String> tiposHardware = new ArrayList<>();
String query = "select NOMBRETIPO from TIPOSHARDWARE";
String query = "select TIPOHARDWAREID from TIPOSHARDWARE";
try (Connection connection = getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)) {
while (resultSet.next()) {
tiposHardware.add(resultSet.getString("nombretipo"));
tiposHardware.add(resultSet.getString("TIPOHARDWAREID"));
}
} catch (SQLException e) {
e.printStackTrace();
@ -133,4 +134,73 @@ public class DatabaseService {
}
return nombreUsuario;
}
/* -------------- Metodos para insetar en la BD ---------------- */
// INSERTAR EN TABLA: MANTENIMINETOS
public int insertarMantenimiento(LocalDate fecha, String tipoMantId, String departamentoId, String empleadoId) {
String query = "INSERT INTO MANTENIMIENTOS (fecha, tipoMantId, departamentoId, empleadoId) VALUES (?, ?, ?, ?)";
int nuevoId = -1;
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) {
preparedStatement.setDate(1, Date.valueOf(fecha));
preparedStatement.setString(2, tipoMantId);
preparedStatement.setString(3, departamentoId);
preparedStatement.setString(4, empleadoId);
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) {
if (generatedKeys.next()) {
nuevoId = generatedKeys.getInt(1);
}
}
}
} catch (SQLException e) {
System.err.println("Error al insertar mantenimiento: " + e.getMessage());
}
return nuevoId;
}
// INSERTAR EN LA TABLA: HARDWAREDET
public boolean insertarHardware(String tipoHardwareId, int numSerie, String modelo, int placa, int mantenimientoId) {
String query = "INSERT INTO HARDWAREDET (tipoHardwareId, numSerie, modelo, placa, mantenimientoId) VALUES (?, ?, ?, ?, ?)";
boolean isInserted = false;
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, tipoHardwareId);
preparedStatement.setInt(2, numSerie);
preparedStatement.setString(3, modelo);
preparedStatement.setInt(4, placa);
preparedStatement.setInt(5, mantenimientoId);
int rowsAffected = preparedStatement.executeUpdate();
isInserted = rowsAffected > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return isInserted;
}
public int getUltimoMantenimientoId() {
int ultimoId = -1;
try (Connection connection = getConnection()) {
String query = "SELECT MAX(mantenimientoid) FROM MANTENIMIENTOS";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
ultimoId = resultSet.getInt(1);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return ultimoId;
}
}

+ 75
- 8
src/main/java/mx/gob/jumapacelaya/ui/MantenimientoView.java View File

@ -30,6 +30,7 @@ import jakarta.annotation.security.PermitAll;
import mx.gob.jumapacelaya.services.DatabaseService;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@ -46,6 +47,8 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse
private final HorizontalLayout firmasLayout;
private final DatePicker fecha;
private final ComboBox<String> tipoMantt;
private final ComboBox<String> area;
private final ComboBox<String> usuario;
private final HorizontalLayout botonesLayout;
@ -77,9 +80,8 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse
//ComboBox Tipo de Mantenimiento
List<String> tiposDeMantenimiento = databaseService.getTiposDeMantenimientos();
this.tipoMantt = new ComboBox<>("Tipo de Mantenimiento");
tipoMantt.setItems(tiposDeMantenimiento);
tipoMantt.setItems(databaseService.getTiposDeMantenimientos());
tipoMantt.addClassName("mantenimiento-combo");
tipoMantt.addValueChangeListener(event -> {
String tipoSeleccionado = event.getValue();
@ -109,13 +111,12 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse
HorizontalLayout departamentoLayout = new HorizontalLayout();
//ComboBox Area o Departamento
ComboBox<String> area = new ComboBox<>("Area o Departamento");
this.area = new ComboBox<>("Area o Departamento");
List<String> areas = databaseService.getDepartamentos();
area.setItems(areas);
ComboBox<String> usuario = new ComboBox<>("Usuario");
List<String> usuarios = databaseService.getUsuarios();
usuario.setItems(usuarios);
this.usuario = new ComboBox<>("Usuario");
usuario.setItems(databaseService.getUsuarios());
usuario.addValueChangeListener(event -> {
String usuarioSeleccionado = event.getValue();
if (usuarioSeleccionado != null) {
@ -163,6 +164,7 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse
//Metodo para agregar un nuevo ComboBox de tipo de hardware y campos de texto
private List<HorizontalLayout> hardwareLayouts = new ArrayList<>();
private void addNuevoTipo() {
ComboBox<String> tipoHardware = new ComboBox<>();
List<String> tiposHardware = databaseService.getTiposHardware();
@ -192,7 +194,8 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse
String tipoSeleccionado = event.getValue();
if (tipoSeleccionado != null) {
if ("MOUSE".equalsIgnoreCase(tipoSeleccionado) || "TECLADO".equalsIgnoreCase(tipoSeleccionado)) {
int tipoId = Integer.parseInt(tipoSeleccionado);
if (tipoId == 6 || tipoId == 7) {
noSerie.setEnabled(false);
modelo.setEnabled(false);
placa.setEnabled(false);
@ -219,6 +222,9 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse
hardwareDetailsLayout.add(tipoHardware, noSerie, modelo, placa, btnEliminar);
hardwareDetailsLayout.setWidthFull();
hardwareLayouts.add(hardwareDetailsLayout);
controlsLayout.add(hardwareDetailsLayout);
//Listener para eliminar el tipo
btnEliminar.addClickListener(event -> {
@ -375,9 +381,70 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse
HorizontalLayout buttonsLayout = new HorizontalLayout();
Button btnGuardar = new Button("Guardar");
btnGuardar.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
btnGuardar.addClickListener(event -> {
LocalDate fechaSeleccionada = fecha.getValue();
String tipoMantId = tipoMantt.getValue();
String departamentoId = area.getValue();
String empleadoId = usuario.getValue();
if (fechaSeleccionada == null || tipoMantId == null || departamentoId == null || empleadoId == null) {
Notification.show("Por favor, completa todos los campos requeridos", 4000, Notification.Position.MIDDLE);
return;
}
int isInserted = databaseService.insertarMantenimiento(fechaSeleccionada, tipoMantId, departamentoId, empleadoId);
if (isInserted > 0) {
int mantenimientoId = databaseService.getUltimoMantenimientoId();
for (HorizontalLayout layout : hardwareLayouts) {
ComboBox<String> tipoHardware = (ComboBox<String>) layout.getComponentAt(0);
NumberField noSerie = (NumberField) layout.getComponentAt(1);
TextField modelo = (TextField) layout.getComponentAt(2);
NumberField placa = (NumberField) layout.getComponentAt(3);
int numSerieInt = noSerie.getValue() != null ? noSerie.getValue().intValue() : 0;
int placaInt = placa.getValue() != null ? placa.getValue().intValue() : 0;
// Validaciones antes de insertar hardware
if (tipoHardware.getValue() == null || modelo.getValue() == null || numSerieInt <= 0 || placaInt <= 0) {
Notification.show("Por favor, completa todos los campos de hardware", 4000, Notification.Position.MIDDLE);
return;
}
boolean isHardwareInserted = databaseService.insertarHardware(
tipoHardware.getValue(),
numSerieInt,
modelo.getValue(),
placaInt,
mantenimientoId
);
tipoHardware.clear();
modelo.clear();
placa.clear();
noSerie.clear();
if (!isHardwareInserted) {
Notification.show("Error al insertar detalles del hardware", 4000, Notification.Position.MIDDLE);
return;
}
}
Notification.show("!!Mantenimiento guardado exitosamente!!", 4000, Notification.Position.MIDDLE);
fecha.clear();
tipoMantt.clear();
area.clear();
usuario.clear();
} else {
Notification.show("Error al guardar el mantenimeinto", 4000, Notification.Position.MIDDLE);
}
});
buttonsLayout.setSizeFull();
buttonsLayout.add(btnGuardar);
botonesLayout.add(buttonsLayout);
}


Loading…
Cancel
Save