Pages

Tuesday, December 11, 2007

Business rules in adf bc ( bc4j )

As an old school oracle developer I developed my business rules in plsql. I did this with cdm ruleframe or with triggers and packages. But now I want to make a demo on the oe sample schema. In this demo I used the orders and the order_items tables and because it is a demo I want to use bc4j for my business rules.
This are the business rules I want to implement. Every order item has to update the total order amount of the order. The Line Item Id of the order items has to an autonumber 1, 2, etc and not a sequence. On the order table the order date has to be current date and the order Id must be the next value of the orders_seq sequence.
Here some pics of the order and items page



This is how I did it on the orders view. First I made two calculated attributes CalculatedOrderTotal and CalculatedOrderLines on the orders entity. These attributes I use on the orders items entity. Then I generate the impl java class of the orders entity so I can change the getters of these calculated attributes.


/**Gets the attribute value for CalculatedOrderTotal, using the alias name CalculatedOrderTotal.
*/
public Double getCalculatedOrderTotal() {

RowIterator rowItr = this.getOrderItems();
Double total = 0.0;
while(rowItr.hasNext()){
Row row = rowItr.next();
Number unitPrice = (Number)row.getAttribute("UnitPrice");
Number quantity = (Number)row.getAttribute("Quantity");
total += (unitPrice.doubleValue() * quantity.intValue());
}
return total;
}
/**Sets value as the attribute value for CalculatedOrderTotal.
*/
public void setCalculatedOrderTotal(Double value) {
setAttributeInternal(CALCULATEDORDERTOTAL, value);
}

/**Gets the attribute value for CalculatedOrderLines, using the alias name CalculatedOrderLines.
*/
public Number getCalculatedOrderLines() {
RowIterator rowItr = this.getOrderItems();
Number lineId = new Number(0);

while(rowItr.hasNext()){
Row row = rowItr.next();
Number lineIdRow = (Number)row.getAttribute("LineItemId");
// check the current Id is greater than lineId

if ( lineIdRow != null && lineIdRow.compareTo(lineId) == 1 ) {
lineId = lineIdRow;
}
}
return lineId;
}

/**Sets value as the attribute value for CalculatedOrderLines.
*/
public void setCalculatedOrderLines(Number value) {
setAttributeInternal(CALCULATEDORDERLINES, value);
}

Now we add some code to the impl of the order items. First I try to set the next number for the line item , Then I set the unit item price of the selected product ( I can do this by using the association between order items and product information )the last step is to update the total order amount of the order


protected void prepareForDML(int dml, TransactionEvent evt) {
if (dml == DML_INSERT || dml == DML_UPDATE || dml == DML_DELETE) {
super.prepareForDML(dml,evt);
businessRules( dml);
}
}

public void businessRules(int dml){
if ( dml == DML_INSERT) {
Number row = this.getOrders().getCalculatedOrderLines();
this.setLineItemId(row.add(1));
}

if ( dml == DML_INSERT || dml == DML_UPDATE) {
ProductInformationImpl product = (ProductInformationImpl) this.getProductInformation();
if(product != null){
Number listPrice = product.getListPrice();
this.setUnitPrice(listPrice);
}
}
if ( dml == DML_UPDATE) {
Number oldQuantity = (Number)getPostedAttribute(getAttributeIndexOf("Quantity"));
System.out.println("new quantity "+this.getQuantity().toString()+" old "+oldQuantity.toString());
}
try {
this.getOrders().setOrderTotal(new Number(this.getOrders().getCalculatedOrderTotal()));;
}
catch ( Exception e) {
e.printStackTrace();
}
}

If you want to check the old and new values of an attribute then you can use getPostedAttribute to get the old value of the attribute. This is the same as :old and :new in the plsql triggers.
Now we implement the business rules on the orders entity. We add the following code to the orders impl

protected void prepareForDML(int dml, TransactionEvent evt) {
if (dml == DML_INSERT || dml == DML_UPDATE || dml == DML_DELETE) {
super.prepareForDML(dml,evt);
businessRules(dml);
}
}

public void businessRules(int dml){
if ( dml == DML_INSERT) {
SequenceImpl sequence = new SequenceImpl("ORDERS_SEQ", getDBTransaction());
this.setOrderId(sequence.getSequenceNumber());
this.setOrderDate(new Date(Date.getCurrentDate()));
}
}

