IBM WebSphere Developer Technical Journal: Accelerated JSF development for XML-based SOA using Rational Application Developer and WebSphere Application Server -- Part 3

Manipulating SDO instances

This is Part 3 in a series that presents a solution that accelerates presentation development of XML-based Service Oriented Architecture (SOA) applications. This solution includes an Eclipse feature that enables the generation of statically typed Service Data Objects (SDO) for an XSD Schema, and provides a runtime framework for using SDOs in the transformation of presentation element data to and from XML data. Part 3 highlights common techniques for manipulating SDO instances.

Share:

Narinder Makin, Software Architect, IBM Rational SOA tools, IBM

Narinder Makin is a software engineer at IBM Research Triangle Part Lab in Durham, North Carolina. He is a developer of J2EE and SOA/SDO tools for the Rational Application Developer team.



23 February 2005

Introduction

This five-part article series presents a solution that accelerates presentation development of XML-based SOA applications, and includes an Eclipse feature that enables the generation of statically typed service data objects (SDO) for an XSD Schema, and a runtime framework for the transformation of presentation element data to/from XML data using SDOs. In this series:

  • Part 1 walked through the development of a simple JavaServer™ Faces (JSF) application using the supplied plug-in set that communicates to a service per the specified XML schema. The application scenario we used makes one service invocation that sends one XML request and receives one XML response, and illustrates the master-detail view of data objects along with pagination and sorting capabilities.
  • Part 2 enhanced the solution from Part 1 to include create, update, and delete functions, plus customizations to the generated SDOs with local variable addition and basic transformations. We illustrated a scenario of multiple schema models, multiple requests/responses for a single page, and showed the JSF dropdown control binding for XML data.
  • In Part 3, we will provide tips for XML SDO usage in IBM® Rational® Application Developer V6 and for using aggregate methods, such as sum, mean, and others, that are provided with the transformation feature.

IBM Rational Application Developer V6 (hereafter referred to as Application Developer) and IBM Websphere® Application Server V6 are required to take advantage of this transformation feature.


Installing the XSD SDO Transform feature

To install the XSD SDO Transform feature, follow the steps detailed in Part 1.


XSD SDO Transform feature contents

The XSD SDO Transform feature contains artifacts for generating the SDO package from an XSD schema, and framework components to transform an SDO package instance to/from an XML instance document:

  1. Create SDO Package Action
    This action on the XSD resources generates the SDO package from an XSD schema (Figure 1).
    Figure 1. Create SDO Package
    Create SDO Package
  2. dw.ibm.etools.xsd.sdo.xmltransformservice.XMLTransformServiceFactory
    This class provides the APIs to transform an SDO package instance to/from XML instance document.
  3. dw.ibm.etools.xsd.sdo.xmltransforms.util.ObjectUtil
    This class provides aggregate and helper methods that operate on the SDO objects.

Import project interchange

Import the project interchange xyzinsurancetestclient_pi.zipdownload file into the Application Developer workspace. As a result, the XYZInsuranceTestClient project is imported (Figure 2). This Java™ project contains a unit test Java class with all the methods described below. The SDO package in included in this Java project for simplicity, and the SDO runtime is present in its classpath. You can choose to reference the Web project that contains the SDO package, or create this Java class within the project that contains the SDO package. The supplied Eclipse Modeling Framework (EMF) JAR file, emf.jar, or the equivalent JAR files from the EMF Web Site (ecore.sdo.jar, commonj.sdo.jar, ecore.xmi.jar, ecore.jar, common.jar), should be added to the project's classpath and the build path. The xsdsdotransform.jar file is also added to the project.

Later, we will describe using the SDO generator feature shipped with Application Developer to create the SDO package from an XSD in a standalone Java project.

Figure 2. Imported project interchange
Imported project interchange

Analyzing the SDOs

In this section, we will analyze the Java client that shows the APIs used to manipulate the generated SDO package. We will also look at other API usage on the XMLTransformServiceFactory, as well as the aggregate methods provided with a utility class, ObjectUtil.

As illustrated in the previous articles, the SDO package name for an XSD is determined from the targetNamespace declaration of the schema definition (Figure 3). The generated SDO package contains a DocumentRoot class that acts as a root container for all the types that are defined in the schema (Figure 4). A Java class is generated for every type that is defined in the schema. This generated Java class contains all the elements and attributes (as Java types) that are defined for that XSD type, a list for all multi-valued elements, and a reference for any single valued element.

Figure 3. TargetNamespace definition
TargetNamespace definition
Figure 4. Generated SDO java package
Generated SDO java package

The classes that are generated for the XSD schema types are initialized in the package registry, such that the XSD types to and from the Java types can can be resolved when transformation takes place. In the case of a large schema that generates a large number of classes, the initialization code is performed by loading an EMF resource. In case of smaller schema (determined based on the computation of total number of classes, attributes, and so on), the initialization code is generated within the Package class itself.

The create<Element Name> methods are also generated for all the XSD reference types in the Java class. These APIs can be used to create an instance of any of the generated SDO Java types. As shown in Listing 1, an instance of brokerType is created and added to the list contained in the instance of the BrokerServiceType SDO.

