Difference between revisions of "EclipseLink/Examples/SDO/DynamicAPI"

From Eclipsepedia

Jump to: navigation, search
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Overview==
+
==Creating DataObjects==
 
+
===Using DataFactory===
The following example will demonstrate how to use EclipseLink's SDO functionality to:
+
DataObjects can be created by Type using DataFactory:
* Define a set of SDO Types from an XML Schema
+
* Load an XML file and modify its data
+
* Monitor changes made to the data
+
* Save the modified data to XML
+
 
+
==Setup==
+
 
+
# Ensure that you have EclipseLink correctly installed and configured for your environment.  Please see [[EclipseLink/Installing and Configuring EclipseLink]] for more information.
+
# Ensure that you have ANT correctly installed and configured.
+
# Unzip the Example ZIP file to a new directory.
+
# Edit the <tt>env.properties</tt> file in the root directory of the example and specify the path to your EclipseLink <tt>jlib</tt> directory:
+
<source lang="xml">
+
...
+
# Edit eclipselink.jlib to point to your EclipseLink jlib directory
+
eclipselink.jlib=C:/EclipseLink-1.0/eclipselink/jlib
+
...
+
</source>
+
 
+
You can compile and run the Example at any time by typing <tt>ant</tt> from the Example directory. 
+
 
+
==Initializing the Types from XML Schema==
+
 
+
The first thing that needs to be done in an SDO application is to set up the metadata for the Types and Properties. This is most commonly done by loading an XML schema, although it may also be done programmatically.
+
 
+
 
<source lang="java">
 
<source lang="java">
FileInputStream xsdInputStream = new FileInputStream("Customer.xsd");
+
Type customerType = TypeHelper.INSTANCE.getType("http://www.example.org/customer-example", "customer-type");
XSDHelper.INSTANCE.define(xsdInputStream, null);
+
DataObject customerDO = DataFactory.INSTANCE.create(customerType);
 
</source>
 
</source>
  
==Unmarshalling the XML Document==
+
===Using DataObject===
 
+
Once you have a DataObject you can use it to create child DataObjects based on its properties:
With the SDO Types and Properties defined from the schema, we can now load an XML document based on that schema, and then obtain a DataObject and begin modifying its contents.
+
 
+
 
<source lang="java">
 
<source lang="java">
FileInputStream xmlInputStream = new FileInputStream("Customer-data.xml");
+
DataObject contactInfoDO = customerDO.createDataObject("contact-info");
XMLDocument xmlDocument = XMLHelper.INSTANCE.load(xmlInputStream);
+
DataObject billingAddressDO = contactInfoDO.createDataObject("billing-address");
DataObject customerDO = xmlDocument.getRootObject();
+
 
</source>
 
</source>
  
==Tracking Object Modifications using Change Summary==
+
==Getting/Setting Properties==
 
+
An SDO path (similar to XPath) can be used with the String based accessors:
SDO's Change Summary provides access to change history information for the DataObjects in a data graph.  A change history covers any modifications that have been made to a data graph starting from the point when logging was activated.
+
 
+
In order to use Change Summary, we have defined an element of type "<tt>sdo:ChangeSummaryType</tt>" on our root complex type:
+
 
+
<source lang="xml">
+
<xs:complexType name="customer-type">
+
    <xs:sequence>
+
        ...
+
        <xs:element name="changeSummary" type="sdo:ChangeSummaryType" minOccurs="0"/>
+
 
+
    </xs:sequence>
+
</xs:complexType>
+
</source>
+
 
+
Before we start modifying our data, we must enable logging:
+
 
+
 
<source lang="java">
 
<source lang="java">
customerDO.getChangeSummary().beginLogging();
+
DataObject billingAddressDO = customerDO.getDataObject("contact-info/billing-address");
 
</source>
 
</source>
 
From this point on, any modifications to the DataObject will be captured in the Change Summary, until logging is deactivated:
 
 
<source lang="java">
 
customerDO.getChangeSummary().endLogging();
 
</source>
 
 
==Modifying the DataObjects==
 
 
Below are examples of manipulating the DataObjects using the dynamic APIs.  Note how the dynamic accessors take an XPath instead of just a property name.
 
 
Modifying the ShippingAddress ZipCode:
 
<source lang="java">
 
DataObject addressDO = customerDO.getDataObject("contact-info").getDataObject("shipping-address");
 
addressDO.set("zip-code", "27601");
 
</source>
 
 
Adding a new PhoneNumber:
 
<source lang="java">
 
DataObject newPhoneNumberDO = DataFactory.INSTANCE.create("urn:customer-example", "phone-number");
 
newPhoneNumberDO.set("number-type", "home");
 
newPhoneNumberDO.set("value", "(613) 555-3333");
 
