Friday, December 20, 2013

ADF - Calculate accumulative value of a Column in a Table

In this post I tried to show how to calculate the accumulative value of salary column in Employee table of oracle HR schema for example, where you can use it in specific scenario
As shown in the images below, Having  AccumulativeSalary transient attribute which it's value represent the sum of salaries of above rows.
1. add transient attribute and generate Employee ViewObjectImpl and ViewObjectRowImp
2.write the following code of get method of AccumulativeSalary transient attribute as the following:-
    /**
     * Gets the attribute value for the calculated attribute AccumulativeSalary.
     * @return the AccumulativeSalary
     */
    public Number getAccumulativeSalary() {
        if (getAttributeInternal(ACCUMULATIVESALARY) == null) {
            EmployeesViewImpl empVo = (EmployeesViewImpl)getViewObject();
//create separate RowSetIterator  of employee viewObject where you can iterate
            RowSetIterator rsi = empVo.createRowSetIterator(null);
            Number temp = getSalary();
            while (rsi.hasNext()) {
                EmployeesViewRowImpl r = (EmployeesViewRowImpl)rsi.next();
                if (r == this) {
                    setAttributeInternal(ACCUMULATIVESALARY, temp);
                    break;
                } else {
                    temp = temp.add(r.getSalary());                        
                }
            }            
            return temp;  
        }
        return (Number)getAttributeInternal(ACCUMULATIVESALARY);
    }
The JDeveloper 11.1.1.7.0 workspace of the sample application can be downloaded from here

No comments:

Post a Comment