Monday 5 October 2015

Object scopes in Fusion page file cycle

At runtime, there are six types of scopes in a Fusion web application
  • Application scope
  • Session scope
  • Page flow scope
  • View scope
  • Request scope
  • Backing  bean scope
Application scope: The object is available for the duration of the application.

Session scope: The object is available for the duration of the session.

Page flow scope: The object is available for the duration of a bounded task flow.

Request scope: The object is available from the time an HTTP request is made until a response is sent back to the client.

Backing bean scope: Used for managed beans for page fragments and declarative components only, the object is available from the time an HTTP request is made until a response is sent back to the client. This scope is needed for fragments and declarative components because there may be more than one page fragment or declarative component on a page, and to prevent collisions, any values must be kept in separate scope instances. Therefore, any managed bean for a page fragment or declarative component must use backing bean scope.

View scope: The object is available until the view ID for the current view activity changes. This scope can be used to hold values for a given page. However, unlike request scope, which can be used to store a value needed from one page to the next, anything stored in view scope will be lost once the view ID changes.

Monday 29 June 2015

How to know whether current row is new row or from database?

A row or view has four states:
  • New
  • Modified
  • Un-Modified
  • Initialized
To get the state using expression: #{row.row.entities[0].entityState}

You can use this expression to control the display or rows in the UI. Like to display new rows in diff colors.
    Ex: #{row.row.entities[0].entityState==0 ? 'background-color:yellow' :  ''} 
Now in RowImpl, you can access the state using:
    Ex: byte entityState = this.getEntity(0).getEntityState() 

Friday 23 January 2015

How to filter rows in VO

There are different ways to filter the data/rows in a VO.

1. Using Where clause:
  You can use setWhereCluse() method from VO to set filter constraint before calling executeQuery() method to filter data based on the input condition. This means we are appending the where clause while querying the data to the DB. This will take string as in input.
Ex: ViewObjectImpl empVO = am.getEmployeeVO();
   //filtering data using  setWhereClause on employee_id
   empVO.setWhereClause("employee_id=" + empId);
   empVO.executeQuery();
 


Note: Here employee_id is the column name from the db table.

 You can have multiple constraints in where clause just like in sql.
 emp.setWhereClause("employee_id=" + empId + "employee_name like " + empName);

2. Using getFilteredRows(): getFilteredRows() is from VO, this method is used to filter rows based on one column only and returns Row[]. getFilteredRows() returns the Row[] by filtering the rows from VO cache based on the column attribute passed.
Ex: ViewObjectImpl empVO = am.getEmployeeVO();
    //filtering data using  setWhereClause on employee_id
    empVO.executeQuery();
    Row[] rows = empV.getFilteredRows("EmployeeId", empId);


Note: EmployeeId is the attribute name from VO.


3. Using Row Qualifier: Using RowQualifier you can filter the data based on multiple columns from VO cache.
Ex: ViewObjectImpl empVO = am.getEmployeeVO();
    //Create RowQualifier from VO 
    RowQualifier rowQualifier = new RowQualifier(empVO);
    //set where clause to row qualifier 
    rowQualifier.setWhereClause("EmployeeId="+empId+" AND EmployeeName="+empName);
    //get filtered rows using row qualifier 
    Row[] filteredRows = emp.getFilteredRows(rowQualifier);  

Note: EmployeeId & EmployeeName are the attribute names from VO.

Monday 19 January 2015

Select All rows in ADF tabel

I got a requirement to provide 'Select All' option for ADF table. Tried a lot in different ways and finally got the solution with the following approach.

Code in UI:
 
<af:commandToolbarButton partialSubmit="true" text="Select All" id="cb1"
actionListener="#{myBean.selectAllListener}">
</af:commandToolbarButton> 

Code in the bean for selection listener is like this:
public void selectAllListener() {
  RowKeySet rkset = new RowKeySetImpl();
  CollectionModel model = (CollectionModel)myTable.getValue();
  int rowcount = model.getRowCount();
 
  for (int i = 0; i < rowcount; i++) {
     model.setRowIndex(i);
     Object key = model.getRowKey();
     rkset.add(key);
  }
 
  myTable.setSelectedRowKeys(rkset);
}

When we click on 'Select All' button, it will select all the rows from the model, adds to the selected row keys of the table and also displays the selected row count in the 'Rows selected' section in the status bar.

If the selection listener has makeCurrent entry, then the above approach may fail. When you select one row and then go for 'Select All', this wont work  to overcome this issue you can have selection listener code in the bean as follows:

public void selectAllListener() {
  RowKeySet rkset = myTable.getSelectedRowKeys();
  CollectionModel model = (CollectionModel)myTable.getValue();
  int rowcount = model.getRowCount();
 
  for (int i = 0; i < rowcount; i++) {
     model.setRowIndex(i);
     Object key = model.getRowKey();
     rkset.add(key);
  }
}

This means that, you are going to add remaining rows on top of selected rows.