Showing posts with label #af:Table. Show all posts
Showing posts with label #af:Table. Show all posts

Friday, 16 March 2018

Automatic Refresh table data in Oracle ADF


Hi,

Today I am going to post about how we can automatically refresh a table on particular time interval. For this we will be using af:poll component.

Let us start by simply creating a sample application with hr Schema.

And then drop a viewObject as a table on a page.

Here is the page.


Correction : To refresh the table on every refresh we will have to disable the cache from the Table Iterator.[This step can pe skipped].



After that when we run the application.


 After that, we change the data in the database.


Then when a poll is called then the department name Admin changed to Admin New


Here is the code of the Page used in this project.


<af:panelBox text="Sample Auto Table Refresh" id="pb1" showDisclosure="false">
    <f:facet name="toolbar">
      <af:toolbar id="t2">
        <af:selectBooleanCheckbox label="Auto Refresh" id="sbc1"
                                  value="#{viewScope.smplPageRefreshBean.autoRefreshEnabled}"
                                  autoSubmit="true"/>
        <af:inputText label="Refresh Duration" id="it1" contentStyle="width:50px;"
                      value="#{viewScope.smplPageRefreshBean.refreshDuration}" autoSubmit="true"
                      partialTriggers="sbc1" visible="#{viewScope.smplPageRefreshBean.pollOn}"/>
        <af:outputText value="Last Refresh on : #{viewScope.smplPageRefreshBean.lastRefreshedOn}" id="ot5"
                       partialTriggers="sbc1 p1" visible="#{viewScope.smplPageRefreshBean.pollOn}"/>
      </af:toolbar>
    </f:facet>
    <af:panelGroupLayout id="pgl1" layout="vertical">
      <af:poll id="p1" interval="#{viewScope.smplPageRefreshBean.pollDuration}" partialTriggers="sbc1"
               pollListener="#{viewScope.smplPageRefreshBean.pollListner}"/>
      <af:table value="#{bindings.DepartmentsVO1.collectionModel}" var="row" rows="#{bindings.DepartmentsVO1.rangeSize}"
                emptyText="#{bindings.DepartmentsVO1.viewable ? 'No data to display.' : 'Access Denied.'}"
                rowBandingInterval="0" selectedRowKeys="#{bindings.DepartmentsVO1.collectionModel.selectedRow}"
                selectionListener="#{bindings.DepartmentsVO1.collectionModel.makeCurrent}" rowSelection="single"
                fetchSize="#{bindings.DepartmentsVO1.rangeSize}" id="t1" partialTriggers="::sbc1 ::p1" autoHeightRows="10"
                styleClass="AFStretchWidth">
        <af:column headerText="#{bindings.DepartmentsVO1.hints.DeptId.label}" id="c1">
          <af:outputText value="#{row.DeptId}" shortDesc="#{bindings.DepartmentsVO1.hints.DeptId.tooltip}" id="ot1">
            <af:convertNumber groupingUsed="false" pattern="#{bindings.DepartmentsVO1.hints.DeptId.format}"/>
          </af:outputText>
        </af:column>
        <af:column headerText="#{bindings.DepartmentsVO1.hints.DeptNm.label}" id="c2">
          <af:outputText value="#{row.DeptNm}" shortDesc="#{bindings.DepartmentsVO1.hints.DeptNm.tooltip}" id="ot2"/>
        </af:column>
        <af:column headerText="#{bindings.DepartmentsVO1.hints.DeptLoc.label}" id="c3">
          <af:outputText value="#{row.DeptLoc}" shortDesc="#{bindings.DepartmentsVO1.hints.DeptLoc.tooltip}" id="ot3">
            <af:convertNumber groupingUsed="false" pattern="#{bindings.DepartmentsVO1.hints.DeptLoc.format}"/>
          </af:outputText>
        </af:column>
        <af:column headerText="#{bindings.DepartmentsVO1.hints.DeptMngr.label}" id="c4">
          <af:outputText value="#{row.DeptMngr}" shortDesc="#{bindings.DepartmentsVO1.hints.DeptMngr.tooltip}" id="ot4">
            <af:convertNumber groupingUsed="false" pattern="#{bindings.DepartmentsVO1.hints.DeptMngr.format}"/>
          </af:outputText>
        </af:column>
      </af:table>
    </af:panelGroupLayout>

And in the Bean.


package sampleautorefreshtable.adfjavacodes.view.bean;

import java.util.Date;

import javax.faces.application.FacesMessage;

import oracle.adf.model.BindingContext;

import oracle.binding.OperationBinding;

import org.apache.myfaces.trinidad.event.PollEvent;

