Samples Tutorial

     Previous  Next    Open TOC in new window    View as PDF - New Window  Get Adobe Reader - New Window
Content starts here

Performing Custom Data Manipulation Using Update Override

ALDSP permits customized updates through the use of the update override feature. The update override logic, which is triggered prior to submitting data, can be used for custom data manipulation, update overrides, logging, debugging, or other custom logic needs.

In this lesson, you will write an update override that computes total orders, based on the quantity and price of each order.

 


Objectives

After completing this lesson, you will be able to:

 


Overview

An update override, which you assign to a data service, performs custom logic prior to submitting data. The update override is a Java class that implements the com.bea.sdo.mediator.UpdateOverride class. Using that class's performChange (DataGraph graph) method, a Data Graph instance of the current data service is returned. The Data Graph can then be manipulated in using the update override logic.

For example, you can get the CustomerProfileDocument DataObject through the data graph

 	(CustomerProfileDocument) graph.getRootObject(); 

You could also get the Change Logging summary through graph.getChangeSummary()

On return of the Data Graph, the following conditions apply:

 


23.2 Creating an Update Override

An update override enables custom manipulation of data within data service.

Objectives

In this exercise, you will:

Instructions

  1. Create a new Java class by completing the following steps:
    1. Right-click the CustomerManagement folder, located in the DataServices folder.
    2. Choose New Arrow symbol Java Class.
    3. Enter CustomerProfileExit in the File Name field.
    4. Click Create.
  2. Build the DataServices project.
  3. Open the CustomerProfileExit.java file.
  4. Import and implement the update override, by completing the following steps:
    1. Import the update override by entering the following code:
    2. import com.bea.ld.dsmediator.update.UpdateOverride;
    3. Implement the update override by modifying the public class CustomerProfileExit code, as follows:
    4. public class CustomerProfileExit implements UpdateOverride
    5. Press Alt + Enter, and then click OK to add the performChange(DataGraph) signature.
    6. Implement the performChange(DataGraph graph) method by modifying the code to read as follows:
    7. public boolean performChange(DataGraph graph)

      The DataGraph passed in the argument contains the current SDO instance with all changes, including the change summary.

  5. Access the update override by casting the root object of the data graph to your SDO. Add the following code, after the opening braces:
  6. CustomerProfileDocument customerDocument = 
               (CustomerProfileDocument) graph.getRootObject();
  7. Press Alt+Enter. With this CustomerProfileDocument instance, you can get and set values that will be applied to the SDO before it is submitted.
  8. Write update logic to compute the total order amount, based on the sum of each order item's quantity multiplied by its price (sum of price*qty). You can use this to get the total of each item's quantity*price and to set the total order amount to this value.
  9. Note: Use BigDecimals for computations.

    For example:

    Order[] orders = customerDocument.getCustomerProfile().getCustomerArray(0).getOrders().getOrderArray();
                
    for (int x=0; x<orders.length; x++) {
    BigDecimal total = new BigDecimal(0);
    OrderLine[] items = orders[x].getOrderLineArray();
    for (int y=0; y < items.length; y++) {
    total = total.add(items[y].getQuantity().multiply(items[y].getPrice()));
    }
    orders[x].setTotalOrderAmount(total);
    }
  10. Press Alt + Enter, for all flagged items.
  11. Enter the code necessary to return the results. For example:
  12. System.out.println(">>> CustomerProfile.ds Exit completed");
    return true;
    }
    }
  13. Confirm that your code is as displayed in Figure 23-1.
  14. Build DataServices project.
  15. Figure 23-1 Update Override Code


    Update Override Code

 


23.3 Associating an Update Override to a Logical Data Service

Before you can use the update override, you must associate it with a specific data service.

Objectives

In this exercise you will:

Instructions

  1. Open the CustomerProfile data service in Design View.
  2. Click the CustomerProfile header to activate the Property Editor. (If the Property Editor is not open, press Alt + 6.)
  3. Click the update override class field.
  4. Navigate to the DataServices.jar\CustomerManagement folder.
  5. Select CustomerProfileExit.class and click Open. The update override class field is now populated with CustomerManagement.CustomerProfileExit.
  6. Build the DataServices project.

 


23.4 Testing the Update Override

As with any other data service, you should test the update override to ensure that it works properly.

Objectives

In this exercise you will:

Instructions

  1. Open CustomerPageFlowController.jpf, which is located in the CustomerManagementWebApp\CustomerPageFlow folder.
  2. Click the Start icon to open Workshop Test Browser.
  3. Enter CUSTOMER3 in the CUSTOMER ID field and click Submit.
  4. Note: It may take a few seconds before the information is returned.
  5. Change the order information by adding, modifying or deleting order lines.
  6. Click Submit All Changes.
  7. Click Back to return to the CUSTOMER ID page.
  8. Enter CUSTOMER3 in the CUSTOMER ID field and click Submit.
  9. Confirm if the updated total order information was computed.
  10. Update Override Reference Code 
       package CustomerManagement; 
       import com.bea.ld.dsmediator.update.UpdateOverride;
       import commonj.sdo.DataGraph;
       import java.math.BigDecimal;
       import org.openuri.temp.dataServices.schemas.customerProfile.CustomerProfileDocument;
       import org.openuri.temp.dataServices.schemas.customerProfile.CustomerProfileDocument.CustomerProfile.Customer.Orders.Order;
       import org.openuri.temp.dataServices.schemas.customerProfile.CustomerProfileDocument.CustomerProfile.Customer.Orders.Order.OrderLine;
       public class CustomerProfileExit implements UpdateOverride
       { 
        public boolean performChange(DataGraph graph)
        {
            CustomerProfileDocument customerDocument = (CustomerProfileDocument) graph.getRootObject();
            Order[] orders = customerDocument.getCustomerProfile().getCustomerArray(0).getOrders().getOrderArray();
                for (int x=0; x<orders.length; x++) {
                BigDecimal total = new BigDecimal(0);
                OrderLine[] items = orders[x].getOrderLineArray();
                for (int y=0; y < items.length; y++) {
                total = total.add(items[y].getQuantity().multiply(items[y].getPrice()));
                }
                orders[x].setTotalOrderAmount(total);
                }
            return true;
        }
          }

 


Lesson Summary

In this lesson, you learned how to:


  Back to Top       Previous  Next