Listing 1

public void TestCreateReference(){
	DocumentRoot root = DocumentRoot)XMLTransformServiceFactory.INSTANCE.create
		(BrokerservicePackage.eINSTANCE.getNsURI());
	root.getXMLNSPrefixMap().put("", BrokerservicePackage.eINSTANCE.getNsURI());
	xyz.brokerservice.BrokerServiceType bs = root.createBrokerService();
	BrokerType bType  = bs.createBroker();
	bType.setBrokerId("111");
	bType.setFirstName("Joe");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)root));
}

The generated package class contains a method for creating or accessing the Java types for all the types and attributes (Listing 2).

Listing 2

public void TestTypeCreation(){
	EClass brokerType = BrokerservicePackage.eINSTANCE.getBrokerType();
	System.out.println("Type Name: " + brokerType.getName());
}

The generated factory class provides APIs for creating instances of any generated SDO Java type. As seen in Listing 3, to create an instance of the brokerType, you can use the createBrokerType API. This newly created instance is not added to any list or any reference, as is the related API shown in Listing 1. The setBroker API is used to set the newly created instance in the brokerServiceType. Based on the requirement, you may use either instance creation APIs described here.

Listing 3

public void TestInstanceCreation(){
	DocumentRoot root = DocumentRoot)XMLTransformServiceFactory.INSTANCE.create
		(BrokerservicePackage.eINSTANCE.getNsURI());
	root.getXMLNSPrefixMap().put("", BrokerservicePackage.eINSTANCE.getNsURI());
	xyz.brokerservice.BrokerServiceType bs = root.createBrokerService();
	BrokerType bType  = BrokerserviceFactory.eINSTANCE.createBrokerType();	bs.setBroker(bType);
	bType.setBrokerId("111");
	bType.setFirstName("Joe");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)root));
}

Creating an XML request

In the previous articles, we saw how the XMLTransformFactory load and convert APIs were used to serialize a graph of SDO objects to and from an XML stream. There are also situations when you would need to create a new instance of an SDO object (or a graph of SDO objects), and serialize that into XML. The create API in XMLTransformFactory can be used for this, as shown in Listing 4a. The output of the method is shown in Listing 4b.

Listing 4a. Method

public void TestNewDocumentCreation(){
	DocumentRoot root = DocumentRoot)XMLTransformServiceFactory.INSTANCE.create
		(BrokerservicePackage.eINSTANCE.getNsURI());
	xyz.brokerservice.BrokerServiceType bs = root.createBrokerService();
	BrokerType bType  = bs.createBroker();
	bType.setBrokerId("111");
	bType.setFirstName("Joe");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)root));
}

Listing 4b. Output

<?xml version="1.0" encoding="ASCII"?>
<brokerservice:brokerService
xmlns:brokerservice="http:///xyz.brokerservice.ecore">
<brokerservice:broker brokerId="111">
<brokerservice:firstName>Joe</brokerservice:firstName>
</brokerservice:broker>
</brokerservice:brokerService>

Changing the namespace for the serialized XML

The serialized XML in the above example contains the fully qualified elements with a new namespace declaration. If the back end system that consumes this generated XML is not capable of handling such elements, the serializer can be configured to generate a default namespace declaration. Though not recommended, this can be desirable for specific situations. The method (Listing 5a) and output (Listing 5b) are shown below.

Listing 5a. Method

public void TestNamespaceRemove(){
	DocumentRoot root = DocumentRoot)XMLTransformServiceFactory.INSTANCE.create
		(BrokerservicePackage.eINSTANCE.getNsURI());
	 root.getXMLNSPrefixMap().put("", BrokerservicePackage.eINSTANCE.getNsURI());
	xyz.brokerservice.BrokerServiceType bs = root.createBrokerService();
	BrokerType bType  = bs.createBroker();
	bType.setBrokerId("111");
	bType.setFirstName("Joe");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)root));
}

Listing 5b. Output

<?xml version="1.0" encoding="ASCII"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore">
  <broker brokerId="111">
    <firstName>Joe</firstName>
  </broker>
</brokerService>

Handling schema changes

Let's consider a scenario where the service generating the XML response is upgraded to comply to a new schema definition (BrokerServiceV2.xsd, included in the xsd_sdo_soa_part3_listings.zipdownload file), and that schema definition contains more elements or attributes than the one used for generating the SDO package. The serializer used by the XMLTransformServiceFactory is preconfigured to record any new elements and attributes that were not part of original schema. The "Version 2" XML response (BrokerDetailResponseV2.xml, included in the xsd_sdo_soa_part3_listings.zipdownload file) can be loaded in to an SDO package generated with a "Version 1" schema. If the package is serialized to XML, it will contain the optional elements that were recorded on load, as shown in Figure 5.

Figure 5. BrokerServiceV1.xsd
BrokerServiceV1.xsd

Listing 6. BrokerDetailResponseV1.xml

Click to see code listing

