0

Alright, I have basically killed Google searching for a solution.

Using richfaces and JSF.

Basically I have a h:commandButton that prints a report once clicked, but if you click it more than once the DownloadUtils.download() method gets interrupted and then it breaks. I need to avoid that.

I have tried to disable "onclick". I have tried to disable using a time out - solutions found here: Disable `<h:commandButton>` after clicked, but action should be fired

But those basically either doesnt fire the action at all, or it gets to a point during the download and then stops. It doesnt return anything, so no report gets downloaded.

You can't use ajax based buttons/links because of the download.

Links to confirm a4j limitation here: https://community.jboss.org/thread/8598 Downloading a CSV file using JSF

I have also thought of trying to add a popup panel after the button is clicked without using ajax like this: http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=popup&skin=blueSky

But again it doesnt continue with the download at some point (usually after it prints the XML in my log).

Any ideas welcome, any solution will do at this moment, even redirecting after to a new page.

Button

<h:commandButton action="#{managingBean.download()}" value="#{bundle.download}"/>

Managing Bean

DownloadUtils.download(object.getOrderReference() + ".pdf",
                    service.getReport(ReportFormat.PDF, object.getOrder()));

Service

public InputStream getReport(ReportFormat format, Object object) throws IOException {
    InputStream is = getReportResource("String", format);

    Report report = someService.createReport(object);
    Document doc = toXml(report, Report.class);

    Map<String, Object> params = new HashMap<String, Object>();
    params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, doc);
    params.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "yyyy-MM-dd'T'HH:mm:ss");

    return getReportFromJasper(is, params, format, true, true, false);
}

Thank you in advance.

Community
  • 1
  • 1

1 Answers1

1

2 ways to do this.

Using Jquery :

------------------

<script type="text/javascript">
/* <![CDATA[ */

$("a[id$='submitButtonID']").click(function() {
    $(this).replaceWith( "In Progress");
});
/* ]]> */ 
</script>

<h:commandLink  id="submitButtonID" value="#{bundle.download}" action="#{managingBean.download}"></h:commandLink>

Using Java Script:

-------------------

function displayWaitMsg()
{
         var btnId=getElementForAnId("submitButtonID");
         document.getElementById(btnId).style.display="none";
         var wt=getElementForAnId("waitMsg");
         document.getElementById(wt).innerHTML ="Please wait. Your request is being processed.";
         document.getElementById(wt).style.display="block";
}

function getElementForAnId(str) // To removing appending content
{
        var ret;
        var inputs = document.getElementsByTagName('*');
        for ( var i = 0; i < inputs.length; i++) {
                if (inputs[i].id.indexOf(str) != -1) {

                        ret = inputs[i].id;
                }
        }
        return ret;
}

<h:commandLink id="submitButtonID" action="#{managingBean.download}" onclick="displayWaitMsg();" value="#{bundle.download}">

<div id="waitMsg" align="center" style="display: none;"></div>
Vikas V
  • 3,176
  • 2
  • 37
  • 60