'Primefaces file download handling errors

Basically my problem is: How do I display an error message on my page and cancel a download, when the generation of the file to download has failed. Primefaces 7. I would be willing to update to 11 (which breaks some of my code ..) if I would see a method which achieves this in 11

<p:commandButton id="downloadButtonAllCSV"  widgetVar="downloadButtonAllCSV" value="Download alle CSV"  onclick="PrimeFaces.monitorDownload(start, stop)" icon="ui-icon-arrowthick-1-s"  styleClass="button" update="@form">

        <p:fileDownload value="#{reportingController.CSVAllGCG}" />
</p:commandButton>

<p:growl></p:growl>
<p:dialog widgetVar="csvError" header="Fehler bei CSV Erzeugung">
        <h:outputText value="Fehler bei CSV Erzeugung"></h:outputText>
</p:dialog>

My bean code basically checks if the number of selected samples is smaller than some cofigured value to avoid an OutOfMemory. My problem is that the attempts to show some error on the web page in the else block do not work. Neither the growl, nor the dialog show up.

public StreamedContent getCSVAllGCG() {
    InputStream targetStream = null;
    String csv = "";
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(GCGNachrichtDTO.REP_CSV_HEADER);
    if (getAnzahl().intValue() < this.maxDownloads) {
        List<GCGNachrichtDTO> allDtos = this.reportMessageDAO.findGCGNachichten(0, getAnzahl().intValue(), filter, true);
        String dtoString = allDtos.stream().map(d -> d.getMonCSVString()).collect(Collectors.joining());
        stringBuilder.append(dtoString);
        csv = stringBuilder.toString();
        targetStream = new ByteArrayInputStream(csv.getBytes());
    
    } else {
        String message = "Mehr als " + maxDownloads + " Eintraege koennen nicht verarbeitet werden";
        FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, message, message);
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        message += " Konfiguration mit gcg.monitoring.max.downloads. Ein zu hoher Wert kann zu einer OutOfMemoryError fuehren.";
        log.info(message);              
        PrimeFaces.current().executeScript("PF('csvError').show()");

        targetStream = new ByteArrayInputStream(message.getBytes());
    }
    
    return new DefaultStreamedContent(targetStream, "text/csv", "reporting.csv");
}

@adam:

<p:commandButton id="downloadButtonAllCSV"  widgetVar="downloadButtonAllCSV" value="Download alle CSV" icon="ui-icon-arrowthick-1-s"  styleClass="button" update="@form" actionListener="#{reportingController.actionCSVAllGCG()}">

                            <p:fileDownload value="#{reportingController.stream}" />
                        </p:commandButton>

public void actionCSVAllGCG() {
    InputStream targetStream = null;
    String csv = "";
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(GCGNachrichtDTO.REP_CSV_HEADER);
    if (getAnzahl().intValue() <= this.maxDownloads) {
        List<GCGNachrichtDTO> allDtos = this.reportMessageDAO.findGCGNachichten(0, getAnzahl().intValue(), filter, true);
        String dtoString = allDtos.stream().map(d -> d.getMonCSVString()).collect(Collectors.joining());
        stringBuilder.append(dtoString);
        csv = stringBuilder.toString();
        targetStream = new ByteArrayInputStream(csv.getBytes());
    
    } else {
        String message = "Mehr als " + maxDownloads + " Eintraege koennen nicht verarbeitet werden";
        log.info(message);              
        targetStream = new ByteArrayInputStream(message.getBytes());
        FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, message, message);
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        message += " Konfiguration mit gcg.monitoring.max.downloads. Ein zu hoher Wert kann zu einer OutOfMemoryError fuehren.";
        log.info("------- ******** " + message);                
        PrimeFaces.current().executeScript("PF('csvError').show()");
    }
    
    this.stream = new DefaultStreamedContent(targetStream, "text/csv", "reporting.csv");
}

This does not work either, nothing happens...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source