<?xml version="1.0" encoding="UTF-8"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<broker brokerId="000-00-9999">
	<firstName>Narinder</firstName>
	<lastName>Makin</lastName>
	<client clientId="001-00-9999">
		<firstName>Dan</firstName>
		<lastName>Moore</lastName>
		<dateOfBirth>1967-08-13</dateOfBirth>
		<currentAddress>
			<street>113 Oak Pine St.</street>
			<city>Santa Clara</city>
			<state>LA</state>
			<zip>91929</zip>
			<country>US</country>
		</currentAddress>
		<permanentAddress>
			<street>123 Demi Lane</street>
			<city>Cary</city>
			<state>NC</state>
			<zip>22999</zip>
			<country>US</country>
		</permanentAddress>
		<policy policyId="L000000000">
			<policyName>Life</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>100000.00</policyAmount>
			<policyDescription>Life Insurance policy includes any accidental damages.</policyDescription>
		</policy>
		<policy policyId="H000000000">
			<policyName>House</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>50000.00</policyAmount>
			<policyDescription>Home Insurance</policyDescription>
		</policy>
		<policy policyId="C000000001">
			<policyName>Car 1</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>15000.00</policyAmount>
			<policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription>
		</policy>
		<policy policyId="C000000002">
			<policyName>Car 2</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>5000.00</policyAmount>
			<policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription>
		</policy>
		<policy policyId="B000000002">
			<policyName>Restaurant</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>25000.00</policyAmount>
			<policyDescription>Business Insurance - Restaurant</policyDescription>
		</policy>
	</client>
	<client clientId="002-00-9999">
		<firstName>Tom</firstName>
		<lastName>Cross</lastName>
		<dateOfBirth>1970-11-11</dateOfBirth>
		<currentAddress>
			<street>113 Duke St.</street>
			<city>Shelton</city>
			<state>CT</state>
			<zip>08989</zip>
			<country>US</country>
		</currentAddress>
		<permanentAddress>
			<street>123 Lex Lane</street>
			<city>Fairfield</city>
			<state>NY</state>
			<zip>09833</zip>
			<country>US</country>
		</permanentAddress>
		<policy policyId="H100000000">
			<policyName>House</policyName>	
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>2000.00</policyAmount>
			<policyDescription>Home Insurance</policyDescription>
		</policy>
		<policy policyId="L100000000">
			<policyName>Life</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>100000.00</policyAmount>
			<policyDescription>Life Insurance</policyDescription>
		</policy>
		<policy policyId="C100000001">
			<policyName>Car 1</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>2000.00</policyAmount>
			<policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
		</policy>
	</client>
</broker>
</brokerService>

Continuing with our scenario, to serve the secured service requests for another client, a new optional attribute, securityToken, and a new optional element, ssn, are added to the clientType definition in "Version 2" of brokerservice. A requirement dictates that no modifications should be required in existing client applications for which the SDO packages were generated using the older XSDs. The XMLTransformFactory is configured so that any unknown elements present in the XML stream will be recorded in the SDO instances. When these instances are serialized, these unknown recorded elements (ssn and securityToken) will be serialized as well.

Figure 6. BrokerServiceV2.xsd Modifications
BrokerServiceV2.xsd Modifications

Listing 7. BrokerDetailResponseV2.xml

Click to see code listing

<?xml version="1.0" encoding="UTF-8"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<broker brokerId="000-00-9999">
	<firstName>Narinder</firstName>
	<lastName>Makin</lastName>
	<client clientId="001-00-9999" securityToken="abcded">
		<firstName>Dan</firstName>
		<lastName>Moore</lastName>
		<dateOfBirth>1967-08-13</dateOfBirth>
		<currentAddress>
			<street>113 Oak Pine St.</street>
			<city>Santa Clara</city>
			<state>LA</state>
			<zip>91929</zip>
			<country>US</country>
		</currentAddress>
		<permanentAddress>
			<street>123 Demi Lane</street>
			<city>Cary</city>
			<state>NC</state>
			<zip>22999</zip>
			<country>US</country>
		</permanentAddress>
		<policy policyId="L000000000">
			<policyName>Life</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>100000.00</policyAmount>
			<policyDescription>Life Insurance policy includes any accidental damages.</policyDescription>
		</policy>
		<policy policyId="H000000000">
			<policyName>House</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>50000.00</policyAmount>
			<policyDescription>Home Insurance</policyDescription>
		</policy>
		<policy policyId="C000000001">
			<policyName>Car 1</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>15000.00</policyAmount>
			<policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription>
		</policy>
		<policy policyId="C000000002">
			<policyName>Car 2</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>5000.00</policyAmount>
			<policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription>
		</policy>
		<policy policyId="B000000002">
			<policyName>Restaurant</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>25000.00</policyAmount>
			<policyDescription>Business Insurance - Restaurant</policyDescription>
		</policy>
		<ssn>1234567</ssn>
	</client>
	<client clientId="002-00-9999"  securityToken="xyz">
		<firstName>Tom</firstName>
		<lastName>Cross</lastName>
		<dateOfBirth>1970-11-11</dateOfBirth>
		<currentAddress>
			<street>113 Duke St.</street>
			<city>Shelton</city>
			<state>CT</state>
			<zip>08989</zip>
			<country>US</country>
		</currentAddress>
		<permanentAddress>
			<street>123 Lex Lane</street>
			<city>Fairfield</city>
			<state>NY</state>
			<zip>09833</zip>
			<country>US</country>
		</permanentAddress>
		<policy policyId="H100000000">
			<policyName>House</policyName>	
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>2000.00</policyAmount>
			<policyDescription>Home Insurance</policyDescription>
		</policy>
		<policy policyId="L100000000">
			<policyName>Life</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>100000.00</policyAmount>
			<policyDescription>Life Insurance</policyDescription>
		</policy>
		<policy policyId="C100000001">
			<policyName>Car 1</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>2000.00</policyAmount>
			<policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
		</policy>
		<ssn>12345678</ssn>
	</client>
