Thursday, October 11, 2012

Insert Rows in to ADF View Object Programatically

Following are the use cases pertaining to this topic

1. Insert Rows into ADF View Object Programatically.
2. Insert Rows into a ADF View Object from a ADF Table which is a combination of multiple tables (for example : ADF Table in the jsp page contains mixture of columns from various tables and we need to take only some column data and save it in target ADF VO.

Create a View object from multiple entities (( Assuming you have a View Object SourceVO1 ).Drag and drop the View Object data control on to the page as ADF Table.Create an ADF button at the bottom of the table. Create Action Listener for the button. you must have the target View Object ready to store the values. Since you have created ADF Table on the page the binding section will have the bindings created for the View Object and to access its collection model .For more on bindings section ADF Bindings. Now we need to add the target View Object to the page bindings section.

This part of the section also describes about How to Create Tree Table binding.
Go to the Bindings section of your page. Click Plus icon

 Select tree from the Insert Item window and click ok.


Click Add button as shown . This will list all of the View Objects added to the Application Module

 Choose the desire View Object in which you are going to save the records.


























Click at the plus icon and select Add Rule .

























This window will bring down all of the attributes that target view object has.you can shuttle the necessary attributes to the righter side and click ok . Now the Target View Object added as Tree binding can be seen under Bindings section.




























Now we will write the java code to insert rows. As we have already created an Action Listener for the button,go to the method of that java class and write this code.

//Code to get the bindings for TargetVO :
    
         DCBindingContainer bindings2 =
            (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
             
        JUCtrlHierBinding obj = (JUCtrlHierBinding)bindings2.findCtrlBinding("ProgrammaticVO1");
        ViewObject targetVO = obj.getViewObject();


   DCBindingContainer bindings =
            (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding empIter =
            bindings.findIteratorBinding("SourceVO1Iterator");

//SourceVO1Iterator is the iterator under Executables section for the SourceVO1 bindings.

        RowSetIterator roleRSIters = empIter.getRowSetIterator();
        RowSetIterator rs1 = roleRSIters.getRowSet().getViewObject().createRowSetIterator(null);
        NameValuePairs nvp = null;

        while (rs1.hasNext()) {
            Key key = rs1.next().getKey();
            Row r = rs1.getRow(key);           
           
            nvp = new NameValuePairs();
            nvp.setAttribute("Empid",r.getAttribute("EmployeeId"));
            nvp.setAttribute("Nameone",r.getAttribute("FirstName"));
            nvp.setAttribute("Nametwo",r.getAttribute("LastName"));
            targetVO.createAndInitRow(nvp);
         }
       
        rs1.closeRowSetIterator();
        targetVO.getApplicationModule().getTransaction().commit();

Getting the View object in MB and performing operations on it is not a good practice.

Tuesday, October 9, 2012

How to Identify a View Object is modified

There are cases where we need to show the modified VO rows in separate pop up window or highlight the modified/new rows .

I have a quiet interesting usecase in my development project and thought i would write about it . The usecase is tat an ADF Table in the ui page can  be modified and new rows can also be created.After modifying some of the rows the user has navigated to some other page without committing the modified data. At this point i need to stop the user and alert him saying that some of the VO rows have been modified.

This is a direct approach and can be done in two ways. Either in the managed bean or in the design time page using expressions.According to ADF new/modified rows will be in cache.

Download the Sample Application and run CatchModifiedRows.jspx page.

we will continues using the workspace which i have been using for the earlier post .
Lets start developing it. Create a jspx page CatchModifiedRows.jspx . Drag and drop EmployeesVO1 on to the page as ADF Table and keep the necessary columns you want.keep a button at the bottom of the ADF table to navigate to some other page.












Create Action Listener for the Next button and name the bean as ModifiedVOBean.java and method as chkmeth. Inside the method get the bindings for the EmployessVO1 .

        DCBindingContainer bindings2 =
            (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
              
        JUCtrlHierBinding obj = (JUCtrlHierBinding)bindings2.findCtrlBinding("EmployeesVO1");
        ViewObject targetVO = obj.getViewObject();


we can get the current status of the view object instance using a method isDirty().

Boolean b = targetVO.getApplicationModule().getTransaction().isDirty();

This gives Boolean value of True/False . If something is modified/created the value will be True else False.



Final piece of code is

    public void chkMeth(ActionEvent actionEvent) {
       
        DCBindingContainer bindings2 =
            (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
              
        JUCtrlHierBinding obj = (JUCtrlHierBinding)bindings2.findCtrlBinding("EmployeesVO1");
        ViewObject targetVO = obj.getViewObject();
        Boolean b = targetVO.getApplicationModule().getTransaction().isDirty();
       FacesMessage msg=null;
        Boolean ev = false;
        if(b!=ev){
        msg=new FacesMessage("Hold on ,you have unsaved data.pls commit before proceeding");
        FacesContext.getCurrentInstance().addMessage(null,msg);
        }

  }


The boolean result  can be evulated against True/False and based on it we can throw a message to the user . Now go to the page and change something in any of the column and click Next. you will get the message.Further  you can customize the implementation the way u want like information in the pop up dialog
and so on.



we can also able to get Entity level status from VO using this code

RowSetIterator rs1 =targetVO.createRowSetIterator(null);
while (rs1.hasNext()) {
  EmployeesVORowImpl r = rs1.next();
  Entity eo = r.getEntity(0);
byte currentState= eo.getEntityState();
if(currentState==EntityImpl.STATUS_NEW .......)
{
 ......
}
}

From design time we can access the row status using the following expression
Expression #{row.row.entities[0].entityState}

Getting the View object in MB and performing operations on it is not a good practice.