Reporte con ReportUtils y <jmoordbjsf:paginator>
Last updated
Was this helpful?
Last updated
Was this helpful?
Podemos simplificar los reportes para invocarlo desde el componente <jmoordbjsf:paginator> usando un filtro entre fechas.
se habilita cuando el list del datatable contiene datos,
Al presionar el boton Imprimir
En el paginator validamos que no este vació el list e invocamos al método printAll.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<ui:composition template="/layout/template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:b="http://bootsfaces.net/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:jmoordbjsf="http://jmoordbjsf.com/taglib"
xmlns:e="http://xmlns.jcp.org/jsf/composite/extensions"
>
<ui:define name="content">
<style>
.thumbnail { max-width: 100%; }
img.thumbnail:hover, img.thumbnail:focus {
border: 1px solid;
border-color: #428BCA;
}
</style>
<b:form id="form" prependId="false" rendered="#{p:ifAnyGranted('ADMINISTRADOR, SECRETARIO ADMINISTRATIVO')}" onkeypress="if (event.keyCode == 13) {
return false;
}">
<b:growl id="msgs"/>
<b:panel collapsible="false" title="#{msg['titlelist.programacionvehicular']}" id="content" look="primary" >
<!--<b:panelGrid colSpans="2,10" size="xs">-->
<b:row>
<b:column medium-screen="25" >
<jmoordbjsf:searchBetweenDate
renderedMove="true"
labelDesde="#{msg['field.fechainicio']}"
valueDesde="#{programacionVechicularController.fechaDesde}"
labelHasta="#{msg['field.fechafin']}"
valueHasta="#{programacionVechicularController.fechaHasta}"
renderedList="true"
search="#{programacionVechicularController.searchBy('_betweendates',programacionVechicularController.viaje)}"
update=":form:dataTable "
/>
</b:column>
</b:row>
</b:panel>
<b:panel collapsible="false" id="dataTable" look="primary">
<jmoordbjsf:paginator
controller="#{programacionVechicularController}"
rowPage="#{programacionVechicularController.rowPage}"
page="#{programacionVechicularController.page}"
pages="#{programacionVechicularController.pages}"
renderedPrint="#{programacionVechicularController.availablePrint()}"
renderedNew="false"
skip="ajax:programacionVechicularController.skip()"
url = "/pages/viaje/new.xhtml"
/>
<p:dataTable value="#{programacionVechicularController.programacionVehicular}"
var="item"
id="dataTable2"
>
<f:facet name="header">
<p:outputLabel value="#{msg['titlelist.viajes']}"/>
</f:facet>
<p:column style="width:16px">
<p:rowToggler />
</p:column>
<p:column headerText="#{msg['field.fechapartida']}" style="width:130px">
<h:outputText value="#{programacionVechicularController.showDate(item.fechahorasalida)} #{programacionVechicularController.showHour(item.fechahorasalida)}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}" />
<p:outputLabel value="#{msg['field.dia']} #{item.nombredia}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}"/>
</p:column>
<p:column headerText="#{msg['field.mision']}" style="width:190px ">
<p:outputLabel value="#{item.mision}" style="font-size: 10; color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}"/>
</p:column>
<p:column headerText="#{msg['field.unidad']}" >
<p:outputLabel value="#{item.unidad}"/>
</p:column>
<p:column headerText="#{msg['field.fecharegreso']}" style="width:100px">
<h:outputText value="#{programacionVechicularController.showDate(item.fechahoraregreso)} #{programacionVechicularController.showHour(item.fechahoraregreso)}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}" />
</p:column>
<p:column headerText="#{msg['field.vehiculo']}" >
<p:outputLabel value="#{item.marca} #{item.modelo} #{msg['field.placa']} #{item.placa}"/>
</p:column>
<p:column headerText="#{msg['field.conductor']}" >
<p:outputLabel value="#{item.conductor}"/>
</p:column>
<p:rowExpansion >
<b:panelGrid id="panel" colSpans="2,10" size="sm" rendered="#{p:ifGranted('SECRETARIO ADMINISTRATIVO')}">
<p:outputLabel value="#{msg['field.mision']}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}"/>
<p:outputLabel value="#{item.mision}" />
<p:outputLabel value="#{msg['field.lugarpartida']}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}"/>
<p:outputLabel value="#{item.lugarpartida}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}"/>
<p:outputLabel value="#{msg['field.lugardestino']}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}"/>
<p:outputLabel value="#{item.lugardestino}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}"/>
<p:outputLabel value="#{msg['field.fechasolicitud']}" />
<h:outputText value="#{programacionVechicularController.showDate(item.fechasolicitud)} #{programacionVechicularController.showHour(item.fechasolicitud)}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}" />
<p:outputLabel value="#{msg['field.numerosolicitudes']}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}" />
<p:outputLabel value="#{item.numerosolicitudes}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}"/>
<p:outputLabel value="#{msg['field.solicita']}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}"/>
<p:outputLabel value="#{item.solicita}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}"/>
<p:outputLabel value="#{msg['field.responsable']}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}"/>
<p:outputLabel value="#{item.responsable}" style="color: #{programacionVechicularController.columnColor(item.realizado,item.activo)}"/>
</b:panelGrid>
</p:rowExpansion>
</p:dataTable>
</b:panel>
</b:form>
<jmoordbjsf:denegado renderedcondition="#{!p:ifAnyGranted('ADMINISTRADOR, SECRETARIO ADMINISTRATIVO')}" />
</ui:define>
</ui:composition>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.avbravo.transporte.controller;
// <editor-fold defaultstate="collapsed" desc="imports">
import com.avbravo.jmoordb.configuration.JmoordbContext;
import com.avbravo.jmoordb.configuration.JmoordbControllerEnvironment;
import com.avbravo.jmoordb.interfaces.IController;
import com.avbravo.jmoordbutils.printer.Printer;
import com.avbravo.jmoordb.mongodb.history.services.AutoincrementableServices;
import com.avbravo.jmoordb.mongodb.history.services.ErrorInfoServices;
import com.avbravo.jmoordbutils.DateUtil;
import com.avbravo.jmoordbutils.JmoordbResourcesFiles;
import com.avbravo.jmoordbutils.ReportUtils;
import com.avbravo.transporte.beans.ProgramacionVehicular;
import com.avbravo.transporteejb.datamodel.ViajeDataModel;
import com.avbravo.transporteejb.entity.Solicitud;
import com.avbravo.transporteejb.entity.Unidad;
import com.avbravo.transporteejb.entity.Viaje;
import com.avbravo.transporteejb.entity.Usuario;
import com.avbravo.transporteejb.repository.SolicitudRepository;
import com.avbravo.transporteejb.repository.ViajeRepository;
import com.avbravo.transporteejb.services.EstatusServices;
import com.avbravo.transporteejb.services.ViajeServices;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.*;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.PageSize;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import lombok.Getter;
import lombok.Setter;
import org.bson.Document;
import org.primefaces.event.SelectEvent;
// </editor-fold>
/**
*
* @authoravbravo
*/
@Named
@ViewScoped
@Getter
@Setter
public class ProgramacionVechicularController implements Serializable, IController {
// <editor-fold defaultstate="collapsed" desc="fields">
private static final long serialVersionUID = 1L;
private Boolean writable = false;
private Date fechaDesde;
private Date fechaHasta;
//DataModel
private ViajeDataModel viajeDataModel;
Integer page = 1;
Integer rowPage = 35;
List<Integer> pages = new ArrayList<>();
//Entity
Viaje viaje = new Viaje();
Viaje viajeSelected;
Viaje viajeSearch = new Viaje();
//List
List<Viaje> viajeList = new ArrayList<>();
List<ProgramacionVehicular> programacionVehicular = new ArrayList<>();
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="repository">
//Repository
@Inject
SolicitudRepository solicitudRepository;
@Inject
ViajeRepository viajeRepository;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="services">
//Services
@Inject
AutoincrementableServices autoincrementableServices;
@Inject
EstatusServices estatusServices;
@Inject
ErrorInfoServices errorServices;
@Inject
ViajeServices viajeServices;
@Inject
JmoordbResourcesFiles rf;
@Inject
Printer printer;
// </editor-fold>
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="getter/setter">
public List<Integer> getPages() {
return viajeRepository.listOfPage(rowPage);
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="constructor">
public ProgramacionVechicularController() {
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="init">
@PostConstruct
public void init() {
try {
fechaDesde = DateUtil.primerDiaDelMesEnFecha(DateUtil.anioActual(), DateUtil.mesActual());
fechaHasta = DateUtil.ultimoDiaDelMesEnFecha(DateUtil.anioActual(), DateUtil.mesActual());
/*
configurar el ambiente del controller
*/
HashMap parameters = new HashMap();
Usuario jmoordb_user = (Usuario) JmoordbContext.get("jmoordb_user");
// parameters.put("P_EMPRESA", jmoordb_user.getEmpresa().getDescripcion());
JmoordbControllerEnvironment jmc = new JmoordbControllerEnvironment.Builder()
.withController(this)
.withRepository(viajeRepository)
.withEntity(viaje)
.withService(viajeServices)
.withNameFieldOfPage("page")
.withNameFieldOfRowPage("rowPage")
.withTypeKey("primary")
.withSearchLowerCase(false)
.withPathReportDetail("/resources/reportes/programacionvehicular/details.jasper")
.withPathReportAll("/resources/reportes/programacionvehicular/all.jasper")
.withparameters(parameters)
.withResetInSave(true)
.withAction("golist")
.build();
start();
// String action = "gonew";
// if (getAction() != null) {
// action = getAction();
// }
//
// if (action == null || action.equals("gonew") || action.equals("new") || action.equals("golist")) {
// //inicializar
//
// }
// if (action.equals("view")) {
// //view
// }
setSearchAndValue("searchProgramacionVehicularController", "_betweendates");
} catch (Exception e) {
errorServices.errorMessage(nameOfClass(), nameOfMethod(), e.getLocalizedMessage());
}
}// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="handleSelect">
public void handleSelect(SelectEvent event) {
try {
} catch (Exception e) {
errorServices.errorMessage(nameOfClass(), nameOfMethod(), e.getLocalizedMessage());
}
}// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="move(Integer page)">
@Override
public void move(Integer page) {
try {
programacionVehicular = new ArrayList<>();
this.page = page;
viajeDataModel = new ViajeDataModel(viajeList);
Document doc;
// switch (getSearch()) {
// case "_init":
// case "_autocomplete":
// viajeList = viajeRepository.findPagination(page, rowPage);
// break;
//
// case "activo":
// if (getValueSearch() != null) {
// viajeSearch.setActivo(getValueSearch().toString());
// doc = new Document("activo", viajeSearch.getActivo());
// viajeList = viajeRepository.findPagination(doc, page, rowPage, new Document("fechahorainicioreserva", -1));
// } else {
// viajeList = viajeRepository.findPagination(page, rowPage);
// }
// break;
//
// case "_betweendates":
viajeList = viajeRepository.filterBetweenDatePaginationWithoutHours("activo", "si",
"fechahorainicioreserva", fechaDesde,
"fechahorafinreserva", fechaHasta,
page, rowPage, new Document("fechahorainicioreserva", -1));
// break;
//
// default:
// viajeList = viajeRepository.findPagination(page, rowPage);
// break;
// }
if (viajeList == null || viajeList.isEmpty()) {
} else {
for (Viaje v : viajeList) {
if (v.getRealizado().equals("ca")) {
// esta cancelado
} else {
ProgramacionVehicular pv = new ProgramacionVehicular();
pv.setConductor(v.getConductor().getNombre());
pv.setFechahoraregreso(v.getFechahorafinreserva());
pv.setFechahorasalida(v.getFechahorainicioreserva());
pv.setIdviaje(v.getIdviaje());
pv.setMarca(v.getVehiculo().getMarca());
pv.setModelo(v.getVehiculo().getModelo());
pv.setPlaca(v.getVehiculo().getPlaca());
pv.setNombredia(DateUtil.nameOfDay(pv.getFechahorasalida()));
pv.setResponsable(v.getRealizado());
pv.setActivo(v.getActivo());
pv.setLugardestino(v.getLugardestino());
pv.setLugarpartida(v.getLugarpartida());
pv.setMision(v.getMision());
//Datos de la solicitud
pv.setFechasolicitud(v.getFechahorainicioreserva());
pv.setNumerosolicitudes("");
pv.setUnidad("");
pv.setResponsable("");
pv.setSolicita("");
Solicitud solicitud = new Solicitud();
//Busco la solicitud
Document search = new Document("viaje.idviaje", v.getIdviaje());
List<Solicitud> list = solicitudRepository.findBy(search);
if (list == null || list.isEmpty()) {
} else {
String unidad = "";
String numeroSolicitudes = "";
String responsable = "";
String solicita = "";
//Se recorren todas las solicitudes que tengan ese viaje asignado.
for (Solicitud s : list) {
pv.setFechasolicitud(s.getFecha());
for (Unidad u : s.getUnidad()) {
unidad += " " + u.getIdunidad();
}
numeroSolicitudes += " " + String.valueOf(s.getIdsolicitud());
solicita += " " + s.getUsuario().get(0).getNombre();
responsable += " " + s.getUsuario().get(1).getNombre();
}
pv.setUnidad(unidad.trim());
pv.setNumerosolicitudes(numeroSolicitudes.trim());
pv.setResponsable(responsable.trim());
pv.setSolicita(solicita.trim());
}
programacionVehicular.add(pv);
}
}//for
}
viajeDataModel = new ViajeDataModel(viajeList);
} catch (Exception e) {
errorServices.errorMessage(nameOfClass(), nameOfMethod(), e.getLocalizedMessage());
}
}// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="String showDate(Date date)">
public String showDate(Date date) {
return viajeServices.showDate(date);
}// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="String showHour(Date date)">
public String showHour(Date date) {
return viajeServices.showHour(date);
}// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="String columnColor(String realizado, String activo)">
public String columnColor(String realizado, String activo) {
return viajeServices.columnColor(realizado, activo);
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="void imprimir() ">
@Override
public String printAll() {
com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4.rotate());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
PdfWriter.getInstance(document, baos);
//METADATA
document.open();
document.add(ReportUtils.paragraph("PROGRAMACION DE FLOTA VEHICULAR", FontFactory.getFont("arial", 12, Font.BOLD), Element.ALIGN_CENTER));
String texto="Desde "+DateUtil.showDate(fechaDesde) + " Hasta: "+DateUtil.showDate(fechaHasta) ;
document.add(ReportUtils.paragraph(texto, FontFactory.getFont("arial", 12, Font.BOLD), Element.ALIGN_CENTER));
Date currentDate = new Date();
String date = DateUtil.showDate(currentDate) + " " + DateUtil.showHour(currentDate);
document.add(ReportUtils.paragraph("Fecha: " + date, FontFactory.getFont("arial", 8, Font.BOLD), Element.ALIGN_RIGHT));
document.add(new Paragraph("\n"));
//Numero de columnas
PdfPTable table = new PdfPTable(8);
//Aqui indicamos el tamaño de cada columna
table.setTotalWidth(new float[]{75, 62, 75, 82, 75, 220, 80, 105});
table.setLockedWidth(true);
table.addCell(ReportUtils.PdfCell("Partida", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
table.addCell(ReportUtils.PdfCell("Dia", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
table.addCell(ReportUtils.PdfCell("Regreso", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
table.addCell(ReportUtils.PdfCell("Unidad", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
table.addCell(ReportUtils.PdfCell("Solicitado", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
table.addCell(ReportUtils.PdfCell("Mision", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
table.addCell(ReportUtils.PdfCell("Conductor", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
table.addCell(ReportUtils.PdfCell("Vehiculo", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
for (ProgramacionVehicular pv : programacionVehicular) {
String fechaPartida = DateUtil.showDate(pv.getFechahorasalida()) + " " + DateUtil.showHour(pv.getFechahorasalida());
String fechaRegreso = DateUtil.showDate(pv.getFechahoraregreso()) + " " + DateUtil.showHour(pv.getFechahoraregreso());
String fechaSolicitado = DateUtil.showDate(pv.getFechasolicitud()) + " " + DateUtil.showHour(pv.getFechasolicitud());
table.addCell(ReportUtils.PdfCell(fechaPartida, FontFactory.getFont("arial", 10, Font.NORMAL)));
table.addCell(ReportUtils.PdfCell(pv.getNombredia(), FontFactory.getFont("arial", 9, Font.NORMAL)));
table.addCell(ReportUtils.PdfCell(fechaRegreso, FontFactory.getFont("arial", 10, Font.NORMAL)));
table.addCell(ReportUtils.PdfCell(pv.getUnidad(), FontFactory.getFont("arial", 9, Font.NORMAL)));
table.addCell(ReportUtils.PdfCell(fechaSolicitado, FontFactory.getFont("arial", 10, Font.NORMAL)));
table.addCell(ReportUtils.PdfCell(pv.getMision(), FontFactory.getFont("arial", 9, Font.NORMAL)));
table.addCell(ReportUtils.PdfCell(pv.getConductor(), FontFactory.getFont("arial", 9, Font.NORMAL)));
table.addCell(ReportUtils.PdfCell(pv.getMarca() + " " + pv.getModelo() + " PLACA:" + pv.getPlaca(), FontFactory.getFont("arial", 9, Font.NORMAL)));
}
document.add(table);
} catch (Exception ex) {
System.out.println("Error " + ex.getMessage());
}
document.close();
ReportUtils.printPDF(baos);
return "";
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="metodo()">
public Boolean availablePrint(){
return !programacionVehicular.isEmpty();
}
// </editor-fold>
}