</broker>
</brokerService>

In the code in Listing 8, BrokerDetailResponseV2.xml is loaded and firstName is modified. The dataobject is then serialized back to XML. The serialized XML (Listing 9) contains the modification, along with the optional elements ssn and securityToken that were recorded during the load.

Listing 8

public void TestSchemaChange()
{
xyz.brokerservice.DocumentRoot loaded = ( xyz.brokerservice.DocumentRoot)
	XMLTransformServiceFactory.INSTANCE.loadFile("./input/brokerDetailResponseV2.xml");
xyz.brokerservice.BrokerServiceType bs = loaded.getBrokerService();
bs.getBroker().setFirstName("NEW NAME");
System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)loaded))     
}

Listing 9

<?xml version="1.0" encoding="UTF-8"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore">
  <broker brokerId="000-00-9999">
    <firstName>NEW NAME</firstName>
    <lastName>Makin</lastName>
    <client clientId="001-00-9999" securityToken="abcded">
      <firstName>Dan</firstName>
      <lastName>Moore</lastName>
      <dateOfBirth>1967-08-13</dateOfBirth>
      <currentAddress>
        <street>113 Oak Pine St.</street>
        <city>Santa Clara</city>
        <state>LA</state>
        <zip>91929</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Demi Lane</street>
        <city>Cary</city>
        <state>NC</state>
        <zip>22999</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="L000000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance policy includes any accidental damages.</policyDescription>
      </policy>
      <policy policyId="H000000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>50000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="C000000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>15000.00</policyAmount>
        <policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription>
      </policy>
      <policy policyId="C000000002">
        <policyName>Car 2</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>5000.00</policyAmount>
        <policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription>
      </policy>
      <policy policyId="B000000002">
        <policyName>Restaurant</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>25000.00</policyAmount>
        <policyDescription>Business Insurance - Restaurant</policyDescription>
      </policy>
      <ssn>1234567</ssn>
    </client>
    <client clientId="002-00-9999" securityToken="xyz">
      <firstName>Tom</firstName>
      <lastName>Cross</lastName>
      <dateOfBirth>1970-11-11</dateOfBirth>
      <currentAddress>
        <street>113 Duke St.</street>
        <city>Shelton</city>
        <state>CT</state>
        <zip>08989</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Lex Lane</street>
        <city>Fairfield</city>
        <state>NY</state>
        <zip>09833</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="H100000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="L100000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance</policyDescription>
      </policy>
      <policy policyId="C100000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
      </policy>
      <ssn>12345678</ssn>
    </client>
  </broker>
</brokerService>

The above behavior may not be required, and so can be turned off by modifying the serializer options shown in Listing 10a, which uses the getOptions API. The serializer throws an exception for the elements that are in the XML being loaded but were not part of the original schema used to generate the package. The XML output is shown in Listing 10b.

Listing 10a. Method

Click to see code listing

public void TestRestrictSchemaChange()
{
	try
	{
		XMLTransformServiceFactory.INSTANCE.getOptions().put
			(org.eclipse.emf.ecore.xmi.XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.FALSE);
	        xyz.brokerservice.DocumentRoot loaded = ( xyz.brokerservice.DocumentRoot)
			XMLTransformServiceFactory.INSTANCE.loadFile("./DataFile/brokerDetailResponseV2.xml");
	        xyz.brokerservice.BrokerServiceType bs = loaded.getBrokerService();
		bs.getBroker().setFirstName("NEW NAME");
		System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)loaded));
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
}

Listing 10b. Output

org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'securityToken' not found. (root.xml, 6, 56)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.reportUnknownFeature(XMLHandler.java:1168)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.handleUnknownFeature(XMLHandler.java:1138)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.setAttribValue(XMLHandler.java:1780)
	at org.eclipse.emf.ecore.xmi.impl.SAXXMLHandler.handleObjectAttribs(SAXXMLHandler.java:145)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectFromFactory(XMLHandler.java:1346)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectFromFeatureType(XMLHandler.java:1318)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObject(XMLHandler.java:1213)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.handleFeature(XMLHandler.java:1022)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XMLHandler.java:448)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:431)
	at org.eclipse.emf.ecore.xmi.impl.SAXWrapper.startElement(SAXWrapper.java:75)
	at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl
		$FragmentContentDispatcher.dispatch(Unknown Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
	at javax.xml.parsers.SAXParser.parse(Unknown Source)
	at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:129)
	at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:155)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:884)
	at dw.ibm.etools.xsd.sdo.xmltransformservice.impl.XMLTransformServiceImpl.load
		(XMLTransformServiceImpl.java:85)
	at dw.ibm.etools.xsd.sdo.xmltransformservice.impl.XMLTransformServiceImpl.load
		(XMLTransformServiceImpl.java:102)
	at dw.ibm.etools.xsd.sdo.xmltransformservice.impl.XMLTransformServiceFactoryImpl.loadStream
		(XMLTransformServiceFactoryImpl.java:50)
	at dw.ibm.etools.xsd.sdo.xmltransformservice.impl.XMLTransformServiceFactoryImpl.loadFile
		(XMLTransformServiceFactoryImpl.java:65)
	at sdotestclient.TestClient.TestRestrictSchemaChange(TestClient.java:101)
	at sdotestclient.TestClient.main(TestClient.java:33)
