@ -6,9 +6,11 @@ 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.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.formlayout.FormLayout ;
import com.vaadin.flow.component.grid.Grid ;
import com.vaadin.flow.component.grid.GridVariant ;
import com.vaadin.flow.component.grid.HeaderRow ;
@ -23,6 +25,7 @@ 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.NumberField ;
import com.vaadin.flow.component.textfield.TextField ;
import com.vaadin.flow.component.upload.Upload ;
import com.vaadin.flow.component.upload.receivers.MemoryBuffer ;
@ -71,7 +74,9 @@ public class PlanAnualView extends VerticalLayout {
Grid < PlanAnual > planAnualGrid ;
Button btnColumns ;
Button btnImprimirRpt ;
Button btnAddEquipo ;
HorizontalLayout btnImprimirLayout ;
private Popover reportePopover ;
public PlanAnualView ( DatabaseService databaseService , Environment env , ReportService reportService ) {
this . databaseService = databaseService ;
@ -191,6 +196,7 @@ public class PlanAnualView extends VerticalLayout {
} ) ;
filtrosLayout . add ( btnPendientes , btnRealizados ) ;
mostrarPopoverReportes ( ) ;
this . setPadding ( false ) ;
@ -299,7 +305,7 @@ public class PlanAnualView extends VerticalLayout {
/ * 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" ) . setKey ( "monitor" ) ;
planAnualGrid . addComponentColumn ( planAnual - > getIcon ( planAnual . isTeclado ( ) ) ) . setHeader ( "Teclado" ) . setKey ( "teclado" ) ;
@ -321,9 +327,14 @@ public class PlanAnualView extends VerticalLayout {
btnColumns . setTooltipText ( "Show/Hide Columns" ) ;
btnImprimirRpt = new Button ( VaadinIcon . PRINT . create ( ) ) ;
btnImprimirRpt . addClickListener ( event - > saca rR eporte( ) ) ;
btnImprimirRpt . addClickListener ( event - > reportePopover . open ( ) ) ;
btnImprimirRpt . setTooltipText ( "Imprimir reporte" ) ;
btnImprimirLayout = new HorizontalLayout ( btnColumns , btnImprimirRpt ) ;
btnAddEquipo = new Button ( VaadinIcon . PLUS . create ( ) ) ;
btnAddEquipo . addClickListener ( event - > addNuevoEquipo ( ) ) ;
btnAddEquipo . setTooltipText ( "Agregar nuevo equipo" ) ;
btnImprimirLayout = new HorizontalLayout ( btnColumns , btnImprimirRpt , btnAddEquipo ) ;
HorizontalLayout columnSelectorLayout = new HorizontalLayout ( ) ;
columnSelectorLayout . setAlignItems ( Alignment . END ) ;
@ -608,4 +619,212 @@ public class PlanAnualView extends VerticalLayout {
dialog . open ( ) ;
}
private void addNuevoEquipo ( ) {
Dialog dialog = new Dialog ( ) ;
dialog . setHeaderTitle ( "Agregar nuevo equipo" ) ;
TextField nombreEquipo = new TextField ( "Nombre equipo:" ) ;
TextField area = new TextField ( "Área:" ) ;
DatePicker . DatePickerI18n i18n = new DatePicker . DatePickerI18n ( )
. setWeekdays ( List . of ( "Domingo" , "Lunes" , "Martes" , "Miércoles" , "Jueves" , "Viernes" , "Sabado" ) )
. setWeekdaysShort ( List . of ( "Dom" , "Lun" , "Mar" , "Mié" , "Jue" , "Vie" , "Sáb" ) )
. setMonthNames ( List . of ( "Enero" , "Febrero" , "Marzo" , "Abril" , "Mayo" , "Junio" , "Julio" , "Agosto" , "Septiembre" , "Octubre" , "Noviembre" , "Diciembre" ) )
. setFirstDayOfWeek ( 1 )
. setToday ( "Hoy" )
. setCancel ( "Cancelar" )
. setDateFormat ( "dd/MM/yyyy" ) ;
DatePicker fechaProgramada = new DatePicker ( "Fecha programada:" ) ;
fechaProgramada . setI18n ( i18n ) ;
List < String > items = List . of ( "ENERO" , "FEBRERO" , "MARZO" , "ABRIL" , "MAYO" , "JUNIO" ,
"JULIO" , "AGOSTO" , "SEPTIEMBRE" , "OCTUBRE" , "NOVIEMBRE" , "DICIEMBRE" ) ;
ComboBox < String > cmbMes = new ComboBox < > ( "Mes Planeado:" ) ;
cmbMes . setItems ( items ) ;
CheckboxGroup < String > group1 = new CheckboxGroup < > ( ) ;
group1 . setLabel ( "Seleccione los componentes del equipo" ) ;
group1 . setItems ( "MONITOR" , "TECLADO" , "MOUSE" , "REGULADOR" ) ;
CheckboxGroup < String > group2 = new CheckboxGroup < > ( ) ;
group2 . setItems ( "CPU" , "IMPRESORA" , "MINIPRINT" , "LAPTOP" , "ESCANER" ) ;
HorizontalLayout layout1 = new HorizontalLayout ( nombreEquipo , area ) ;
HorizontalLayout layout2 = new HorizontalLayout ( fechaProgramada , cmbMes ) ;
VerticalLayout layout3 = new VerticalLayout ( group1 , group2 ) ;
layout3 . setSpacing ( false ) ;
Button btnGuardar = new Button ( "Guardar" , LineAwesomeIcon . SAVE_SOLID . create ( ) ) ;
btnGuardar . addThemeVariants ( ButtonVariant . LUMO_PRIMARY ) ;
btnGuardar . addClickListener ( e - > {
if ( nombreEquipo . getValue ( ) . isEmpty ( ) | | area . getValue ( ) . isEmpty ( ) | |
fechaProgramada . getValue ( ) = = null | | cmbMes . getValue ( ) = = null ) {
Notification . show ( "Por favor, complete todos los campos." , 3000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_ERROR ) ;
return ;
}
final Map < String , Integer > MES_ID_MAP = new HashMap < > ( ) ;
MES_ID_MAP . put ( "ENERO" , 1 ) ;
MES_ID_MAP . put ( "FEBRERO" , 2 ) ;
MES_ID_MAP . put ( "MARZO" , 3 ) ;
MES_ID_MAP . put ( "ABRIL" , 4 ) ;
MES_ID_MAP . put ( "MAYO" , 5 ) ;
MES_ID_MAP . put ( "JUNIO" , 6 ) ;
MES_ID_MAP . put ( "JULIO" , 7 ) ;
MES_ID_MAP . put ( "AGOSTO" , 8 ) ;
MES_ID_MAP . put ( "SEPTIEMBRE" , 9 ) ;
MES_ID_MAP . put ( "OCTUBRE" , 10 ) ;
MES_ID_MAP . put ( "NOVIEMBRE" , 11 ) ;
MES_ID_MAP . put ( "DICIEMBRE" , 12 ) ;
String mesSeleccionado = cmbMes . getValue ( ) ;
Integer mesId = MES_ID_MAP . get ( mesSeleccionado ) ;
if ( group1 . getValue ( ) . isEmpty ( ) & & group2 . getValue ( ) . isEmpty ( ) ) {
Notification . show ( "Debe seleccionar al menos un componente del equipo." , 3000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_ERROR ) ;
return ;
}
Set < String > seleccionados = new HashSet < > ( ) ;
seleccionados . addAll ( group1 . getValue ( ) ) ;
seleccionados . addAll ( group2 . getValue ( ) ) ;
boolean monitor = seleccionados . contains ( "MONITOR" ) ;
boolean teclado = seleccionados . contains ( "TECLADO" ) ;
boolean mouse = seleccionados . contains ( "MOUSE" ) ;
boolean regulador = seleccionados . contains ( "REGULADOR" ) ;
boolean cpu = seleccionados . contains ( "CPU" ) ;
boolean impresora = seleccionados . contains ( "IMPRESORA" ) ;
boolean miniprint = seleccionados . contains ( "MINIPRINT" ) ;
boolean laptop = seleccionados . contains ( "LAPTOP" ) ;
boolean escaner = seleccionados . contains ( "ESCANER" ) ;
databaseService . insertarNuevoEquipo (
nombreEquipo . getValue ( ) . toUpperCase ( ) ,
area . getValue ( ) . toUpperCase ( ) ,
monitor ,
teclado ,
mouse ,
regulador ,
cpu ,
impresora ,
miniprint ,
laptop ,
escaner ,
java . sql . Date . valueOf ( fechaProgramada . getValue ( ) ) ,
"" ,
"PENDIENTE" ,
mesId
) ;
Notification . show ( "Equipo agregado correctamente" , 3000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_SUCCESS ) ;
dialog . close ( ) ;
/ / Recargar el grid para mostrar el nuevo equipo
planAnualGrid . setItems ( databaseService . getPlanAnual ( ) ) ;
} ) ;
Button btnCancelar = new Button ( "Cancelar" , VaadinIcon . CLOSE_CIRCLE . create ( ) , e - > dialog . close ( ) ) ;
dialog . getFooter ( ) . add ( btnGuardar ) ;
dialog . getFooter ( ) . add ( btnCancelar ) ;
dialog . add ( layout1 , layout2 , layout3 ) ;
dialog . open ( ) ;
}
private void mostrarPopoverReportes ( ) {
reportePopover = new Popover ( ) ;
reportePopover . setModal ( true ) ;
reportePopover . setBackdropVisible ( true ) ;
reportePopover . setPosition ( PopoverPosition . BOTTOM_END ) ;
reportePopover . setTarget ( btnImprimirRpt ) ;
Div headding = new Div ( "Reportes disponibles" ) ;
headding . getStyle ( ) . set ( "font-weight" , "600" ) ;
headding . getStyle ( ) . set ( "padding" , "var(--lumo-space-xs)" ) ;
VerticalLayout listaRrportes = new VerticalLayout ( ) ;
/ / listaRrportes . setSpacing ( false ) ;
listaRrportes . setPadding ( false ) ;
Button btnReporteFechas = new Button ( "Mantenimiento por fechas" , VaadinIcon . STAR . create ( ) , e - > {
reportePopover . close ( ) ;
sacarReporte ( ) ;
} ) ;
btnReporteFechas . addThemeVariants ( ButtonVariant . LUMO_TERTIARY_INLINE ) ;
Button btnListadoMant = new Button ( "Listado de mantenimientos" , VaadinIcon . STAR . create ( ) , e - > {
reportePopover . close ( ) ;
obtenerListadoRpt ( ) ;
} ) ;
btnListadoMant . addThemeVariants ( ButtonVariant . LUMO_TERTIARY_INLINE ) ;
listaRrportes . add ( btnReporteFechas , btnListadoMant ) ;
reportePopover . add ( headding , listaRrportes ) ;
}
private void obtenerListadoRpt ( ) {
Dialog dialog = new Dialog ( ) ;
dialog . setHeaderTitle ( "Ingresa el peridodo a consultar" ) ;
TextField txtMes = new TextField ( "Mes:" ) ;
txtMes . setRequired ( true ) ;
NumberField txtAnio = new NumberField ( "Año:" ) ;
txtAnio . setRequired ( true ) ;
HorizontalLayout layout = new HorizontalLayout ( txtMes , txtAnio ) ;
Button btnGenerar = new Button ( "Generar" , VaadinIcon . PRINT . create ( ) ) ;
btnGenerar . addThemeVariants ( ButtonVariant . LUMO_PRIMARY ) ;
btnGenerar . addClickListener ( e - > {
String mes = txtMes . getValue ( ) . toUpperCase ( ) ;
Integer anio = txtAnio . getValue ( ) ! = null ? txtAnio . getValue ( ) . intValue ( ) : null ;
if ( mes . isEmpty ( ) | | anio = = null ) {
Notification . show ( "Por favor, completa todos los campos." , 2000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_ERROR ) ;
return ;
}
try {
/ / Preparando los parámetros para el reporte
Map < String , Object > parametros = new HashMap < > ( ) ;
parametros . put ( "pMES" , mes ) ;
parametros . put ( "pANIO" , anio ) ;
/ / Generando el PDF
byte [ ] pdf = reportService . generarReporte ( "listadoMantenimientos" , parametros ) ;
/ / Creando el recurso de descarga
StreamResource resource = new StreamResource ( "listado_mantenimientos.pdf" , ( ) - > new ByteArrayInputStream ( pdf ) ) ;
Anchor downloadLink = new Anchor ( resource , "Descargar Reporte" ) ;
downloadLink . setTarget ( "_blank" ) ;
downloadLink . setId ( "descargar-listado-link" ) ;
add ( downloadLink ) ;
getUI ( ) . ifPresent ( ui - >
ui . getPage ( ) . executeJs ( "document.getElementById('descargar-listado-link').click();" )
) ;
} catch ( Exception ex ) {
Notification . show ( "Error al generar el reporte: " + ex . getMessage ( ) , 5000 , Notification . Position . MIDDLE )
. addThemeVariants ( NotificationVariant . LUMO_ERROR ) ;
ex . printStackTrace ( ) ;
}
} ) ;
Button btnCancelar = new Button ( "Cancelar" , VaadinIcon . CLOSE_CIRCLE . create ( ) , e - > dialog . close ( ) ) ;
dialog . getFooter ( ) . add ( btnCancelar , btnGenerar ) ;
dialog . add ( layout ) ;
dialog . open ( ) ;
}
}