Content starts here XMLBeans Example Using a Metadata-rich Java Class
This page last changed on Apr 25, 2008.

XMLBeans Example Using a Metadata-rich Java Class

This topic shows an example of an XMLBeans-based Java function that can be imported into a dataspace project. Importing the Java function into a physical data service results in data service functions corresponding to the source Java functions.

The topic provides the following source listings for the example: 



Java Source 

The Java code in the following listing calculates the price of all the items in ORDER_LINE_ITEM to determine the total order amount.

Java Source Listing
package performUpdates;

import java.math.BigDecimal;
import java.util.List;
import java.util.Iterator;


import org.openuri.temp.sampleapp.customerorder.ELEC_ORDER;
import org.openuri.temp.sampleapp.customerorder.ELEC_ORDER.ITEMS;
import org.openuri.temp.sampleapp.customerorder.ELEC_ORDER.ITEMS.ORDER_LINE_ITEM;

public class ELECOrderUpdate {

   // Change the TotalOrderAmount based on the updated LINE ITEMS.
   // Input and return are the Data objects

   public static ELEC_ORDER[] updateOrders(ELEC_ORDER[] custOrders) {
      System.out.println("\n\n>>> ELECOrderUpdate updateTotalAmount started.");

      if (custOrders == null)
         return custOrders;

      int size = custOrders.length;
      for (int i=0; i<size; i++) {
         ELEC_ORDER order = (ELEC_ORDER)custOrders[i];
         BigDecimal subTotal = new BigDecimal(0);
         BigDecimal totalOrderAmount = new BigDecimal(0);
         BigDecimal saleTax = new BigDecimal(0);

         ITEMS items = order.getITEMS();
         List itemlist = items.getORDER_LINE_ITEM();
         Iterator item = itemlist.iterator();

         String maxID = "0";

         // Calculate the subTotal and totalOrderAmount
         while (item.hasNext()) {
            ORDER_LINE_ITEM lineitem = (ORDER_LINE_ITEM)item.next();
            BigDecimal quantity = new BigDecimal(Integer.toString(lineitem.getQUANTITY()));
            subTotal = subTotal.add(quantity.multiply(lineitem.getPRICE()));

            lineitem.setLINE_ID(maxID);
            maxID = Integer.toString(new Integer(maxID).intValue() + 1);
        }

        saleTax = subTotal.multiply(new BigDecimal(".06")).setScale(2,BigDecimal.ROUND_UP);
        totalOrderAmount = subTotal.add(saleTax).add(order.getHANDLING_CHARGE());

        order.setSUBTOTAL(subTotal);
        order.setTOTAL_ORDER_AMOUNT(totalOrderAmount);
        order.setSALE_TAX(saleTax);
     }

     System.out.println(">>> ELECOrderUpdate updateTotalAmount completed.\n\n");
     return custOrders;
   }
}

Schema Definition 

The schema used to create XMLBeans is shown below. It simply models the structure of the complex element; it could have been obtained by first introspecting the data directly.

Schema Definition for ELEC_ORDER
<_p_r_e_:schema targetNamespace="http://temp.openuri.org/SampleApp/CustomerOrder.xsd" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns="http://temp.openuri.org/SampleApp/CustomerOrder.xsd" xmlns:_p_r_e_="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:conv="http://www.openuri.org/2002/04/soap/conversation/" xmlns:cw="http://www.openuri.org/2002/04/wsdl/conversation/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:jms="http://www.openuri.org/2002/04/wsdl/jms/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:s0="http://www.openuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
  <s:element name="ELEC_ORDER">
    <s:complexType>
      <s:sequence>
        <s:element name="ORDER_ID" type="xsd:string" minOccurs="1"/>
        <s:element name="CUSTOMER_ID" type="xsd:string" minOccurs="1"/>
        <s:element name="ORDER_DATE" type="xsd:date" minOccurs="1"/>
        <s:element name="SHIPMENT_METHOD" type="xsd:string" minOccurs="1"/>
        <s:element name="HANDLING_CHARGE" type="xsd:decimal" minOccurs="1"/>
        <s:element name="SUBTOTAL" type="xsd:decimal" minOccurs="1"/>
        <s:element name="TOTAL_ORDER_AMOUNT" type="xsd:decimal" minOccurs="1"/>
        <s:element name="SALE_TAX" type="xsd:decimal" minOccurs="1"/>
        <s:element name="SHIP_TO" type="xsd:string" minOccurs="1"/>
        <s:element name="SHIP_TO_NAME" type="xsd:string" minOccurs="1"/>
        <s:element name="BILL_TO" type="xsd:string" minOccurs="1"/>
        <s:element name="ESTIMATED_SHIP_DATE" type="xsd:date" minOccurs="1"/>
        <s:element name="STATUS" type="xsd:string" minOccurs="1"/>
        <s:element name="TRACKING_NUMBER" type="xsd:string" minOccurs="0" nillable="true"/>
        <s:element name="ITEMS">
          <s:complexType>
            <s:sequence>
              <s:element name="ORDER_LINE_ITEM" minOccurs="0" maxOccurs="unbounded">
                <s:complexType>
                  <s:sequence>
                    <s:element name="LINE_ID" type="xsd:string" minOccurs="1"/>
                    <s:element name="ORDER_ID" type="xsd:string" minOccurs="1"/>
                    <s:element name="PROD_ID" type="xsd:string" minOccurs="1"/>
                    <s:element name="PROD_DESC" type="xsd:string" minOccurs="1"/>
                    <s:element name="QUANTITY" type="xsd:int" minOccurs="1"/>
                    <s:element name="PRICE" type="xsd:decimal" minOccurs="1"/>
                    <s:element name="STATUS" type="xsd:string" minOccurs="1"/>
                  </s:sequence>
                </s:complexType>
              </s:element>
            </s:sequence>
          </s:complexType>
        </s:element>
      </s:sequence>
    </s:complexType>
  </s:element>
</_p_r_e_:schema>

Data Service Function 

The following listing shows the generated data service function based on the imported Java code.

Generated Data Service Function
xquery version "1.0" encoding "UTF-8";

(::pragma xfl <x:xfl xmlns:x="urn:annotations.ld.bea.com">
<creationDate>2008-04-23T10:40:54</creationDate>
<javaFunction class="performUpdates.ELECOrderUpdate"/>
</x:xfl>::)

declare namespace f1 = "ld:UpdateOrder";

import schema namespace t1 = "http://temp.openuri.org/SampleApp/CustomerOrder.xsd" at "ld:schemas/CustomerOrders.xsd";

(::pragma function <f:function xmlns:f="urn:annotations.ld.bea.com" visibility="protected" kind="library" isPrimary="false" nativeName="updateOrders">

<params>
   <param nativeType="[Lorg.openuri.temp.sampleapp.customerorder.ELEC_ORDER;"/>
</params>
</f:function>::)

declare function f1:updateOrders($parameter1 as element(t1:ELEC_ORDER)*) as schema-element(t1:ELEC_ORDER)* external;
Document generated by Confluence on Apr 28, 2008 15:54