woensdag 2 maart 2011

How to create web service based ADF pages

Technology: ADF11g
Developed in: JDeveloper 11.1.1.3.0
Used database schema: none


Summary

In this blog a solution is provided how to create a web service client. This blog is part of a sequence of 3 blogs:
  • How to create a RESTful web service that returns objects
  • How to create a web service client
  • How to create web service based ADF pages
I choose for a solution that:
  • Works for SOAP and RESTful web services
  • The location of the web service host can be changed while running the web service
Especially changing the host was an important criteria for me. For development I would like to use a development webservice and for production of course a production server.

The solution is build into my existing demo ADF application.

Step 1

Create a Common (generic) project in the demo ADF application add the following libraries:
  • BC4J Oracle Domains
  • Oracle JDBC
  • JSF 1.2
  • Commons Logging 1.0.4
And add the HrWebServiceClient.jar created in the ‘How to create a web service client’ blog.

Open the project properties of the Model project and set the dependency to the Common project:



Open the project properties of the ViewController project and also set the dependency to the Common project.

Step 2

In the Common project a basic view object implementation is created that will be used for all web service based view objects.

The class contains converters that converts Oracle data types to Java data types, it sets the end point based on the information of the web.xml (in the ViewController project) and has methods to execute the webservice.

Create a abstract java class WsViewObjectImpl which extends ViewObjectImpl. The implementation:

package nl.hr.demo.model.common;



import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Timestamp;

import java.util.Iterator;

import java.util.List;

import javax.faces.context.FacesContext;

import oracle.jbo.server.ViewObjectImpl;

import oracle.jbo.server.ViewRowImpl;

import oracle.jbo.server.ViewRowSetImpl;



public abstract class WsViewObjectImpl extends ViewObjectImpl {

public WsViewObjectImpl() {

super();

}



protected String getWSBaseURL() {

return FacesContext.getCurrentInstance().getExternalContext().getInitParameter("wsBaseURL");

}



protected oracle.jbo.domain.Date toOracleDate(java.util.Calendar calendar) {

oracle.jbo.domain.Date jboDate = new oracle.jbo.domain.Date();

Timestamp t = jboDate.timestampValue();

t.setTime(calendar.getTime().getTime());

return new oracle.jbo.domain.Date(t);

}

protected oracle.jbo.domain.Number toOracleNumber(int i) {

return new oracle.jbo.domain.Number(i);

}

protected oracle.jbo.domain.Number toOracleNumber(double d) {

try {

return new oracle.jbo.domain.Number(d);

} catch (SQLException e) {

return null;

}

}



protected abstract List retrieveArrayFromWebService(Object qc, Object[] params);

private void storeNewIterator(Object qc, List rs) {

setUserDataForCollection(qc, rs.iterator());

hasNextForCollection(qc);

}

protected void executeQueryForCollection(Object qc, Object[] params, int noUserParams) {

storeNewIterator(qc, retrieveArrayFromWebService(qc, params));

super.executeQueryForCollection(qc, params, noUserParams);

}



private Iterator getArrayIterator(Object qc) {

return (Iterator)getUserDataForCollection(qc);

}

protected boolean hasNextForCollection(Object qc) {

boolean hasNext = getArrayIterator(qc).hasNext();

if (!hasNext) {

setFetchCompleteForCollection(qc, true);

}

return hasNext;

}



protected abstract void fillRow(Object data, ViewRowImpl row);

protected ViewRowImpl createRowFromResultSet(Object qc, ResultSet resultSet) {

Iterator iterator = getArrayIterator(qc);

ViewRowImpl row = createNewRowForCollection(qc);

fillRow(iterator.next(), row);

return row;

}



public long getQueryHitCount(ViewRowSetImpl viewRowSet) {

return super.getQueryHitCount(viewRowSet);

}

}


Step 3

Create a new programmatic view object in the Model project. The new view object:
  • Is programmatically
  • Extends the WsViewObjectImpl class
  • Contains a RowImpl class
  • Has a bind variable b_id data type Number
  • All attributes (of the EmployeeClient) are updatable
  • The EmployeeId attribute is Key Attribute.




Open the view object impl class (EmployeesWsImpl) and implement it (only added / changed methods are described below):

package nl.hr.demo.model.views.ws;



import java.util.ArrayList;

import java.util.List;

import nl.hr.demo.webservices.Employee;

import nl.hr.demo.webservices.client.EmployeeClient;

import oracle.jbo.server.ViewRowImpl;



