Wednesday 27 April 2011

Export Collection in ADF

In many report based or any other data related application export to excel feature is always required.

Export to excel feature can be achieved in ADF very easily, may be just in 10 minutes we can see the result excel sheet. To achieve this in this project I have first created ADF business components from HR.Employee table. Then I created one jspx page to display the employee details table. Now I want to export this  tabular data in one excel named Employee.xls.  want that the employee details data will be displayed under title "List Of Employees " title. 

Now as export to excel will be available on some action so I added one command button here named "Export Collection"


I have inserted one exportCollectionActionListener inside the command button. This takes 5 parameters, 2 of them are mandatory.
Mandatory fields are:

ExportedId: The table or collection id you want to export

Type: The export type. As it is excel here it should be excelHTML

I have provided values for other two optional  fields :

filename: I wanted that the file name should be Employee.xls

title: I wanted the title to be here "List of Employees"


Now if I run the page and click the command button "Export Collection" it will export the table data in one Excel Sheet named "Employee.xls"



The title of the sheet should be "List of Employees " as shown below.




Please comment ....... 
If source code is required please email me.

Friday 22 April 2011

Separate Database As Well As Table structure For Pages


It is some time required to use separate databases as well as separate table structure for separate pages. Or even in same page we can use separate databases. 
In ADF it can be achieved very easily. I am discussing here the simplest one. ApplicationModule in ADF can be compared with persistence units of JPA, not exactly though as ApplicationModule has a lot of  features that persistence units do not. I have compared with persistence unit as in ApplicationModule configuration also we provide the database information. 
In JPA we use different persistence units to define separate databases and use those persistence units when required.
Similarly here we will be defining to different application modules for two different connections. Now we will distribute the view objects accordingly. 


Please see the pictures 



In the example I have used two databases MGR and HR

I have configured MGR in  AppModule2 and HR in AppModule1

Now the view objects using MGR database will be configured in AppModule2 and  view objects using HR database will  be configured in AppModule2.


Now if you see the picture above you can see two separate data controls.

I have created two separate pages for separate data controls. untitled1.jspx uses AppModule1DataControl, untitled2.jspx uses AppModule2DataControl.

I have created one button in each of the pages for navigation between two pages, and configured the navigation in unbounded task flow. 



Now if I run the pages you can see that In First Page Data from HR database is being displayed while navigating to second page through the button, displays data from MGR database.





So you can easily achieve this using two different application modules for different databases. 

This feature can be achieved by different methods like using Factory Classes, setting  JDBC_DS_NAME etc..


But I wanted to use the simplest one.
Please comment........



Monday 11 April 2011

File Download In ADF

It is common requirement in many projects to have file download facility.
File download facility can be achieved by many means, but in 11g + versions we have one very good component
to handle file download facility very quickly.

In 11g + versions we have one listener component called fileDownloadActionListener. Which can be triggered from any command related components like commandButton , commandLink etc.

It has three parameters filename, contentType, method.

filename : It is used to display the file name in response


method:   The method will be present in managed bean. It is used to handle the file download related activities. Signature of this method is

public void <method name> (FacesContext context, OutputStream out) throws IOException
                {
                    ...... your code...
                 }

contentType: It is used to specify the content type.(like it is word document or pdf document etc.)

In the below example I have used fileDownloadActionListener with commandLink.


<af:commandLink id="cl1" text="#{row.fileName}"
                                              partialSubmit="true"
                                              binding="#{fileDownloadBean.downloadLink}">
                                <af:fileDownloadActionListener filename="#{row.fileName}"
                                                               method="#{fileDownloadBean.download}" ></af:fileDownloadActionListener>
                              </af:commandLink>

The method referred is



    public void download(FacesContext context,
                         OutputStream out) throws IOException {
        File f =
            new File(this.directoryPath.getValue().toString() + "/" + downloadLink.getText());
        FileInputStream fis;
        byte[] b;
        try {
            fis = new FileInputStream(f);

            int n;
            while ((n = fis.available()) > 0) {
                b = new byte[n];
                int result = fis.read(b);
                out.write(b, 0, b.length);
                if (result == -1)
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
         out.flush();
    }

So whatever file I want to download it will be downloaded using this listener. The coding type will be least one.



Please comment...