@ -7,6 +7,7 @@ import com.vaadin.flow.component.checkbox.CheckboxGroup;
import com.vaadin.flow.component.combobox.ComboBox ;
import com.vaadin.flow.component.datepicker.DatePicker ;
import com.vaadin.flow.component.dependency.CssImport ;
import com.vaadin.flow.component.dialog.Dialog ;
import com.vaadin.flow.component.html.H1 ;
import com.vaadin.flow.component.html.H2 ;
import com.vaadin.flow.component.html.Image ;
@ -29,10 +30,7 @@ import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route ;
import de.f0rce.signaturepad.SignaturePad ;
import jakarta.annotation.security.PermitAll ;
import mx.gob.jumapacelaya.models.DepartamentosModel ;
import mx.gob.jumapacelaya.models.TiposHardware ;
import mx.gob.jumapacelaya.models.TiposMantenimiento ;
import mx.gob.jumapacelaya.models.Usuario ;
import mx.gob.jumapacelaya.models.* ;
import mx.gob.jumapacelaya.services.DatabaseService ;
import mx.gob.jumapacelaya.services.EmailService ;
import mx.gob.jumapacelaya.services.SecurityService ;
@ -74,6 +72,10 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse
private SignaturePad userSignPad ;
private SignaturePad smtSignPad ;
/ / private SignaturePad gciatiSignPad ;
private PlanAnual planAnualActual ;
private LocalDate fechaProgramada ;
private LocalDate fechaSeleccionada ;
private TextArea jsutificacion ;
Span userSignSpan = new Span ( "Nombre Usuario" ) ;
@ -496,209 +498,244 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse
btnGuardar . addClickListener ( event - > {
LocalDate fechaSeleccionada = fecha . getValue ( ) ;
TiposMantenimiento tiposMantenimiento = tipoMantt . getValue ( ) ;
String tipoMantId = tiposMantenimiento ! = null ? tiposMantenimiento . getTipomantId ( ) : null ;
String planAnualValue = txtPlananualID . getValue ( ) ;
if ( planAnualActual = = null ) {
Notification . show ( "No se pudo cargar el Plan Anual. Verifica el ID." , 4000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_ERROR ) ;
}
DepartamentosModel departamentoSeleccionado = area . getValue ( ) ;
String departamentoId = departamentoSeleccionado ! = null ? departamentoSeleccionado . getDepartamentoId ( ) . toString ( ) : null ;
if ( planAnualActual ! = null ) {
fechaProgramada = planAnualActual . getFechaProgramada ( ) ;
fechaSeleccionada = fecha . getValue ( ) ;
boolean mismoMes = fechaSeleccionada ! = null & &
fechaSeleccionada . getMonthValue ( ) = = fechaProgramada . getMonthValue ( ) & &
fechaSeleccionada . getYear ( ) = = fechaProgramada . getYear ( ) ;
if ( ! mismoMes ) {
Dialog confirmDialog = new Dialog ( ) ;
Span aviso = new Span ( "El mantenimiento esta fuera de tiempo, justifique el motivo" ) ;
jsutificacion = new TextArea ( ) ;
jsutificacion . setSizeFull ( ) ;
Button btnSi = new Button ( "Guardar" , LineAwesomeIcon . SAVE_SOLID . create ( ) , e - > {
confirmDialog . close ( ) ;
guardarMantenimiento ( ) ;
} ) ;
btnSi . addThemeVariants ( ButtonVariant . LUMO_PRIMARY ) ;
VerticalLayout layout = new VerticalLayout ( aviso , jsutificacion , btnSi ) ;
layout . setSizeFull ( ) ;
layout . setAlignItems ( Alignment . CENTER ) ;
confirmDialog . add ( layout ) ;
confirmDialog . open ( ) ;
} else {
guardarMantenimiento ( ) ;
}
}
} ) ;
Usuario usuarioSeleccionado = usuario . getValue ( ) ;
String empleadoId = usuarioSeleccionado ! = null ? usuarioSeleccionado . getEmpleadoId ( ) . toString ( ) : null ;
String equipoId = txtNombreEquipo . getValue ( ) ;
buttonsLayout . setSizeFull ( ) ;
buttonsLayout . add ( btnGuardar ) ;
buttonsLayout . setAlignItems ( Alignment . CENTER ) ;
botonesLayout . add ( buttonsLayout ) ;
botonesLayout . setSizeFull ( ) ;
/ / botonesLayout . setAlignItems ( Alignment . CENTER ) ;
}
String formaSeleccionada = formaGroup . getValue ( ) ;
String formaMantt = null ;
if ( "Remota" . equals ( formaSeleccionada ) ) {
formaMantt = "R" ;
} else if ( "Manual" . equals ( formaSeleccionada ) ) {
formaMantt = "M" ;
}
/ / Validación de campos obligatorios
if ( fechaSeleccionada = = null | | tipoMantId = = null | | departamentoId = = null | | empleadoId = = null | |
equipoId = = null | | equipoId . trim ( ) . isEmpty ( ) | | formaMantt = = null | | planAnualValue = = null | | planAnualValue . trim ( ) . isEmpty ( ) ) {
Notification . show ( "Por favor, completa todos los campos requeridos" , 4000 , Notification . Position . MIDDLE ) ;
return ;
}
/ / METODO PARA ENVIAR CORREOS ELECTRONICOS
private void enviarCorreo ( ) {
Usuario usuarioSeleccionado = usuario . getValue ( ) ;
if ( usuarioSeleccionado ! = null & & usuarioSeleccionado . getEmail ( ) ! = null ) {
String destinatario = usuarioSeleccionado . getEmail ( ) ;
String asunto = "MANTENIMIENTO DE EQUIPO DE COMPUTO REALIZADO" ;
String cuerpo = "<html>" +
"<body>" +
"<img src='cid:image_id'/>" +
"</body>" +
"</html>" ;
String imagePath = "META-INF/resources/images/imgCorreo/correoMantt.png" ;
byte [ ] userSignatureBytes = userSignPad . getImageBase64 ( ) ;
byte [ ] smtSignatureBytes = smtSignPad . getImageBase64 ( ) ;
/ / byte [ ] gciaSignatureBytes = gciatiSignPad . getImageBase64 ( ) ;
emailService . enviarCorreo ( destinatario , asunto , cuerpo , imagePath ) ;
String userSignatureBase64 = Base64 . getEncoder ( ) . encodeToString ( userSignatureBytes ) ;
String smtSignatureBase64 = Base64 . getEncoder ( ) . encodeToString ( smtSignatureBytes ) ;
/ / String gciaSignatureBase64 = Base64 . getEncoder ( ) . encodeToString ( gciaSignatureBytes ) ;
} else {
Notification . show ( "Por favor selecciona un usuario" , 4000 , Notification . Position . MIDDLE ) ;
}
}
if ( esFirmaVacia ( userSignatureBase64 ) ) userSignatureBase64 = null ;
if ( esFirmaVacia ( smtSignatureBase64 ) ) smtSignatureBase64 = null ;
/ / if ( esFirmaVacia ( gciaSignatureBase64 ) ) gciaSignatureBase64 = null ;
private void guardarMantenimiento ( ) {
TiposMantenimiento tiposMantenimiento = tipoMantt . getValue ( ) ;
String tipoMantId = tiposMantenimiento ! = null ? tiposMantenimiento . getTipomantId ( ) : null ;
String planAnualValue = txtPlananualID . getValue ( ) ;
String justificacionValue = jsutificacion ! = null ? jsutificacion . getValue ( ) : null ;
/ / Validación de campos de hardware
List < Map < String , String > > detallesHardware = new ArrayList < > ( ) ;
for ( HorizontalLayout layout : hardwareLayouts ) {
ComboBox < TiposHardware > tipoHardware = ( ComboBox < TiposHardware > ) layout . getComponentAt ( 0 ) ;
TextField noSerie = ( TextField ) layout . getComponentAt ( 1 ) ;
TextField modelo = ( TextField ) layout . getComponentAt ( 2 ) ;
TextField placa = ( TextField ) layout . getComponentAt ( 3 ) ;
TiposHardware tipoSeleccionado = tipoHardware . getValue ( ) ;
if ( tipoSeleccionado = = null ) {
Notification . show ( "Por favor, selecciona un tipo de hardware" , 4000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_WARNING ) ;
return ;
}
DepartamentosModel departamentoSeleccionado = area . getValue ( ) ;
String departamentoId = departamentoSeleccionado ! = null ? departamentoSeleccionado . getDepartamentoId ( ) . toString ( ) : null ;
Usuario usuarioSeleccionado = usuario . getValue ( ) ;
String empleadoId = usuarioSeleccionado ! = null ? usuarioSeleccionado . getEmpleadoId ( ) . toString ( ) : null ;
String equipoId = txtNombreEquipo . getValue ( ) ;
String formaSeleccionada = formaGroup . getValue ( ) ;
String formaMantt = null ;
if ( "Remota" . equals ( formaSeleccionada ) ) {
formaMantt = "R" ;
} else if ( "Manual" . equals ( formaSeleccionada ) ) {
formaMantt = "M" ;
}
/ / Validación de campos obligatorios
if ( fechaSeleccionada = = null | | tipoMantId = = null | | departamentoId = = null | | empleadoId = = null | |
equipoId = = null | | equipoId . trim ( ) . isEmpty ( ) | | formaMantt = = null | | planAnualValue = = null | | planAnualValue . trim ( ) . isEmpty ( ) ) {
Notification . show ( "Por favor, completa todos los campos requeridos" , 4000 , Notification . Position . MIDDLE ) ;
return ;
}
boolean esOpcional = Arrays . asList ( "TECLADO" , "MOUSE" ) . contains ( tipoSeleccionado . getNombreHardware ( ) ) ;
String numSerie = noSerie . getValue ( ) ;
String modeloVal = modelo . getValue ( ) ;
String placaVal = placa . getValue ( ) ;
byte [ ] userSignatureBytes = userSignPad . getImageBase64 ( ) ;
byte [ ] smtSignatureBytes = smtSignPad . getImageBase64 ( ) ;
/ / byte [ ] gciaSignatureBytes = gciatiSignPad . getImageBase64 ( ) ;
if ( ! esOpcional & & ( modeloVal = = null | | modeloVal . isEmpty ( ) | | numSerie = = null | | numSerie . isEmpty ( ) | | placaVal = = null | | placaVal . isEmpty ( ) ) ) {
Notification . show ( "Por favor, completa todos los campos de hardware." , 4000 , Notification . Position . MIDDLE )
String userSignatureBase64 = Base64 . getEncoder ( ) . encodeToString ( userSignatureBytes ) ;
String smtSignatureBase64 = Base64 . getEncoder ( ) . encodeToString ( smtSignatureBytes ) ;
/ / String gciaSignatureBase64 = Base64 . getEncoder ( ) . encodeToString ( gciaSignatureBytes ) ;
if ( esFirmaVacia ( userSignatureBase64 ) ) userSignatureBase64 = null ;
if ( esFirmaVacia ( smtSignatureBase64 ) ) smtSignatureBase64 = null ;
/ / if ( esFirmaVacia ( gciaSignatureBase64 ) ) gciaSignatureBase64 = null ;
/ / Validación de campos de hardware
List < Map < String , String > > detallesHardware = new ArrayList < > ( ) ;
for ( HorizontalLayout layout : hardwareLayouts ) {
ComboBox < TiposHardware > tipoHardware = ( ComboBox < TiposHardware > ) layout . getComponentAt ( 0 ) ;
TextField noSerie = ( TextField ) layout . getComponentAt ( 1 ) ;
TextField modelo = ( TextField ) layout . getComponentAt ( 2 ) ;
TextField placa = ( TextField ) layout . getComponentAt ( 3 ) ;
TiposHardware tipoSeleccionado = tipoHardware . getValue ( ) ;
if ( tipoSeleccionado = = null ) {
Notification . show ( "Por favor, selecciona un tipo de hardware" , 4000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_WARNING ) ;
return ;
}
return ;
}
boolean esOpcional = Arrays . asList ( "TECLADO" , "MOUSE" ) . contains ( tipoSeleccionado . getNombreHardware ( ) ) ;
Map < String , String > hw = new HashMap < > ( ) ;
hw . put ( "tipoHardwareId" , tipoSeleccionado . getTipoHardwareId ( ) ) ;
hw . put ( "numSerie" , numSerie ) ;
hw . put ( "modelo" , modeloVal ) ;
hw . put ( "placa" , placaVal ) ;
detallesHardware . add ( hw ) ;
String numSerie = noSerie . getValue ( ) ;
String modeloVal = modelo . getValue ( ) ;
String placaVal = placa . getValue ( ) ;
if ( ! esOpcional & & ( modeloVal = = null | | modeloVal . isEmpty ( ) | | numSerie = = null | | numSerie . isEmpty ( ) | | placaVal = = null | | placaVal . isEmpty ( ) ) ) {
Notification . show ( "Por favor, completa todos los campos de hardware." , 4000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_WARNING ) ;
return ;
}
Set < String > actualizacionesSeleccionadas = actualizaciones . getSelectedItems ( ) ;
String otrasActualizaciones = null ;
if ( "Si" . equals ( masActualizacionesGroup . getValue ( ) ) ) {
otrasActualizaciones = txtCuales . getValue ( ) ;
if ( otrasActualizaciones = = null | | otrasActualizaciones . trim ( ) . isEmpty ( ) ) {
Notification . show ( "Especifica las otras actualizaciones" , 4000 , Notification . Position . MIDDLE )
Map < String , String > hw = new HashMap < > ( ) ;
hw . put ( "tipoHardwareId" , tipoSeleccionado . getTipoHardwareId ( ) ) ;
hw . put ( "numSerie" , numSerie ) ;
hw . put ( "modelo" , modeloVal ) ;
hw . put ( "placa" , placaVal ) ;
detallesHardware . add ( hw ) ;
}
Set < String > actualizacionesSeleccionadas = actualizaciones . getSelectedItems ( ) ;
String otrasActualizaciones = null ;
if ( "Si" . equals ( masActualizacionesGroup . getValue ( ) ) ) {
otrasActualizaciones = txtCuales . getValue ( ) ;
if ( otrasActualizaciones = = null | | otrasActualizaciones . trim ( ) . isEmpty ( ) ) {
Notification . show ( "Especifica las otras actualizaciones" , 4000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_WARNING ) ;
return ;
}
return ;
}
}
/ / Una vez que validamos todos los campos del formulario , procedemos a insertar en BD
try {
int isInserted = databaseService . insertarMantenimiento ( fechaSeleccionada , tipoMantId , departamentoId , empleadoId , formaMantt , equipoId , userSignatureBase64 , smtSignatureBase64 , planAnualValue ) ;
/ / Una vez que validamos todos los campos del formulario , procedemos a insertar en BD
try {
int isInserted = databaseService . insertarMantenimiento ( fechaSeleccionada , tipoMantId , departamentoId , empleadoId , formaMantt , equipoId , userSignatureBase64 , smtSignatureBase64 , planAnualValue , justificacion Value ) ;
if ( isInserted < = 0 ) {
Notification . show ( "Error al guardar el mantenimiento" , 4000 , Notification . Position . MIDDLE )
if ( isInserted < = 0 ) {
Notification . show ( "Error al guardar el mantenimiento" , 4000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_ERROR ) ;
return ;
}
return ;
}
int mantenimientoId = databaseService . getUltimoMantenimientoId ( ) ;
int mantenimientoId = databaseService . getUltimoMantenimientoId ( ) ;
for ( Map < String , String > hw : detallesHardware ) {
boolean ok = databaseService . insertarHardware (
for ( Map < String , String > hw : detallesHardware ) {
boolean ok = databaseService . insertarHardware (
hw . get ( "tipoHardwareId" ) ,
hw . get ( "numSerie" ) ,
hw . get ( "modelo" ) ,
hw . get ( "placa" ) ,
mantenimientoId
) ;
) ;
if ( ! ok ) {
Notification . show ( "Error al insertar detalles del hardware: " , 4000 , Notification . Position . MIDDLE )
if ( ! ok ) {
Notification . show ( "Error al insertar detalles del hardware: " , 4000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_ERROR ) ;
return ;
}
return ;
}
}
for ( String act : actualizacionesSeleccionadas ) {
boolean ok = databaseService . insertActualizacionSeg ( act , null , mantenimientoId ) ;
if ( ! ok ) {
Notification . show ( "Error al insertar actualización de seguridad" , 4000 , Notification . Position . MIDDLE )
for ( String act : actualizacionesSeleccionadas ) {
boolean ok = databaseService . insertActualizacionSeg ( act , null , mantenimientoId ) ;
if ( ! ok ) {
Notification . show ( "Error al insertar actualización de seguridad" , 4000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_ERROR ) ;
return ;
}
return ;
}
}
if ( otrasActualizaciones ! = null ) {
boolean ok = databaseService . insertActualizacionSeg ( "Otras" , otrasActualizaciones , mantenimientoId ) ;
if ( ! ok ) {
Notification . show ( "Error al insertar otras actualizaciones" , 4000 , Notification . Position . MIDDLE )
if ( otrasActualizaciones ! = null ) {
boolean ok = databaseService . insertActualizacionSeg ( "Otras" , otrasActualizaciones , mantenimientoId ) ;
if ( ! ok ) {
Notification . show ( "Error al insertar otras actualizaciones" , 4000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_ERROR ) ;
return ;
}
return ;
}
}
/ / = = = EXITO = = =
Notification . show ( "¡Mantenimiento guardado exitosamente!" , 4000 , Notification . Position . MIDDLE )
/ / = = = EXITO = = =
Notification . show ( "¡Mantenimiento guardado exitosamente!" , 4000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_SUCCESS ) ;
try {
enviarCorreo ( ) ;
} catch ( Exception e ) {
Notification . show ( "Error al enviar el correo" , 4000 , Notification . Position . MIDDLE )
try {
enviarCorreo ( ) ;
} catch ( Exception e ) {
Notification . show ( "Error al enviar el correo" , 4000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_ERROR ) ;
e . printStackTrace ( ) ;
}
/ / Limpiar campos
fecha . clear ( ) ;
txtPlananualID . clear ( ) ;
tipoMantt . clear ( ) ;
area . clear ( ) ;
usuario . clear ( ) ;
txtNombreEquipo . clear ( ) ;
formaGroup . clear ( ) ;
actualizaciones . clear ( ) ;
masActualizacionesGroup . clear ( ) ;
txtCuales . clear ( ) ;
userSignPad . clear ( ) ;
smtSignPad . clear ( ) ;
/ / gciatiSignPad . clear ( ) ;
for ( HorizontalLayout layout : hardwareLayouts ) {
( ( ComboBox < ? > ) layout . getComponentAt ( 0 ) ) . clear ( ) ;
( ( TextField ) layout . getComponentAt ( 1 ) ) . clear ( ) ;
( ( TextField ) layout . getComponentAt ( 2 ) ) . clear ( ) ;
( ( TextField ) layout . getComponentAt ( 3 ) ) . clear ( ) ;
}
UI . getCurrent ( ) . navigate ( "/" ) ;
} catch ( Exception ex ) {
Notification . show ( "Ocurrio un error inesperado: " + ex . getMessage ( ) , 5000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_ERROR ) ;
ex . printStackTrace ( ) ;
e . printStackTrace ( ) ;
}
} ) ;
buttonsLayout . setSizeFull ( ) ;
buttonsLayout . add ( btnGuardar ) ;
buttonsLayout . setAlignItems ( Alignment . CENTER ) ;
botonesLayout . add ( buttonsLayout ) ;
botonesLayout . setSizeFull ( ) ;
/ / botonesLayout . setAlignItems ( Alignment . CENTER ) ;
}
/ / METODO PARA ENVIAR CORREOS ELECTRONICOS
private void enviarCorreo ( ) {
Usuario usuarioSeleccionado = usuario . getValue ( ) ;
if ( usuarioSeleccionado ! = null & & usuarioSeleccionado . getEmail ( ) ! = null ) {
String destinatario = usuarioSeleccionado . getEmail ( ) ;
String asunto = "MANTENIMIENTO DE EQUIPO DE COMPUTO REALIZADO" ;
String cuerpo = "<html>" +
"<body>" +
"<img src='cid:image_id'/>" +
"</body>" +
"</html>" ;
/ / Limpiar campos
fecha . clear ( ) ;
txtPlananualID . clear ( ) ;
tipoMantt . clear ( ) ;
area . clear ( ) ;
usuario . clear ( ) ;
txtNombreEquipo . clear ( ) ;
formaGroup . clear ( ) ;
actualizaciones . clear ( ) ;
masActualizacionesGroup . clear ( ) ;
txtCuales . clear ( ) ;
userSignPad . clear ( ) ;
smtSignPad . clear ( ) ;
/ / gciatiSignPad . clear ( ) ;
String imagePath = "META-INF/resources/images/imgCorreo/correoMantt.png" ;
for ( HorizontalLayout layout : hardwareLayouts ) {
( ( ComboBox < ? > ) layout . getComponentAt ( 0 ) ) . clear ( ) ;
( ( TextField ) layout . getComponentAt ( 1 ) ) . clear ( ) ;
( ( TextField ) layout . getComponentAt ( 2 ) ) . clear ( ) ;
( ( TextField ) layout . getComponentAt ( 3 ) ) . clear ( ) ;
}
emailService . enviarCorreo ( destinatario , asunto , cuerpo , imagePath ) ;
UI . getCurrent ( ) . navigate ( "/" ) ;
} else {
Notification . show ( "Por favor selecciona un usuario" , 4000 , Notification . Position . MIDDLE ) ;
} catch ( Exception ex ) {
Notification . show ( "Ocurrio un error inesperado: " + ex . getMessage ( ) , 5000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_ERROR ) ;
ex . printStackTrace ( ) ;
}
}
@ -716,6 +753,14 @@ public class MantenimientoView extends VerticalLayout implements BeforeEnterObse
String idParam = beforeEnterEvent . getLocation ( ) . getQueryParameters ( ) . getParameters ( ) . get ( "id" ) ! = null
? beforeEnterEvent . getLocation ( ) . getQueryParameters ( ) . getParameters ( ) . get ( "id" ) . stream ( ) . findFirst ( ) . orElse ( null )
: null ;
if ( idParam ! = null ) {
try {
int id = Integer . parseInt ( idParam ) ;
planAnualActual = databaseService . getPlanAnualPorId ( id ) ;
} catch ( NumberFormatException e ) {
planAnualActual = null ;
}
}
String equipoParam = beforeEnterEvent . getLocation ( ) . getQueryParameters ( ) . getParameters ( ) . get ( "nomEquipo" ) ! = null
? beforeEnterEvent . getLocation ( ) . getQueryParameters ( ) . getParameters ( ) . get ( "nomEquipo" ) . stream ( ) . findFirst ( ) . orElse ( null )