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...

Friday, 25 March 2011

Setting AM Parameters at Server Startup

It is always possible to set the am p[arameters like jbo.ampool.initpoolsize , jbo.dofailover etc while starting a weblogic server. It will be applicable for all the applications deployed in the server. To do this we must set the environ ment variable without tampering the default setDomainEnv.cmd or startWebLogic.cmd file.


To do this I have created one custom script

For my case the domain's bean location is D:\Oracle\Middleware\user_projects\domains\LoanDeliveryDomain\bin

My script looks like below


cd \
D:
cd Oracle\Middleware
cd user_projects\domains\LoanDeliveryDomain\bin
set JAVA_OPTIONS=%JAVA_OPTIONS%
set JAVA_OPTIONS=%JAVA_OPTIONS% -Djbo.ampool.initpoolsize=110
set JAVA_OPTIONS=%JAVA_OPTIONS% -Djbo.ampool.minavailablesize=50
set JAVA_OPTIONS=%JAVA_OPTIONS% -Djbo.dofailover=true
startWebLogic.cmd

as I am using windows environment. It should be almost same for unix environment also.

Now if I start the server it will be automatically set during server startup

Below is the screenshot



Please add comments :)


Monday, 7 March 2011

ADF Popup

Like any web application it is required to show pop up window in certain cases in ADF also. In ADF it can be shown in  many ways. Like we can show popup using javascript, using taskflow using pop up component etc. Here I have discussed ADF's popup component for showing the pop up, the simplest one I think. To show popup I first made one web application with one test page.



I have added one command button. I will show the popup for buton action.


I have added one <af:popup> component in the page as shown below in the below page. Added one dialog in that popup window.



Now I want to show the popup window on button action so I have added <af:showPopupBehavior> inside the command button component.



In show popup behavior I have added the popup id and button action.


Now if I run the page and click the button it will show the popup window.




This popup window can be shown from managed bean also.

Custom Filter/Servlet In ADF

Like any web application some time it is required to use custom filter/ servlet in ADF also. Here is the description of for my case. To use my custom filter I have created one Fusion Web application first.


Then I created my custom Filter named MyFilter.java. I have written my custom code in the overridden doFilter method of my custom filter.

I have used the below code in my doFilter method.

        long initialTime = (new Date()).getTime();
        filterChain.doFilter(servletRequest, servletResponse);
        long afterTime = (new Date()).getTime();
        System.out.println("Time To Process Request:"+ (afterTime-initialTime));


Here the filter is added in web.xml with filter name myCustomFilter. The screen shots are below. I want that every pages should pass through this filter. Any one can use directly <filter-mapping> also.



Now I have created one bounded task flow to design page flow from First Page to Second Page. I do not want to create the pages as page fragments. So I unselected this option.



I created the pages as viewq.jspx and view2.jspx . In view1.jspx I have added one button whoose action will redirect me to second page.








Now if I run the taskflow then I can see from the console(sysout from my Custom Filter) that it is showing time to process the requests.

The filters may be added in other ways also. This is just one example.

Please comment.



Wednesday, 23 February 2011

Pagination In ADF

Pagination is one common requirement in many projects. But in ADF 11g we have only scrollable facility available. So it becomes quiet tedious job to handle huge data.

First create one Fusion Web Application.

And create the ADF Business Components (I have used Employees Table of HR schema (Oracle XE))





Now create your page. For saving time I have created One ADF Read Only Table by dragging EmployeesView1 from data control to the page.



Now create <af:iterator> where <af:table> tag was present . remove all the table related tags

The complete code is below.

The iterator code is like below:
<af:iterator id="i1"
                               value="#{bindings.EmployeesView1.collectionModel}"
                               var="row"
                               binding="#{pageFlowScope.testPageBean.myIterator}">


Now create one managed bean (TestPagebean.java here). I have created the bean in page flow scope.

create the value change listener and action listener.

The valuechange listener code will be like this :

