@ -4,11 +4,14 @@ import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button ;
import com.vaadin.flow.component.button.ButtonVariant ;
import com.vaadin.flow.component.checkbox.Checkbox ;
import com.vaadin.flow.component.checkbox.CheckboxGroup ;
import com.vaadin.flow.component.checkbox.CheckboxGroupVariant ;
import com.vaadin.flow.component.dependency.CssImport ;
import com.vaadin.flow.component.grid.Grid ;
import com.vaadin.flow.component.grid.GridVariant ;
import com.vaadin.flow.component.grid.HeaderRow ;
import com.vaadin.flow.component.grid.dataview.GridListDataView ;
import com.vaadin.flow.component.html.Div ;
import com.vaadin.flow.component.html.H4 ;
import com.vaadin.flow.component.html.H5 ;
import com.vaadin.flow.component.html.Span ;
@ -18,6 +21,8 @@ import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.notification.NotificationVariant ;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout ;
import com.vaadin.flow.component.orderedlayout.VerticalLayout ;
import com.vaadin.flow.component.popover.Popover ;
import com.vaadin.flow.component.popover.PopoverPosition ;
import com.vaadin.flow.component.radiobutton.RadioButtonGroup ;
import com.vaadin.flow.component.textfield.TextField ;
import com.vaadin.flow.component.upload.Upload ;
@ -29,26 +34,19 @@ import com.vaadin.flow.router.Route;
import jakarta.annotation.security.PermitAll ;
import mx.gob.jumapacelaya.models.PlanAnual ;
import mx.gob.jumapacelaya.services.DatabaseService ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.poi.ss.usermodel.* ;
import org.springframework.core.env.Environment ;
import org.vaadin.lineawesome.LineAwesomeIcon ;
import java.io.ByteArrayInputStream ;
import java.io.ByteArrayOutputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.* ;
import java.lang.reflect.Array ;
import java.time.LocalDate ;
import java.time.Year ;
import java.time.format.DateTimeFormatter ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.List ;
import java.util.* ;
import java.util.function.Consumer ;
import javax.xml.crypto.Data ;
import static java.util.regex.Pattern.matches ;
@PermitAll
@PageTitle ( "Plan Anual de Mantenimiento" )
@Route ( value = "/" , layout = MainLayout . class )
@ -65,6 +63,8 @@ public class PlanAnualView extends VerticalLayout {
HorizontalLayout uploadLayout = new HorizontalLayout ( ) ;
HorizontalLayout filtrosLayout = new HorizontalLayout ( ) ;
private final DatabaseService databaseService ;
Grid < PlanAnual > planAnualGrid ;
Button btnColumns ;
public PlanAnualView ( DatabaseService databaseService , Environment env ) {
this . databaseService = databaseService ;
@ -92,17 +92,17 @@ public class PlanAnualView extends VerticalLayout {
/ * headerRow . getCell ( planAnualGrid . getColumnByKey ( "smtColumnKey" ) )
. setComponent ( createFilterHeader ( "S.M.T" , planAnualFilter : : setSmt ) ) ; * /
headerRow . getCell ( planAnualGrid . getColumnByKey ( "equipoColumnKey " ) )
headerRow . getCell ( planAnualGrid . getColumnByKey ( "equipo" ) )
. setComponent ( createFilterHeader ( "Equipo" , planAnualFilter : : setEquipo ) ) ;
headerRow . getCell ( planAnualGrid . getColumnByKey ( "departamentoColumnKey " ) )
headerRow . getCell ( planAnualGrid . getColumnByKey ( "departamento" ) )
. setComponent ( createFilterHeader ( "Departamento" , planAnualFilter : : setDepartamento ) ) ;
/ / Componente UPLOAD para subir archivos
MemoryBuffer buffer = new MemoryBuffer ( ) ;
Upload upload = new Upload ( buffer ) ;
Upload upload = new Upload ( ) ;
upload . setAcceptedFileTypes ( ".xls" , ".xlsx" ) ;
upload . setMaxFiles ( 1 ) ;
upload . setDropLabel ( new com . vaadin . flow . component . html . Span ( "Arrastra un archivo Excel aquí o selecciona uno" ) ) ;
@ -111,7 +111,7 @@ public class PlanAnualView extends VerticalLayout {
try {
/ / Almacena el contenido del archivo en un arreglo de bytes
try ( InputStream inputStream = buffer . getInputStream ( ) ;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ( ) ) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ( ) ) {
inputStream . transferTo ( byteArrayOutputStream ) ;
fileContent = byteArrayOutputStream . toByteArray ( ) ;
@ -140,7 +140,6 @@ public class PlanAnualView extends VerticalLayout {
ntfError . add ( notificationLayout ) ;
ntfError . open ( ) ; / / Abrir la notificación en pantalla
/ / Ocultar el botón de insertar si hay errores
btnInsertar . setVisible ( false ) ;
} else {
@ -155,7 +154,7 @@ public class PlanAnualView extends VerticalLayout {
btnInsertar = new Button ( "Insertar archivo" , event - > insertarDatos ( ) ) ;
btnInsertar . setVisible ( false ) ;
gridLayout . add ( planAnualGrid ) ;
gridLayout . add ( btnColumns , planAnualGrid ) ;
gridLayout . setSizeFull ( ) ;
uploadLayout . add ( upload , btnInsertar ) ;
@ -166,11 +165,8 @@ public class PlanAnualView extends VerticalLayout {
filtrosLayout . setWidthFull ( ) ;
filtrosLayout . setJustifyContentMode ( JustifyContentMode . CENTER ) ;
Button btnPendientes = new Button ( "Pendientes" ) ;
Button btnRealizados = new Button ( "Realizados" ) ;
/ / btnPendientes . addThemeVariants ( ButtonVariant . LUMO_TERTIARY_INLINE ) ;
/ / btnRealizados . addThemeVariants ( ButtonVariant . LUMO_TERTIARY_INLINE ) ;
Button btnPendientes = new Button ( "Pendientes" , VaadinIcon . CLOCK . create ( ) ) ;
Button btnRealizados = new Button ( "Realizados" , VaadinIcon . CHECK_CIRCLE . create ( ) ) ;
btnPendientes . addClassName ( "selected-tab" ) ;
@ -214,10 +210,10 @@ public class PlanAnualView extends VerticalLayout {
add ( headerLayout ) ;
}
private Grid < PlanAnual > setupGrid ( ) {
Grid < PlanAnual > planAnualGrid = new Grid < > ( PlanAnual . class , false ) ;
private Grid < PlanAnual > setupGrid ( ) {
planAnualGrid = new Grid < > ( PlanAnual . class , false ) ;
planAnualGrid . addColumn ( ( ValueProvider < PlanAnual , Integer > ) PlanAnual : : getNumero )
. setHeader ( "No." ) . setSortable ( true ) . setAutoWidth ( true ) . setFrozen ( true ) ;
@ -264,52 +260,97 @@ public class PlanAnualView extends VerticalLayout {
} ) . setAutoWidth ( true ) . setFrozen ( true ) ;
planAnualGrid . addColumn ( ( ValueProvider < PlanAnual , String > ) PlanAnual : : getNomEquipo )
. setHeader ( "Equipo" ) . setAutoWidth ( true ) . setKey ( "equipoColumnKey " ) ;
. setHeader ( "Equipo" ) . setAutoWidth ( true ) . setKey ( "equipo" ) ;
planAnualGrid . addColumn ( ( ValueProvider < PlanAnual , String > ) PlanAnual : : getDepartamento )
. setHeader ( "Departamento" ) . setAutoWidth ( true ) . setKey ( "departamentoColumnKey" ) ;
planAnualGrid . addThemeVariants ( GridVariant . LUMO_WRAP_CELL_CONTENT ) ;
planAnualGrid . addThemeVariants ( GridVariant . LUMO_ROW_STRIPES ) ;
. setHeader ( "Departamento" ) . setAutoWidth ( true ) . setKey ( "departamento" ) ;
planAnualGrid . addColumn ( planAnual - > {
String mesPlaneado = planAnual . getMesplaneado ( ) ;
return mesPlaneado ! = null ? mesPlaneado . toUpperCase ( ) : "N/A" ;
} ) . setHeader ( "Mes Planeado" ) . setAutoWidth ( true ) ;
} ) . setHeader ( "Mes Planeado" ) . setAutoWidth ( true ) . setKey ( "mesplaneado" ) ;
/ / Fechas formateadas
planAnualGrid . addColumn ( planAnual - > {
LocalDate fecha = planAnual . getFechaProgramada ( ) ;
return fecha ! = null ? fecha . format ( DateTimeFormatter . ofPattern ( "dd/MM/yyyy" ) ) : "No programada" ;
} ) . setHeader ( "Fecha Programada" ) . setAutoWidth ( true ) . setSortable ( true ) ;
} ) . setHeader ( "Fecha Programada" ) . setAutoWidth ( true ) . setSortable ( true ) . setKey ( "fechaProgramada" ) ;
planAnualGrid . addColumn ( planAnual - > {
LocalDate fecha = planAnual . getFechaMantenimiento ( ) ;
return fecha ! = null ? fecha . format ( DateTimeFormatter . ofPattern ( "dd/MM/yyyy" ) ) : "" ;
} ) . setHeader ( "Fecha Realización" ) . setAutoWidth ( true ) . setSortable ( true ) ;
} ) . setHeader ( "Fecha Realización" ) . setAutoWidth ( true ) . setSortable ( true ) . setKey ( "fechaMantenimiento" ) ;
planAnualGrid . addColumn ( ( ValueProvider < PlanAnual , String > ) PlanAnual : : getEstado )
. setHeader ( "Estado" ) . setAutoWidth ( true ) ;
. setHeader ( "Estado" ) . setAutoWidth ( true ) . setKey ( "estado" ) ;
planAnualGrid . addColumn ( ( ValueProvider < PlanAnual , String > ) PlanAnual : : getSituacion )
. setHeader ( "Situación" ) . setAutoWidth ( true ) ;
. setHeader ( "Situación" ) . setAutoWidth ( true ) . setKey ( "situacion" ) ;
/ * planAnualGrid . addColumn ( ( ValueProvider < PlanAnual , String > ) PlanAnual : : getSmt )
. setHeader ( "S.M.T" ) . setKey ( "smtColumnKey" ) ; * /
/ / Iconos en columnas booleanas
/ * planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isMonitor ( ) ) ) . setHeader ( "Monitor" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isTeclado ( ) ) ) . setHeader ( "Teclado" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isMouse ( ) ) ) . setHeader ( "Mouse" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isRegulador ( ) ) ) . setHeader ( "Regulador" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isCpu ( ) ) ) . setHeader ( "CPU" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isImpresora ( ) ) ) . setHeader ( "Impresora" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isMiniPrint ( ) ) ) . setHeader ( "MiniPrint" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isLaptop ( ) ) ) . setHeader ( "Laptop" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isEscaner ( ) ) ) . setHeader ( "Escáner" ) ; * /
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isMonitor ( ) ) ) . setHeader ( "Monitor" ) . setKey ( "monitor" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isTeclado ( ) ) ) . setHeader ( "Teclado" ) . setKey ( "teclado" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isMouse ( ) ) ) . setHeader ( "Mouse" ) . setKey ( "mouse" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isRegulador ( ) ) ) . setHeader ( "Regulador" ) . setKey ( "regulador" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isCpu ( ) ) ) . setHeader ( "CPU" ) . setKey ( "cpu" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isImpresora ( ) ) ) . setHeader ( "Impresora" ) . setKey ( "impresora" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isMiniPrint ( ) ) ) . setHeader ( "MiniPrint" ) . setKey ( "miniprint" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isLaptop ( ) ) ) . setHeader ( "Laptop" ) . setKey ( "laptop" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isEscaner ( ) ) ) . setHeader ( "Escáner" ) . setKey ( "escaner" ) ;
planAnualGrid . addThemeVariants ( GridVariant . LUMO_WRAP_CELL_CONTENT ) ;
planAnualGrid . addThemeVariants ( GridVariant . LUMO_ROW_STRIPES ) ;
btnColumns = new Button ( VaadinIcon . GRID_H . create ( ) ) ;
btnColumns . addThemeVariants ( ButtonVariant . LUMO_ICON ) ;
btnColumns . setAriaLabel ( "Show/Hide Columns" ) ;
HorizontalLayout columnSelectorLayout = new HorizontalLayout ( ) ;
columnSelectorLayout . setAlignItems ( Alignment . END ) ;
Popover popover = new Popover ( ) ;
popover . setModal ( true ) ;
popover . setBackdropVisible ( true ) ;
popover . setPosition ( PopoverPosition . BOTTOM_END ) ;
popover . setTarget ( btnColumns ) ;
Div heading = new Div ( "Ver/Ocultar columnas" ) ;
heading . getStyle ( ) . set ( "font-weight" , "600" ) ;
heading . getStyle ( ) . set ( "padding" , "var(--lumo-space-xs)" ) ;
List < String > columns = List . of ( "equipo" , "departamento" , "mesplaneado" , "fechaProgramada" ,
"fechaMantenimiento" , "estado" , "situacion" , "monitor" , "teclado" , "mouse" , "regulador" ,
"cpu" , "impresora" , "miniprint" , "laptop" , "escaner" ) ;
CheckboxGroup < String > chkColumns = new CheckboxGroup < > ( ) ;
chkColumns . addThemeVariants ( CheckboxGroupVariant . LUMO_VERTICAL ) ;
chkColumns . setItems ( columns ) ;
chkColumns . setItemLabelGenerator ( ( item ) - > {
String label = StringUtils
. join ( StringUtils . splitByCharacterTypeCamelCase ( item ) , " " ) ;
return StringUtils . capitalize ( label . toLowerCase ( ) ) ;
} ) ;
chkColumns . addValueChangeListener ( ( e ) - > {
columns . forEach ( ( key ) - > {
Grid . Column < ? > col = planAnualGrid . getColumnByKey ( key ) ;
if ( col ! = null ) {
col . setVisible ( e . getValue ( ) . contains ( key ) ) ;
} else {
System . out . println ( "Columna no encontrada para key: " + key ) ;
}
} ) ;
} ) ;
Set < String > defaultColumns = Set . of ( "equipo" , "departamento" , "mesplaneado" ,
"fechaProgramada" , "fechaMantenimiento" , "estado" , "situacion" ) ;
chkColumns . setValue ( defaultColumns ) ;
popover . add ( heading , chkColumns ) ;
/ / Cargar datos
planAnualGrid . setItems ( databaseService . getPlanAnual ( ) ) ;
return planAnualGrid ;