customerDO.getList("contact-info/phone-number").add(newPhoneNumberDO);
 
</source>
 
 
Removing all "cell" PhoneNumbers:
 
<source lang="java">
 
ArrayList phoneNumbersToRemove = new ArrayList();
 
List phoneNumbers = customerDO.getDataObject("contact-info").getList("phone-number");
 
Iterator it = phoneNumbers.iterator();
 
while (it.hasNext()) {
 
    DataObject phoneNumberDO = (DataObject) it.next();
 
    if (phoneNumberDO.get("number-type").equals("cell")) {
 
        phoneNumbersToRemove.add(phoneNumberDO);
 
    }
 
}
 
phoneNumbers.removeAll(phoneNumbersToRemove);
 
</source>
 
 
There are general accessors for properties, i.e., <tt>get()</tt> and <tt>set()</tt>, as well as specific accessors for the primitive types and commonly used data types like String, Date, List, BigInteger, and BigDecimal.  For more information see the [http://help.eclipse.org/help32/topic/org.eclipse.emf.ecore.sdo.doc/references/javadoc/commonj/sdo/DataObject.html documentation for DataObject].
 
 
==Marshalling the DataObjects==
 
 
The following code segment demonstrates how to marshal DataObjects wrapped in a <tt>commonj.sdo.helper.XMLDocument</tt> back to XML.  In this example the stream we are saving to is <tt>System.out</tt>, so the XML text will be printed to the console.
 
 
<source lang="java">
 
XMLHelper.INSTANCE.save(xmlDocument, System.out, null);
 
</source>
 
 
==Interpreting the Change Summary==
 
 
When the document is saved to <tt>System.out</tt>, we can see the change summary information in the XML:
 
 
<source lang="xml">
 
<ns1:customer ...>
 
  ...
 
  <changeSummary logging="false" create="#/ns1:contact-info/ns1:phone-number[2]" delete="#/changeSummary/ns1:contact-info/ns1:phone-number[2]" xmlns:sdo="commonj.sdo">
 
      <ns1:contact-info sdo:ref="#/ns1:contact-info">
 
        <ns1:phone-number sdo:ref="#/ns1:contact-info/ns1:phone-number[1]"/>
 
        <ns1:phone-number number-type="cell">(613) 555-2222</ns1:phone-number>
 
      </ns1:contact-info>
 
      <shipping-address sdo:ref="#/ns1:contact-info/shipping-address">
 
        <zip-code>12345</zip-code>
 
      </shipping-address>
 
  </changeSummary>
 
</ns1:customer>
 
</source>
 
 
* For DataObjects with modified data type properties, the Change Summary element contains a copy of the DataObject from the data graph, but containing only the properties which have changed, and showing their old values.&nbsp; In our example, we see a "<tt>shipping-address</tt>" element which references "<tt>#/ns1:contact-info/shipping-address</tt>" (the element that was modified), along with its old value, "<tt>12345</tt>".
 
 
* DataObjects which are currently in the data graph, but were not present when logging was started are indicated in the change summary with a "<tt>create</tt>" attribute.  If more than one DataObject had been created, the attribute would contain a space-separated list of references, one for each DataObject.  In our example, we see a "<tt>create</tt>" attribute indicating that "<tt>#/ns1:contact-info/ns1:phone-number\[2\]</tt>" (the second phone number in the XML) is the newly created one.
 
 
* DataObjects deleted during logging are flagged with the "<tt>delete</tt>" attribute. In this case the change summary also contains a deep copy of the object which was deleted, as it no longer appears in the data graph.  Here, we see a "<tt>delete</tt>" attribute indicating that "<tt>#/changeSummary/ns1:contact-info/ns1:phone-number\[2\]</tt>" (the second phone number in the ''Change Summary'') is the one that was deleted from the XML.
 
 
==Download==
 
 
[http://wiki.eclipse.org/images/8/8e/Org.eclipse.persistence.example.sdo.dynamicapi.zip org.eclipse.persistence.example.sdo.dynamicapi.zip]
 

Latest revision as of 17:01, 24 February 2009

Contents

[edit] Creating DataObjects

[edit] Using DataFactory

DataObjects can be created by Type using DataFactory:

Type customerType = TypeHelper.INSTANCE.getType("http://www.example.org/customer-example", "customer-type");
DataObject customerDO = DataFactory.INSTANCE.create(customerType);

[edit] Using DataObject

Once you have a DataObject you can use it to create child DataObjects based on its properties:

DataObject contactInfoDO = customerDO.createDataObject("contact-info");
DataObject billingAddressDO = contactInfoDO.createDataObject("billing-address");

[edit] Getting/Setting Properties

An SDO path (similar to XPath) can be used with the String based accessors:

DataObject billingAddressDO = customerDO.getDataObject("contact-info/billing-address");