public class smplPageRefreshBean {
    // To decide if refresh is enabled or not
    private boolean autoRefreshEnabled = false;
    // To decide the last refreshed time
    private Date lastRefreshedOn = new Date(System.currentTimeMillis());
    // To decide refresh time interval in seconds
    private long refreshDuration = 10;
    // To set poll to on or off
    private boolean pollOn = false;

    public boolean isPollOn() {
        return pollOn;
    }

    public smplPageRefreshBean() {
    }

    public void setAutoRefreshEnabled(boolean autoRefreshEnabled) {
        if (autoRefreshEnabled) {
            pollOn = true;
            refreshDuration = 10;
        } else {
            pollOn = false;
        }
        this.autoRefreshEnabled = autoRefreshEnabled;
    }

    public boolean isAutoRefreshEnabled() {
        return autoRefreshEnabled;
    }

    public void setLastRefreshedOn(Date lastRefreshedOn) {
        this.lastRefreshedOn = lastRefreshedOn;
    }

    public Date getLastRefreshedOn() {
        return lastRefreshedOn;
    }

    public void setRefreshDuration(long refreshDuration) {
        this.refreshDuration = refreshDuration;
    }

    public long getRefreshDuration() {
        return refreshDuration;
    }

    public long getPollDuration() {
        return (pollOn ? refreshDuration * 1000 : -1);
    }

    public void pollListner(PollEvent pollEvent) {
        OperationBinding o = BindingContext.getCurrent().getCurrentBindingsEntry().getOperationBinding("Execute");
        o.execute();
        System.out.println("refreshed");
        lastRefreshedOn = new Date(System.currentTimeMillis());
    }
}

You can find the sample project here: SampleAutoRefreshTable

Thanks!

References : http://pamkoertshuis.blogspot.in/2016/01/auto-refresh-op-table-in-adf.html

Monday, 28 December 2015

ADF | Fetching Multiple selected Rows from a Table populated from POJO in ADF

Hi,

This is again an Extension to my previous post on how we can perform edit on a table populated from POJO. http://adfjavacodes.blogspot.com/2015/12/adf-performing-edit-in-table-from-pojo.html

Everything is same from my previous post.
There are certain changes in the application. They are :

Here I have enabled Multiple row selection for the table from the property inspectior to
 <af:table var="row" rowBandingInterval="0" id="t1" value="#{viewScope.BookBean.bookInfo}"
                                  rowSelection="multiple" binding="#{viewScope.BookBean.tableBinding}">

I have also created a binding of the table for getting the  Multiple selected rows from the table.
Here is the code of the of the Bean where I have created a method that will fetch the selected rows from the table.


package edittableonpojo.view.bean;

import edittableonpojo.view.dc.BookInfo;

import java.util.ArrayList;

import java.util.Iterator;

import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.view.rich.component.rich.data.RichTable;

import org.apache.myfaces.trinidad.model.RowKeySet;

public class BookBean {
    private ArrayList<BookInfo> bookDtls = new ArrayList<BookInfo>();
    private RichTable tableBinding;

    public BookBean() {
        bookDtls.add(new BookInfo("1A", "The Alchemist", "Paulo Cohelo"));
        bookDtls.add(new BookInfo("2A", "Game Of Thrones", "George R. R. Martin"));
        bookDtls.add(new BookInfo("3A", "Five Point Someone", "Chetan Bhagat"));
        bookDtls.add(new BookInfo("4A", "Harry Potter", "J.K.Rowling"));
        bookDtls.add(new BookInfo("5A", "Wings of Fire", "A.P.J Abdul Kalam"));
    }

    public ArrayList<BookInfo> getBookInfo() {
        return bookDtls;
    }
    
    public void printValues(ActionEvent ace){
        for(BookInfo row : bookDtls){
            System.out.println("Row : "+row.toString());
        }
    }

    public void fetchSelectedValuesAL(ActionEvent actionEvent) {
        ArrayList<BookInfo> selectedRows = new ArrayList<BookInfo>();
        RowKeySet keySet = getTableBinding().getSelectedRowKeys();
        Iterator<Object> itr = keySet.iterator();
        while(itr.hasNext()){
            // next gives the index of the selected row
            Object next = itr.next();
            // here we have fetched the row on index from the bookDtls list
            selectedRows.add(bookDtls.get((Integer)next));
        }
        System.out.println("Selected Rows are : "+selectedRows.toString());
    }

    public void setTableBinding(RichTable tableBinding) {
        this.tableBinding = tableBinding;
    }

    public RichTable getTableBinding() {
        return tableBinding;
    }
}

On running the application ,  I have selected Two rows from the table.

On clicking "Get Selected Values" button.



Here is the Sample Application : EditableTableOnPOJO.rar