UIXIterator valueIterator = getMyIterator();
        if (!valueChangeEvent.getNewValue().equals(valueChangeEvent.getOldValue())) {
            int newPage =
                Integer.parseInt(valueChangeEvent.getNewValue().toString());
            int pageStart = (newPage) * valueIterator.getRows();
            valueIterator.setFirst(pageStart);
            AdfFacesContext.getCurrentInstance().addPartialTarget(valueIterator);




Now run the page you will get desired output.

The complete code is below.









Please add in comments for queries..............

Thursday, 17 February 2011

Executing Particular Method Before Page Loads


Sometimes It is needed to Execute Some methods before page loads or component loads.

These can be done in many ways like:

1> Using an ADF/JSF Page Phase Listener (oracle.adf.controller.v2.lifecycle.PagePhaseListener/javax.faces.event.PhaseListener)

2> Using a page backing bean superclass which provides an onLoad( ) method

3> Using a custom Page Controller (oracle.adf.controller.v2.lifecycle.PageController)

4> Using managed bean for page 

5> Using Page Def for particular page

6> Using Task Flow 

and may be many more .. :) 

First start with simplest one : Using managed bean 

First Create one Test Fusion Application then create the testPage.jspx

I have created the backing bean (managed bean) at page creation time to save time for creating bindings needed. It does not matter if create managed bean and do not expose all the page components.



I have written my test method which should be executed before components load. 

 I want that the method should be executed before document loads and form loads.

So I have called the method in both the components getter method.




Now if we run the page it will be executed before the document and form loads.





I will be updating the other ways also in my blog  in future.
There may be specific scenarios where other ways should be followed (like: even before the page is hit some methods should be executed).

Please comment for queries. 

Saturday, 12 February 2011

Using jQuery in ADF Pages

JQuery is one rich AJAX library. Which is very handy and very popular library used in modern web applications.

It gives a very rich look and feel also. For detail study of JQuery you can refer the site
JQuery.

We can easily use JQuery in ADF pages also. But there is one problem regarding this is that for many ADF components we can't use components id to be used in JQuery as the ids are rendered like <id provided>::content and JQuery would not allow ":" like special characters in $("#id") statement.

So I have taken a different approach here.

First create one Fusion Web Project. In the Webcontent create one folder to put the jquery javscripts in the folder.You can download the jquery javascripts from JQuery website.




Now create one web page where to use the JQuery



Add resource tag in the page <af:resource type="javascript" source="/<directory name>/jquery-1.x.min.js"/>




Now switch to the Source view instead of design view.

and add your javascript code like below:


 <af:resource type="javascript">
        function showInput() {
            if ($("input[name=it1]").val() != null) {
                if (($("input[name=it1]").val().length &gt; 3) && ($("input[name=it1]").val().length &lt; 6)) {
                    $("input[name=it1]").css("color", "red");
                }
                else if (($("input[name=it1]").val().length >= 6) && ($("input[name=it1]").val().length &lt; 9 )){
                  $("input[name=it1]").css("color", "blue");
                }
                else {
                  $("input[name=it1]").css("color", "green");
                }
            }
        }
      </af:resource>



Here in the above code if the length is below 4 or greater than or equal 9 then the text color will be green otherwise it will show red or blue according to condition.

The complete code is here


<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <af:document id="d1" title="My JQuery Test Page">
      <af:resource type="javascript" source="/js/jquery-1.4.4.min.js"/>
      <af:resource type="javascript"
                   source="/js/jquery-ui-1.8.9.custom.min.js"/>
      <af:resource type="javascript">
        function showInput() {
            if ($("input[name=it1]").val() != null) {
                if (($("input[name=it1]").val().length &gt; 3) && ($("input[name=it1]").val().length &lt; 6)) {
                    $("input[name=it1]").css("color", "red");
                }
                else if (($("input[name=it1]").val().length >= 6) && ($("input[name=it1]").val().length &lt; 9 )){
                  $("input[name=it1]").css("color", "blue");
                }
                else {
                  $("input[name=it1]").css("color", "green");
                }
            }
        }
      </af:resource>
      <af:form id="f1">
        <af:panelStretchLayout id="psl1">
          <f:facet name="top"/>
          <f:facet name="center">
            <!-- id="af_one_column_header_and_footer_scroll"   -->
            <af:panelGroupLayout layout="scroll" id="pgl1">
              <af:inputText label="Input Value" id="it1">
                <af:clientListener method="showInput" type="keyPress"/>
              </af:inputText>
            </af:panelGroupLayout>
          </f:facet>
          <f:facet name="bottom"/>
        </af:panelStretchLayout>
      </af:form>
    </af:document>
  </f:view>
</jsp:root>

Now if you run the page you will see the text color change o entering value ,

like below :





Please add comments for further queries .. :)