Tuesday, October 14, 2008

Call method on page load in ADF

call method on page load in ADF – [JDeveloper Version 10.1.3]

In this example we will see how we can call a method inside your backing bean or application module when page is getting loaded.
The scenario I am taking here is you have to show the current row index of the view object row on page load in ADF table column.
1) Create a view object based on Job table in HR schema with name OnPageLoadJobVO. Do generate the VOImpl and VORowImpl java classes for the created view object.



2) Add a transient variable inside your view object with name
“myCurrentRowIndex”



3) Add the above created view Object inside your application module.
4) Add the following code inside your application module Impl class.

public void setJobCurrentRowIndex() {
System.out.println("I am here inside AM");
OnPageLoadJobVOImpl vo = getOnPageLoadJobVO1();
OnPageLoadJobVORowImpl row=null;
long fetchedRowCount = vo.getEstimatedRowCount();
RowSetIterator jobVoIter = vo.createRowSetIterator("jobVoIter");
if (fetchedRowCount > 0)
{
jobVoIter.setRangeStart(0);
jobVoIter.setRangeSize((int)fetchedRowCount);
for (int count = 0; count < fetchedRowCount; count++)
{
row=(OnPageLoadJobVORowImpl)jobVoIter.getRowAtRangeIndex(count);
row.setmyCurrentRowIndex(new Number(count));
}
}
jobVoIter.closeRowSetIterator();
}

5) Expose the above created method in application module to the client.
6) Drag and drop the OnPageLoadJobVO object to your jspx page as ADF read only table.
7) Create a new Managed bean using faces-config.xml with
name= OnPageLoadBackingBean and

class= OnPageLoadBean

8) Copy and paste the following code inside your newly created managed bean class.

package viewlayer.backing;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import model.common.AppModule;
import oracle.adf.controller.v2.lifecycle.Lifecycle;
import oracle.adf.controller.v2.lifecycle.PagePhaseEvent;
import oracle.adf.controller.v2.lifecycle.PagePhaseListener;


