Friday, 24 July 2015

ADF- Download file using fileDownloadActionListener

If you want to download file, you can achieve this using fileDownloadActionListener in ADF application.
Below are the steps to download file.


 1.    Add fileDownloadActionListener to your “Download” button:

                                <af:button text="DownLoad" id="b5"  inlineStyle="float: right;">
                                                <af:fileDownloadActionListener filename="abc.txt"   contentType="text/plain; charset=utf-8"
                                                           method="#{<your method>}"/> </af:button>

2.     In your managed bean method add below code:
        
         public void yourmethod(FacesContext facesContext, OutputStream outputStream){

            File file = new File("E:\\templates\\abc.txt");
            FileInputStream fdwd;
            byte[] bt;
            try {
                fdwd = new FileInputStream(file);
                int checkline;
                while ((checkline = fdwd.available()) > 0) {
                    bt = new byte[checkline];
                    int rst = fdwd.read(bt);
                    outputStream.write(bt, 0, bt.length);
                    if (rst == -1)
                        break;
                }
                outputStream.flush();
            } catch (IOException e) {
                ….
            }
      }
Note: This will download file from your file system.

3.  If you want to download file from database, write below code:

                     public void downloadChart(FacesContext facesContext, OutputStream outputStream)                             {
                          // get row containing file that you want to download
                         blob = (BlobDomain) row.getAttribute("DataFile");
                           try {
                             IOUtils.copy(blob.getInputStream(), outputStream);
                            blob.closeInputStream();
                                outputStream.flush();
                          } catch (IOException e) {
                               e.printStackTrace();
   FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,                  e.getMessage(), "");
                            facesContext.addMessage(null, msg);
                          }
                       }


Note: you have to get the row containing file.


Hope this will be helpful
Ravi


Friday, 17 July 2015

ADF – Assign value to bind Variable using Groovy Expression


In some cases, we need to assign default value to bind variable which can be another view object’s current row’s attribute value. We can do this using groovy expression.

Below is the groovy expression.

if (adf.object.applicationModule.findViewObject('<YourVO>').currentRow != null)


{ return adf.object.applicationModule.findViewObject('<YourVO>').currentRow.<nameofAttribute>}


Note: <nameofAttribute> is View Object's attribute name



Hope this will be useful

Ravi

Tuesday, 14 July 2015

ADF: Insert row at the bottom of table



In most scenarios there is a requirement to insert row at bottom of table.
Lets see how can we achieve this.

Step 1-  Generate VOimpl class of your View Object.

Step 2- Go to VOimpl class and add below code

        @Override
    public void insertRow(Row row) {
        //go to the end of Rowset if it has rows
        Row lastRow = this.last();
        if (lastRow != null) {
            //insert new row at the end and make it current
            int indx = this.getRangeIndexOf(lastRow) + 1;
            this.insertRowAtRangeIndex(indx, row);
            this.setCurrentRow(row);
        } else { // empty Rowset
            super.insertRow(row);
        }
    }

In this way you can insert row at the bottom of table.


Hope this will be helpful.

Ravi