diff --git a/src/main/java/mx/gob/jumapacelaya/models/PlanAnual.java b/src/main/java/mx/gob/jumapacelaya/models/PlanAnual.java index 25f7c11..3efcaa1 100644 --- a/src/main/java/mx/gob/jumapacelaya/models/PlanAnual.java +++ b/src/main/java/mx/gob/jumapacelaya/models/PlanAnual.java @@ -1,5 +1,7 @@ package mx.gob.jumapacelaya.models; +import java.time.LocalDate; + public class PlanAnual { private int numero; private String nomEquipo; @@ -13,17 +15,18 @@ public class PlanAnual { private boolean miniPrint; // Cambiado a boolean private boolean laptop; // Cambiado a boolean private boolean escaner; // Cambiado a boolean - private String fecha; + private LocalDate fechaProgramada; + private LocalDate fechaMantenimiento; private String smt; private String estado; - private String fechaRealizado; // Constructor public PlanAnual(int numero, String nomEquipo, String departamento, boolean monitor, boolean teclado, boolean mouse, boolean regulador, boolean cpu, boolean impresora, boolean miniPrint, - boolean laptop, boolean escaner, String fecha, - String smt, String estado, String fechaRealizado) { + boolean laptop, boolean escaner, LocalDate fechaProgramada, + LocalDate fechaMantenimiento, String smt, String estado) { + this.numero = numero; this.nomEquipo = nomEquipo; this.departamento = departamento; @@ -36,10 +39,10 @@ public class PlanAnual { this.miniPrint = miniPrint; this.laptop = laptop; this.escaner = escaner; - this.fecha = fecha; + this.fechaProgramada = fechaProgramada; + this.fechaMantenimiento = fechaMantenimiento; this.smt = smt; this.estado = estado; - this.fechaRealizado = fechaRealizado; } // Getters @@ -91,9 +94,6 @@ public class PlanAnual { return escaner; } - public String getFecha() { - return fecha; - } public String getSmt() { return smt; @@ -103,7 +103,23 @@ public class PlanAnual { return estado; } - public String getFechaRealizado() { - return fechaRealizado; + public void setNumero(int numero) { + this.numero = numero; + } + + public LocalDate getFechaMantenimiento() { + return fechaMantenimiento; + } + + public void setFechaMantenimiento(LocalDate fechaMantenimiento) { + this.fechaMantenimiento = fechaMantenimiento; + } + + public LocalDate getFechaProgramada() { + return fechaProgramada; + } + + public void setFechaProgramada(LocalDate fechaProgramada) { + this.fechaProgramada = fechaProgramada; } } diff --git a/src/main/java/mx/gob/jumapacelaya/models/SignaturePad.java b/src/main/java/mx/gob/jumapacelaya/models/SignaturePad.java new file mode 100644 index 0000000..9188c45 --- /dev/null +++ b/src/main/java/mx/gob/jumapacelaya/models/SignaturePad.java @@ -0,0 +1 @@ +package mx.gob.jumapacelaya.models; \ No newline at end of file diff --git a/src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java b/src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java index 8606b7e..eeadf17 100644 --- a/src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java +++ b/src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java @@ -16,13 +16,14 @@ import java.util.List; public class DatabaseService { - private final String url = "jdbc:mysql://mhdb.jumapacelaya.gob.mx:33006/Mantenimientos"; - private final String user = "root"; - private final String pass = "mantenimientos"; - private Connection getConnection() throws SQLException { + String url = "jdbc:mysql://mhdb.jumapacelaya.gob.mx:33006/Mantenimientos"; + String user = "root"; + String pass = "mantenimientos"; + return DriverManager.getConnection(url, user, pass); + } // Método para obtener los tipos de mantenimientos @@ -54,16 +55,17 @@ public class DatabaseService { try (Connection connection = getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(query)) { - preparedStatement.setString(1, tipoMantenimiento); - ResultSet resultSet = preparedStatement.executeQuery(); - if (resultSet.next()) { - nomenclatura = resultSet.getString("nomenclatura"); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + nomenclatura = resultSet.getString("nomenclatura"); + } } } catch (SQLException e) { e.printStackTrace(); + System.err.println("Error al obtener nomenclatura: " + e.getMessage()); } return nomenclatura; } @@ -161,7 +163,12 @@ public class DatabaseService { /* ----------------Obtener el Plan Anual de Mantenimiento ---------------- */ public List getPlanAnual() { List planAnualList = new ArrayList<>(); - String query = "SELECT * FROM MTTOPROGRAMADOS"; + String query = "SELECT p.mttoprogramadoid, p.nomEquipo, p.departamento, " + + "p.monitor, p.teclado, p.mouse, p.regulador, " + + "p.cpu, p.impresora, p.miniPrint, p.laptop, p.escaner, " + + "p.fechaprog, m.fecha AS fechaMantenimiento, p.tecnicosmt, p.estado " + + "FROM MTTOPROGRAMADOS p " + + "LEFT JOIN MANTENIMIENTOS m ON p.mttoprogramadoid = m.mttoprogramadoid"; try (Connection connection = getConnection(); Statement statement = connection.createStatement(); @@ -181,10 +188,10 @@ public class DatabaseService { resultSet.getBoolean("miniPrint"), resultSet.getBoolean("laptop"), resultSet.getBoolean("escaner"), - resultSet.getString("fechaprog"), + resultSet.getDate("fechaprog").toLocalDate(), // Cambiado a LocalDate + resultSet.getDate("fechaMantenimiento") != null ? resultSet.getDate("fechaMantenimiento").toLocalDate() : null, // Cambiado a LocalDate resultSet.getString("tecnicosmt"), - resultSet.getString("estado"), - resultSet.getString("fechaRealizado") + resultSet.getString("estado") ); planAnualList.add(planAnual); } @@ -202,31 +209,86 @@ public class DatabaseService { /*-=iii=<() *-=iii=<()*/ /* ( ͡° ͜ʖ ͡°) Metodos para insetar en la BD ( ͡° ͜ʖ ͡°) ( ͡° ͜ʖ ͡°) Metodos para insetar en la BD ( ͡° ͜ʖ ͡°) ( ͡° ͜ʖ ͡°) Metodos para insetar en la BD ( ͡° ͜ʖ ͡°) */ // INSERTAR EN TABLA: MANTENIMINETOS - public int insertarMantenimiento(LocalDate fecha, String tipoMantId, String departamentoId, String empleadoId, String formaMant, String equipoId) { - String query = "INSERT INTO MANTENIMIENTOS (fecha, tipoMantId, departamentoId, empleadoId, formaMant, nombreequipo) VALUES (?, ?, ?, ?, ?, ?)"; + public int insertarMantenimiento(LocalDate fecha, String tipoMantId, String departamentoId, String empleadoId, + String formaMant, String equipoId, String userSignatureBase64, + String smtSignatureBase64, String gciaSignatureBase64, String planAnualId) { + + String query = "INSERT INTO MANTENIMIENTOS (fecha, tipoMantId, departamentoId, empleadoId, formaMant, nombreequipo," + + " firmaUsuario, firmaSmt, firmaGcia, mttoprogramadoid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + + String updateStatusQuery = "UPDATE MTTOPROGRAMADOS SET estado = 'REALIZADO' WHERE mttoprogramadoid = ?"; + int nuevoId = -1; - try (Connection connection = getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { + Connection connection = null; - preparedStatement.setDate(1, Date.valueOf(fecha)); - preparedStatement.setString(2, tipoMantId); - preparedStatement.setString(3, departamentoId); - preparedStatement.setString(4, empleadoId); - preparedStatement.setString(5, formaMant); - preparedStatement.setString(6, equipoId); + try { + connection = getConnection(); - int rowsAffected = preparedStatement.executeUpdate(); - if (rowsAffected > 0) { - try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) { - if (generatedKeys.next()) { - nuevoId = generatedKeys.getInt(1); + if (connection == null) { + throw new SQLException("No se pudo establecer la conexion con la base de datos."); + } + + connection.setAutoCommit(false); + + // Paso 1: primero inserta el mantenimiento + try (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); + preparedStatement.setString(5, formaMant); + preparedStatement.setString(6, equipoId); + preparedStatement.setString(7, userSignatureBase64); + preparedStatement.setString(8, smtSignatureBase64); + preparedStatement.setString(9, gciaSignatureBase64); + preparedStatement.setString(10, planAnualId); + + int rowsAffected = preparedStatement.executeUpdate(); + if (rowsAffected > 0) { + try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) { + if (generatedKeys.next()) { + nuevoId = generatedKeys.getInt(1); + } } } } + + // Paso 2: Si se inserto el mantenimiento, se actualiza el estado en MTTOPROGRAMADOS + if (nuevoId != -1) { + try (PreparedStatement updateStatement = connection.prepareStatement(updateStatusQuery)) { + updateStatement.setString(1, planAnualId); + updateStatement.executeUpdate(); + } + } + + // Confirmar la transaccion si ambos pasos son exitosos + connection.commit(); + } catch (SQLException e) { System.err.println("Error al insertar mantenimiento: " + e.getMessage()); + // En caso de error, se hace rollback + if (connection != null) { + try { + if (connection != null) { + connection.rollback(); + } + } catch (SQLException rollbackEx) { + System.err.println("Error al hacer rollback: " + rollbackEx.getMessage()); + } + } + } finally { + // Cerrar la conexion + if (connection != null) { + try { + connection.close(); + } catch (SQLException closeEx) { + System.err.println("Error al cerrar la conexion: " + closeEx.getMessage()); + } + } } + return nuevoId; } /*( •_•)>⌐■-■ (⌐■_■)( •_•)>⌐■-■ (⌐■_■)( •_•)>⌐■-■ (⌐■_■)( •_•)>⌐■-■ (⌐■_■)( •_•)>⌐■-■ (⌐■_■)( •_•)>⌐■-■ (⌐■_■) */ diff --git a/src/main/java/mx/gob/jumapacelaya/ui/MantenimientoView.java b/src/main/java/mx/gob/jumapacelaya/ui/MantenimientoView.java index db4d3ce..2957bc4 100644 --- a/src/main/java/mx/gob/jumapacelaya/ui/MantenimientoView.java +++ b/src/main/java/mx/gob/jumapacelaya/ui/MantenimientoView.java @@ -33,11 +33,10 @@ import mx.gob.jumapacelaya.models.TiposHardware; import mx.gob.jumapacelaya.models.TiposMantenimiento; import mx.gob.jumapacelaya.models.Usuario; import mx.gob.jumapacelaya.services.DatabaseService; +import mx.gob.jumapacelaya.services.UserService; import java.time.LocalDate; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; @PermitAll @@ -52,21 +51,26 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse private final VerticalLayout etiquetaLayout; private final HorizontalLayout firmasLayout; private final DatePicker fecha; + private final TextField txtPlananualID; private final ComboBox tipoMantt; private final ComboBox area; private final ComboBox usuario; private final TextField nombreEquipo; + private final UserService userService; private RadioButtonGroup formaGroup; private CheckboxGroup actualizaciones; private final HorizontalLayout botonesLayout; private TextArea txtCuales; private RadioButtonGroup masActualizacionesGroup; + private SignaturePad userSignPad; + private SignaturePad smtSignPad; + private SignaturePad gciatiSignPad; Span userSignSpan = new Span("Nombre Usuario"); - public MantenimientoView() { + public MantenimientoView(UserService userService) { this.databaseService = new DatabaseService(); this.controlsLayout = new VerticalLayout(); this.actualizacionesLayout = new VerticalLayout(); @@ -90,6 +94,11 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse //fecha.setPlaceholder("Fecha"); + // Campo de texto para el ID de MTTOPROGRAMADO + this.txtPlananualID = new TextField("Plan Anual ID"); + txtPlananualID.setReadOnly(true); + txtPlananualID.addClassName("mantenimiento-text-field"); + // ComboBox Tipo de Mantenimiento this.tipoMantt = new ComboBox<>("Tipo de Mantenimiento"); @@ -128,7 +137,7 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse etiquetaLayout.removeAll(); // Limpia el layout } }); - fechaLayout.add(this.fecha); + fechaLayout.add(this.fecha/*, this.txtPlananualID*/); fechaLayout.addAndExpand(new HorizontalLayout()); fechaLayout.add(tipoMantt, nomenclatura); fechaLayout.setWidthFull(); @@ -174,6 +183,7 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse add(fechaLayout, departamentoLayout, controlsLayout, actualizacionesLayout, etiquetaLayout, firmasLayout, botonesLayout); + this.userService = userService; } @@ -372,7 +382,7 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse private void signLayout() { VerticalLayout userSignLayout = new VerticalLayout(); - SignaturePad userSignPad = new SignaturePad(); + userSignPad = new SignaturePad(); userSignPad.setBackgroundColor(0, 0, 0, 0); userSignPad.setHeight("200px"); userSignPad.setPenColor("#000000"); @@ -385,7 +395,7 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse userSignLayout.add(userSignPad, userSignSpan, tituloUser); VerticalLayout smtSignLayout = new VerticalLayout(); - SignaturePad smtSignPad = new SignaturePad(); + smtSignPad = new SignaturePad(); smtSignPad.setHeight("200px"); smtSignPad.setBackgroundColor(0, 0, 0, 0); smtSignPad.setPenColor("#000000"); @@ -399,13 +409,13 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse smtSignLayout.add(smtSignPad, smtSignSpan, tituloSMT); VerticalLayout gcialSignLayout = new VerticalLayout(); - SignaturePad gciatiSignPad = new SignaturePad(); + gciatiSignPad = new SignaturePad(); gciatiSignPad.setBackgroundColor(0, 0, 0, 0); gciatiSignPad.setHeight("200px"); gciatiSignPad.setPenColor("#000000"); gciatiSignPad.getElement().getStyle().set("border", "1px solid black"); - Span gciatiSignSpan = new Span("L.I. Sandra E. Rodriguez Ramirez"); - Span tituloGerente = new Span("Gerencia de Tecnologias de la Información"); + Span gciatiSignSpan = new Span("L.I. David Olivares Ramos"); + Span tituloGerente = new Span("Encargado de Despacho Gcia. T.I"); gcialSignLayout.setSizeFull(); gcialSignLayout.setSpacing(false); gcialSignLayout.setAlignItems(Alignment.CENTER); @@ -428,6 +438,7 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse LocalDate fechaSeleccionada = fecha.getValue(); TiposMantenimiento tiposMantenimiento = tipoMantt.getValue(); String tipoMantId = tiposMantenimiento != null ? tiposMantenimiento.getTipomantId() : null; + String planAnualvalue = txtPlananualID.getValue(); // Cambié aquí para obtener el departamento seleccionado correctamente DepartamentosModel departamentoSeleccionado = area.getValue(); @@ -448,13 +459,38 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse formaMantt = "M"; } + + // VALIDACION PARA QUE TODOS LOS CAMPOS OBLIGATORIOS SEAN LLENADOS - if (fechaSeleccionada == null || tipoMantId == null || departamentoId == null || empleadoId == null || equipoId == null || formaMantt == null) { + if (fechaSeleccionada == null || tipoMantId == null || departamentoId == null || empleadoId == null || equipoId == null || formaMantt == null || planAnualvalue == null) { Notification.show("Por favor, completa todos los campos requeridos", 4000, Notification.Position.MIDDLE); return; } - int isInserted = databaseService.insertarMantenimiento(fechaSeleccionada, tipoMantId, departamentoId, empleadoId, formaMantt, equipoId); + + // Captura las firmas en Base64 usando el metodo getImageBase64 + byte[] userSignatureBytes = userSignPad.getImageBase64(); + byte[] smtSignatureBytes = smtSignPad.getImageBase64(); + byte[] gciatiSignatureBytes = gciatiSignPad.getImageBase64(); + + // Valida si los campos de firma estan vacios + if (userSignatureBytes == null || userSignatureBytes.length == 0 || + smtSignatureBytes == null || smtSignatureBytes.length == 0 || + gciatiSignatureBytes == null || gciatiSignatureBytes.length == 0) { + + Notification.show("Por favor, firme el mantenimiento.", 4000, Notification.Position.MIDDLE) + .addThemeVariants(NotificationVariant.LUMO_WARNING); + return; + } + + // Convierte los byte[] a String en formato Base64 + String userSignatureBase64 = Base64.getEncoder().encodeToString(userSignatureBytes); + String smtSignatureBase64 = Base64.getEncoder().encodeToString(smtSignatureBytes); + String gciaSignatureBase64 = Base64.getEncoder().encodeToString(gciatiSignatureBytes); + + int isInserted = databaseService.insertarMantenimiento( + fechaSeleccionada, tipoMantId, departamentoId, empleadoId, formaMantt, equipoId, + userSignatureBase64, smtSignatureBase64, gciaSignatureBase64, planAnualvalue); // AQUI SE MANEJA LA INSERCION DE LOS DETALLES DE HARDWARE (TIPO DE HARDWARE, NO. DE SERIE, MODELO, PLACA) if (isInserted > 0) { @@ -550,6 +586,7 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse .addThemeVariants(NotificationVariant.LUMO_SUCCESS); fecha.clear(); + txtPlananualID.clear(); tipoMantt.clear(); area.clear(); usuario.clear(); @@ -558,12 +595,16 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse actualizaciones.clear(); masActualizacionesGroup.clear(); txtCuales.clear(); + userSignPad.clear(); + smtSignPad.clear(); + gciatiSignPad.clear(); + + UI.getCurrent().navigate("/"); } else { Notification.show("Error al guardar el mantenimeinto", 4000, Notification.Position.MIDDLE); } }); - buttonsLayout.setSizeFull(); buttonsLayout.add(btnGuardar); botonesLayout.add(buttonsLayout); @@ -577,7 +618,11 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse : null; String tipoParam = beforeEnterEvent.getLocation().getQueryParameters().getParameters().get("tipo") != null - ? beforeEnterEvent.getLocation().getQueryParameters().getParameters("tipo").stream().findFirst().orElse(null) + ? beforeEnterEvent.getLocation().getQueryParameters().getParameters().get("tipo").stream().findFirst().orElse(null) + : null; + + String idParam = beforeEnterEvent.getLocation().getQueryParameters().getParameters().get("id") != null + ? beforeEnterEvent.getLocation().getQueryParameters().getParameters().get("id").stream().findFirst().orElse(null) : null; if (fechaParam != null) { @@ -614,5 +659,12 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse tipoMantt.setReadOnly(true); } } + + // Establecemos el ID desde el plan anual + if (idParam != null) { + txtPlananualID.setValue(idParam); // Establece el ID en el campo de texto + } else { + txtPlananualID.setValue("Sin ID"); // O un valor por defecto si no hay ID + } } } diff --git a/src/main/java/mx/gob/jumapacelaya/ui/PlanAnualView.java b/src/main/java/mx/gob/jumapacelaya/ui/PlanAnualView.java index ad42179..bc24607 100644 --- a/src/main/java/mx/gob/jumapacelaya/ui/PlanAnualView.java +++ b/src/main/java/mx/gob/jumapacelaya/ui/PlanAnualView.java @@ -29,6 +29,7 @@ import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Array; import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -155,16 +156,27 @@ public class PlanAnualView extends VerticalLayout { planAnualGrid.addComponentColumn(planAnual -> getIcon(planAnual.isLaptop())).setHeader("Laptop").setAutoWidth(true); planAnualGrid.addComponentColumn(planAnual -> getIcon(planAnual.isEscaner())).setHeader("Escáner").setAutoWidth(true); - planAnualGrid.addColumn(PlanAnual :: getFecha).setHeader("Fecha Programada").setAutoWidth(true); + planAnualGrid.addColumn(planAnual -> { + LocalDate fechaProgramada = planAnual.getFechaProgramada(); + return fechaProgramada != null ? fechaProgramada.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")) : "No programada"; // Formato deseado + }).setHeader("Fecha Programada").setAutoWidth(true); + + planAnualGrid.addColumn(planAnual -> { + LocalDate fechaMantenimiento = planAnual.getFechaMantenimiento(); + return fechaMantenimiento != null ? fechaMantenimiento.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")) : ""; // Formato deseado + }).setHeader("Fecha Realizacion").setAutoWidth(true); + + planAnualGrid.addColumn(PlanAnual :: getEstado).setHeader("Estado").setAutoWidth(true); + planAnualGrid.addColumn(PlanAnual :: getSmt).setHeader("S.M.T").setAutoWidth(true); - planAnualGrid.addColumn(PlanAnual :: getFechaRealizado).setHeader("Fecha Realizado").setAutoWidth(true); planAnualGrid.addComponentColumn(planAnual -> { Button btnRealizar = new Button("Realizar"); btnRealizar.addClickListener(event -> { + int idPlananual = planAnual.getNumero(); LocalDate fechaSistema = LocalDate.now(); String fechaStr = fechaSistema.toString(); - btnRealizar.getUI().ifPresent(ui -> ui.navigate("mantenimiento?fecha=" + fechaStr + "&tipo=1")); + btnRealizar.getUI().ifPresent(ui -> ui.navigate("mantenimiento?id=" + idPlananual + "&fecha=" + fechaStr + "&tipo=1")); }); return btnRealizar; }).setHeader("Realizado").setAutoWidth(true);