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

Sunday, November 3, 2013

ADF Tree based on self join case, 3 functionality.(drag & drop, delete & move children, delete & delete multiple level children)

In this post I tried to show 3 operation for ADF tree using simple solution.   the drag and drop , delete node and move it's children to up level and delete node and delete multiple level children. 
the drag and drop
As shown in the images below, Having a two level tree. Expanding a node will make all detail nodes appear. drag  node from one level and drop him to another.


In the Model project we just need the  view objects  should be updatable connected with a viewLink based on common attribute ParentId. In the view object we need a bind variable which we also add to the query so that we can execute-with-params.



About delete and node and move it's children node to level up 
through application module method I get select node and update its children parent value by the value of selected root node parent value then delete value
About  delete node and delete multiple level children
  I used the power of sql to get all children for selected root node though customize the query of another updatable view object get multi level children, In the view object we need a bind variable which we also add to the query so that we can execute-with-params
And through application module method I get selected node and delete its multi level children then delete root node


The JDeveloper 11.1.1.7.0 workspace of the sample application and script file for this post can be downloaded from here

Thursday, October 31, 2013

Reorder rows in an ADF table

Reorder rows in an ADF table. As shown in the images below, applying the code in this post will allow you to re-arrange rows within a table using "up and down" buttons Its simply and change rank attribute value of swiped rows and change in the indexing of the iterator. 


Selecting a row and move it to a step up or down ,then will  show it in its new position within the table
and you may commit later


Note that reordering the rows in the table will only have an effect to the DCIteratorBinding (more precise, its an effect to the RowSetIterator it decorates) in the Pagedef file. 

Managed Bean

    /**  The selected row takes the position of the previous row.
     *   synchronize the underlying ADF model I needed to accept a
     *   "flicker" of the table after the action *
     *   @param actionEvent Event passed in from ADF Faces at the end of the action
     *  */

    public void upButton(ActionEvent actionEvent) {
        // Add event code here...
        Row currentRow;
        Row previousRow;
        currentRow =
                ADFUtils.findIterator("TreeTableView1Iterator").getCurrentRow();

        Number tRank = new Number();
        try {
            tRank = (Number)currentRow.getAttribute("Rank");
        } catch (Exception e) {
        }

        if (ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().hasPrevious()) {
            previousRow =
                    ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().previous();

            currentRow.setAttribute("Rank", previousRow.getAttribute("Rank"));
            previousRow.setAttribute("Rank", tRank);
            int indexOfPreviousRow =                ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().getRangeIndexOf(previousRow);
            //remove selected row from collection so it can be added back
            currentRow.removeAndRetain();
            ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().insertRowAtRangeIndex(indexOfPreviousRow,currentRow);
            //make row current in ADF iterator.
            ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().setCurrentRowAtRangeIndex(indexOfPreviousRow);

        }
    }

    /**  The selected row takes the position of the next row.
     *   synchronize the underlying ADF model I needed to accept a
     *   "flicker" of the table after the action *
     *   @param actionEvent Event passed in from ADF Faces at the end of the action
     *  */

    public void downButton(ActionEvent actionEvent) {
        Row currentRow;
        Row nextRow;
        currentRow =
                ADFUtils.findIterator("TreeTableView1Iterator").getCurrentRow();
        System.out.println(currentRow.getAttribute("Rank"));
        Number tRank = new Number();
        try {
            tRank = (Number)currentRow.getAttribute("Rank");
        } catch (Exception e) {
        }

        if (ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().hasNext()) {
            nextRow =
                    ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().next();

            currentRow.setAttribute("Rank", nextRow.getAttribute("Rank"));
            nextRow.setAttribute("Rank", tRank);
            int indexOfSelectedRow =
                ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().getRangeIndexOf(currentRow);
            //remove  selected row from collection so it can be added back
            nextRow.removeAndRetain();
            ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().insertRowAtRangeIndex(indexOfSelectedRow, nextRow);
            //make row current in ADF iterator.
            ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().setCurrentRowAtRangeIndex(indexOfSelectedRow + 1);

        }
    }

The JDeveloper 11.1.1.7.0 workspace of the sample application and script file for this post can be downloaded from here