Exporting viewObject data in text file using af:fileDownloadActionListener in Oracle ADF (12.1.3)

Sharing is Caring

Hello All
This post is about using af:fileDownloadActionListener to generate the file on run-time, exporting viewobject data, suppose I have a view object and I want to export it’s data in CSV or plain text so for this requirement we can generate the file at run-time and send it to client UI to download

What is af:fileDownloadActionListener-
as per oracle docs-
The fileDownloadActionListener tag is a declarative way to allow an action source (<commandButton>, <commandLink>, etc.) to programmatically send the contents of a file to the user, optionally with a specific content type and filename. Since file downloads must be processed with an ordinary request – not XMLHttp AJAX requests – this tag forces partialSubmit to be false on the parent component if it supports that attribute.
The fileDownloadActionListener uses the native (browser built-in) filedownload popup, so this popup cannot be configured.

In this post, I am using Departments table of HR schema

    • Prepare model part using Departments table and drop it on the page as a table and add a button to UI for downloading

 

 

    • drop af:fileDownloadActionListener as child of button and set it’s property as contentType, method , fileName 

 

 

    • Now see code written in download Listener-

    /**Method to get BindingsContainer for current page
     * @return
     */
    public BindingContainer getBindingsCont() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    /**Method to download ViewObject's data in plain text file
 * @param facesContext
 * @param outputStream
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
    public void fileDownloadListener(FacesContext facesContext,
OutputStream outputStream) throws UnsupportedEncodingException, IOException {
OutputStreamWriter w = new OutputStreamWriter(outputStream, "UTF-8");
        //Get itertaor from bindings
DCIteratorBinding deptIter = (DCIteratorBinding) getBindingsCont().get("Departments1Iterator");
        //Get ViewObject from Iterator
ViewObjectImpl vo = (ViewObjectImpl) deptIter.getViewObject();
ViewAttributeDefImpl[] attrDefs = vo.getViewAttributeDefImpls();
        int count = 0;
RowSetIterator rsi = vo.createRowSetIterator(null);
        while (rsi.hasNext()) {
Row nextRow = rsi.next();
            if (nextRow != null) {
                // Code to iterate over ViewObject's column to get all columns value at runtime
                for (ViewAttributeDefImpl attrDef : attrDefs) {
                    byte attrKind =
attrDefs[count].getAttributeKind(); //checks attribute kind for each element in an array of AttributeDefs
                    if (attrKind != AttributeDef.ATTR_ASSOCIATED_ROW &&
attrKind != AttributeDef.ATTR_ASSOCIATED_ROWITERATOR) {
String columnName = attrDef.getName();
w.write(columnName + " - " + nextRow.getAttribute(columnName) + " ");
                    }
                }
                // Code to create new line text line for new Row
w.write(System.getProperty("line.separator"));
            }
        }
        //Flush the writer after wrting file
w.flush();
    }
}

 

  • Run this application and click on download button, browser download box appears, open downloaded file and see how your data appears in the text file, to export data in a pdf file you have to generate PDF file using some API then you can download it in the same way

 

Exporting viewObject data

Cheers:) Happy Learning

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

4 thoughts on “Exporting viewObject data in text file using af:fileDownloadActionListener in Oracle ADF (12.1.3)”

  1. Using adf download listerner , full page is getting refresed which caused partial trigger unregistered components error.
    Please help how to avoid full page refresh In adf download listener .

  2. i have tried the partial submit true also. it is also not working. its refreshing the page always.
    How to handle the exception and error, when no file is present . its always downloading the blank file when no file / no bytes is present in managed bean.
    how to show the error warning in this downloadActionListener.

  3. Capil I have checked and it is not happening in my case , Try using af:link instead of button
    And you need to handle that in bean if there is no file present then make that button disable

    Ashish

Leave a Reply

Your email address will not be published. Required fields are marked *