In this method I set a database sequence on the Order Id and the current Date in the Order Date attribute.
Here you have the sample project it works on jdeveloper 11g and on the oe sample schema

Saturday, December 8, 2007

11g plsql web service

Making a plsql web service in 11g is very easy but you have to think about your function, procedure and parameters names else it can result in a strange wsdl method and parameter names.
Here is a example of a function.

create or replace package scott_ws is
function get_current_time ( date_format varchar2)
return varchar2
;
end scott_ws;

create or replace package body scott_ws is
function get_current_time ( date_format varchar2)
return varchar2
is
v_time varchar2(20);
begin
if date_format is null
then
return 'empty format';
end if;
select to_char(sysdate,date_format) into v_time from dual;
return v_time;
exception
when others
then
return 'error';
end;
end scott_ws;

The package name is not used in the wsdl creation. If you want to have a capital character then you have to use a underscore _ . Jdelevoper automatically remove the underscore and capitalize the following character. In Oracle plsql is common to use p_format as a parameter of a function but this results in pFormat.
So we are ready to create a plsql web service.

Here we make a database connection and we select the oracle package.

Select our function. And we are ready with our webservice. Here is how the wsdl looks like

Let's make it a bit more interesting by using security and logging.

Let's start the webservice and look at the result. As username I use a standard account oc4jadmin. You can also use an another account but you have to edit the jazn of the embedded oc4j.

Wednesday, December 5, 2007

11g list of values LOV

In JDeveloper 11g TP2 ( In TP3 is the af:inputListOfValues a bit different) can you define a List of values on an attribute of a bc4j viewobject. This is very easy but you can do it also manually so you can use it with toplink or with webservices. In this blog I make an jsf page on employees where you can update the department of this employee with a list of values or a lov combobox.This is a very simple LOV but you can make it more complex by using a search area in this. I will explain this in a other blog. But how do we manually create a LOV page. First we drop employees view from the data control on the jsf page. We select an ADF Form
We use all employees attributes with include navigation controls plus include submit button.

Now go to the pagedef of this page where we create a list of values binding on the department iterator. We need this for the lov page

We don't have the department iterator so we have to select it first.
We select all the attributes of the department iterator.
Open the pagedef and rename the list of values ID to DeptnoLOV. This is how the list of values looks in the pagedef.

Now go to the page and select the last column and insert input list of values.

Now we update the af:inputListOfValues element in jspx page with this one

returnPopupListener attribute of inputListOfValues is very important, here is the method defined which copies the value of the lov to the deptno of the employee. You can also have launchPopupListener where you can limit the records of the lov.


package nl.ordina.beans;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.event.ReturnPopupEvent;
import oracle.binding.BindingContainer;
import oracle.jbo.Row;

public class LovDepartment {


public void DeptLOVReturnListener(ReturnPopupEvent returnPopupEvent) {
// Add event code here...
DCIteratorBinding EmpViewIterator = (DCIteratorBinding) getBindings().get("EmpView1Iterator");
Row rw = EmpViewIterator.getViewObject().getCurrentRow();
rw.setAttribute("Deptno",getDeptLovID());

}

private BindingContainer getBindings() {
FacesContext fc = FacesContext.getCurrentInstance();
Application app = fc.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = fc.getELContext();
ValueExpression valueExp = elFactory.createValueExpression(elContext, "#{bindings}", Object.class);
return (BindingContainer) valueExp.getValue(elContext);
}

private String getDeptLovID() {
FacesContext fc = FacesContext.getCurrentInstance();
Application app = fc.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = fc.getELContext();
ValueExpression valueExp = elFactory.createValueExpression(elContext, "#{bindings.DeptnoLOV.inputValue}", Object.class);
return valueExp.getValue(elContext).toString();
}
}

We only have to register this bean in the adfc-config.xml

Now we can delete in the jsf page the deptno column of the employee form because we have now the lov. If you don't like the LOV or inputListOfValues you can also use a combobox or inputComboboxListOfValues. You only have to use this code in the page and replace the inputListOfValues element.

Here you have the example project