gbasec
gbasec

Reputation: 19

Primefaces pagination (lazydatamodel) load method

I´ve a couple issues with a page I´m working on.

I have two sections: frmConf and frmData.

The first one has some parameters I use for searching purposes.

The second one has a DataTable defined as lazy and algo I have it´s corresponding backing wich extends LazyDatamodel.

In the mentioned DataTable in section 2 I have a column with a commandLink which creates a PDF document for the selected row in the DataTable.

The problem I´m facing is it works just fine the first time I load the page:

It calls the load (@override) method from LazyDatamodel and presents the first 10 records of the query results from the DB.

Then, when I click on the commandLink column it shows the "save as" dialog for the corresponding PDF just fine and I can proceed to download the file.

But then, for some reason wich I don´t yet understand, if I click another row´s PDF commandLink it goes and calls the referred load method a requeries the DB assuming I´m trying to do some pagination and it doesn´t even generate the required PDF.

What I´m trying to achieve is just load the page for every pagination case and not for any unrelated event like clicking the PDF button for the selected row.

Why is it reloading and requerying (even though it shows the same results as the indexes for pagination haven´t change) the entire page content after the first (correct)PDF generation? It´s my guess something happens after the first PDF generation (it leaves the page with some "reload required" state.

I´m leaving the markup from the page as well as the corresponding backing bean PDF generation method.

Please I´d really appreciate if someone could help me with this.

Thanks!

JSF markup:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<ui:composition template="/templates/cimaTemplate.xhtml">
    <ui:define name="tituloPagina">
        <h:outputText value="Consulta Documentos" />
    </ui:define>

    <ui:define name="tituloFormulario">
        <h:outputText value="Documentos" styleClass="titulo" />
    </ui:define>
    <ui:define name="contenido">

        <style>
.style1 {
    text-align: right;
}
.style3 {
    text-align: left;
}

.style2 {
    text-align: center;
    background: blue;
    color: white;
    font-weight: bold;
}

.colEstadoStyle {
    td:nth-child(1) 
    width: 20px;
}

.imageClass {
    display: block;
    background-image: url('/imagenes/pdf.png') no-repeat 100%;
}
</style>

    <h:form id="frmConf" styleClass="form" >
        <p:growl id="growl" showDetail="true" sticky="true" />
        <p:panel id="datosEmisorPanel">
            <p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
            <p:panelGrid style="margin: 0 auto;" id="panelG"> 
                <p:row>
                    <p:column>
                        <p:panelGrid>
                        <p:row>
                            <p:column style="text-align: center;" colspan="2">
                                <strong><h:outputLabel styleClass="label" value="Seleccionar Archivo" /></strong>

                                <p:selectManyCheckbox style="text-align: center;"
                                    value="#{cabDocumentoMB.selectedOptionsEmail}">
                                    <f:selectItem itemValue="PDF" itemLabel="PDF" />
                                    <f:selectItem itemValue="XML" itemLabel="XML" />
                                </p:selectManyCheckbox>
                            </p:column>
                        </p:row> <p:row>
                            <p:column colspan="2">
                                <strong><h:outputLabel styleClass="label"
                                        value="E-Mails" /></strong>
                                <br></br>
                                <h:inputTextarea value="#{cabDocumentoMB.correos}"
                                    cols="40" />
                                <br></br>
                            </p:column>
                        </p:row> <p:row>
                            <p:column colspan="2">
                                <strong><h:outputLabel styleClass="style1"
                                        value="Formato: [email protected],[email protected]" /></strong>
                            </p:column>
                        </p:row> <p:row>
                            <p:column rowspan="2">
                                <p:commandButton icon="ui-icon-mail-closed"
                                    value=" Envio normal" id="bto_enviarEmail"
                                    oncomplete="bloquearPagina.hide()"
                                    onstart="bloquearPagina.show()"
                                    action="#{cabDocumentoMB.EnviarCorreos('Normal')}"
                                    update="growl">
                                </p:commandButton>
                                <p:blockUI block="frmConf" widgetVar="bloquearPagina">
                                Enviando correo...<br />
                                    <p:graphicImage value="/imagenes/ajax-loader.gif" />
                                </p:blockUI>

                                <p:blockUI block="frmConf" widgetVar="bloquearReproceso">
                                Reprocesando...<br />
                                    <p:graphicImage value="/imagenes/ajax-loader.gif" />
                                </p:blockUI>

                            </p:column>
                            <p:column rowspan="2">
                                <p:commandButton value=" Envio ZIP" icon="ui-icon-tag"
                                    action="#{cabDocumentoMB.EnviarCorreos('ZIP')}"
                                    oncomplete="bloquearPagina.hide()"
                                    onstart="bloquearPagina.show()" update="growl">
                                </p:commandButton>
                                <br />
                            </p:column>
                        </p:row> </p:panelGrid>
                    </p:column>

                    <p:column colspan="0">
                        <p:panelGrid> <p:row>
                            <p:column colspan="1" styleClass="style1">
                                <strong><h:outputLabel styleClass="label" size="30"
                                        value="Cédula/RUC/Pasaporte" /></strong>
                            </p:column>
                            <p:column colspan="1" styleClass="style3">
                                <h:inputText styleClass="text" style="font-weight: bold;"
                                    value="#{cabDocumentoMB.ruc}" />
                            </p:column>
                        </p:row> <p:row>
                            <p:column colspan="1" styleClass="style1">
                                <strong><h:outputLabel styleClass="label" size="30"
                                        value="Nombres:" /></strong>
                            </p:column>
                            <p:column colspan="1" styleClass="style3">
                                <h:inputText styleClass="text" style="font-weight: bold;"
                                    value="#{cabDocumentoMB.razonSocial}" />
                            </p:column>
                        </p:row> <p:row>
                            <p:column styleClass="style1">
                                <strong><h:outputLabel styleClass="label"
                                        value="Número del Documento:" /></strong>
                            </p:column>
                            <p:column styleClass="style3">
                                <h:inputText value="#{cabDocumentoMB.numDocumento}"
                                    size="30" title="Número del Documento" placeHolder="TBD"/>
                            </p:column>
                        </p:row> <p:row>
                            <p:column styleClass="style1">
                                <strong><h:outputLabel styleClass="label"
                                        value="Tipo de Documento:" /></strong>
                            </p:column>
                            <p:column styleClass="style3">
                                <p:selectOneMenu
                                    value="#{cabDocumentoMB.seleccionTipo}">
                                    <f:selectItems value="#{cabDocumentoMB.tipo}" />
                                </p:selectOneMenu>
                            </p:column>
                        </p:row> <p:row>
                            <p:column styleClass="style1">
                                <strong><h:outputLabel styleClass="label"
                                        value="Estados:" /></strong>
                            </p:column>
                            <p:column styleClass="style3">
                                <p:selectOneMenu
                                    value="#{cabDocumentoMB.seleccionTipoEstado}">
                                    <f:selectItems value="#{cabDocumentoMB.tipoEstados}" />
                                </p:selectOneMenu>
                            </p:column>
                        </p:row> <p:row>
                            <p:column>
                                <strong><h:outputLabel styleClass="label"
                                        value="Fecha Inicial:" /></strong>
                                <br></br>
                                <p:calendar id="txt_inicial"
                                    value="#{cabDocumentoMB.fechaInicio}"
                                    pattern="yyyy-MM-dd" mask="true" mode="popup" showOn="button"
                                    readonlyInput="true">
                                    <f:convertDateTime type="date" dateStyle="short"
                                        pattern="yyyy-MM-dd" />
                                </p:calendar>
                            </p:column>
                            <p:column>
                                <strong><h:outputLabel styleClass="label"
                                        value="Fecha Final:" /></strong>
                                <br></br>
                                <p:calendar id="txt_final"
                                    value="#{cabDocumentoMB.fechaFinal}"
                                    pattern="yyyy-MM-dd" mask="true" mode="popup" showOn="button"
                                    readonlyInput="true">
                                    <f:convertDateTime type="date" dateStyle="short"
                                        pattern="yyyy-MM-dd" />
                                </p:calendar>
                            </p:column>
                        </p:row> <p:row>
                            <p:column style="text-align: center;">
                                <p:commandButton value=" Busqueda " icon="ui-icon-search"
                                    title="Buscar Filtrado"
                                    action="#{cabDocumentoMB.findDocumentos()}"
                                    update=":frmData:listaDocumentos,:frmConf:datosEmisorPanel">
                                </p:commandButton>
                            </p:column>
                            <p:column>
                                <p:commandButton value="Reprocesar documentos "
                                    icon="ui-icon-tag"
                                    action="#{CabDocumentoMB.reprocesarDocumentos()}"

                                    disabled="#{CabDocumentoMB.botonReprocesarActivo}"
                                    update=":frmData:listaDocumentos,:frmConf:datosEmisorPanel">

                                </p:commandButton>
                                <br />
                            </p:column>
                        </p:row> </p:panelGrid>
                    </p:column>
                </p:row> 
            </p:panelGrid>
        </p:panel>
    </h:form>   


    <h:form id="frmData" styleClass="form">
        <p:panel id="datosDetalle22" >
                  <h:panelGroup id="listaDocumentos">
                    <p:dataTable id="lazyDataTable" 
                                 var="documento"
                                 value="#{cabDocumentoMB.allDocumentos}"
                                 paginator="true" rows="10" rowsPerPageTemplate="10,50,100"
                                 selection="#{cabDocumentoMB.seleccionDocu}"  
                                 update=":frmData:listaDocumentos"
                                 lazy="true">
                        <f:facet name="header">  
                            Consulta de Documentos
                        </f:facet>
                        <p:column headerText="Identificación" style="width:10% ; font-size:11px" >
                            <h:outputText value="#{documento.identificacionComprador}" style="font-size:11px;" />
                        </p:column>
                        <p:column headerText="Razón Social" style="width:25% ; font-size:11px">
                            <h:outputText value="#{documento.razonSocialComprador}" style="font-size:11px;" />
                        </p:column>
                        <p:column headerText="Comprobante" style="width:10% ; font-size:11px">
                            <h:outputText value="#{documento.codigoDocumento}" style="font-size:11px;" />
                        </p:column>
                        <p:column headerText="Número" style="width:10% ; font-size:11px">
                            <h:outputText value="#{documento.id.serie}" style="font-size:11px;" />
                        </p:column>
                        <p:column headerText="Importe total" style="width:7% ; text-align:right ; font-size:11px">
                            <h:outputText value="#{documento.importeTotal}" style="font-size:11px;" />
                        </p:column>
                        <p:column headerText="Fecha Emisión" style="width:7% ; text-align:center ; font-size:11px">
                            <h:outputText value="#{documento.fechaEmision}" style="font-size:11px;" />
                        </p:column>
                        <p:column headerText="Ambiente"
                            style="width:10% ; text-align:center ; font-size:11px">
                            <h:outputText value="#{documento.id.ambiente}" style="font-size:11px;" />
                        </p:column>

                        <p:column headerText="Estado" style="width:20% ; font-size:11px"> 
                            <p:row> 
                                <p:column>
                                    <h:outputText value="#{documento.estadoTransaccion}" style="font-size:11px;" />
                                </p:column>

                                <p:column>
                                    <p:commandButton update=":frmData:display" oncomplete="detalleEstadoDiag.show()" icon="ui-icon-search" style="float:right;">
                                         <f:setPropertyActionListener                                           
                                            target="#{cabDocumentoMB.seleccionDocumentos}" 
                                            value="#{documento}"
                                         />
                                    </p:commandButton>
                                </p:column>
                            </p:row>
                        </p:column>

                        <p:column headerText="PDF" style="font-size:11px" >
                            <p:commandLink action="#{cabDocumentoMB.pdf(documento,'')}"
                                    ajax="false"
                                    disabled="#{CabDocumentoMB.desactivaBotonPDF}"
                                    update=":frmData:datosDetalle22">
                                        <p:graphicImage value="/imagenes/pdf.png" ></p:graphicImage>
                                        <f:setPropertyActionListener
                                            target="#{cabDocumentoMB.documentos}"
                                            value="#{documentos}" >
                                        </f:setPropertyActionListener>
                                </p:commandLink>
                        </p:column>

                        <p:column headerText="XML" style="font-size:11px" >
                            <p:commandLink
                                action="#{cabDocumentoMB.xml(documento)}"
                                ajax="false"
                                update=":frmData:datosDetalle22"
                                disabled="#{CabDocumentoMB.desactivaBotonXML}">
                                <p:graphicImage value="/imagenes/xml.png"></p:graphicImage>
                                <f:setPropertyActionListener
                                    target="#{cabDocumentoMB.documentos}"
                                    value="#{documentos}">
                                </f:setPropertyActionListener>
                            </p:commandLink>
                        </p:column>
                        <p:column selectionMode="multiple"/>
                    </p:dataTable>

                    <p:dialog header="Detalle de estado" widgetVar="detalleEstadoDiag" resizable="false" width="400" showEffect="explode" hideEffect="explode" style="font-size:11px" >
                            <h:panelGrid id="display" columns="2" cellpadding="4">                              
                                <div align="left" style="font-size:11px;"><h:outputText value="Descripción: " style="font-weight:bold" /></div>
                                    <h:outputText value="#{cabDocumentoMB.seleccionDocumentos.msjError}" />
                            </h:panelGrid>
                    </p:dialog>
                </h:panelGroup>
              </p:panel>

              <p:panel id="datosExcel" style="align=center">
                    <h:commandLink>
                        <p:graphicImage value="/imagenes/excel.png" />
                        <p:dataExporter type="xls"
                            postProcessor="#{manager.postProcessXLS}" target="myData"
                            fileName="myexcel" />
                    </h:commandLink>
              </p:panel>
        </h:form>
    </ui:define>
</ui:composition>
</html>

Backing: (PDF Method) ...

JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(detallesAdiciones);            
String reportPath = pathReports.get(doc.getId().getCodigoDocumento());
JasperPrint jasperPrint = null;
try {
    jasperPrint = JasperFillManager.fillReport(reportPath, obtenerMapaParametrosReportes(obtenerParametrosInfoTriobutaria(doc), obtenerInfoFactura(lstFactDetAdictDocumento,doc)), beanCollectionDataSource);
    } catch (JRException e) {
        e.printStackTrace();
    }           
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.addHeader("Content-disposition", "attachment; filename="+name);

ServletOutputStream servletOutputStream = null;
try {
        servletOutputStream = httpServletResponse.getOutputStream();                
    } catch (IOException e) {
        e.printStackTrace();
    } 
try {
    JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
} catch (JRException e) {
        e.printStackTrace();
        }  
FacesContext.getCurrentInstance().responseComplete();

Upvotes: 0

Views: 1044

Answers (0)

Related Questions