Wednesday, January 18, 2017

af:Table AutoSave using SelectionListener and IsDirty method

Scenario : upon changing any field , committing data using Auto Save

Below application was developed for committing the data on change of Phone number only
Step1 : create JSPX apge and drag and drop vo from DC as a read only table and then drag and drop same vo as form below to the table and then select phone number and set auto submit to true.

and then select table and go to selection listener and copy the existing code for future needs.
and use custom method which was called from the back end bean.
#{pageFlowScope.TestBean.customListener}

created af:messages on UI and set inline true and then created binding for both table and messages.
using isDirty method in bean i am committing the changes , isDirty will check the AM instance changed or not,so if we don't put autosubmit true in form(phone-number) AM instance will not get modified.

when ever user select row and change phone number and then when he select another row i am committing the changes.

Structure of jspx :








af:messages and af:table @jspx
 
af:panelFormLayout



code in my backing bean
package view;
import javax.el.ELContext;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.component.rich.output.RichMessages;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.faces.application.FacesMessage;
import oracle.adf.view.rich.context.AdfFacesContext;
import oracle.adf.view.rich.event.QueryEvent;
import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;
import oracle.jbo.ApplicationModule;
import org.apache.myfaces.trinidad.event.SelectionEvent;
public class TestBean {
    private RichMessages message;
    private RichTable table;
    public TestBean() {
    }
    public boolean isDirt(){
        DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        ApplicationModule am = bindings.getDataControl().getApplicationModule();
            return am.getTransaction().isDirty();
            }
    public BindingContainer getBindings(){
        return BindingContext.getCurrent().getCurrentBindingsEntry();
        }
    public void customListener(SelectionEvent selectionEvent) {
        // Add event code here...
        if(isDirt()){
            BindingContainer bc = getBindings();
            OperationBinding opr = bc.getOperationBinding("Commit");
            opr.execute();
            if(opr.getErrors().isEmpty()){
                        FacesMessage msg=new FacesMessage("Data Saved to DB success");
                        msg.setSeverity(FacesMessage.SEVERITY_INFO);
                        FacesContext fctx=FacesContext.getCurrentInstance();
                        fctx.addMessage(null, msg);
                this.getMessage().setVisible(true);
                AdfFacesContext.getCurrentInstance().addPartialTarget(this.getTable());
                    }
        }else{
            this.getMessage().setVisible(false);
            }
        AdfFacesContext.getCurrentInstance().addPartialTarget(this.getMessage());
//        resloveMethodExpression("#{bindings.EmployeesView1.collectionModel.makeCurrent}");
       this.invokeMethodExpression("#{bindings.EmployeesView1.collectionModel.makeCurrent}",Object.class, new Class[] { SelectionEvent.class },new Object[] { selectionEvent });
    }
    public Object invokeMethodExpression(String expr, Class returnType,
                                           Class[] argTypes, Object[] args)
      {
        FacesContext fc = FacesContext.getCurrentInstance();
        ELContext elctx = fc.getELContext();
        ExpressionFactory elFactory =
          fc.getApplication().getExpressionFactory();
        MethodExpression methodExpr =
          elFactory.createMethodExpression(elctx, expr, returnType, argTypes);
        return methodExpr.invoke(elctx, args);
      }
   public void setMessage(RichMessages message) {
        this.message = message;
    }
    public RichMessages getMessage() {
        return message;
    }
    public void setTable(RichTable table) {
        this.table = table;
    }
    public RichTable getTable() {
        return table;
    }  
}


Final output looks like below:

No comments:

Post a Comment