Adding a custom method in an ADF BC Service Interface – update of a single attribute in a selected row

3

The article “Quickly creating, deploying and testing a WebService interface for ADF Business Components” (http://technology.amis.nl/blog/9726/quickly-creating-reploying-and-testing-a-webservice-interface-for-adf-business-components) that I recently published describes a way of very rapidly creating the HRService Web Service – an ADF BC driven WebService on top of the EmployeesView ViewObject that exposes the EMPLOYEES table in the HR schema through the Employee EntityObject. However, that article’s true purpose is to show how to create the deployment profile and deploy and test this service, either on the integrated WLS or on a standalone WebLogic Server, in the easiest way possible. This article is an extension of the previous one: it demonstrates how to extend the Service Interface with a custom method (or an operation in terms of WebServices).

As it happens, I required an operation for updating just the salary of an Employee. I will show how I create a custom method in the Application Module’s implementation class, how I added this method to the Client Interface of the Application Module and subsequently to the Service Interface. After redeploying – using the same deployment profile that was created previously – we can test the updateSalary method as a new operation in the WebService.


Open the Application Module editor. Go to the Java tab and click on the edit (pencil) icon. Check the checkbox to Generate Application Module Class HRServiceImpl.
001-generateAppModImpl

Then press OK.

Click on the hyperlink for HRServiceImpl. The java editor opens for class HRServiceImpl.

Implement method updateSalary. The method takes two integers – the employee identifier and the newly awarded salary – and returns a String as result.

002-implementUpdateSalary

The code will first leverage the EmployeesView1 ViewObject to get hold of the employee’s Row for the employeeId (the primary key for this ViewObject). When found, the Salary attribute is updated, and the result string is composed.

    public String updateSalary(Integer employeeId, Integer newSalary) {
        String result="";
            Row[] rows =
                getEmployeesView1().findByKey(new Key(new Object[] { employeeId }),
                                              1);
            if (rows != null && rows.length > 0) {
                Row row = rows[0];
                row.setAttribute("Salary", newSalary);
                result =
                        "Updated Salary for " + row.getAttribute("FirstName") +
                        " " + row.getAttribute("LastName") + " to " +
                        newSalary;
            }
      return result;
    }

With the method now available in the ApplicationModuleImpl class, we can add it to the Client Interface.

Back in the Java tab for the Application Module, click on the edit (pencil) icon to edit the Client Interface:L

003-editClientInterface

In the Client Interface editor, select the new method updateSalary to add to the Client Interface.

004-addUpdateSalaryMethod

Click on OK.

With the Client Interface appropriately extended, we can now edit the Service Interface likewise.

Go to the Service Interface tab for the Application Module. Click on the icon to edit the Custom Methods for the Service interface.

005-editServiceInterface

Select the updateSalary method. Then press OK.

006-addUpdateSalaryToServiceInterface

The definition of the Service Interface for Application Module HRService is now extended with the updateSalary salary, that will be exposed as Operation updateSalary in the Web Service.

007-serviceInterface

Redeploy the application using the application deployment profile discussed in the prequel to this article.

Then open your browser to test the web service, in my case at: http://localhost:8001/HRDataServices/HRService.

008-testUpdateSalaryServiceOperation

Select operation updateSalary that is now available for the first time. Specify employee identifier and new salary. Then press the Invoke button.

The response from the WebService appears:

009-responseAfterUpdateSalary

When we check the database, we will find that the salary has indeed been updated:

010-verifyInDatabase

Through Custom Methods, it becomes very easy to provide dedicated, fine grained operations (or composite, very coarse grained operations)  in the WebServices published for our Application Modules.

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

3 Comments

  1. Hi Lucas, that’s a nice post. I’m wondering if I can use a custom method in the appModule using a ViewObjectNameImpl and calling the createRow and the insertRow method of the view.
    Thanks in advance.

  2. Great post. Thanks.
     
    Now what if I need to expose a method in AMImpl which has let’s say a complex type, for example a DTO or an VO itself with limited number of attributes ?

  3. Hey I put my code in the impl but the function name does not appear in the list from i can shuttle it, Any suggestions I am trying to do it declaratively and trying to avoid coding it in

Leave a Reply