java.lang.NullPointerException
	at sdotestclient.TestClient.TestRestrictSchemaChange(TestClient.java:102)
	at sdotestclient.TestClient.main(TestClient.java:33)

SDO API usage

The generated SDO package uses an underlying Eclipse Modeling Framework implementation. The EMF provides a generic navigation model for the graph or related (or unrelated) objects, and contains a built-in change notification mechanism. Thus, the generated SDO packages have APIs that enable traversing the related (or unrelated) SDOs. Any modifications to any of these object instances or their lists result in notification issues to manage the changes, as per the containment of these objects. The next sections highlight basic considerations when manipulating SDO instances loaded from an XML stream. The functions covered are:

(See Resources for more information on EMF and SDOs.)

Add

As we saw in the previous section, a new instance of an SDO can be created either by using the factory class generated in the util package, or by using the create<SDO Name> for the related SDO. Let's consider an example where we have an instance of an SDO graph loaded from an XML request. We now need to create a new SDO graph that contains the instances from the loaded SDO graph.

In Listing 11a, the broker instance from moveFrom is set in the moveTo SDO. If we check the contents of moveFrom, we will see that it no longer contains the broker instance, while the moveTo SDO does. A simple, add operation in this case, by default, results in a move from the original container to the new container. An SDO instance thus can only be contained by one container at a time.

Listing 11a. Method

Click to see code listing

public void TestMove()
   {
	xyz.brokerservice.DocumentRoot moveFrom = ( xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.loadFile("./DataFile/brokerDetailResponseV1.xml");        
	xyz.brokerservice.DocumentRoot moveTo = xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.create(BrokerservicePackage.eINSTANCE.getNsURI());
	moveTo.getXMLNSPrefixMap().put("", BrokerservicePackage.eINSTANCE.getNsURI());
	moveTo.createBrokerService().setBroker(moveFrom.getBrokerService().getBroker());
	System.out.println("**********Changes in Original XML********");	    
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)moveFrom));
	System.out.println("**********************************************************");
	System.out.println("**********Target XML**************************");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)moveTo));
	System.out.println("**********************************************************");
   }

Listing 11b. Output

**********Changes in Original XML********
<?xml version="1.0" encoding="UTF-8"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore"/>
**********************************************************
**********Target XML**************************
<?xml version="1.0" encoding="ASCII"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore">
  <broker brokerId="000-00-9999">
    <firstName>Narinder</firstName>
    <lastName>Makin</lastName>
    <client clientId="001-00-9999">
      <firstName>Dan</firstName>
      <lastName>Moore</lastName>
      <dateOfBirth>1967-08-13</dateOfBirth>
      <currentAddress>
        <street>113 Oak Pine St.</street>
        <city>Santa Clara</city>
        <state>LA</state>
        <zip>91929</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Demi Lane</street>
        <city>Cary</city>
        <state>NC</state>
        <zip>22999</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="L000000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance policy includes any accidental damages.</policyDescription>
      </policy>
      <policy policyId="H000000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>50000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="C000000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>15000.00</policyAmount>
        <policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription>
      </policy>
      <policy policyId="C000000002">
        <policyName>Car 2</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>5000.00</policyAmount>
        <policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription>
      </policy>
      <policy policyId="B000000002">
        <policyName>Restaurant</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>25000.00</policyAmount>
        <policyDescription>Business Insurance - Restaurant</policyDescription>
      </policy>
    </client>
    <client clientId="002-00-9999">
      <firstName>Tom</firstName>
      <lastName>Cross</lastName>
      <dateOfBirth>1970-11-11</dateOfBirth>
      <currentAddress>
        <street>113 Duke St.</street>
        <city>Shelton</city>
        <state>CT</state>
        <zip>08989</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Lex Lane</street>
        <city>Fairfield</city>
        <state>NY</state>
        <zip>09833</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="H100000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="L100000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance</policyDescription>
      </policy>
      <policy policyId="C100000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
      </policy>
    </client>
  </broker>
</brokerService>
**********************************************************

Copy

If a true copy operation is required, such that both the SDO graphs will contain the SDOs, an ObjectUtil.copyFrom operation should be used instead, since an EMF API is used internally. This API performs a deep copy, which includes all the nested children of the object being copied into the new graph.