public class EmployeesWsImpl extends WsViewObjectImpl {

protected List retrieveArrayFromWebService(Object qc, Object[] params) {

try {

EmployeeClient client = new EmployeeClient(getWSBaseURL());

if (getb_id() == null) {

return client.getAllEmployees();

} else {

List<Employee> result = new ArrayList<Employee>();

result.add(client.getEmployeeById(getb_id().intValue()));

return result;

}

} catch (Exception e) {

e.printStackTrace();

return null;

}

}



protected void fillRow(Object data, ViewRowImpl row) {

Employee empe = (Employee) data;

populateAttributeForRow(row, EmployeesWsRowImpl.EMPLOYEEID, toOracleNumber(emp.getEmployeeId()));

populateAttributeForRow(row, EmployeesWsRowImpl.FIRSTNAME, emp.getFirstName());

populateAttributeForRow(row, EmployeesWsRowImpl.LASTNAME, emp.getLastName());

populateAttributeForRow(row, EmployeesWsRowImpl.HIREDATE, toOracleDate(emp.getHireDate()));

populateAttributeForRow(row, EmployeesWsRowImpl.EMAIL, emp.getEmail());

populateAttributeForRow(row, EmployeesWsRowImpl.PHONENUMBER, emp.getPhoneNumber());

populateAttributeForRow(row, EmployeesWsRowImpl.JOBID, emp.getJobId());

populateAttributeForRow(row, EmployeesWsRowImpl.SALARY, toOracleNumber(emp.getSalary()));

populateAttributeForRow(row, EmployeesWsRowImpl.COMMISSIONPCT, toOracleNumber(emp.getCommissionPct()));

populateAttributeForRow(row, EmployeesWsRowImpl.MANAGERID, toOracleNumber(emp.getManagerId()));

populateAttributeForRow(row, EmployeesWsRowImpl.DEPARTMENTID, toOracleNumber(emp.getDepartmentId()));

}

}


Step 4

Add the view object to the application module.

Step 5

Create a new page: EmployeesWebServiceTable

Drag and drop the EmployeesWs onto it as read only table:



Add above the table a panelFormLayout containing:

<af:panelFormLayout id="pfl1">

<af:inputText binding="#{employeeBean.id}"

label="ID"

id="idFilter">

<af:convertNumber groupingUsed="false"

integerOnly="true"/>

</af:inputText>

<f:facet name="footer">

<af:commandButton text="Filter"

id="filter"

actionListener="#{employeeBean.filterWs}"/>

</f:facet>

</af:panelFormLayout>

Add to the af:table tag:

partialTriggers="::filter"

styleClass="AFStretchWidth"


Step 6

Create a java class EmployeeBean for the page. This bean class holds the ID input text and queries the view object when the filter button is used.

package nl.hr.demo.view.beans;



import javax.faces.context.FacesContext;

import nl.hr.demo.model.services.HrAppModuleImpl;

import nl.hr.demo.model.views.ws.EmployeesWsImpl;

import oracle.adf.model.binding.DCBindingContainer;

import oracle.adf.model.binding.DCDataControl;

import oracle.adf.view.rich.component.rich.input.RichInputText;

import oracle.binding.BindingContainer;

import oracle.jbo.domain.Number;



public class EmployeeBean {



private RichInputText id;



public EmployeeBean() {

super();

}



public void filterWs (ActionEvent actionEvent) {

EmployeesWsImpl view = getService().getEmployeesWs();

view.setb_id(getValue(id));

view.executeQuery();

}

private Number getValue(RichInputText item) {

if (item == null || item.getValue() == null) {

return null;

}

return new Number(((Long)item.getValue()).intValue());

}



private static HrAppModuleImpl getService() {

DCBindingContainer bc = (DCBindingContainer)FacesContext.getCurrentInstance().getApplication().evaluateExpressionGet(FacesContext.getCurrentInstance(), "#{bindings}", BindingContainer.class);

DCDataControl dc = bc.findDataControl("HrAppModuleDataControl");

return (HrAppModuleImpl) dc.getDataProvider();

}



public void setId(RichInputText id) {

this.id = id;

}

public RichInputText getId() {

return id;

}

}


Step 7

Add the page and the bean to the unbounded task flow:



Managed bean property Value
Name employeeBean
Class nl.hr.demo.view.beans.EmployeeBean
Scope Request

Step 8

Start the page it opens getAllEmployees is executed of the web service:



And if you fill an ID and click filter the web service is called again:

1 opmerking: