Samples Tutorial

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

Updating Data Services Using Java

One of the features introduced with Data Services Platform (ALDSP) is the ability to write data back to the underlying data sources. This write service is built on top of the Service Data Object (SDO) specification, and provides the ability to update, insert, and delete results returned by a data service. It also provides the ability to submit all changes to the SDO (inserts, deletes, and updates) to the underlying data sources for persisting.

 


Objectives

After completing this lesson, you will be able to:

 


Overview

When you update, add, or delete from data service objects, all changes are logged in the SDO's change summary.

 


10.1 Modifying and Saving Changes to the Underlying Data Source

Although the steps in the next three exercises are different, the underlying principle is the same: When you update, add, or delete from data service objects, all changes are logged in the SDO's change summary. When the change is submitted, items indicated in the Change Summary log are applied in a transactionally-safe manner, and then persisted to the underlying data source. Changes to relational data sources are automatically applied, while changes to other data services, such as Web services and portals, are applied using a ALDSP update framework.

Objectives

In this exercise, you will:

Instructions

  1. Open the DataServiceClient.java file, located in the DataServiceClient project folder.
  2. Change the first and last name of CUSTOMER3 from Brett Pierce to Joe Smith, by using the set() methods of the Customer data object instance. You do this by adding the set() method to the //Show Customer Data section (new code is displayed in boldface type):
  3. Customer customer = doc[0].getCustomerProfile().getCustomerArray(0);
    customer.setLastName("Smith");
    customer.setFirstName("Joe");
    System.out.println("Customer Name: " + customer.getLastName() + 
    ", " + customer.getFirstName());
    Note: The Array of function has been deprecated. Ensure that you modify doc.getCustomerProfile().getCustomerArray(0) to doc[0].getCustomerProfile().getCustomerArray(0):
    Figure 10-1 Set() Method Specified


    Set() Method Specified

  4. Save your work.
  5. Right-click the DataServiceClient project folder and choose Build DataServiceClient.
  6. Click the DataServiceClient.java file's Start icon (or press Ctrl + F5).
  7. Confirm that the changes were submitted, by viewing the results in the Output window. (If the window is not open, choose View Arrow symbol Windows Arrow symbol Output.)
  8. Note: At this point, the changes only exist as entries in the SDO Change Summary Log, not in the data source. You must complete the remaining steps in this exercise to ensure that the underlying data source is updated.
    Figure 10-2 Change Results in Output Window


    Change Results in Output Window

  9. Invoke the Mediator API's submit() method and save the changes to the data source, by using the data service instance. The submit() method takes two parameters: the document to submit and the data service name. You do this by adding the following code into the //Show Customer Data section of the file:
  10. ds.submit(doc);
  11. Change the output code, as follows:
  12. System.out.println("Change Submitted");
    Figure 10-3 submit() and Output Method Specified


    submit() and Output Method Specified

  13. Click the DataServiceClient.java file's Start icon (or press Ctrl + F5).
  14. Open DataServices\CustomerManagement\CustomerProfile.ds in Test View.
  15. Select the getCustomerProfile(CustomerID) function.
  16. Enter CUSTOMER3 in the xs:string CustomerID field.
  17. Click Execute. The results should show the customer name as Joe Smith.

 


10.2 Inserting New Data to the Underlying Data Source Using Java

You can use the Mediator API to add new information to the underlying data source, thereby reducing the need to know a variety of data source APIs.

Objectives

In this exercise, you will:

Instructions

  1. In WebLogic Workshop open the DataServiceClient.java file.
  2. Add a new item to ORDER_3_0 (the first order placed by CUSTOMER3), by using the addNewOrderLine() method of the Order Item data object instance. You do this by inserting the following code into the //Show Customer Data section, after System.out.println("Change Submitted"):
  3.  // Get the order
           Order myorder = customer.getOrders().getOrderArray(0);
    	// Create a new order item
           OrderLine newitem = myorder.addNewOrderLine();
  4. Set the values of the new order item, including values for all required columns. (You can check the physical or logical .xsd file to determine what elements are required.) All foreign keys must be valid; therefore, use APPA_GL_3 as the Product ID.
  5. You do not need to setOrderID(); the SDO update will automatically set the foreign key to match its parent because the item will be added as a child of ORDER_3_0.

    To set the values, insert the following code above the //Show Order Data section of the Java file:

    // Fill the values of the new order item	
           newitem.setLineId("8");
           newitem.setProductId("APPA_GL_3");
           newitem.setProduct("Shirt");
           newitem.setQuantity(new BigDecimal(10));
           newitem.setPrice(new BigDecimal(10));
           newitem.setStatus("OPEN");
  6. Press Alt + Enter to enable java.math.BigDecimal.
  7. Invoke the Mediator API's submit method and save the changes to the data source, by using the data service instance. (The submit() method takes: the document to submit as a parameter)
  8. You do this by inserting the following code before the //Show Order Data section of the java file:

    // Submit new order item
           ds.submit(doc, "ld:DataServices/CustomerManagement/CustomerProfile.ds");
           System.out.println("Change Submitted");
  9. Comment out the code where customer first name and last name were set, including call to submit method
  10. Confirm that the //Show Customer Data section of your java file is as displayed in Figure 10-4.
  11. Figure 10-4 xJava Code to Add Line Item


    xJava Code to Add Line Item

  12. Open DataServices\CustomerManagement\ CustomerProfile.ds in TestView.
  13. Enter CUSTOMER3 in the xs:string CustomerID field.
  14. Click Execute. The result should contain the new order information.

 


10.3 Deleting Data from the Underlying Data Source Using Java

You can use the Mediator API to delete information to the underlying data source, thereby reducing the need to know a variety of data source APIs.

Objectives

In this exercise, you will:

Instructions

  1. In Workshop Test Browser, determine the new item's placement in the array and subtract 1. For example, if line item with line_id = 8 is the fifth item for ORDER_3_0, its order placement is 4.
  2. Close Workshop Test Browser.
  3. In the DataServicesClient.java file delete or comment out the code that added a new order line item.
  4. Add an instance of the item that you want to delete, by inserting the following code file:
  5. // Get the order item
         OrderLine myItem = customer.getOrders().getOrderArray(0).getOrderLineArray(4);
    Note: The getOrderLineArray() is based on the item's placement in the array. In this case, 8 is the fifth item, making the variable 4. You should use the variable that is correct for your situation.
  6. Call the delete method by inserting the following code:
  7. // Delete the order item
    myItem.delete();
  8. Submit the changes, using the Mediator API's submit() method.
  9. // Submit delete order item 
    " ds.submit(doc);
        System.out.println("Change Submitted");
  10. Confirm that the code is as displayed in Figure 10-5.
  11. Figure 10-5 Java Code to Delete Line Item


    Java Code to Delete Line Item

  12. Build the DataServiceClient project.
  13. Click the DataServiceClient.java file's Start icon (or press Ctrl + F5) to run the program.
  14. Confirm that the changes persisted to the underlying data source by completing the following steps:
    1. Click the CustomerPageFlowController.jpf application's Start icon (or press Ctrl+F5) to open the Workshop Test Browser.
    2. In the Workshop Test Browser, enter CUSTOMER3 in the Customer ID field and click Submit.
    3. Find ORDER_3_0 and verify that Line 8 is no longer present.
    4. Close the Workshop Test Browser

 


Lesson Summary

In this lesson, you learned how to:


  Back to Top       Previous  Next