In Listing 12a, the contents of copyFrom are not altered from before, but copyTo contains a new instance of the broker with exactly the same structured data as was retrieved for it from the copyFrom SDO.

Listing 12a. Method

public void TestCopy()
   {
	xyz.brokerservice.DocumentRoot copyFrom = (xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.loadFile("./DataFile/brokerDetailResponseV1.xml");
	xyz.brokerservice.DocumentRoot copyTo = (xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.create(BrokerservicePackage.eINSTANCE.getNsURI());
	copyTo.getXMLNSPrefixMap().put("", BrokerservicePackage.eINSTANCE.getNsURI());
	copyTo.createBrokerService().setBroker((BrokerType)ObjectUtil.INSTANCE.copyFrom((DataObject)
		copyFrom.getBrokerService().getBroker()));
	System.out.println("**********No Changes in Original XML********");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)copyFrom));
	System.out.println("**********************************************************");
	System.out.println("**********Target XML**************************");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)copyTo));
	System.out.println("**********************************************************");
   }

Listing 12b. Output

**********No Changes in Original XML********
<?xml version="1.0" encoding="UTF-8"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore">
  <broker brokerId="000-00-9999">
    <firstName>Narinder</firstName>
    <lastName>Makin</lastName>
    <client clientId="001-00-9999">
      <firstName>Dan</firstName>
      <lastName>Moore</lastName>
      <dateOfBirth>1967-08-13</dateOfBirth>
      <currentAddress>
        <street>113 Oak Pine St.</street>
        <city>Santa Clara</city>
        <state>LA</state>
        <zip>91929</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Demi Lane</street>
        <city>Cary</city>
        <state>NC</state>
        <zip>22999</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="L000000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance policy includes any accidental damages.</policyDescription>
      </policy>
      <policy policyId="H000000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>50000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="C000000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>15000.00</policyAmount>
        <policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription>
      </policy>
      <policy policyId="C000000002">
        <policyName>Car 2</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>5000.00</policyAmount>
        <policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription>
      </policy>
      <policy policyId="B000000002">
        <policyName>Restaurant</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>25000.00</policyAmount>
        <policyDescription>Business Insurance - Restaurant</policyDescription>
      </policy>
    </client>
    <client clientId="002-00-9999">
      <firstName>Tom</firstName>
      <lastName>Cross</lastName>
      <dateOfBirth>1970-11-11</dateOfBirth>
      <currentAddress>
        <street>113 Duke St.</street>
        <city>Shelton</city>
        <state>CT</state>
        <zip>08989</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Lex Lane</street>
        <city>Fairfield</city>
        <state>NY</state>
        <zip>09833</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="H100000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="L100000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance</policyDescription>
      </policy>
      <policy policyId="C100000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
      </policy>
    </client>
  </broker>
</brokerService>
**********************************************************
**********Target XML**************************
<?xml version="1.0" encoding="ASCII"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore">
  <broker brokerId="000-00-9999">
    <firstName>Narinder</firstName>
    <lastName>Makin</lastName>
    <client clientId="001-00-9999">
      <firstName>Dan</firstName>
      <lastName>Moore</lastName>
      <dateOfBirth>1967-08-13</dateOfBirth>
      <currentAddress>
        <street>113 Oak Pine St.</street>
        <city>Santa Clara</city>
        <state>LA</state>
        <zip>91929</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Demi Lane</street>
        <city>Cary</city>
        <state>NC</state>
        <zip>22999</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="L000000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance policy includes any accidental damages.</policyDescription>
      </policy>
      <policy policyId="H000000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>50000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="C000000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>15000.00</policyAmount>
        <policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription>
      </policy>
      <policy policyId="C000000002">
        <policyName>Car 2</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>5000.00</policyAmount>
        <policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription>
      </policy>
      <policy policyId="B000000002">
        <policyName>Restaurant</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>25000.00</policyAmount>
        <policyDescription>Business Insurance - Restaurant</policyDescription>
      </policy>
    </client>
    <client clientId="002-00-9999">
      <firstName>Tom</firstName>
      <lastName>Cross</lastName>
      <dateOfBirth>1970-11-11</dateOfBirth>
      <currentAddress>
        <street>113 Duke St.</street>
        <city>Shelton</city>
        <state>CT</state>
        <zip>08989</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Lex Lane</street>
        <city>Fairfield</city>
        <state>NY</state>
        <zip>09833</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="H100000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="L100000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance</policyDescription>
      </policy>
      <policy policyId="C100000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
      </policy>
    </client>
  </broker>
</brokerService>
**********************************************************

Delete

The SDO can be deleted from the container as a simple list "remove" operation.. As shown in Listing 13a, the instance of the client is removed from the broker instance.

Listing 13a. Method

Click to see code listing

public void TestDelete()
   {
	xyz.brokerservice.DocumentRoot deleteFrom = ( xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.loadFile("./DataFile/brokerDetailResponseV1.xml");
	BrokerType brokerType = deleteFrom.getBrokerService().getBroker();
	List clientList = brokerType.getClient();
	Iterator clientListItr = clientList.iterator();
	ClientType clientMoore = null;
	while(clientListItr.hasNext()){
		ClientType clientType = (ClientType)clientListItr.next();
		if(clientType.getLastName().equals("Moore")){
			clientMoore = clientType;
			break;
		}
        }
	clientList.remove(clientMoore);        
		System.out.println("**********Client with last name 'Moore'  removed from Original XML********");
		System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)deleteFrom));
		System.out.println("**********************************************************");
   }

