Jump to: navigation, search

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

(XMLHelper - Handle XML Data as DataObjects)
 
(9 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
==HelperContext==
 
==HelperContext==
 +
The helper context is the access point for all the SDO helpers and factories.  If a type is defined in the TypeHelper for a HelperContext, then the DataFactory from that HelperContext can be used to create a DataObject of that Type.
 +
===Default HelperContext===
 +
SDO provides a default helper context. The helpers and factories obtained from the default helper context are the same ones available from the static INSTANCE variables on each of the helpers and factory classes. De-serialization is always performed in the scope of the default HelperContext.
 +
<source lang="java">
 +
HelperContext helperContext = HelperProvider.getDefaultContext();
 +
</source>
 +
===Local HelperContext===
 +
SDO also allows for HelperContexts other than the default
 +
<source lang="java">
 +
HelperContext helperContext = new SDOHelperContext();
 +
</source>
 +
If you are using type safe DataObjects then you can provide the ClassLoader that contains the generated interfaces.
 +
<source lang="java">
 +
HelperContext helperContext = new SDOHelperContext(aClassLoader);
 +
</source>
  
 
==TypeHelper - Access Metadata==
 
==TypeHelper - Access Metadata==
Line 5: Line 20:
 
<source lang="java">
 
<source lang="java">
 
TypeHelper typeHelper = helperContext.getTypeHelper();
 
TypeHelper typeHelper = helperContext.getTypeHelper();
 +
</source>
 +
 +
===Get Types===
 +
You can lookup the SDO type by name and uri.
 +
<source lang="java">
 +
typeHelper.getType("http://www.example.org/customer-example", "customer-type");
 +
</source>
 +
You can also use the type safe interface classes to look up the corresponding SDO type.
 +
<source lang="java">
 +
typeHelper.getType(Customer.class);
 +
</source>
 +
 +
===Get Open Content Properties===
 +
You can lookup open content properties by name and uri.
 +
<source lang="java">
 +
typeHelper.getOpenContentProperty("http://www.example.org/customer-example", "phone-number");
 
</source>
 
</source>
  
Line 11: Line 42:
 
<source lang="java">
 
<source lang="java">
 
XSDHelper xsdHelper = helperContext.getXSDHelper();
 
XSDHelper xsdHelper = helperContext.getXSDHelper();
 +
</source>
 +
 +
===Define===
 +
The XSDHelper contains a number of define methods for generating SDO types from an XML Schema.
 +
<source lang="java">
 +
URL xsdURL = new URL("http://www.example.org/customer-example/customer.xsd");
 +
List types = xsdHelper.define(xsdUrl.openStream(), xsdUrl.toExternalForm());
 +
</source>
 +
 +
===Generate===
 +
The XSDHelper contains a number of generate methods to generate an XML Schema from SDO types.
 +
<source lang="java">
 +
String xmlSchema = xsdHelper.generate(types);
 
</source>
 
</source>
  
Line 18: Line 62:
 
DataFactory dataFactory = helperContext.getDataFactory();
 
DataFactory dataFactory = helperContext.getDataFactory();
 
</source>
 
</source>
 +
 +
===Create===
  
 
==DataHelper - Convert Simple Values==
 
==DataHelper - Convert Simple Values==
Line 32: Line 78:
  
 
===Load===
 
===Load===
 
+
Load methods are used to unmarshal XML into DataObjects.
===Create Document===
+
<source lang="java">
 +
FileInputStream xmlInputStream = new FileInputStream("customer.xml");
 +
XMLDocument xmlDocument = xmlHelper.load(xmlInputStream);
 +
DataObject customerDO = xmlDocument.getRootObject;
 +
</source>
  
 
===Save===
 
===Save===
 +
Save methods are used to convert DataObjects to XML.
 +
<source lang="java">
 +
xmlHelper.save(customerDO, "http://www.example.org/customer-example", "customer", System.out);
 +
</source>
 +
The XML root information can also be encapsulated in an XMLDocument object.
 +
<source lang="java">
 +
XMLDocument xmlDocument =
 +
    xmlHelper.createDocument(customerDO, "http://www.example.org/customer-example", "customer");
 +
xmlHelper.save(xmlDocument, System.out, null);
 +
</source>
  
 
==CopyHelper - Create Copies of DataObjects==
 
==CopyHelper - Create Copies of DataObjects==
Line 72: Line 132:
 
boolean isDeepEqual = equalityHelper.equal(dataObject1, dataObject2);
 
boolean isDeepEqual = equalityHelper.equal(dataObject1, dataObject2);
 
</source>
 
</source>
 +
 +
[[Category:EclipseLink/Example/SDO]]

Latest revision as of 12:52, 24 June 2010

HelperContext

The helper context is the access point for all the SDO helpers and factories. If a type is defined in the TypeHelper for a HelperContext, then the DataFactory from that HelperContext can be used to create a DataObject of that Type.

Default HelperContext

SDO provides a default helper context. The helpers and factories obtained from the default helper context are the same ones available from the static INSTANCE variables on each of the helpers and factory classes. De-serialization is always performed in the scope of the default HelperContext.

HelperContext helperContext = HelperProvider.getDefaultContext();

Local HelperContext

SDO also allows for HelperContexts other than the default

HelperContext helperContext = new SDOHelperContext();

If you are using type safe DataObjects then you can provide the ClassLoader that contains the generated interfaces.

HelperContext helperContext = new SDOHelperContext(aClassLoader);

TypeHelper - Access Metadata

TypeHelper provides a convenient means of handling SDO metadata.

TypeHelper typeHelper = helperContext.getTypeHelper();

Get Types

You can lookup the SDO type by name and uri.

typeHelper.getType("http://www.example.org/customer-example", "customer-type");

You can also use the type safe interface classes to look up the corresponding SDO type.

typeHelper.getType(Customer.class);

Get Open Content Properties

You can lookup open content properties by name and uri.

typeHelper.getOpenContentProperty("http://www.example.org/customer-example", "phone-number");

XSDHelper - Access XML Schema Metadata

XSDHelper provides a convenient means of handling XML Schema metadata.

XSDHelper xsdHelper = helperContext.getXSDHelper();

Define

The XSDHelper contains a number of define methods for generating SDO types from an XML Schema.

URL xsdURL = new URL("http://www.example.org/customer-example/customer.xsd");
List types = xsdHelper.define(xsdUrl.openStream(), xsdUrl.toExternalForm());

Generate

The XSDHelper contains a number of generate methods to generate an XML Schema from SDO types.

String xmlSchema = xsdHelper.generate(types);

DataFactory - Create DataObjects

DataFactory provides a convenient means of creating SDO DataObjects.

DataFactory dataFactory = helperContext.getDataFactory();

Create

DataHelper - Convert Simple Values

DataHelper provides a convenient means of converting SDO data type values.

DataHelper dataHelper = helperContext.getDataHelper();

XMLHelper - Handle XML Data as DataObjects

XMLHelper provides a convenient means of handling XML data as DataObjects.

XMLHelper xmlHelper = helperContext.getXMLHelper();

Load

Load methods are used to unmarshal XML into DataObjects.

FileInputStream xmlInputStream = new FileInputStream("customer.xml");
XMLDocument xmlDocument = xmlHelper.load(xmlInputStream);
DataObject customerDO = xmlDocument.getRootObject;

Save

Save methods are used to convert DataObjects to XML.

xmlHelper.save(customerDO, "http://www.example.org/customer-example", "customer", System.out);

The XML root information can also be encapsulated in an XMLDocument object.

XMLDocument xmlDocument = 
    xmlHelper.createDocument(customerDO, "http://www.example.org/customer-example", "customer");
xmlHelper.save(xmlDocument, System.out, null);

CopyHelper - Create Copies of DataObjects

CopyHelper provides a convenient means of copying DataObjects.

CopyHelper copyHelper = helperContext.getCopyHelper();

Shallow Copy

Create a new DataObject containing the values of all dataType=true properties (excluding the ChangeSummary property).

DataObject shallowCopy = copyHelper.copyShallow(dataObject);

Deep Copy

Create a copy of the entire tree.

DataObject deepCopy = copyHelper.copy(dataObject);

EqualityHelper - Compare DataObjects

EqualityHelper provides a convenient means of comparing DataObjects.

EqualityHelper equalityHelper = helperContext.getEqualityHelper();

Shallow Equal

Two DataObjects are considered shallow equal if the data objects are of the same type and all the values of all dataType=true properties (excluding the ChangeSummary property) are equal.

boolean isShallowEqual = equalityHelper.equalShallow(dataObject1, dataObject2);

Deep Equal

Two DataObjects are considered deep equal if the data objects and the trees that they belong to are both equal.

boolean isDeepEqual = equalityHelper.equal(dataObject1, dataObject2);