Monday, February 27, 2012

ADF 11G : File Downloading


How to Implement the functionality of File Downloading in ADF 11G?
--------------------------------------------------------------------------------------
             
             
                        
             
             
                                             filename="#{bindings.FirstName.inputValue}"
                                             method="#{DownloadBean.startDownload}"/>
              
           
---------------------------------------------------------------------------------------

import java.io.IOException;
import java.io.OutputStream;

import java.io.OutputStreamWriter;

import javax.el.ELContext;
import javax.el.ExpressionFactory;

import javax.el.ValueExpression;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;

public class Download {
    public Download() {
    }

    public void startDownload(FacesContext facesContext,
                              OutputStream outputStream) {
        // Add event code here...
       
        try {
            System.out.println("startDownload()");

            OutputStreamWriter w =
                new OutputStreamWriter(outputStream, "UTF-8");
            w.write("Neelmani here!::  ");
            w.write("------------------");
            String empName  = resolveExpression("#{bindings.FirstName.inputValue}").toString();
            String empNumber  = resolveExpression("#{bindings.EmployeeId.inputValue}").toString();
            w.write(empName);
            w.write("------------------");
            w.write(empNumber);
           
            // The stream is automatically closed, but since we wrapped it,
            // we'd better flush our writer
            w.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    public static Object resolveExpression(String expression) {
    FacesContext facesContext = getFacesContext();
    Application app = facesContext.getApplication();
    ExpressionFactory elFactory = app.getExpressionFactory();
    ELContext elContext = facesContext.getELContext();
    ValueExpression valueExp =
    elFactory.createValueExpression(elContext, expression,
    Object.class);
    return valueExp.getValue(elContext);
    }
   
    public static FacesContext getFacesContext() {
    return FacesContext.getCurrentInstance();
    }


}