Listing 13b. Output

**********Client with last name 'Moore'  removed from Original XML********
<?xml version="1.0" encoding="UTF-8"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore">
  <broker brokerId="000-00-9999">
    <firstName>Narinder</firstName>
    <lastName>Makin</lastName>
    <client clientId="002-00-9999">
      <firstName>Tom</firstName>
      <lastName>Cross</lastName>
      <dateOfBirth>1970-11-11</dateOfBirth>
      <currentAddress>
        <street>113 Duke St.</street>
        <city>Shelton</city>
        <state>CT</state>
        <zip>08989</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Lex Lane</street>
        <city>Fairfield</city>
        <state>NY</state>
        <zip>09833</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="H100000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="L100000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance</policyDescription>
      </policy>
      <policy policyId="C100000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
      </policy>
    </client>
  </broker>
</brokerService>
**********************************************************

Traverse

Since the SDO implementation is EMF based, the EMF traversal APIs can be used in order to traverse an SDO graph. You can either access the related children, or you can traverse the parent object tree using the getParent API in ObjectUtil. This API traverses the reference to the container SDO that contains the SDO as a typed reference or in a list. In Listings 14a and 14b, the broker instance is traversed from an instance of client SDO.

Listing 14a. Code

public void TestGraphTravesal()
   {
	xyz.brokerservice.DocumentRoot traverseFrom = ( xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.loadFile("./DataFile/brokerDetailResponseV1.xml");
	BrokerType brokerType = traverseFrom.getBrokerService().getBroker();
	System.out.println("BrokerType is: " + brokerType.getFirstName());
	List clientList = brokerType.getClient();
	Iterator clientListItr = clientList.iterator();
	ClientType clientMoore = null;
	while(clientListItr.hasNext()){
		ClientType clientType = (ClientType)clientListItr.next();
		BrokerType parent = (BrokerType)ObjectUtil.INSTANCE.getParent(((DataObject)clientType));
		System.out.println("Parent brokerType is: " + parent.getFirstName());
	}
   }

Listing 14b. Results

BrokerType is: Narinder
Parent brokerType is: Narinder
Parent brokerType is: Narinder

Aggregate API usage

Utility APIs, such as sort, sum, mean, and so on, are commonly used within and throughout applications. An application created for an XML-based SOA may typically require coding of such APIs. Since we are using an SDO implementation, these methods can be provided generically across the SDO instances. The ObjectUtil helper class, provided with the XSD SDO Transform feature, contains aggregate and other utility APIs that operate generically on the SDO objects. This section illustrates the usage of these APIs:

Sum

The Sum API can be used to provide a summation of the values of an EAttribute present with in a list. This API is used with a simple call that takes in the list containing the instances and the EAttribute for the attribute. EPackage provides the access to all the EMF types for the EObject and EAttributes present in the SDO package. Attributes of type Integer, Float, Long, Short, Double, BigDecimal, and String are valid for Sum. For an attribute of type String, an attempt is made to implicitly convert it into Long, Double, or Float.

Listing 15. Sum API

/**
 * Get the sum for the values of the attribute in an EList
 * @exception Exception Throws java.lang.Exception
 * @param list a EList containing the attribute values
 * @param attr The EAttribute for the attribute whose sum is to be determined
 * @return Object  The sum of the value of the EAttributes in the list.If the length is less than 0
 * then return null. If the attribute is not valid for doing sum and mean then return null
 */
public Object sum(List list, EAttribute attr) throws Exception

Mean

The Mean API can be used to provide the mean of the EAttribute values present within a list. This API is also used with a simple call that takes in the list containing the instances and the EAttribute for the attribute. Valid attribute types are the same as for Sum.

Listing 16. Mean API

/**
 * Get the mean for the values of the attribute in an EList
 * @exception Exception Throws java.lang.Exception
 * @param list a EList containing the attribute values
 * @param attr The EAttribute for the attribute whose mean is to be determined
 * @return Object  The mean of the value of the EAttributes in the list.If the length is less than 0
 * then return null. If the attribute is not valid for doing sum and mean then return null
 */
public Object mean(List list, EAttribute attr) throws Exception

Min

The Min API can be used to provide the minimum of the values of an EAttribute present within a list. This API is also used with a simple call that takes in the list containing the instances and the EAttribute for the attribute. An attribute of type Integer, Float, Long, Short, Double, BigDecimal, Date, Time, Timestamp, and String are considered valid for Min.

Listing 17. Min API

/**
 * Get the minimum value from the attributes in the EList
 * @param list a EList containing the attribute values
 * @param attr an EAttribute defining the EType of the attributes in the list
 * @return Object  The minimum value in the list 
 * If the length is less than 0 then return null.
 * If the attribute is not valid then return null
 */
public Object min(List list, EAttribute attr) throws Exception

Max

The Max API can be used to provide the maximum of the values of an EAttribute present with in a list. This API is also used as a simple call that takes in the list containing the instances and the EAttribute for the attribute. Valid attribute types are the same as for Min.

Listing 18. Max API

/**
 * Get the maximum value from the attributes in the EList
 * @param list a EList containing the attribute values
 * @param attr an EAttribute defining the EType of the attributes in the list
 * @return Object  The maximum value in the list 
 * If the length is less than 0 then return null.
 * If the attribute is not valid then return null
 */
public Object max(List list, EAttribute attr) throws Exception

The coding and results of each of these aggregate APIs are shown in Figures 19a and 19b.

Listing 19a. Code

public void TestAggregateAPI(){
	xyz.brokerservice.DocumentRoot root = ( xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.loadFile("./DataFile/brokerDetailResponseV1.xml");
	BrokerServiceType bs = root.getBrokerService();
	try {
		//sum test
		System.out.println("Sum is : " + ObjectUtil.INSTANCE.sum(bs.getBroker()
			.getClientAsArray()[0].getPolicy(), 
			BrokerservicePackage.eINSTANCE.getPolicyType_PolicyAmount()));
			
		//mean test
		System.out.println("Mean is : " + ObjectUtil.INSTANCE.mean(bs.getBroker()
			.getClientAsArray()[0].getPolicy(), 
			BrokerservicePackage.eINSTANCE.getPolicyType_PolicyAmount()));
			
		//max test
		System.out.println("Max is : " + ObjectUtil.INSTANCE.max(bs.getBroker()
			.getClientAsArray()[0].getPolicy(), 
			BrokerservicePackage.eINSTANCE.getPolicyType_PolicyName()));
			
		//min test
		System.out.println("Min is : " + ObjectUtil.INSTANCE.min(bs.getBroker()
			.getClientAsArray()[0].getPolicy(), 
			BrokerservicePackage.eINSTANCE.getPolicyType_PolicyName()));
	} 
	catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

Listing 19b. Results

Sum is : 195000.0
Mean is : 39000.0
Max is : Restaurant
Min is : Car 1

Using the XSD-to-SDO generator

Application Developer also ships with a feature for generating the SDO package from an XSD document. You can use this feature to generate the SDO Java package rather than use the Create SDO Package action. The code generated with the Create SDO Package action contains the typed array methods, along with the list methods for the related SDO objects. This eases the binding of the datatable to the collection without the prompt for defining the type contained in the collection. Along with that, the generator also adds the required framework JARs to the project so that no manual setup is required. It is viable to use the Create SDO Package action when you require more than just the creation of the SDO package, like when developing a Web project. When a simple Java project is being used, though, you may wish to use the XSD SDO generator and manually add the framework JAR file, xsdsdotransform.jar, to the classpath, and build path of the project if transform capability is required. The EMF framework JARs are automatically added to the project by the generator. (These JARs are also available for download at the EMF Web site, and are equivalent to the emf.jar that is shipped with the Application Developer or WebSphere Application Server runtime.)

To use the SDO generator option in Application Developer:

  1. Turn on the XML development capability in the Preferences dialog (Figure 7).
    Figure 7. Turn on XML capability
    Turn on XML capability
  2. This enables a context menu action for generating Java when an XSD document is selected (Figure 8).
    Figure 8. Menu action
    Menu action
  3. In the Generate Java dialog, select SDO Generator to enable generation of the XSD SDO Java package (Figure 9).
    Figure 9. XSD SDO generator wizard
    XSD SDO generator wizard
  4. As a result of the wizard operation, the SDO Java package is created (Figure 10) and the required framework JARs are added to the build and classpath of the Java project.
    Figure 10. SDO Java Package
    SDO Java Package

Known limitation: XSD inheritance is not supported in the generation of the SDO package. Before creating the SDO package, you will need to fix the generated code manually or merge the schema into a single file.


Conclusion

Part 3 of this article series highlighted different techniques that are commonly used for manipulating SDO instances when building an XML-based SOA application. How to use the SDO generator, and the differences in the code generated using the Create SDO package action, were also discussed.

Part 4 will focus on portlet development for the XML-based SOA that uses the XSD SDO transform feature, addressing JSR 168 portlet creation, parameter passing across portlets, and setting the target portlet view dynamically on a portlet action in any other portlet. Part 5 will discuss localizing JSF and portlet applications for the XML-based SOA that uses the XSDO SDO transform feature, and will address the localizing static and dynamic content that is displayed based on the preferred locale.


Downloads

DescriptionNameSize
Download file 1xyzinsurancetestclient_pi.zip  ( HTTP | FTP )2.0 MB
Download file 2xsdsdotransform-feature.zip  ( HTTP | FTP )52 KB
Download file 3xsd_sdo_soa_part3_listings.zip  ( HTTP | FTP )4 KB

Resources

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into WebSphere on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere, Rational, Agile transformation
ArticleID=48782
ArticleTitle=IBM WebSphere Developer Technical Journal: Accelerated JSF development for XML-based SOA using Rational Application Developer and WebSphere Application Server -- Part 3
publish-date=02232005