Search code examples
oracle-adfjdeveloperjspx

Oracle ADF: Close popup after downloading file


So here's the thing. I have a popup that has a button, the button itself has a fileDownloadActionListener, this one is responsible for downloading an excel file. So what I need basically is to hide the popup right after I generate the file.

Here's my .jspx file (Just the popup)

    <af:popup childCreation="deferred" autoCancel="enabled"
              id="myPopUp"
              contentDelivery="lazyUncached"
              binding="#{viewScope.mbMyBean.myPopUp}"
              partialTriggers="b17">
        <af:dialog id="d16" type="cancel"
                   title="Do you wish to download a file?"
                   inlineStyle="width:400px;">
            <af:panelGroupLayout id="pgl32"
                                 inlineStyle="max-width: 200px;">
            <af:outputText value="You're about to download a file. Ready?" id="ot45"
                               />
            </af:panelGroupLayout>
            <f:facet name="buttonBar">
                <af:button text="GO" id="b17"
                    <af:fileDownloadActionListener contentType="excelHTML"
                                                   filename="#{viewScope.mbMyBean.FileName}"
                                                   method="#{viewScope.mbMyBean.GenerateEmptyExcel}"
                                                   />
                </af:button>
            </f:facet>
        </af:dialog>
    </af:popup>

And here's the java method:

public void GenerateEmptyExcel(FacesContext facesContext, OutputStream outputStream) {

    try {


        HSSFWorkbook wb1 = generateEmptyExcelFile();
        wb1.write(outputStream);


        outputStream.flush();
        outputStream.close();

        this.myPopUp.hide();

        AdfFacesContext.getCurrentInstance().addPartialTarget(this.myPopUp);

        System.gc();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

PROBLEM

The popup won't hide.

NOTES

  1. The popup is properly binded within the bean
  2. I do not own this code and I'm doing a maintainance.
  3. I do not know why the programmer used System.gc() since I consider it as a bad practice. Here's a good reason

Solution

  • I had the same problem after downloading a file, you should try this:

    Use a resource type javascript to trigger an event click in commandButton

    <af:resource type="javascript">              
    
              function customHandler(evt) {
                  console.log(evt);
    
                  var exportCmd = AdfPage.PAGE.findComponentByAbsoluteId("pt1:b17");
                  console.log(exportCmd);
                  var actionEvent = new AdfActionEvent(exportCmd);
                  console.log(actionEvent);
                  actionEvent.forceFullSubmit();
                  actionEvent.noResponseExpected();
                  actionEvent.queue(false);
    
                  setTimeout(function(){hidePopup();}, 1000);    
    
    
              }                                    
    
              function hidePopup() {
    
                  var popup = AdfPage.PAGE.findComponent("pt1:popupAceptarDescargarPlantilla::content");
    
                  popup.hide();
    
              }
    
            </af:resource>
    

    You should have the following buttons:

    <af:commandButton text="Aceptar" id="b17" visible="false" clientComponent="true" partialSubmit="true">
                                                                            <af:fileDownloadActionListener contentType="excelHTML" filename="#{viewScope.mbGestionArchivos.nombre_archivo}" method="#{viewScope.mbGestionArchivos.generateExcelVacio}"/>
                                                                        </af:commandButton>
                                                                        <af:button text="Aceptar" id="botonPrueba" actionListener="#{viewScope.mbInformeDetalle.prepareForDownloadAction}" clientComponent="true" partialSubmit="true"></af:button>
    

    This is the java method called by the button :

        public void prepareForDownloadAction(ActionEvent act) {
    
        FacesContext context = FacesContext.getCurrentInstance();
        ExtendedRenderKitService erks =
        Service.getService(context.getRenderKit(),
               ExtendedRenderKitService.class);
    
        erks.addScript(context, "customHandler();");
        }
    

    The hidden button is triggered using javascript a ADF methods, the magic happen in the setTimeout, When executing this function we avoid for a second to make submit but the request travels to the server, here we can observe how the file is started.