public class OnPageLoadBean implements PagePhaseListener{
public OnPageLoadBean() {
}
public void afterPhase(PagePhaseEvent event) {
}
public void beforePhase(PagePhaseEvent event) {
if (event.getPhaseId() == Lifecycle.PREPARE_MODEL_ID) {
if (!isPostback())
/*
System.out.println("i am here inside backing bean");
this.getApplicationModule().setJobCurrentRowIndex();
}
}
private boolean isPostback() {
return Boolean.TRUE.equals(resolveExpression("#{adfFacesContext.postback}"));
}
private Object resolveExpression(String expression) {
FacesContext ctx = FacesContext.getCurrentInstance();
Application app = ctx.getApplication();
ValueBinding bind = app.createValueBinding(expression);
return bind.getValue(ctx);
}
private AppModule getApplicationModule() {
return (AppModule)resolveExpression ("#data.AppModuleDataControl.dataProvider}");
}
}
9) Right click on your jspx page and go to your page definition. Where set the attribute ControllerClass inside your pageDefinition tag as the name of the managed bean.


Run your page to check the results.

Add new row in ADF table on button click

Add new row in ADF table on button click – [JDeveloper Version 10.1.3]
In this example we will add a new row at last position in an ADF table. I am taking an example of Employee table in HR schema. We need this approach if we want to create an empty row in ADF table and then initialize it with some values or to add the empty row at the end of the table.

Step 1) Create a new Employee View Object(say EmpViewObjEOBased) based on an Employee Entity object.

Step 2) Add the above view object in your application module.

Step 3) Drop it on your page as a ADF table (not as ADF readonly table).

Step 4) Add the following code inside your application module class.


public void createRow() {
Row newRow = getEmpViewObjEOBased1().createRow();
newRow.setNewRowState(Row.STATUS_INITIALIZED);
//get instance of the above created view object
ViewObjectImpl vo=getEmpViewObjEOBased1();
// to insert row at the end of the table
vo.insertRowAtRangeIndex(vo.getRangeSize()-1,newRow);
System.out.println(getEmpViewObjEOBased1().getCurrentRowIndex());
}

If you want to insert the row at the begening of the table replace line vo.insertRowAtRangeIndex(vo.getRangeSize()-1,newRow); with
vo.insertRow(newRow);

Step 5) Expose this application module method to client.




This will expose the method to be used in UI and method will appear in the data control.

Step 6) Drop the createRow button inside the “Table facets->action” as a ADF command button.


Now run your page and check the result...

Wednesday, October 8, 2008

Jdeveloper 11g production

Finally after much of wait , Jdeveloper 11g productions is released and is
Ready for download.
http://www.oracle.com/technology/software/products/jdev/htdocs/soft11.html .

JDeveloper 11g — Online Demonstrations

http://www.oracle.com/technology/products/jdev/viewlets/11/index.html

Documentation and Developer guide

http://download.oracle.com/docs/cd/E12839_01/index.htm

Important code samples commonly used in ADF

In this post I am listing up important code samples commonly used in ADF .

Code to access the application module methods from the managed bean

To make sure that your application module method is accessible from the view layer, in the application module editor, do shift the method from “Avaliable” to “Selected List” under the client interface option.

FacesContext fc = FacesContext.getCurrentInstance();
ValueBinding vb = fc.getApplication().createValueBinding("#{data}");
BindingContext bc = (BindingContext)vb.getValue(fc);
DCDataControl dc = bc.findDataControl("HRAppModuleDataControl");
ApplicationModule am = (ApplicationModule)dc.getDataProvider();
HRAppModule hrAm = (HRAppModule)am;
hrAm.setCurrentEmpRow(empId1);

In the above example HRAppModuleDataControl is the Data control used in your application.You can find the exact name used in your application in the DataBindings.cpx file inside the .HRAppModule is the Application module client interface class. And setCurrentEmpRow is the method inside the Applicatin module which is need to be called.

Code to access the page bindings from the Managed bean

DCBindingContainer bc1 = this.getBindings();
String empId1 = bc1.findIteratorBinding("EmpDeptTableIterator").
getCurrentRow().getAttribute("EmployeeId").toString();

In the above example EmpDeptTableIterator is the iterator name from which I want to access the EmployeeId.

In the above example getBinding() refers to the method which returns the object of type DCBindingContainer. If its not already there, add the following code snippet to your same managed bean.

private DCBindingContainer bindings;
public DCBindingContainer getBindings()
{ return bindings;
}
public void setBindings(DCBindingContainer bindings)
{ this.bindings = bindings; }

An inside your faces-config.xml add the following managed property inside your managed bean.
<managed-property>
<property-name>bindings</property-name>
<property-class>oracle.adf.model.binding.DCBindingContainer</property-class>
<value>#{bindings}</value>
</managed-property>

How to create the Simple Login page in ADF

I am using the employee table in HR schema , where I am using the employee first name as the user name and the employee last name as the password. We will create three jsf pages. On valid user name password user will navigate to success page and on error it will navigate to failure page

Step 1) Create a read only View Object with bind variable with name EmpLoginViewObj. Following is the SQL query:-
SELECT
EMPLOYEES.EMPLOYEE_ID EMPLOYEE_ID,
EMPLOYEES.FIRST_NAME FIRST_NAME,
EMPLOYEES.LAST_NAME LAST_NAME
FROM
EMPLOYEES
WHERE EMPLOYEES.LAST_NAME =:LastName

Create the bind variable with name LastName. Make sue that you are generating the ViewObjImpl class for the above created view object.

Step 2) Add the above created View object in the Application module.

Step 3) Add the following code in the application module's Java Impl class

public void checkLoginCredentials(String ename,String pwd_form)
{
System.out.println(ename + " " + pwd_form);
EmpLoginViewObjImpl vo = (EmpLoginViewObjImpl)getEmpLoginViewObj1();
//set the bind variable value to last name
vo.setNamedWhereClauseParam("LastName",pwd_form);
vo.executeQuery();
int rowCount=vo.getEstimatedRangePageCount();
System.out.println("rowCount="+rowCount);
if(rowCount==0) {
throw new JboException("Password doesn't match");
}
}

Import oracle.jbo.JboException class in the AMImpl class.

Step 4) Expose the above method in the Client Interface of the application module.
Now the method checkLoginCredentials will appear in the data control palette.

Step 5) Create three jspx pages. LoginPage.jspx, WelcomePage.jspx and FaliurePage.jspx

Step 6) Drag and drop the checkLoginCredentials on your LoginPage.jspx page as the parameter->ADF parameter Form.

Step 7) Double click the command button on LoginPgae.jspx and define a new action binding by first defining the managed bean(with Name backing_LoginPage) and action binding method name as loginBtn_action()

Step 8) Add the following code inside your loginBtn_action() method.

public String commandButton_action() ()
{
String returnStr="error";
System.out.println("Inside loginBtn_action");
BindingContainer bindings = getBindings();
OperationBinding operationBinding =
bindings.getOperationBinding("checkLoginCredentails");
Object result = operationBinding.execute();
System.out.println(result);
if (operationBinding.getErrors().isEmpty()) {
returnStr= "success";
}
System.out.println("returnStr= " + returnStr);
return returnStr;
}

Step 8 ) Set the navigation rule as
<navigation-rule>
<from-view-id>/LoginPage.jspx</from-view-id>
<navigation-case>
<from-action>#{backing_LoginPage.commandButton_action}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/WelcomePage.jspx</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{backing_LoginPage.commandButton_action}</from-action>
<from-outcome>error</from-outcome>
<to-view-id>/FaliurePage.jspx</to-view-id>
</navigation-case>
</navigation-rule>


run your login page to check the results.

Monday, October 6, 2008

How to apply the CSS in the ADF Application

ADF applications are provided by three default skins :-
1) oracle 2) simple 3) minimal. thought these skins provide good look and feel but what if you have to add your own skin in your ADF applications......


A skin consists of the following artifacts:
-> A CSS file that defines the actual look of the components
-> A configuration file. In our case it will be "adf-faces-skins.xml" which lists up all skins available for our application (not including Oracle, Minimal, and Simple). This file has to be located in your applications WEB-INF directory
-> An entry in the ADF Faces configuration file - adf-faces-config.xml

Creating a CSS file:

1) Right-click the project that contains the code for the user interface and choose
2) New to open the New Gallery.
3) In the New Gallery, expand the Web Tier node and select HTML.
4) Double-click CSS File.
5) Complete the Create Cascading Style Sheet dialog by giving CSS name.
6) Add various styles according to your requirement in the CSS

Register the CSS file:

1) Right-click your view project and choose New to open the New Gallery.
2) The New Gallery launches. The file launches in the Source editor.
In the Categories tree on the left, select XML. If XML is not displayed, use the
Filter By dropdown list at the top to select All Technologies.
3) In the Items list, select XML Document and click OK.
4) Name the file adf-faces-skins.xml, place it in the /WEB-INF directory, and click OK.
5) Replace the generated code with the code shown in

<?xml version="1.0" encoding="ISO-8859-1"?>
<skins xmlns="http://xmlns.oracle.com/adf/view/faces/skin">
<skin>
<id>id name</id>
<family>family name</family>
<render-kit-id>oracle.adf.desktop</render-kit-id>
<style-sheet-name>path to CSS</style-sheet-name>
</skin>
</skins>


For example



Configure an application to use a skin:

1) Open the adf-faces-config.xml file.
2) Replace the <skin-family&gt value with the family name for the skin you wish to use