Home » General

Oracle ADF Basics: Duplicating an existing row

Written By: Pascal Alma on April 9, 2008 One Comment

I don't know if it is just a coincidence but at almost all the Oracle ADF projects (ADF Business Components + Struts or JSF) I have done until now there was a need for additional functionality with which the user could duplicate a row and then modify it. After reinventing this thing several times, I now decided to put it on my blog, so I can use it on my future projects.
For this example, I use the TUHRA application that is introduced by the book 'Oracle JDeveloper 10g for Forms & PL/SQL Developers' (see my blog here). But I think you will find the example very clear, so you can easily use it at your situation.

In this application there is an employees overview screen. Now imagine you want to be able to select an employee, copy that row and open the edit screen, so you can modify some of the values of the newly created row.
How do we start? First we create the business service to create the new row. Second we bind this action to the page, by adding it to the page definition of the page where we want to execute the function. Then we add a new button to the page that calls a method in the backing bean of the page. Finally we implement this method in the backing bean. Now this might seem complicated when you are starting with Oracle ADF, but you will see, it is actually quite simple to do this with Oracle ADF and JDeveloper.

  1. Create the business function
  2. Since we want to copy an employee record, we will create the business function in the view object for the employees, in this case 'AllEmployees'. If you select this view object you will see there is a 'AllEmployeesImpl' class. But before we add the code, I first open up the view object editor and select the tick box to have a Java class created for the AllEmployeesRowImpl.

    AllEmployees VO

    This makes it easier to create the business function later, as we will see.
    I then open up the source for the 'AllEmployeesImpl' class and add the code necessary to create a new row and copy the values of the selected row to it. Here is the code:

    JAVA:
    1. public void copyRow()
    2.   {
    3.     AllEmployeesRowImpl curRow = (AllEmployeesRowImpl)this.getCurrentRow();
    4.     AllEmployeesRowImpl newRow = (AllEmployeesRowImpl)this.createRow();
    5.    
    6.     newRow.setEmployeeId( new DBSequence( BigDecimal.valueOf(-1)));
    7.     newRow.setCommissionPct( curRow.getCommissionPct());
    8.     newRow.setDepartmentId( curRow.getDepartmentId());
    9.     newRow.setEmail(curRow.getEmail()+ "abc");
    10.     newRow.setFirstName(curRow.getFirstName() );
    11.     newRow.setLastName(curRow.getLastName());
    12.     newRow.setManagerId( curRow.getManagerId());
    13.     newRow.setHireDate(curRow.getHireDate());
    14.     newRow.setJobId(curRow.getJobId());
    15.  
    16.     this.insertRow(newRow);
    17.   }

    Two remarks here:
    1. As you can see I am using the AllEmployeesRowImpl class. This class is available because I have set tick box in the beginning of this step.
    2. The employeeId is now always set to -1, you will need to something more fancy here (see this post for example).
    After we have added this method and it compiles, we have to do one more thing and that is to make the method visible for the view-controller layer. We do this, by adding the method to the 'client interface' in the view object editor.AllEmpoyeesVO Client Interface

  3. Bind this action to the page
  4. To bind our new business method to the page, we make use of the power of JDeveloper. Just open up the JSF page to which you want to add the copy functionality. In the Data Control Palette, you see, in the branch for the 'AllEmployees' view object a new item 'copyRow()'. you can drag-and-drop it as a button on your page. Automagically JDeveloper will add the necessary binding to the page definition file. It will add something like this to it:

    XML:
    1. <methodAction id="copyRow"
    2.                   InstanceName="TuhraServiceDataControl.AllEmployees"
    3.                   DataControl="TuhraServiceDataControl" MethodName="copyRow"
    4.                   RequiresUpdateModel="true" Action="999"
    5.                   IsViewObjectMethod="true"/>

    Now we can do two things:
    1. you can set the action of the new button to "editEmp". This will result in a navigation to the Edit Employee page after our copy function has been executed. But this way you can not add some custom code if necessary.
    2. You can create a new button that is bound to the backing bean of the page and remove the button you just created. This step is described in the next part.

  5. implement the method in the backing bean
  6. To have more control about what is happening when we click at our copy button, we add a new button that we bind to an action in our backing bean. Just drag and drop a 'CommandButton' from the JSF Core library from the Component Palette on our page. Double click on it and bind it to a new action in the backing bean like this:
    BackingBean copyRow
    By the way, you can remove the button that is created in the former step. We are only interested in the binding that was created in that step and this binding will still exist if you delete the button from the page.
    In the backing bean 'searchEmployees' we can now implement the code for the button like this:

    JAVA:
    1. public String copyRow()
    2.   {
    3.      LOG.info("copyRow()");
    4.  
    5.      BindingContainer bindings = getBindings();
    6.      OperationBinding operationBinding =
    7.        bindings.getOperationBinding("copyRow");
    8.      Object result = operationBinding.execute();
    9.  
    10.      operationBinding =
    11.            bindings.getOperationBinding("Commit");
    12.      result = operationBinding.execute();
    13.  
    14.     return "editEmp";
    15.   }

    As you can see I have added a call to the commit action as an example. By returning the string "editEmp" I make sure we navigate to the Edit Employee page.

Although this post is about the basic usage of Oracle ADF, I hope you might find something useful in it :-)

Tags:

Digg this!Add to del.icio.us!Stumble this!Add to Techorati!Share on Facebook!Seed Newsvine!Reddit!Add to Yahoo!

One Response to “Oracle ADF Basics: Duplicating an existing row”

  1. Pascal’s Blog » ADF Basic: Partial Page Rendering with input and outputText said:

    [...] In the first input field the user will enter an ItemCode. When the user leaves that field, the ItemCode is checked against the database and only if one item is found for the entered code, the output field is updated to reflect the ItemDescription. Next the user can enter a quantity and tab to the button and give enter to create the orderline. The first issue we have to deal with, is to trigger when the value of the ItemCode has changed, so we can check the value against the database when the field is left. To do this, we register a ValueChangeListener at the ItemCode field, like this: Next thing we must be able to do, is to update the output text of the ItemDescription. To prepare this, we bind the field itself (not just its value) to the backing bean. We can do this like this: To check if the entered Item Code is valid and to get the ItemDescription, I use a method which I created in the Application Module (to see how you can bind a method of the Application Module to the page and execute it in the backing bean, see my earlier post here). [...]

Copyright © 2009 Pascal’s Blog, All rights reserved.| Powered by WordPress