summaryrefslogtreecommitdiffstats
path: root/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java')
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples.java199
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SdoSampleConstants.java72
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/CreateCompany.java249
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/CreatePurchaseOrder.java174
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/PurchaseOrderCmdLine.java398
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/PurchaseOrderControl.java496
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/ReadPurchaseOrder.java143
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/package.html42
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/overview.html57
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/package.html33
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectPropertiesByName.java154
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectUsingValidXPath.java146
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXmlString.java163
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXsdAndXmlFiles.java196
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/DynamicCustomerTypeSample.java192
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/ObtainingDataGraphFromXml.java189
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/PrintPropertiesOfDataObject.java177
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/UsingXPath.java149
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/package.html40
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessDataObjectsUsingXPath.java251
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingDataObjectsViaPropertyIndex.java232
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingTheContentsOfASequence.java194
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java136
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingXmlFromDataObjects.java160
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/SerializingDesearializingADataObject.java183
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java190
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/package.html40
27 files changed, 4655 insertions, 0 deletions
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples.java
new file mode 100644
index 0000000000..8a151aa3af
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples.java
@@ -0,0 +1,199 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+import org.apache.tuscany.samples.sdo.otherSources.*;
+import org.apache.tuscany.samples.sdo.specCodeSnippets.*;
+import org.apache.tuscany.samples.sdo.specExampleSection.*;
+
+/**
+ * Executes all command line samples. <br>
+ * <br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.ExecuteSamples </LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ */
+public class ExecuteSamples {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ try {
+
+ System.out.println("Welcome to SDO Sample Land");
+ System.out.println("This class will simply execute samples within this jar file");
+
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ System.out.println();
+ System.out.println();
+ System.out.println("There are currently three groups of samples");
+ System.out.println("1. Simple code snippets located throughout the specifiction with comparative methods for acheiving the same results");
+ System.out.println("2. Full examples located within Examples section of specification");
+ System.out.println("3. Other sample sources such as white papers and online articles");
+ System.out.println(">>>Press Enter to continue");
+ in.readLine();
+ System.out
+ .println("--------------------------------------------------------------------------------------------------------------------------");
+ System.out
+ .println("Lets start with some basics. These samples are located within the org.apache.tuscany.sdo.samples.specCodeSnipets package."
+ + " They demonstrate many code snippets that are located throughout the SDO specification");
+ System.out
+ .println("--------------------------------------------------------------------------------------------------------------------------");
+
+ System.out.println(">>>Press Enter to continue");
+ in.readLine();
+ CreateDataObjectFromXmlString.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+ CreateDataObjectFromXsdAndXmlFiles.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+ AccessDataObjectPropertiesByName.main(args);
+ System.out.println(">>>Press Enter to continue");
+ in.readLine();
+
+ //The following samples are still under construction
+
+ //CreateXmlAndXsdFromDataObject.main(args);
+ //System.out.println(">>>Press Enter for next sample");
+ //in.readLine();
+ //DataObjectWithChangeSummary.main(args);
+ //System.out.println(">>>Press Enter for next sample");
+ //in.readLine();
+
+ DynamicCustomerTypeSample.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+ PrintPropertiesOfDataObject.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+ UsingXPath.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+ ObtainingDataGraphFromXml.main(args);
+
+ System.out.println();
+ System.out
+ .println("--------------------------------------------------------------------------------------------------------------------------");
+ System.out
+ .println("The following group of samples implement the examples located within Examples section of specification and are located in "
+ + "the org.apache.tuscany.sdo.samples.specExampleSection package");
+ System.out
+ .println("--------------------------------------------------------------------------------------------------------------------------");
+
+ System.out.println(">>>Press Enter to continue");
+ in.readLine();
+ AccessDataObjectsUsingXPath.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+
+ AccessingDataObjectsViaPropertyIndex.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+
+ AccessingTheContentsOfASequence.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+
+ CreatingDataObjectTreesFromXMLDocuments.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+
+ CreatingXmlFromDataObjects.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+
+ SerializingDesearializingADataObject.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+
+ UsingTypeAndPropertyWithDataObjects.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+
+ System.out.println();
+ System.out
+ .println("--------------------------------------------------------------------------------------------------------------------------");
+ System.out.println("The following group of samples come from sources other than the specification");
+ System.out
+ .println("--------------------------------------------------------------------------------------------------------------------------");
+
+ System.out
+ .println("The next two samples are based upon Fuhwei Lwo's paper http://www-128.ibm.com/developerworks/webservices/library/ws-sdoxmlschema/");
+ System.out.println(">>>Press Enter to continue");
+ in.readLine();
+ CreatePurchaseOrder.main(args);
+ System.out.println(">>>Press Enter for next sample");
+ in.readLine();
+ ReadPurchaseOrder.main(args);
+
+ System.out.println();
+ System.out.println();
+ System.out
+ .println("The following sample is slightly more extensive and provides a interactive menu to play with purchase order DataObjects");
+ System.out.println(">>>Press Enter to continue");
+ in.readLine();
+ PurchaseOrderCmdLine.main(args);
+
+ } catch (Exception e) {
+ System.out.println("Sorry an exception was encountered " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("All done, goodBye");
+
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SdoSampleConstants.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SdoSampleConstants.java
new file mode 100644
index 0000000000..ff36dd0ae8
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SdoSampleConstants.java
@@ -0,0 +1,72 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo;
+
+/**
+ * Interface provides some common Constants that are used across the samples.
+ *
+ * @author Robbie Minshall
+ */
+public interface SdoSampleConstants {
+
+ /**
+ * Defines xsd resource contained within jar file for PurchaseOrder DataObject
+ */
+ public static final String PO_XSD_RESOURCE = "po.xsd";
+
+ /**
+ * Defines xml resource contained within jar file that is used to populate PurchaseOrder DataObjects
+ */
+ public static final String PO_XML_RESOURCE = "po.xml";
+
+ /**
+ * Default file name to use when generated xml file for purchase order
+ * @see org.apache.tuscany.samples.sdo.CreatePurchaseOrder
+ */
+ public static final String PO_XML_GENERATED = "temporaryPoGenerated.xml";
+
+ /**
+ * Fake namespace used for PurchaseOrder
+ */
+ public static final String PO_NAMESPACE = "http://www.example.com/PO";
+
+ /**
+ * Bogus company namespace
+ */
+ public static final String COMPANY_NAMESPACE = "company.xsd";
+
+ /**
+ * previously defined XSD file used
+ */
+ public static final String COMPANY_XSD = "company.xsd";
+
+ /**
+ * XML file containing DataGraph representing a company. This xml file conforms to the company model defined in 'Complete DataGraph for Company
+ * Example' section of the SDO specification
+ */
+ public static final String COMPANY_DATAGRAPH_XML = "companyDataGraphGenerated.xml";
+
+ /**
+ * Generated DataGraph {@link org.apache.tuscany.samples.sdo.otherSources.CreateCompany}
+ */
+ public static final String COMPANY_DATAOBJECT_XML = "companyGenerated.xml";
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/CreateCompany.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/CreateCompany.java
new file mode 100644
index 0000000000..c4eddabb6b
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/CreateCompany.java
@@ -0,0 +1,249 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.otherSources;
+
+import java.io.BufferedReader;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import java.util.List;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.Type;
+import commonj.sdo.DataGraph;
+import commonj.sdo.helper.TypeHelper;
+import commonj.sdo.helper.DataFactory;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+import org.apache.tuscany.sdo.util.SDOUtil;
+
+/**
+ * Demonstrates creating a DataObject using the Types defined in a previously generated xsd {@link SdoSampleConstants#COMPANY_XSD} then persisting to a XML file
+ * This example is useful as a reference for creating valid xml documents that conform to an existing XSD and has been used to create valid XML files for use
+ * with specification code samples.
+ *
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.otherSources.CreateCompany</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ *
+ */
+public class CreateCompany {
+
+ /**
+ * XML file generated for the company DataObject
+ */
+ public static final String COMPANY_GENERATED_XML = "companyGenerated.xml";
+
+ /**
+ * XML file generated for the company DataGraph
+ */
+ public static final String COMPANY_DATAGRAPH_GENERATED_XML = "companyDataGraphGenerated.xml";
+
+
+ /**
+ * Main method. Execute this method in order to run sample
+ * @param args
+ * @throws Exception
+ */
+ public static void main(String[] args) throws Exception {
+
+ System.out.println("***************************************");
+ System.out.println("SDO Sample CreateCompany");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates creating a DataObject using the Types defined in a previously generated xsd {@link SdoSampleConstants#COMPANY_XSD} then persisting to a XML file");
+ System.out.println("***************************************");
+ /**
+ * Use xsd to define model
+ */
+ defineCompanyTypes();
+
+ /**
+ * DataObjects can exist on there own or can be associated with a DataGraph. This sample allows you to create and then generate xml for a
+ * company DataObject That is either associated with a DataGraph or independant
+ */
+ boolean useDataGraph = shouldUseDataGraph();
+
+ // define these so that they will be scoped appropiately.
+ DataGraph dataGraph = null;
+ DataObject company = null;
+
+ if (useDataGraph) {
+
+ /**
+ * The SDO specification says the following about creating a DataGraph A DataGraph is created by a DAS, which returns either an empty
+ * DataGraph, or a DataGraph filled with DataObjects. An empty DataGraph can have a root assigned by the createRootObject() methods.
+ * However, if a previous root DataObject exists then an IllegalStateException is thrown.
+ *
+ * In order to create a simply sample which creates a DataGraph without the use of a DAS this sample will use the
+ * {@link org.apache.tuscany.sdo.util.SDOUtil} class to create a DataObject
+ */
+
+ dataGraph = SDOUtil.createDataGraph();
+ company = dataGraph.createRootObject(SdoSampleConstants.COMPANY_NAMESPACE, "CompanyType");
+
+ } else {
+
+ /*
+ * The following creates a DataObject without a DataGraph
+ */
+ Type companyType = TypeHelper.INSTANCE.getType(SdoSampleConstants.COMPANY_NAMESPACE, "CompanyType");
+ // this is equivilent to
+ // DataObject company = DataFactory.INSTANCE.create(SdoSampleConstants.COMPANY_NAMESPACE, "CompanyType");
+ company = DataFactory.INSTANCE.create(companyType);
+ }
+
+ System.out.println("Populating company DataObject");
+ company.setString("name", "ACME");
+ company.setString("employeeOfTheMonth", "E0001");
+
+ System.out.println("Creating Deptartment");
+ DataObject depts = company.createDataObject("departments",SdoSampleConstants.COMPANY_NAMESPACE,"DepartmentType");
+
+ depts.setString("name", "Advanced Technologies");
+ depts.setString("location", "NY");
+ depts.setString("number", "123");
+
+ System.out.println("Creating employee John Jones");
+ DataObject johnJones = depts.createDataObject("employees",SdoSampleConstants.COMPANY_NAMESPACE, "EmployeeType");
+ johnJones.setString("name", "John Jones");
+
+ johnJones.setString("SN", "E0001");
+
+ System.out.println("Creating employee Jane Doe");
+ DataObject janeDoe = depts.createDataObject("employees",SdoSampleConstants.COMPANY_NAMESPACE, "EmployeeType");
+ janeDoe.setString("name", "Jane Doe");
+ janeDoe.setString("SN", "E0003");
+
+ System.out.println("Creating manager A Varone");
+ DataObject fVarone = depts.createDataObject("employees",SdoSampleConstants.COMPANY_NAMESPACE, "EmployeeType");
+ fVarone.setString("name", "A Varone");
+ fVarone.setString("SN", "E0004");
+ fVarone.setString("manager", "true");
+ System.out.println("DataObject creation completed");
+ System.out.println();
+
+ // print out the generated company DataObject or DataGraph according to user preferences
+ if (useDataGraph) {
+ //write the DataGraph to generated xml file
+ FileOutputStream fos = new FileOutputStream(COMPANY_DATAGRAPH_GENERATED_XML);
+ System.out.println("Writing company DataGraph to " + COMPANY_DATAGRAPH_GENERATED_XML);
+ // print the Company DataObject
+ SDOUtil.saveDataGraph(dataGraph, fos, null);
+
+ // print the company DataGraph to System.out
+ System.out.println("The xml for the created DataGraph would like : ");
+ SDOUtil.saveDataGraph(dataGraph, System.out, null);
+
+ } else {
+
+ //write out to a generated xml file
+ FileOutputStream fos = new FileOutputStream(COMPANY_GENERATED_XML);
+ System.out.println("Writing company DataObject to " + COMPANY_GENERATED_XML);
+ // print the Company DataObject
+ XMLHelper.INSTANCE.save(company, SdoSampleConstants.COMPANY_NAMESPACE, "company", fos);
+
+ // print the company DataObject to System.out
+ String generatedXml = XMLHelper.INSTANCE.save(company, SdoSampleConstants.COMPANY_NAMESPACE, "company");
+ System.out.println("The generated xml for the DataObject would look like : " );
+ System.out.println(generatedXml);
+ }
+
+ }
+
+ /**
+ * Defines company types using XSD.
+ * @throws Exception
+ */
+ private static void defineCompanyTypes() throws Exception {
+
+ System.out.println("Defining Types using XSD");
+ InputStream is = null;
+
+ System.out.println("Opening input stream to " + SdoSampleConstants.COMPANY_XSD);
+ is = ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_XSD);
+ List types = XSDHelper.INSTANCE.define(is, null);
+ is.close();
+ System.out.println("Type definition completed");
+
+ }
+
+ /**
+ * Utility method to obtain information from the user about whether or not they would like to use a DataGraph or simply use a DataObject
+ *
+ * @return whether or not a DataGraph should be used
+ * @throws Exception
+ */
+ private static boolean shouldUseDataGraph() throws Exception {
+
+ System.out.print("Create company DataObject associated with a DataGraph {y,n} :");
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ String answer = in.readLine();
+ while ((! answer.equalsIgnoreCase("n")) && (! answer.equalsIgnoreCase("y"))) {
+
+ System.out.println();
+ System.out.print("Sorry, please enter 'y' or 'n':");
+ answer = in.readLine();
+ }
+ System.out.println();
+
+ if (answer.equalsIgnoreCase("y")) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/CreatePurchaseOrder.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/CreatePurchaseOrder.java
new file mode 100644
index 0000000000..f3c0a6e704
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/CreatePurchaseOrder.java
@@ -0,0 +1,174 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.otherSources;
+
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.DataFactory;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+/**
+ * Demonstrates creating a purchaseOrder DataObject from an existing XSD and then
+ * persisting to disk. Uses previously defined model
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} to
+ * define types then generates a purchase order DataObject and persists to
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_GENERATED}. This
+ * sample was used to generate valid XML for Fuhwei Lwo's paper <A
+ * HREF="http://www-128.ibm.com/developerworks/webservices/library/ws-sdoxmlschema/"
+ * title="Me" onClick="checkLinks(this)">Create and read an XML document based on XML
+ * Schema</A>
+ *
+ * * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.otherSources.CreatePurchaseOrder</LI>
+ * </UL>
+ *
+ * @author Fuhwei Lwo
+ * @author Robbie Minshall
+ * @see ReadPurchaseOrder
+ * @see org.apache.tuscany.samples.sdo.specExampleSection.CreatingXmlFromDataObjects
+ */
+
+public class CreatePurchaseOrder {
+
+ /**
+ * Defines purchase order types using
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE}
+ *
+ * @throws Exception
+ */
+ private static void definePOTypes() throws Exception {
+
+ InputStream is = ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE);
+ if (is == null) {
+ System.out.println("InputStream is null");
+ } else {
+ System.out.println("Obtained Input Stream from resoruce");
+ }
+ XSDHelper.INSTANCE.define(is, null);
+ is.close();
+ }
+
+ /**
+ * Drives sample
+ *
+ * @param args.
+ * none required.
+ */
+ public static void main(String[] args) {
+ try {
+
+ System.out.println("***************************************");
+ System.out.println("SDO Sample CreatePurchaseOrder");
+ System.out.println("***************************************");
+ System.out.println("This sample is based upon Fuhwei Lwo's paper "
+ + "http://www-128.ibm.com/developerworks/webservices/library/ws-sdoxmlschema/"
+ + " and demonstrates creating a purchaseOrder DataObject from an "
+ + " existing XSD and then persisting to disk. Uses previously defined model");
+
+ System.out.println("***************************************");
+
+ definePOTypes();
+ System.out.println("Defined Types using xsd");
+ DataObject purchaseOrder = DataFactory.INSTANCE.create(SdoSampleConstants.PO_NAMESPACE, "PurchaseOrderType");
+ System.out.println("Created DataObject using DataFactory");
+
+ purchaseOrder.setString("orderDate", "1999-10-20");
+
+ DataObject shipTo = purchaseOrder.createDataObject("shipTo");
+ shipTo.set("country", "US");
+ shipTo.set("name", "Alice Smith");
+ shipTo.set("street", "123 Maple Street");
+ shipTo.set("city", "Mill Valley");
+ shipTo.set("state", "CA");
+ shipTo.setString("zip", "90952");
+ DataObject billTo = purchaseOrder.createDataObject("billTo");
+ billTo.set("country", "US");
+ billTo.set("name", "Robert Smith");
+ billTo.set("street", "8 Oak Avenue");
+ billTo.set("city", "Mill Valley");
+ billTo.set("state", "PA");
+ billTo.setString("zip", "95819");
+ purchaseOrder.set("comment", "Hurry, my lawn is going wild!");
+ System.out.println("Created orderDate, shipTo, billTo and comment properties");
+
+ DataObject items = purchaseOrder.createDataObject("items");
+
+ DataObject item1 = items.createDataObject("item");
+ item1.set("partNum", "872-AA");
+ item1.set("productName", "Lawnmower");
+ item1.setInt("quantity", 1);
+ item1.setString("price", "148.95");
+
+ item1.set("comment", "Confirm this is electric");
+
+ DataObject item2 = items.createDataObject("item");
+ item2.set("partNum", "926-AA");
+ item2.set("productName", "Baby Monitor");
+ item2.setInt("quantity", 1);
+ item2.setString("price", "39.98");
+ item2.setString("shipDate", "1999-05-21");
+ System.out.println("Created 2 items");
+
+ OutputStream stream = new FileOutputStream(SdoSampleConstants.PO_XML_GENERATED);
+ XMLHelper.INSTANCE.save(purchaseOrder, SdoSampleConstants.PO_NAMESPACE, "purchaseOrder", stream);
+ System.out.println("Created file " + SdoSampleConstants.PO_XML_GENERATED);
+ } catch (Exception e) {
+ System.out.println("Sorry an error occured " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/PurchaseOrderCmdLine.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/PurchaseOrderCmdLine.java
new file mode 100644
index 0000000000..148f677a5f
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/PurchaseOrderCmdLine.java
@@ -0,0 +1,398 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.otherSources;
+
+import java.io.*;
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+/**
+ * Provides interactive menu based command line interface to modify, save, view purchase order DataObject.
+ *
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.otherSources.PurchaseOrderCmdLine</LI>
+ * </UL>
+ * @author Robbie Minshall
+ * @see org.apache.tuscany.samples.sdo.SdoSampleConstants
+ * @see org.apache.tuscany.samples.sdo.otherSources.PurchaseOrderControl
+ */
+
+public class PurchaseOrderCmdLine {
+
+ private static final int MENU = 1;
+
+ private static final int PRINT_SUMMARY = 2;
+
+ private static final int PRINT_ALL_PROPERTIES = 3;
+
+ private static final int PRINT_BILLTO = 4;
+
+ private static final int PRINT_SHIPTO = 5;
+
+ private static final int APPEND_COMMENT = 6;
+
+ private static final int MODIFY_SHIP_TO = 7;
+
+ private static final int MODIFY_BILL_TO = 8;
+
+ private static final int ADD_ITEM = 9;
+
+ private static final int REMOVE_ITEM = 10;
+
+ private static final int SAVE = 11;
+
+ private static final int SAVE_AS = 12;
+
+ private static final int SHOW_CHANGE_SUMMARY = 13;
+
+ private static final int SHOW_XML_DOCUMENT = 14;
+
+ private static final int QUIT = 15;
+
+ /**
+ * Drives sample program
+ *
+ * @param args.
+ * args[0] is optional xsdFilename, args[1] is optional xmlFileName
+ */
+ public static void main(String[] args) {
+
+ System.out.println("***************************************");
+ System.out.println("SDO Purchase Order Sample Command Line Interface");
+ System.out.println("***************************************");
+ System.out.println("Provides an interactive menu to play with purchase order DataObjects");
+ System.out.println("***************************************");
+
+ printUsage();
+
+ PurchaseOrderControl poc = null;
+ String xsdFileName = null;
+ String xmlFileName = null;
+
+ // see if we should load an xsd file or if we should use a resource
+ if ((args.length >= 1)) {
+ xsdFileName = args[0];
+ }
+
+ // see if we should load an xml file or if we should use a resource
+ if ((args.length >= 2)) {
+ xmlFileName = args[1];
+ }
+ try {
+ poc = new PurchaseOrderControl(xsdFileName, xmlFileName);
+ } catch (Exception e) {
+ System.out.println("Sorry could not initialize PurchaseOrderControl. Error: " + e.toString());
+ e.printStackTrace();
+ return;
+ }
+ boolean inOperation = true;
+ try {
+
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ int command = QUIT;
+
+ printMenu();
+ while (inOperation) {
+ System.out.print(">>> ");
+ try {
+ command = Integer.valueOf(in.readLine()).intValue();
+ } catch (Exception e) {
+ System.out.println("Invalid option, please try again.");
+ command = MENU;
+
+ }
+ switch (command) {
+ case MENU:
+ printMenu();
+
+ break;
+ case PRINT_SUMMARY:
+ poc.printPurchaseOrderSummary();
+ break;
+ case PRINT_ALL_PROPERTIES:
+ try {
+ poc.printPurchaseOrderProperties();
+ } catch (Exception e) {
+ System.out.println("Sorry the following error was received when printing all properties " + e.toString());
+ e.printStackTrace();
+ }
+ break;
+ case PRINT_BILLTO:
+ poc.printBillTo();
+ break;
+ case PRINT_SHIPTO:
+ poc.printShipTo();
+ break;
+ case APPEND_COMMENT:
+ try {
+
+ System.out.println("Append Comment: ");
+ poc.appendComment(in.readLine());
+ } catch (Exception e) {
+ System.out.println("Sorry unexpected exception occured appending comment" + e.toString());
+ e.printStackTrace();
+ }
+
+ break;
+ case MODIFY_SHIP_TO:
+ try {
+
+ System.out.println("Modify Ship To ( press enter to leave unchanged )");
+ poc.printShipTo();
+ System.out.print("Name: ");
+ String name = in.readLine();
+ System.out.println();
+
+ System.out.print("Street: ");
+ String street = in.readLine();
+ System.out.println();
+
+ System.out.print("City: ");
+ String city = in.readLine();
+ System.out.println();
+
+ System.out.print("State: ");
+ String state = in.readLine();
+ System.out.println();
+
+ System.out.print("Zip: ");
+ String zip = in.readLine();
+ if ((zip != null) && (!zip.equals(""))) {
+ selectSetDecimalOption(in, poc);
+ }
+ System.out.println();
+
+ poc.modifyShipTo(name, street, city, state, zip);
+ } catch (Exception e) {
+ System.out.println("Sorry unexpected exception occured modifying ShipTo address :" + e.toString());
+ e.printStackTrace();
+ }
+
+ break;
+ case MODIFY_BILL_TO:
+ try {
+
+ System.out.println("Modify Bill To ( press enter to leave unchanged )");
+ poc.printBillTo();
+ System.out.print("Name: ");
+ String name = in.readLine();
+ System.out.println();
+
+ System.out.print("Street: ");
+ String street = in.readLine();
+ System.out.println();
+
+ System.out.print("City: ");
+ String city = in.readLine();
+ System.out.println();
+
+ System.out.print("State: ");
+ String state = in.readLine();
+ System.out.println();
+
+ System.out.print("Zip: ");
+ String zip = in.readLine();
+ if ((zip != null) && (!zip.equals(""))) {
+ selectSetDecimalOption(in, poc);
+ }
+ System.out.println();
+
+ poc.modifyBillTo(name, street, city, state, zip);
+ } catch (Exception e) {
+ System.out.println("Sorry unexpected exception occured when modifying address :" + e.toString());
+ e.printStackTrace();
+ }
+
+ break;
+
+ case ADD_ITEM:
+
+ try {
+ System.out.println("Add item to Purchase Order");
+ System.out.print("Product Number: ");
+ String productNumber = in.readLine();
+ System.out.println();
+
+ System.out.print("Product Name: ");
+ String productName = in.readLine();
+ System.out.println();
+
+ System.out.print("Quantity: ");
+ int quantity = Integer.valueOf(in.readLine()).intValue();
+ System.out.println();
+
+ System.out.print("Price: ");
+ double price = Double.valueOf(in.readLine()).doubleValue();
+ System.out.println();
+
+ System.out.print("Comment: ");
+ String comment = in.readLine();
+ System.out.println();
+
+ poc.addItem(productNumber, productName, price, quantity, comment);
+ } catch (Exception e) {
+ System.out.println("Sorry unexpected exception occured when adding item :" + e.toString());
+ e.printStackTrace();
+ }
+
+ break;
+ case REMOVE_ITEM:
+ try {
+ System.out.print("Please enter the index of the item you would like to remove :");
+ String index = in.readLine();
+ poc.removeItem(Integer.valueOf(index).intValue());
+
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error when remove item : " + e.toString());
+ e.printStackTrace();
+
+ }
+ break;
+ case SAVE:
+ try {
+ poc.save();
+ } catch (IOException e) {
+ System.out.println("Sorry an error was received : " + e.toString());
+ e.printStackTrace();
+ }
+ break;
+ case SAVE_AS:
+ System.out.print("FileName:");
+ String fileName = in.readLine();
+ try {
+ poc.saveAs(fileName);
+ System.out.println("File saved to " + fileName);
+ } catch (IOException e) {
+ System.out.println("Sorry an error was received : " + e.toString());
+ e.printStackTrace();
+ }
+ break;
+ case SHOW_CHANGE_SUMMARY:
+ try {
+
+ poc.printChangeSummary();
+ } catch (Exception e) {
+ System.out.println("Sorry exception caught when accessing change summary : " + e.toString());
+ e.printStackTrace();
+ }
+
+ break;
+ case SHOW_XML_DOCUMENT:
+ try {
+ System.out.println("XMLDocument representing purchase order: ");
+ System.out.println(poc.getXMLString());
+ } catch (Exception e) {
+ System.out.println("Sorry there was an erorr when printing the XMLDocument :" + e.toString());
+ e.printStackTrace();
+ }
+ break;
+ case QUIT:
+ inOperation = false;
+ break;
+ default:
+ System.out.println("Sorry I did not understand the command");
+ printMenu();
+ }
+ }
+ System.out.println("Goodbye");
+
+ } catch (Exception e) {
+ System.out.println("Unexpected exception occured during execution: " + e.toString());
+ e.printStackTrace();
+ }
+
+ }
+
+ private static void selectSetDecimalOption(BufferedReader in, PurchaseOrderControl poc) {
+ try {
+
+ System.out.println("SDO provides a number of methods to set properties on a DataObject");
+ System.out.println("Please select the method that you would like to use in order to set decimal values");
+ System.out.println("1. dataObject.set(. . . )");
+ System.out.println("2. dataObject.setString(. . . )");
+ System.out.println("3. dataObject.setDouble(. . . )");
+ System.out.print(">>>");
+
+ poc.setDecimalPropertyMethod(Integer.valueOf(in.readLine()).intValue());
+ } catch (Exception e) {
+ System.out.println("invalid option - oh well");
+ }
+
+ }
+
+ private static void printUsage() {
+ System.out.println("***************************************");
+ System.out.println("java org.apache.tuscany.samples.sdo.otherSources.PurchaseOrderCmdLine <xsdFileName> <xmlFileName>");
+ System.out.println("If xsdFileName and xmlFileName are not specified " + SdoSampleConstants.PO_XSD_RESOURCE + " and "
+ + SdoSampleConstants.PO_XML_RESOURCE + " will be used as resources");
+ System.out.println("Specify null if you wish to use the appropiate resource from the jar rather specifying a fileName");
+ System.out.println("***************************************");
+ }
+
+ private static void printMenu() {
+ System.out.println("***************************************");
+ System.out.println("Purchase Order Control Menu");
+ System.out.println(MENU + ". MENU");
+ System.out.println(PRINT_SUMMARY + ". PRINT_SUMMARY");
+ System.out.println(PRINT_ALL_PROPERTIES + ". PRINT_ALL_PROPERTIES");
+ System.out.println(PRINT_BILLTO + ". PRINT_BILLTO");
+ System.out.println(PRINT_SHIPTO + ". PRINT_SHIPTO");
+ System.out.println(APPEND_COMMENT + ". APPEND_COMMENT");
+ System.out.println(MODIFY_SHIP_TO + ". MODIFY_SHIP_TO");
+ System.out.println(MODIFY_BILL_TO + ". MODIFY_BILL_TO");
+ System.out.println(ADD_ITEM + ". ADD_ITEM");
+ System.out.println(REMOVE_ITEM + ". REMOVE_ITEM");
+ System.out.println(SAVE + ". SAVE");
+ System.out.println(SAVE_AS + ". SAVE_AS");
+ System.out.println(SHOW_CHANGE_SUMMARY + ". SHOW_CHANGE_SUMMARY");
+ System.out.println(SHOW_XML_DOCUMENT + ". SHOW_XML_DOCUMENT");
+ System.out.println(QUIT + ". QUIT");
+ System.out.println("***************************************");
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/PurchaseOrderControl.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/PurchaseOrderControl.java
new file mode 100644
index 0000000000..9a5a3279f6
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/PurchaseOrderControl.java
@@ -0,0 +1,496 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.otherSources;
+
+import java.io.*;
+
+import java.util.List;
+import java.math.BigDecimal;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.ChangeSummary;
+import commonj.sdo.helper.XMLDocument;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import commonj.sdo.helper.CopyHelper;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+import org.apache.tuscany.samples.sdo.specCodeSnippets.*;
+
+/**
+ * Provides operational API for purchase order sample for SDO. It is used by
+ * {@link PurchaseOrderCmdLine} for an interactive sample. Often relies upon
+ * methods that are located within the {@link org.apache.tuscany.samples.sdo.otherSources} package.
+ *
+ * @author Robbie Minshall
+ * @see org.apache.tuscany.samples.sdo.otherSources.PurchaseOrderCmdLine
+ *
+ */
+public class PurchaseOrderControl {
+
+ // there are a number of ways to set properties
+ // these constants define which mechanism the user
+ // would like to use.
+ public static final int SET_DECIMAL_PROPERTY_VIA_SET = 1;
+
+ public static final int SET_DECIMAL_PROPERTY_VIA_SET_STRING = 2;
+
+ public static final int SET_DECIMAL_PROPERTY_VIA_SET_DOUBLE = 3;
+
+ // local variable to determine which of the above mechanisms to use
+ private int decimalPropertyMethod = SET_DECIMAL_PROPERTY_VIA_SET_STRING;
+
+ // these files can be optionally used to specify where to locate xsd and xml
+ // to define types and populate DataObject
+ // if these are null will use the resources above
+ private String xmlFileName;
+
+ private String xsdFileName;
+
+ // root DataObject
+ private DataObject purchaseOrder;
+
+ /**
+ * Uses resources files included in jar file to define types (using xsd), and
+ * populate DataObjects (xml)
+ *
+ * @throws Exception
+ */
+ public PurchaseOrderControl() throws Exception {
+ this(null, null);
+ }
+
+ /**
+ * Uses files on the file system to define types (using xsd), and populate
+ * DataObjects (xml)
+ *
+ * @param xsdFileName
+ * location of po.xsd
+ * @param xmlFileName
+ * location of xml to populate DataObject (for example po.xml)
+ * @throws Exception
+ */
+ public PurchaseOrderControl(String xsdFileName, String xmlFileName) throws Exception {
+ this.xsdFileName = xsdFileName;
+ this.xmlFileName = xmlFileName;
+
+ // define purchase order types for SDO
+ definePurchaseOrderTypes();
+ // read in existing xml and populate DataObjects
+ readPurchaseOrderXml();
+ }
+
+ /**
+ * Method to take XSD (w3c) to define SDO types. This is static so that it can be
+ * used as a helper method for some simpler examples Uses either fileName used to
+ * construct this class or will use resource ( po.xsd ) included within jar file
+ */
+ private void definePurchaseOrderTypes() throws Exception {
+
+ if ((xsdFileName == null) || (xsdFileName.equals("")) || (xsdFileName.equalsIgnoreCase("null"))) {
+
+ // use simple example to define type from resource
+ CreateDataObjectFromXsdAndXmlFiles.definePurchaseOrderTypeUsingXsdResource();
+
+ } else {
+ System.out.println("Using file to access xsd in order to define types");
+ try {
+ FileInputStream fis = new FileInputStream(xsdFileName);
+ XSDHelper.INSTANCE.define(fis, null);
+ fis.close();
+ System.out.println("Sucessfully used " + xsdFileName + " to define types");
+
+ } catch (Exception e) {
+ System.out.println("Could not access file " + xsdFileName);
+ e.printStackTrace();
+ throw e;
+ }
+
+ }
+ }
+
+ /**
+ * Reads in xml and populates DataGraph.
+ *
+ * Will set the purchaseOrder to the root object Uses either fileName used to
+ * construct this class or will use resource ( po.xml ) included within jar file
+ */
+ public void readPurchaseOrderXml() throws Exception, FileNotFoundException {
+
+ if ((xmlFileName == null) || (xmlFileName.equals("")) || (xmlFileName.equalsIgnoreCase("null"))) {
+
+ purchaseOrder = CreateDataObjectFromXsdAndXmlFiles.createPurchaseOrderDataObjectUsingXmlResource();
+ } else {
+ try {
+
+ System.out.println("Using file to access xml to populate DataObjects");
+ FileInputStream fis = new FileInputStream(xmlFileName);
+ XMLDocument xmlDoc = XMLHelper.INSTANCE.load(fis);
+ purchaseOrder = xmlDoc.getRootObject();
+ System.out.println("Sucessfully used file to populate DataObjects");
+ fis.close();
+ } catch (Exception e) {
+ System.out.println("Could not open and use file " + xmlFileName);
+ e.printStackTrace();
+ throw e;
+ }
+ }
+
+ }
+
+ /**
+ * Appends new comment to comment property of purchase order Accesses comment
+ * property by name
+ *
+ * @param newComment
+ */
+ public void appendComment(String newComment) {
+ String oldComment = (String) purchaseOrder.get("comment");
+ setComment(oldComment + "-" + newComment);
+ }
+
+ /**
+ * Sets comment property on purchase order by accessing property by name
+ *
+ * @param comment
+ */
+ public void setComment(String comment) {
+ // set using path info
+ purchaseOrder.set("comment", comment);
+ }
+
+ /**
+ * Modifies complex address type for billTo. If any parameter is null it will not
+ * be modified
+ *
+ * @param name
+ * @param street
+ * @param city
+ * @param state
+ * @param zip
+ */
+ public void modifyBillTo(String name, String street, String city, String state, String zip) {
+
+ DataObject billTo = purchaseOrder.getDataObject("billTo");
+ modifyAddress(billTo, name, street, city, state, zip);
+ }
+
+ /**
+ * Modifies complex address type for shipTo. If any parameter is null it will not
+ * be modified
+ *
+ * @param name
+ * @param street
+ * @param city
+ * @param state
+ * @param zip
+ */
+ public void modifyShipTo(String name, String street, String city, String state, String zip) {
+
+ DataObject shipTo = purchaseOrder.getDataObject("shipTo");
+ modifyAddress(shipTo, name, street, city, state, zip);
+ }
+
+ /**
+ * Modifies complex address type ( used for either billTo or shipTo ) If any
+ * parameter is null it will not be modified
+ *
+ * @param name
+ * @param street
+ * @param city
+ * @param state
+ * @param zip
+ */
+ private void modifyAddress(DataObject dataObject, String name, String street, String city, String state, String zip) {
+ if ((name != null) && (!name.equals(""))) {
+ dataObject.set("name", name);
+ }
+ if ((street != null) && (!street.equals(""))) {
+ dataObject.set("street", street);
+ }
+ if ((city != null) && (!city.equals(""))) {
+ dataObject.set("city", city);
+ }
+ if ((state != null) && (!state.equals(""))) {
+ dataObject.set("state", state);
+ }
+ if ((zip != null) && (!zip.equals(""))) {
+ // there are a number of ways to set a decimal property
+ // so in order to demonstrate the various output results
+ // the user can determine which manner to do so
+ setDecimalPropertyOnDataObject(dataObject, "zip", zip);
+ }
+ }
+
+ /**
+ * Adds an additional item to the items list of the item property on purchase
+ * order DataObject
+ *
+ * @param partNumber
+ * @param productName
+ * @param price
+ * @param quantity
+ * @param comment
+ */
+ public void addItem(String partNumber, String productName, double price, int quantity, String comment) {
+ // get items dataObject
+ DataObject items = purchaseOrder.getDataObject("items");
+
+ // create a new child data object
+ DataObject newItem = items.createDataObject("item");
+ newItem.setString("partNum", partNumber);
+ newItem.setString("productName", productName);
+ newItem.setInt("quantity", quantity);
+ newItem.setDouble("price", price);
+ newItem.setString("comment", comment);
+
+ }
+
+ /**
+ * Removes a particular item from the items list. Obtains the List associated
+ * with the item property and removes by index
+ *
+ * @param index
+ */
+ public void removeItem(int index) {
+ // TODO: this is not working due to java.lang.ClassCastException:
+ // org.eclipse.emf.ecore.util.EcoreEList$Dynamic incompatible with
+ // commonj.sdo.DataObject
+ // access a DataObject by the index and then remove it
+ DataObject items = purchaseOrder.getDataObject("items");
+ List itemList = items.getList("item");
+ itemList.remove(index);
+ }
+
+ /**
+ * Saves modified purchase order DataObject to original xml used to populate it
+ * If resource packaged within jar is used will create a new file on file system
+ *
+ * @throws IOException
+ */
+ public void save() throws IOException {
+ if (this.xmlFileName != null) {
+ saveAs(xmlFileName);
+ } else {
+ System.out.println("Sorry, can not save to resource within jar, will create file called " + SdoSampleConstants.PO_XML_RESOURCE
+ + " and save");
+ saveAs(SdoSampleConstants.PO_XML_RESOURCE);
+ }
+ }
+
+ /**
+ * Saves modified purchaseOrder DataObject to file
+ *
+ * @param fileName.
+ * Location to save file to save DataObject to
+ * @throws IOException
+ */
+ public void saveAs(String fileName) throws IOException {
+ OutputStream stream = new FileOutputStream(fileName);
+ XMLHelper.INSTANCE.save(purchaseOrder, SdoSampleConstants.PO_NAMESPACE, "purchaseOrder", stream);
+ System.out.println("Saved to " + fileName);
+ }
+
+ // PRINTING METHODS TO SYSTEM.OUT
+
+ /**
+ * Prints purchase order summary to System.out
+ */
+
+ public void printChangeSummary() {
+
+ // TODO: change summary is not currently working
+ ChangeSummary changeSummary = getChangeSummary();
+ boolean logging = changeSummary.isLogging();
+ if (!logging) {
+ System.out.println("Change logging is disabled");
+ } else {
+ System.out.println("Change logging is enabled");
+ }
+ // TODO: complete this method
+
+ }
+
+ /**
+ * Uses simple sample PrintPropertiesOfDataObject
+ */
+ public void printPurchaseOrderProperties() throws Exception {
+ // use previously created method from simple sample
+ PrintPropertiesOfDataObject.printDataObjectProperties(purchaseOrder);
+ }
+
+ /**
+ * Accesses key properties via name and prints to System.out
+ *
+ */
+ public void printPurchaseOrderSummary() {
+ // use previously created method from simple sample
+ // for an example of accessing properties by name see
+ // AccessDataObjectPropertiesByName or
+ // the method below printAddressInfo
+ AccessDataObjectPropertiesByName.printPurchaseOrderSummary(purchaseOrder);
+ }
+
+ /**
+ * Accesses properties of USAddress Type by name and prints to System.out
+ *
+ * @param dataObject.
+ * Either shipTo and billTo property of purchase order
+ */
+ private void printAddressInfo(DataObject dataObject) {
+ System.out.println(" name: " + dataObject.get("name"));
+ System.out.println(" street: " + dataObject.get("street"));
+ System.out.println(" city: " + dataObject.get("city"));
+ System.out.println(" state: " + dataObject.get("state"));
+ System.out.println(" zip: " + dataObject.get("zip"));
+ }
+
+ /**
+ * Prints properties of shipTo
+ *
+ */
+ public void printBillTo() {
+ DataObject billTo = purchaseOrder.getDataObject("billTo");
+ printAddressInfo(billTo);
+ }
+
+ /**
+ * Prints properties of billTo
+ *
+ */
+ public void printShipTo() {
+ DataObject shipTo = purchaseOrder.getDataObject("shipTo");
+ printAddressInfo(shipTo);
+ }
+
+ // GETTERS AND SETTERS
+
+ /**
+ * Sets the mechanism to set decimal property for zip code Decimals can be set in
+ * a number of ways. This scenario controls how this class will set the zip code
+ * when modifying address
+ */
+ public void setDecimalPropertyMethod(int i) {
+ decimalPropertyMethod = i;
+ }
+
+ /**
+ * Gets the mechanism used to zip decimal property for the zip code of an address
+ *
+ * @return
+ */
+ public int getDecimalPropertyMethod() {
+ return decimalPropertyMethod;
+ }
+
+ // METHODS TO GET AND COPY DATAOBJECTS
+ /**
+ * @return purchaseOrder DataObject
+ */
+ public DataObject getPurchaseOrder() {
+ return purchaseOrder;
+ }
+
+ /**
+ *
+ * @return deep copy of the purchase order DataObject
+ */
+ public DataObject getDeepCopyPurchaseOrder() {
+ return CopyHelper.INSTANCE.copy(purchaseOrder);
+
+ }
+
+ /**
+ *
+ * @return shallow copy of the purchase order DataObject
+ */
+ public DataObject getShallowCopyPurchaseOrder() {
+ return CopyHelper.INSTANCE.copyShallow(purchaseOrder);
+ }
+
+ /**
+ * Currently this does not work
+ *
+ * @return Change summary for purchaseOrder
+ *
+ */
+ public ChangeSummary getChangeSummary() {
+ // TODO: why is the data graph null
+ return purchaseOrder.getDataGraph().getChangeSummary();
+ }
+
+ /**
+ *
+ * @return XMLDocument that represents current purchase order DataObject
+ */
+ public XMLDocument getXMLDocuement() {
+ return XMLHelper.INSTANCE.createDocument(purchaseOrder, SdoSampleConstants.PO_NAMESPACE, "purchaseOrder");
+ }
+
+ /**
+ *
+ * @return String xml representation of current purcahse order DataObject
+ */
+ public String getXMLString() {
+ // alternativly could use the following
+ // return XMLHelper.INSTANCE.createDocument(purchaseOrder, PO_NAMESPACE,
+ // "purchaseOrder").toString();
+ return XMLHelper.INSTANCE.save(purchaseOrder, SdoSampleConstants.PO_NAMESPACE, "purchaseOrder");
+ }
+
+ // PRIVATE HELPER METHODS
+
+ /**
+ * Uses a variety of ways to set a decimal property on a DataObject
+ *
+ * @param dataObject
+ * @param propertyName
+ * @param propertyValue
+ */
+ private void setDecimalPropertyOnDataObject(DataObject dataObject, String propertyName, String propertyValue) {
+ // since the type specified in the xsd is of type decimal when we
+ // wish to just use the set method one must construct a BigDecimal or
+ // else a ClassCastException will occur
+ // alternativly one could call dataObject.setString(zip) or
+ // dataObject.setDouble( Double.valueOf(zip))
+ switch (decimalPropertyMethod) {
+
+ case (SET_DECIMAL_PROPERTY_VIA_SET): {
+ dataObject.set(propertyName, new BigDecimal(propertyValue));
+ break;
+ }
+ case (SET_DECIMAL_PROPERTY_VIA_SET_DOUBLE): {
+ dataObject.setDouble(propertyName, Double.valueOf(propertyValue).doubleValue());
+ break;
+ }
+ case (SET_DECIMAL_PROPERTY_VIA_SET_STRING): {
+ dataObject.setString(propertyName, propertyValue);
+ break;
+ }
+ default: {
+ dataObject.setString(propertyName, propertyValue);
+ }
+
+ }// end of switch
+
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/ReadPurchaseOrder.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/ReadPurchaseOrder.java
new file mode 100644
index 0000000000..1e9b8e7c2d
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/ReadPurchaseOrder.java
@@ -0,0 +1,143 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.otherSources;
+
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.List;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.XMLDocument;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+/**
+ * Reads purchase order DataObject from XML, based upon Fuhwei Lwo's paper <A
+ * HREF="http://www-128.ibm.com/developerworks/webservices/library/ws-sdoxmlschema/"
+ * title="Me" onClick="checkLinks(this)">Create and read an XML document based on XML
+ * Schema</A>. Uses file
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_GENERATED}
+ * previously generated by {@link CreatePurchaseOrder} to populate purchase order
+ * DataObject
+ *
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.otherSources.ReadPurchaseOrder</LI>
+ * </UL>
+ *
+ * @author Fuhwei Lwo
+ * @author Robbie Minshall
+ */
+public class ReadPurchaseOrder {
+
+ private static void definePOTypes() throws Exception {
+ InputStream is = ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE);
+ if (is == null) {
+ System.out.println("InputStream is null");
+ } else {
+ System.out.println("Obtained Input Stream from resource");
+ }
+ XSDHelper.INSTANCE.define(is, null);
+ is.close();
+ }
+
+ public static void main(String[] args) {
+ System.out.println("***************************************");
+ System.out.println("SDO Sample ReadPurchaseOrder");
+ System.out.println("***************************************");
+ System.out.println("This sample is based upon Fuhwei Lwo's paper http://www-128.ibm.com/developerworks/webservices/library/ws-sdoxmlschema/");
+ System.out.println("previously generated by CreatePurchaseOrder sample to populate purchase order DataObject");
+
+ System.out.println("***************************************");
+
+ try {
+ definePOTypes();
+
+ FileInputStream fis = null;
+ try {
+ fis = new FileInputStream(SdoSampleConstants.PO_XML_GENERATED);
+ } catch (Exception e) {
+ System.out.println("Sorry an error occcured, please run CreatePurchaseOrder sample first");
+ System.out.println("Exception : " + e.toString());
+ e.printStackTrace();
+ return;
+ }
+
+ XMLDocument xmlDoc = XMLHelper.INSTANCE.load(fis);
+
+ DataObject purchaseOrder = xmlDoc.getRootObject();
+
+ System.out.println("Order date: " + purchaseOrder.get("orderDate"));
+ System.out.println("Comment: " + purchaseOrder.get("comment"));
+
+ DataObject shipTo = purchaseOrder.getDataObject("shipTo");
+ System.out.println("Ship to name: " + shipTo.get("name"));
+
+ DataObject billTo = purchaseOrder.getDataObject("billTo");
+ System.out.println("Bill to name: " + billTo.get("name"));
+ System.out.println();
+
+ DataObject items = purchaseOrder.getDataObject("items");
+ List itemList = items.getList("item");
+ for (int i = 0; i < itemList.size(); i++) {
+
+ DataObject item = (DataObject) itemList.get(i);
+
+ System.out.println("Part num: " + item.get("partNum"));
+ System.out.println("Product name: " + item.get("productName"));
+ } // for
+ } catch (Exception e) {
+ System.out.println("Sorry an error was encountered " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/package.html b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/package.html
new file mode 100644
index 0000000000..357048094b
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/package.html
@@ -0,0 +1,42 @@
+<html>
+<!--
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * -->
+<head>
+ <title>package information</title>
+</head>
+<body>
+
+Provides examples from sources other than the SDO specification. These examples are from whitepapers,
+ or are custom examples designed to show SDO in a more elegant light than the specification. They
+ also include basic interactive examples, and useful scenarios not outlined in the specification.
+ The user is encouraged to modify and execute these code examples.
+
+<h2>Related Documentation</h2>
+
+<h2>Related Documentation</h2>
+For overviews, tutorials, examples, guides, and tool documentation, please see
+
+<ul>
+ <li><a href="http://incubator.apache.org/tuscany/java_sdo_overview.html"> SDO Java Overview</a> section of the Tuscany website.</li>
+ <li><a href="http://www-128.ibm.com/developerworks/webservices/library/ws-sdoxmlschema/">Create and read an XML document based on XML Schema</a></li>
+</ul>
+
+</body>
+</html> \ No newline at end of file
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/overview.html b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/overview.html
new file mode 100644
index 0000000000..17310acb4d
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/overview.html
@@ -0,0 +1,57 @@
+<html>
+<head>
+ <title>SDO Sample Overview</title>
+</head>
+<BODY>
+<h1>Tuscany Service Data Object (SDO) Sample Programs</h1>
+<h2>Overview</h2>
+This sample code is from the <a href="http://incubator.apache.org/tuscany"
+ target="_blank"> Apache Tuscany</a> project. The samples were written to help users
+understand and experiment with SDO. They are grouped into three packages according to the source of the example material. Some are based upon example code contained
+within the <a
+ href="http://osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ target="_bank">SDO Specification</a>, and are intended to be read alongside the specification. Others are based upon papers and published material
+ (see the documentation for each sample for the reference to the publication),
+ or were simply written specifically for these samples.
+<H2>Experimentation</H2>
+The SDO samples are written to be used in conjunction with the specification and other documents. However,
+the most important function that these samples provide is a place to begin experimenting with the SDO API
+and programming model. Developers are encouraged to extend and experiment with the current set of samples.
+If a sample is unclear, is missing documentation or a tutorial, or does not cover a desired area ,
+please send feedback to the tuscany mailing lists,
+or better still send us a patch or join the Tuscany team and contribute back to the
+<A href="http://incubator.apache.org/tuscany">project</A>.
+
+
+<h3>Getting Ready to run the Samples</h3>
+The samples depend on the following libraries<UL>
+ <LI>The SDO API and the Tuscany SDO Implementation
+ <UL>
+ <LI>sdo-api-{version}.jar - SDO API</LI>
+ <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation</LI>
+ </UL>
+ </LI>
+ <LI>EMF dependencies.
+ <UL>
+ <LI>emf-common-{version}.jar - some common framework utility and base
+ classes</LI>
+ <LI>emf-ecore-{version}.jar - the EMF core runtime implementation
+ classes (the Ecore metamodel)</LI>
+ <LI>emf-ecore-change-{version}.jar - the EMF change recorder and
+ framework</LI>
+ <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI)
+ serializer and loader</LI>
+ <LI>xsd-{version}.jar - the XML Schema model</LI></UL></LI></UL>
+<P>If you are running these samples from a Tuscany samples distribution you can resolve these dependencies by ensuring that all the jar files in the lib directory of the extracted distribution are on your classpath.<BR>
+<BR>
+If you are running these samples as a result of building Tuscany SDO for Java from a Tuscany source distribution using maven then these dependencies will be resolved by having followed the build instructions.<BR>
+<BR>To run these samples against the current Tuscany codebase, follow the instructions at <A
+ href="http://incubator.apache.org/tuscany/java_sdo_overview.html">Tuscany SDO Java Overview</A>, which describes how to build Tuscany SDO for Java in an Eclipse SDK environment.</P>
+<H3>Running the Samples</H3>
+<P>In order to get an overview of the samples the user may wish to run
+{@link org.apache.tuscany.samples.sdo.ExecuteSamples} which will execute
+all the command line examples.<BR>
+<FONT face="Courier">java org.apache.tuscany.samples.sdo.ExecuteSamples</FONT></P><P>Alternatively, browse the JavaDoc for the sample program classes and run an
+individual example of your choice in the same way. Another suggestion is to read the
+SDO specifciaton in conjunction with the examples in {@link
+org.apache.tuscany.samples.sdo.specExampleSection}.</P></BODY></html>
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/package.html b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/package.html
new file mode 100644
index 0000000000..68ff7a8914
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/package.html
@@ -0,0 +1,33 @@
+<html>
+<!--
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * -->
+<head>
+ <title>package information</title>
+</head>
+<body>
+Provides common constants and a command line java interface for executing samples.
+<h2>Related Documentation</h2>
+For overviews, tutorials, examples, guides, and tool documentation, please see the
+<a href="http://incubator.apache.org/tuscany/java_sdo_overview.html"> SDO Java Overview
+</a>
+section of the Tuscany website.
+
+</body>
+</html> \ No newline at end of file
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectPropertiesByName.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectPropertiesByName.java
new file mode 100644
index 0000000000..7aa9909657
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectPropertiesByName.java
@@ -0,0 +1,154 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specCodeSnippets;
+
+import java.util.List;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+/**
+ * Demonstrates accessing a created DataObject's properties by name.
+ *
+ * This sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon code snippets contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>.
+ *
+ * <br>
+ * <br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specCodeSnippets.AccessDataObjectPropertiesByName</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ */
+public class AccessDataObjectPropertiesByName {
+
+ /**
+ * Prints a subset of PurchaseOrder properties to System.out
+ *
+ * @param purchaseOrder.
+ * DataObject defined by Types in
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE}
+ */
+ public static void printPurchaseOrderSummary(DataObject purchaseOrder) {
+
+ // example accessing properties via property names
+ System.out.println("Purchase Order: ");
+ System.out.println(" Order date: " + purchaseOrder.get("orderDate"));
+ System.out.println(" Comment: " + purchaseOrder.get("comment"));
+
+ DataObject shipTo = purchaseOrder.getDataObject("shipTo");
+ System.out.println(" Ship to name: " + shipTo.get("name"));
+
+ DataObject billTo = purchaseOrder.getDataObject("billTo");
+ System.out.println(" Bill to name: " + billTo.get("name"));
+
+ DataObject items = purchaseOrder.getDataObject("items");
+ List itemList = items.getList("item");
+
+ System.out.println(" Items:");
+ for (int i = 0; i < itemList.size(); i++) {
+ DataObject item = (DataObject) itemList.get(i);
+ System.out.println(" Item " + i);
+ System.out.println(" Part num: " + item.get("partNum"));
+ System.out.println(" Product name: " + item.get("productName"));
+ }
+ }
+
+ /**
+ * Prints properties of a purchase order DataObject( properties are defined in
+ * the xsd
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} and
+ * populated by xml
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_RESOURCE} )
+ *
+ * @param args.
+ * No parameters required.
+ */
+ public static void main(String[] args) {
+
+ // information
+ System.out.println("***************************************");
+ System.out.println("SDO Sample AccessDataObjectPropertiesByName");
+ System.out.println("***************************************");
+ System.out.println("This sample will access a DataObject properties by name");
+ System.out.println("***************************************");
+
+ // create a DataObejct
+ DataObject purchaseOrder = null;
+ try {
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null);
+ purchaseOrder = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XML_RESOURCE)).getRootObject();
+ System.out.println("DataObject created");
+ } catch (Exception e) {
+ System.out.println("Error creating DataObject " + e.toString());
+ e.printStackTrace();
+ return;
+ }
+
+ // start of sample
+ try {
+ System.out.println("Accessing properties by name");
+ printPurchaseOrderSummary(purchaseOrder);
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error accessing properties by name " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+ // end of sample
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectUsingValidXPath.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectUsingValidXPath.java
new file mode 100644
index 0000000000..3906ed3c32
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectUsingValidXPath.java
@@ -0,0 +1,146 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specCodeSnippets;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+
+/**
+ * Demonstrates accessing a created DataObject's properties using xPath.
+ *
+ * This sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon code snipets contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://download.boulder.ibm.com/ibmdl/pub/software/dw/specs/ws-sdo/SDO_Specification_Java_V2.01.pdf"
+ * target="_bank">SDO Specification</a>. <br>
+ * <br>
+ * It is worth noting that the Examples section of the specification, and the
+ * {@link org.apache.tuscany.samples.sdo.specExampleSection.AccessingDataObjectsUsingXPath}
+ * use unpure xpath expression. This sample attempts to demonstrate a more appropiate
+ * subset of xPath operations.<br>
+ * <br>
+ * <br>
+ * <br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependencies </a> </LI>
+ * Execute: java
+ * org.apache.tuscany.samples.sdo.specCodeSnippets.AccessDataObjectUsingValidXPath
+ *
+ * @author Robbie Minshall
+ */
+public class AccessDataObjectUsingValidXPath {
+
+ /**
+ * Prints a subset of item properties to System.out where the individual item was
+ * accessed using an xPath expression
+ *
+ * @param purchaseOrder.
+ * DataObject defined by Types in
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE}
+ */
+ public static void accessDataObjectUsingXPath(DataObject purchaseOrder) {
+
+ System.out.println("Accessing individual item from list using xpath");
+ // TODO: use variety of xpath expressions such as items/item[1]
+ // TODO: add to junit test cases for test cases above
+ DataObject item = purchaseOrder.getDataObject("items/item[1]");
+ System.out.println("Item toString : " + item.toString());
+
+ System.out.println("Item name:" + item.get("productName"));
+ System.out.println("Part num: " + item.get("partNum"));
+
+ }
+
+ /**
+ * Accesses and modifies properties of a purchase order DataObject using xPath(
+ * properties are defined in the xsd
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} and
+ * populated by xml
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_RESOURCE} )
+ *
+ * @param args.
+ * No parameters required.
+ */
+ public static void main(String[] args) {
+
+ // information
+ System.out.println("***************************************");
+ System.out.println("SDO Sample AccessDataObjectUsingValidXPath");
+ System.out.println("***************************************");
+ System.out.println("Demonstrats accessing a created DataObject's properties using xPath.");
+ System.out.println("***************************************");
+
+ // create a DataObejct
+ DataObject purchaseOrder = null;
+ try {
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null);
+ purchaseOrder = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XML_RESOURCE)).getRootObject();
+ System.out.println("DataObject created");
+ } catch (Exception e) {
+ System.out.println("Error creating DataObject " + e.toString());
+ e.printStackTrace();
+ return;
+ }
+
+ // start of sample
+ try {
+
+ accessDataObjectUsingXPath(purchaseOrder);
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error accessing properties by name " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+ // end of sample
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXmlString.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXmlString.java
new file mode 100644
index 0000000000..b58c7070e9
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXmlString.java
@@ -0,0 +1,163 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.samples.sdo.specCodeSnippets;
+
+import java.util.List;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.XMLHelper;
+
+/**
+ * Demonstrates creating a DataObject from a String of XML.
+ *
+ * The following sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon code snippets contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>. Specifically this sample is based upon section titled "Creating DataObjects from XML documents" <br>
+ * <br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specCodeSnippets.CreateDataObjectFromXmlString</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ */
+public class CreateDataObjectFromXmlString {
+
+ /**
+ * String representing a PurchaseOrder in XML format
+ */
+ public static final String XML_STRING = "<purchaseOrder orderDate='1999-10-20'>" + " <shipTo country='US'>" + " <name>Alice Smith</name>"
+ + " <street>123 Maple Street</street>" + " <city>Mill Valley</city>" + " <state>PA</state>" + " <zip>90952</zip>" + "</shipTo>"
+ + "</purchaseOrder>";
+
+ /**
+ * Creates a DataObject from an Xml String
+ *
+ * @return
+ */
+ public static DataObject createDataObjectFromXmlString() {
+
+ // TODO: do this with and without defining the schema
+ DataObject po = XMLHelper.INSTANCE.load(XML_STRING).getRootObject();
+ System.out.println("DataObject has been created : " + po.toString());
+ return po;
+ }
+
+ /**
+ * @param args
+ * None required
+ */
+ public static void main(String[] args) {
+
+ // information
+ System.out.println("***************************************");
+ System.out.println("SDO Sample CreateDataObjectFromXmlString");
+ System.out.println("***************************************");
+ System.out.println("Demonstrats creating a DataObject from a String of XML, based upon section titled 'Creating DataObjects from XML documents'");
+ System.out.println("***************************************");
+
+ // sample
+ try {
+
+ System.out.println("Use the following XML String: " + XML_STRING);
+ System.out.println("Creating DataObject");
+ DataObject po = createDataObjectFromXmlString();
+ System.out.println("The following DataObject sucessfully created from XML String");
+
+ System.out.println(XMLHelper.INSTANCE.save(po, "http://example.com/purchaseOrder", "purchaseOrder"));
+
+ /*
+ * Without a Type definition accessing a DataObject is slightly more
+ * difficult. This is because without a Type definition SDO does not know
+ * the intended multiplicity of properties. Currently the Tuscany
+ * implementation assumes that elements and attributes are lists. There
+ * is some discussion (SDO-3, also mentioned in SDO-22) about adding
+ * annotations to the XML (sdo:many="false") to control the indended
+ * multiplicity of DataObjects created without a model.
+ *
+ * The following code will not work when a model/schema is not defined
+ * shipTo = purchaseOrder.getDataObject("shipTo"); assertEquals("testing
+ * ship to name", shipTo.getString("name"), "Alice Smith");
+ * assertEquals("testing ship to zip", shipTo.getString("zip"), "90952");
+ */
+
+ System.out.println("Programatically access content of DataObject - getting name of the person this is getting shipped to");
+ // access the shipTo DataObject
+ List shipToList = po.getList("shipTo");
+ DataObject shipTo = (DataObject) shipToList.get(0);
+ // access the name DataObject
+
+ /*
+ * This is currently quite cumbersome. In future specifications accessing
+ * the name element will be simplier and the user could simply execute:
+ * List nameList = shipTo.getList("name"); String actualName = (String)
+ * nameList.get(0);
+ *
+ * Or if name is an attribute ("<shipTo country='US' name='Alice
+ * Smith'>") simply: String actualName = shipTo.getString("name");
+ *
+ * There are currently more elegant means to perform this task using the xPath support
+ */
+ List nameList = shipTo.getList("name");
+ DataObject name = (DataObject) nameList.get(0);
+ // access the contents of the name DataObject
+ List textList = name.getList("text");
+ String actualName = (String) textList.get(0);
+ System.out.println("Name being shipped to: " + actualName);
+
+ } catch (Exception e) {
+ System.out.println("Sorry, exception caught when creating DataObject : " + e.toString());
+ e.printStackTrace();
+ }
+ // end of sample
+ System.out.println("GoodBye");
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXsdAndXmlFiles.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXsdAndXmlFiles.java
new file mode 100644
index 0000000000..6f1d8c60db
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXsdAndXmlFiles.java
@@ -0,0 +1,196 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specCodeSnippets;
+
+import java.io.InputStream;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.XMLDocument;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+
+
+/**
+ * Demonstrates defining Types by using a predefined xsd resource, and populating a DataObject by using a previously created xml file.
+ *
+ * The following sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon code snippets contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>. <br>
+ * <br>
+ * <br>Uses {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} and {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_RESOURCE} in order to define purcahseOrder DataObjects
+ * <br><br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specCodeSnippets.CreateDataObjectFromXsdAndXmlFiles</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ */
+public class CreateDataObjectFromXsdAndXmlFiles {
+
+ public static boolean typesDefined = false;
+
+
+ /**
+ * Loads the {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} resource in order to define PurchaseOrder
+ * Types
+ */
+ public static void definePurchaseOrderTypeUsingXsdResource()
+ throws Exception {
+
+ InputStream is = null;
+ try {
+ System.out.println("Attempting to define types using resource "
+ + SdoSampleConstants.PO_XSD_RESOURCE);
+ is = ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE);
+ if (is == null) {
+ System.out.println("InputStream is null, you may need to add the resources directory is on the classpath");
+ throw new Exception("InputStream for " + SdoSampleConstants.PO_XSD_RESOURCE + " was null, ensure that it is located on the classpath");
+ } else {
+ System.out.println("Obtained Input Stream from resoruce");
+ }
+ XSDHelper.INSTANCE.define(is, null);
+
+ } catch (Exception e) {
+ System.out
+ .println("Exception caught defining Types from resoruce stream to "
+ + SdoSampleConstants.PO_XSD_RESOURCE);
+ throw e;
+ } finally {
+ // try to close the input stream
+ if (is != null) {
+ try {
+ is.close();
+ System.out.println("Closed input stream");
+ } catch (Exception e) {
+ System.out.println("could not close input stream " + e.toString());
+ }
+ }
+ }
+ }
+
+ /**
+ * Uses resource {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_RESOURCE} to populate DataObject
+ * @return populated purchase order DataObject
+ */
+ public static DataObject createPurchaseOrderDataObjectUsingXmlResource()
+ throws Exception {
+
+ if (! typesDefined){
+ definePurchaseOrderTypeUsingXsdResource();
+ }
+ DataObject purchaseOrder = null;
+ InputStream is = null;
+
+ try {
+
+ System.out
+ .println("Using resource as stream to access xml to populate DataObjects");
+ // FileInputStream fis = new FileInputStream(xmlFileName);
+ is = ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XML_RESOURCE);
+ if (is == null) {
+ System.out.println("InputStream is null");
+ } else {
+ System.out
+ .println("Successfully obtained InputStream from resource");
+ }
+ XMLDocument xmlDoc = XMLHelper.INSTANCE.load(is);
+ purchaseOrder = xmlDoc.getRootObject();
+
+ } catch (Exception e) {
+ System.out.println("Could not use resourceAsStream "
+ + e.toString());
+ e.printStackTrace();
+ throw e;
+ }finally {
+ // try to close the input stream
+ if (is != null) {
+ try {
+ is.close();
+ System.out.println("Closed input stream");
+ } catch (Exception e) {
+ System.out.println("Could not close input stream " + e.toString());
+ }
+ }
+ }
+
+ return purchaseOrder;
+ }
+
+ /**
+ * @param args. No arguments required
+ */
+ public static void main(String[] args) {
+ // information
+ System.out.println("***************************************");
+ System.out.println("SDO Sample CreateDataObjectFromXsdAndXmlFiles");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates defining Types by using a predefined xsd resource, and populating a DataObject by using a previously created xml file");
+ System.out.println("***************************************");
+
+
+ try {
+ // define the types
+ definePurchaseOrderTypeUsingXsdResource();
+
+ // create a dataObject
+ DataObject purchaseOrder = createPurchaseOrderDataObjectUsingXmlResource();
+ System.out.println("DataObject sucessfully created");
+
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error encountered " + e.toString());
+ }
+ System.out.println("GoodBye");
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/DynamicCustomerTypeSample.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/DynamicCustomerTypeSample.java
new file mode 100644
index 0000000000..7df6e9fc93
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/DynamicCustomerTypeSample.java
@@ -0,0 +1,192 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specCodeSnippets;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.Type;
+import commonj.sdo.helper.DataFactory;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.TypeHelper;
+import java.util.List;
+
+/**
+ * Demonstrates creating and using Types dynamically. This sample is from the <a
+ * href="http://incubator.apache.org/tuscany" target="_blank"> Apache Tuscany</a>
+ * project. It was written to help users understand and experiement with SDO. It is
+ * based upon code snippets contained within, and is meant for use with, and reference
+ * to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>. Specifically, this sample is based upon the
+ * section titled "Defining SDO Types Dynamically" <br>
+ * <br> * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specCodeSnippets.DynamicCustomerTypeSample</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ */
+public class DynamicCustomerTypeSample {
+
+ /**
+ * XSD file used to define the model and Types for Customer Dataobject
+ */
+ public static final String CUSTOMER_MODEL = "customer.xsd";
+
+ /**
+ * Default file name to use when generated customer XSD files.
+ *
+ * @see org.apache.tuscany.samples.sdo.specCodeSnippets.CreateXmlAndXsdFromDataObject
+ */
+ public static final String CUSTOMER_MODEL_GENERATED = "generatedCustomer.xsd";
+
+ /**
+ * Fake namespace used for Customer
+ */
+ public static final String CUSTOMER_NAMESPACE = "http://www.example.com/customer";
+
+ /**
+ * XML file used to populate Customer DataObjects
+ */
+ public static final String CUSTOMER_XML = "customer.xml";
+
+ /**
+ * Default file name to use when generated customer XML files.
+ *
+ * @see org.apache.tuscany.samples.sdo.specCodeSnippets.CreateXmlAndXsdFromDataObject
+ */
+ public static final String CUSTOMER_XML_GENERATED = "generatedCustomer.xml";
+
+ /**
+ * Method dynamically defines customer types
+ */
+ public static void defineCustomerTypes() {
+ // get an instance of the type helper
+ TypeHelper types = TypeHelper.INSTANCE;
+ Type intType = types.getType("commonj.sdo", "Int");
+ Type stringType = types.getType("commonj.sdo", "String");
+
+ // create a new Type for Customers
+ DataObject customerType = DataFactory.INSTANCE.create("commonj.sdo", "Type");
+ customerType.set("uri", "http://example.com/customer");
+ customerType.set("name", "Customer");
+
+ // create a customer number property
+ DataObject custNumProperty = customerType.createDataObject("property");
+ custNumProperty.set("name", "custNum");
+ custNumProperty.set("type", intType);
+
+ // create a last name property
+ DataObject lastNameProperty = customerType.createDataObject("property");
+ lastNameProperty.set("name", "lastName");
+ lastNameProperty.set("type", stringType);
+
+ // create a first name property
+ DataObject firstNameProperty = customerType.createDataObject("property");
+ firstNameProperty.set("name", "firstName");
+ firstNameProperty.set("type", stringType);
+
+ // now define the Customer type so that customers can be made
+ types.define(customerType);
+
+ Type testType = TypeHelper.INSTANCE.getType("http://example.com/customer", "Customer");
+ List props = testType.getProperties();
+ for (int i = 0; i < props.size(); i++) {
+ System.out.println(props.get(i));
+ }
+ }
+
+ /**
+ * Creates a new Customer using dynamically defined Customer Type
+ *
+ * @param custNum
+ * @param firstName
+ * @param lastName
+ * @return
+ */
+ private static DataObject createCustomer(int custNum, String firstName, String lastName) {
+ DataFactory factory = DataFactory.INSTANCE;
+ DataObject customer1 = factory.create("http://example.com/customer", "Customer");
+ customer1.setInt("custNum", custNum);
+ customer1.set("firstName", firstName);
+ customer1.set("lastName", lastName);
+ return customer1;
+ }
+
+ /**
+ * Drives sample methods
+ *
+ * @param args
+ * no arguments required
+ */
+ public static void main(String args[]) {
+ System.out.println("***************************************");
+ System.out.println("SDO Sample DynamicCustomerTypeSample");
+ System.out.println("***************************************");
+ System.out.println("Demonstrats creating and using Types dynamically");
+ System.out.println("***************************************");
+ try {
+ System.out.println("Dynamically defining Customer Type");
+ defineCustomerTypes();
+
+ System.out.println("Creating new customer DataObject");
+ DataObject customer1 = createCustomer(1, "John", "Adams");
+ System.out.println("Customer Created");
+
+ System.out.println("converting to xml");
+ String xmlDocString = XMLHelper.INSTANCE.save(customer1, CUSTOMER_NAMESPACE, "customer");
+ System.out.println(xmlDocString);
+
+ } catch (Exception e) {
+ System.out.println("Sorry unexpected exception caught during sample execution : " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/ObtainingDataGraphFromXml.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/ObtainingDataGraphFromXml.java
new file mode 100644
index 0000000000..69c2098798
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/ObtainingDataGraphFromXml.java
@@ -0,0 +1,189 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specCodeSnippets;
+
+import java.io.InputStream;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+import org.apache.tuscany.sdo.util.SDOUtil;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import commonj.sdo.DataObject;
+import commonj.sdo.DataGraph;
+import commonj.sdo.Sequence;
+
+/**
+ * Demonstrates a vareity of methods available to obtain the root DataObject
+ * contained within an xml representation of a DataGraph. This is currently a grey
+ * area of the specification and this samples demonstrates spec complant means, as
+ * well as utility methods that have been added to Tuscany to address issues within
+ * the specification.
+ *
+ * The following sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon example code contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>. This sample addresses confusing aspects of
+ * the the AccessDataObjectsUsingXPath example from the Examples section of the SDO
+ * specification.<br>
+ * <br>
+ * To define the correct Types for each DataObject ( CompanyType, DepartmentType etc )
+ * this sample relies upon
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#COMPANY_XSD} which is
+ * provided in the resources directory of these samples. The xml file
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#COMPANY_DATAGRAPH_XML} is
+ * used to load the DataGraph and is also located in this resources directory. <br>
+ * <br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specCodeSnippets.ObtainingDataGraphFromXml</LI>
+ * </UL>
+ *
+ * @see org.apache.tuscany.samples.sdo.specExampleSection.AccessDataObjectsUsingXPath
+ * @author Robbie Minshall
+ */
+
+public class ObtainingDataGraphFromXml {
+
+ /**
+ * Execute this method in order to run the sample.
+ *
+ * @param args
+ */
+ public static void main(String[] args) {
+ System.out.println("***************************************");
+ System.out.println("SDO Sample ObtainingDataGraphFromXml");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates a vareity of methods available to obtain the root DataObject"
+ + "contained within an xml representation of a DataGraph.");
+ System.out.println("***************************************");
+
+
+ // define Types
+ try {
+ System.out.println("Defining Types using XSD");
+ InputStream is = null;
+ is = ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_XSD);
+ XSDHelper.INSTANCE.define(is, null);
+ is.close();
+ System.out.println("Type definition completed");
+ } catch (Exception e) {
+ System.out.println("Exception caught defining types " + e.toString());
+ e.printStackTrace();
+ }
+
+ try {
+
+ /**
+ * A DataGraph is an optional envelope for a graph of DataObjects and an
+ * associated ChangeSummary.
+ *
+ * The specification group is currently investigating options regarding
+ * obtaining a DataGraph from an xml file as well as the role of the
+ * DataGraph within the SDO specification.
+ *
+ * In order to obtain the rootObject contained within the specification
+ * the specification demonstrates reading in an XML file and obtaining a
+ * DataObject that represents the datagraph contained within the file.
+ * Note, that the specification does not obtain a DataGraph - there are
+ * not mechanisms within the specification for obtaining a DataGraph from
+ * xml. Obtaining the root DataObject from this datagraph representation
+ * is quite cumbersome and is demonstrated below.
+ *
+ * In order to address this current limitation within the specification
+ * Tuscany has added a utility method to SDOUtil that allows a user to
+ * obtain an actual DataGraph from xml.
+ */
+
+ DataObject company = null;
+
+ /**
+ * Use specification mechanism for obtaining a DataObject that represents
+ */
+ // the a datagraph
+ System.out.println("Using specification methods to obtain DataObject represeenting a datagraph from xml");
+
+ // When the xml file represents a DataGraph the root Object
+ // of the XMLDocument is a DataGraph
+ DataObject dataObjectRepresentingDataGraph = XMLHelper.INSTANCE.load(
+ ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_DATAGRAPH_XML)).getRootObject();
+
+ // Obtain the company DataObject from the DataObject representing the datagraph
+ Sequence mySeq = (Sequence) dataObjectRepresentingDataGraph.getSequence("any");
+ company = (DataObject) mySeq.getValue(0);
+
+ System.out.println("Obtained DataObject representing datagraph");
+ System.out.println(dataObjectRepresentingDataGraph);
+ System.out.println("Obtained root DataObject from datagraph");
+ System.out.println(company);
+ System.out.println();
+
+
+ /**
+ * Use utility method to obtain an actual DataGraph from the xml
+ */
+ DataGraph datagraph = SDOUtil.loadDataGraph(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_DATAGRAPH_XML), null);
+ // Obtain the company DataObject from the actual DataGraph
+ company = datagraph.getRootObject();
+ System.out.println("Obtained actual DataGraph:");
+ System.out.println(datagraph);
+ System.out.println("Obtained root DataObject from DataGraph:");
+ System.out.println(company);
+ System.out.println();
+
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error encountered " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/PrintPropertiesOfDataObject.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/PrintPropertiesOfDataObject.java
new file mode 100644
index 0000000000..453cec211a
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/PrintPropertiesOfDataObject.java
@@ -0,0 +1,177 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specCodeSnippets;
+
+import java.util.List;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.Property;
+import commonj.sdo.Type;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+
+/**
+ * Demonstrates iterating through, and inspecting properites of a DataObject. This
+ * sample shows listing properties of a dataObject, determining if the properties are
+ * set, are simple properties or represent DataObjects <br>
+ * <br>
+ * This sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon code snippets contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>. This specific examples was based upon code
+ * snippets in the section titled 'Property Indexes'. <br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specCodeSnippets.PrintPropertiesOfDataObject</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ */
+
+public class PrintPropertiesOfDataObject {
+
+ /**
+ * Calls recursive method to inspect and print properties of a DataObject.
+ *
+ * @param DataObject.
+ * The DataObject to print the properties of
+ */
+ public static void printDataObjectProperties(DataObject myDo) throws Exception {
+ System.out.println("Recursivly inspecting and printing properties of DataObject");
+ printAndInspectDataObject(" ", myDo);
+ }
+
+ /**
+ * Recursivly inspects and prints DataObject. This method will list all set
+ * properties
+ *
+ * @param buffer.
+ * Blank string to help with formating
+ * @param myDo.
+ * DataObject to recursivly inspect and print the properties of.
+ * @throws Exception
+ */
+ private static void printAndInspectDataObject(String buffer, DataObject myDo) throws Exception {
+
+ for (int i = 0; i < myDo.getInstanceProperties().size(); i++) {
+ Property p = (Property) myDo.getInstanceProperties().get(i);
+ // just print out the name, type and string value of the property
+ Type type = p.getType();
+ System.out.print(buffer + " " + p.getName() + ",type=" + type.getName());
+ if (myDo.isSet(p)) {
+ // see if this represents a simple property or if the type is also a
+ // datatype
+ if (type.isDataType()) {
+ System.out.println(", simple type : " + myDo.get(i));
+ } else {
+
+ // check to see if the property has many values
+ if (p.isMany()) {
+ System.out.println(buffer + ", many valued with list of DataObjects : ");
+ List dataObjects = myDo.getList(p);
+ for (int x = 0; x < dataObjects.size(); x++) {
+ printAndInspectDataObject(buffer + " ", (DataObject) dataObjects.get(x));
+ System.out.println();
+ }
+
+ } else {
+
+ System.out.println(buffer + ", data object : ");
+ DataObject newDataObject = myDo.getDataObject(p);
+ printAndInspectDataObject(buffer + " ", newDataObject);
+ System.out.println("");
+ }
+ }
+ } else {
+ System.out.println(buffer + ", is not set");
+ }
+ }
+
+ }
+
+ /**
+ * @param args.
+ * none required.
+ */
+ public static void main(String[] args) {
+ // information
+ System.out.println("***************************************");
+ System.out.println("SDO Sample InspectAndPrintPropertiesOfDataObject");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates a common pattern of looping through and inspecting the META-DATA for all instance properties."
+ + "This example uses recursion to iterate through properties which themselves are DataObjects.");
+ System.out.println("***************************************");
+
+ // create a DataObejct
+ DataObject purchaseOrder = null;
+ try {
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null);
+ purchaseOrder = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XML_RESOURCE)).getRootObject();
+ System.out.println("DataObject created");
+ } catch (Exception e) {
+ System.out.println("Error creating DataObject " + e.toString());
+ e.printStackTrace();
+ return;
+ }
+
+ //print and inspect properties of the DataObject
+ try {
+ System.out.println("Will iterate and inspect the properties of the following data object " + purchaseOrder.toString());
+ printDataObjectProperties(purchaseOrder);
+
+ } catch (Exception e) {
+ System.out.println("Sorry, an error was encountered inspecting DataObject " + e.toString());
+ }
+ System.out.println("GoodBye");
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/UsingXPath.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/UsingXPath.java
new file mode 100644
index 0000000000..b243bc0361
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/UsingXPath.java
@@ -0,0 +1,149 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specCodeSnippets;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+
+/**
+ * Demonstrates accessing a created DataObject's properties using xPath.
+ *
+ * This sample is from the <a href="http://incubator.apache.org/tuscany" target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon code snippets contained within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1" target="_bank">SDO Specification</a>.
+ * <br>
+ * <br>
+ * It is worth noting that the Examples section of the specification, and the
+ * {@link org.apache.tuscany.samples.sdo.specExampleSection.AccessingDataObjectsUsingXPath} use unpure xpath expression. This sample attempts to
+ * demonstrate a more appropiate subset of xPath operations.<br>
+ * <br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or the sample-sdo project is imported into Eclipse as an
+ * existing project. <br>
+ * <br>
+ * If executing as a standalone application please do the following: <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specCodeSnippets.UsingXPath</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ */
+public class UsingXPath {
+
+ /**
+ * Prints a subset of item properties to System.out where the individual item was accessed using an xPath expression
+ *
+ * @param purchaseOrder.
+ * DataObject defined by Types in {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE}
+ */
+ public static void accessDataObjectUsingXPath(DataObject purchaseOrder) throws Exception {
+ // TODO: use variety of xpath expressions such as items/item[1]
+ // TODO: add to junit test cases for test cases above
+
+ // xpath expressions for obtaining various items
+ String[] xPathExpressions = { "items/item[1]","items/item[productName=\"Baby Monitor\"]" };
+
+ for (int i = 0; i < xPathExpressions.length; i++) {
+ try {
+ System.out.println("Accessing individual item from list using xpath expression" + xPathExpressions[i]);
+
+ DataObject item = purchaseOrder.getDataObject(xPathExpressions[i]);
+ System.out.println(" DataObject toString : " + item.toString());
+
+ System.out.println(" Item name:" + item.get("productName"));
+ System.out.println(" Part num: " + item.get("partNum"));
+ } catch (Exception e) {
+ System.out.println(" Sorry there was an error executing expression " + xPathExpressions[i]);
+ throw e;
+ }
+ }
+
+ //TODO: select various lists of properties from DataObjects using xPath.
+ }
+
+ /**
+ * Accesses and modifies properties of a purchase order DataObject using xPath( properties are defined in the xsd
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} and populated by xml
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_RESOURCE} )
+ *
+ * @param args.
+ * No parameters required.
+ */
+ public static void main(String[] args) {
+
+ // information
+ System.out.println("***************************************");
+ System.out.println("SDO Sample UsingXPath");
+ System.out.println("***************************************");
+ System.out.println("Demonstrats accessing a created DataObject's properties using xPath.");
+ System.out.println("***************************************");
+
+ // create a DataObejct
+ DataObject purchaseOrder = null;
+ try {
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null);
+ purchaseOrder = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XML_RESOURCE)).getRootObject();
+ System.out.println("DataObject created");
+ } catch (Exception e) {
+ System.out.println("Error creating DataObject " + e.toString());
+ e.printStackTrace();
+ return;
+ }
+
+ // start of sample
+ try {
+
+ accessDataObjectUsingXPath(purchaseOrder);
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error accessing properties by name " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+ // end of sample
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/package.html b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/package.html
new file mode 100644
index 0000000000..ac784deabc
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/package.html
@@ -0,0 +1,40 @@
+<html>
+<!--
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * -->
+<head>
+ <title>package information</title>
+</head>
+<body>
+
+Provides examples containing code snippets located throughout the SDO Specification. In general these samples attempt to use the
+code and comments contained within the specification but allow a certain degree of flexibility to extend or improve
+upon the code snippets where desirable.
+ The user is encouraged to modify and execute these code examples.
+
+
+<h2>Related Documentation</h2>
+
+For overviews, tutorials, examples, guides, and tool documentation, please see the
+<a href="http://incubator.apache.org/tuscany/java_sdo_overview.html"> SDO Java Overview
+</a>
+section of the Tuscany website.
+
+</body>
+</html> \ No newline at end of file
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessDataObjectsUsingXPath.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessDataObjectsUsingXPath.java
new file mode 100644
index 0000000000..97441c7473
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessDataObjectsUsingXPath.java
@@ -0,0 +1,251 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specExampleSection;
+
+import java.util.List;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import commonj.sdo.DataObject;
+
+/**
+ * Demonstrates accessing the properties of a DataObject using XPath queries.
+ *
+ * The following sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon example code contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ * <br>
+ * This specific sample is based upon the AccessDataObjectsUsingXPath example from
+ * the Examples section of the SDO specification. It shows the use of DataObjects and
+ * the XMLHelper and demonstrates accessing the properties of a DataObject using
+ * XPath queries. <br>
+ * <br>
+ * This sample reads an xml file representing a DataObject of a company. A
+ * DataAccessService (DAS) is simply a component which creates a DataGraph or
+ * DataObject, it can also be responcible for creating SDO Types for created
+ * DataObjects. In order to create a DataObject or DataGraph this sample relies upon
+ * XMLHelper class which is essentially an example of a XML DAS. The code shown here
+ * would work just as well against an equivalent DataGraph or DataObject that was
+ * provided by any DAS that creates an identical DataObject/DataGraph of the same
+ * Type used here. <br>
+ * <br>
+ * To define the correct Types for each DataObject ( CompanyType, DepartmentType etc )
+ * this sample relies upon
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#COMPANY_XSD} which is
+ * provided in the resources directory of these samples. The xml file
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#COMPANY_DATAOBJECT_XML}
+ * used to load the DataObject is also located in this resources directory. The xsd
+ * the xml was generated by
+ * {@link org.apache.tuscany.samples.sdo.otherSources.CreateCompany} which is a good
+ * resource for how to populate or create DataObjects or DataGraphs dynamically. <br>
+ * <br>
+ * The following example has the same effect as
+ * {@link org.apache.tuscany.samples.sdo.specExampleSection.AccessDataObjectsViaPropertyIndex}.
+ * <br><br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specExampleSection.AccessDataObjectsUsingXPath </LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ * @see org.apache.tuscany.samples.sdo.specCodeSnippets.ObtainingDataGraphFromXml
+ */
+
+public class AccessDataObjectsUsingXPath {
+
+ /**
+ * Execute this method in order to run the sample.
+ *
+ * @param args
+ */
+ public static void main(String[] args) {
+ System.out.println("***************************************");
+ System.out.println("SDO Sample AccessDataObjectsUsingXPath");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates accessing the properties of a DataObject using XPath queries");
+ System.out.println("***************************************");
+
+ /**
+ * In order to associate the correct Type, to the DataObjects represented in
+ * the xml Read in the model/schema which is declared within the xsd so that
+ * it can be associated. If the Types were not defined in this manner you can
+ * still populate, access and use a DataObject or DataGraph but would not
+ * have the protection and use of use afforded by the relationships declared
+ * within the model.
+ */
+ try {
+ System.out.println("Defining Types using XSD");
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_XSD), null);
+ System.out.println("Type definition completed");
+ } catch (Exception e) {
+ System.out.println("Exception caught defining types " + e.toString());
+ e.printStackTrace();
+ }
+
+ try {
+
+ /**
+ * The specification Example demonstrates using both a DataObject and
+ * DataGraph representation of a compony model. This example simply uses
+ * a DataObject representation.
+ *
+ * Obtain the RootObject from the XMLDocument obtained from the XMLHelper
+ * instance. In this case the root object is a DataObject representing a
+ * company
+ */
+
+ DataObject company = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_DATAOBJECT_XML)).getRootObject();
+
+ // print out some information to show the user what the objects
+ // look like
+ System.out.println("Company DataObject:");
+ System.out.println(company);
+
+ String generatedXml = XMLHelper.INSTANCE.save(company, SdoSampleConstants.COMPANY_NAMESPACE, "company");
+ System.out.println("Company data object xml representation: ");
+ System.out.println(generatedXml);
+
+ /*
+ * If we wish to change the name of the company DataObject from "ACME" to
+ * "MEGACORP" , we could use the following:
+ */
+
+ // Set the "name" property for the company
+ System.out.println("Setting 'name' property for the company DataObject to 'MEGACORP'");
+ company.setString("name", " MegaCorp");
+ System.out.println();
+
+ /*
+ * Now, suppose we wish to access the employee whose serial number is
+ * "E0002" If we know that this employee is located at index 1 within the
+ * department that is located at index 0 within the company object one
+ * way to do this is by traversing each reference in the data graph and
+ * locating each DataObject in the many-valued department property using
+ * it's index in the list. For example, from the company, we can get a
+ * list of departments, from that list we can get the department at index
+ * 0, from there we can get a list of employees, and from that list we
+ * can get the employee at index 1.
+ */
+
+ System.out
+ .println("Obtain employee whose serial number is 'E0002' by acessing list of departments via index, and then accessing specific employee by index. This requies that we know the indices of both the department and employee");
+ // Get the list of departments
+ List departments = company.getList("departments");
+ // Get the department at index 0 on the list
+ DataObject department = (DataObject) departments.get(0);
+ // Get the list of employees for the department
+ List employees = department.getList("employees");
+ // Get the employee at index 2 on the list
+ DataObject employeeFromList = (DataObject) employees.get(2);
+
+ /*
+ * Alternatively, we can write a single XPath expression that directly
+ * accesses the employee from the root company. Please note that the
+ * specification's use of . indexing starting at index 0 is not true
+ * xPath. Please see the
+ * org.apache.tuscany.samples.sdo.specCodeSnippets.UsingXPath
+ * example for more examples of using xPath. A more appropiate use of
+ * xPath might be
+ *
+ * employeeFromXPath =
+ * company.getDataObject("departments[1]/employees[3]");
+ *
+ */
+
+ System.out.println("Obtain employee using xPath expression");
+ DataObject employeeFromXPath = company.getDataObject("departments.0/employees.2");
+
+ /*
+ * In order to remove that employee from the data graph, we could
+ */
+
+ // remove the employee from the list of employees
+ System.out.println("Removing employee " + employeeFromXPath.getString("name") + " from list of employees");
+ employeeFromXPath.detach();
+
+ /*
+ * And, finally, to create a new employee:
+ */
+ // create a new employee
+ System.out.println("Creating new employee (manager) Al Smith and adding to list");
+ DataObject newEmployee = department.createDataObject("employees");
+
+ newEmployee.set("name", "Al Smith");
+ newEmployee.set("SN", "E0005");
+ newEmployee.setBoolean("manager", true);
+
+ // Reset employeeOfTheMonth to be the new employee
+ System.out.println("Setting employee of the month to new employee");
+ company.set("employeeOfTheMonth", newEmployee.getString("SN"));
+
+ // print out some information to show the user what the objects
+ // look like
+ System.out.println("The modified company DataObject: ");
+ System.out.println(company);
+ System.out.println();
+
+ generatedXml = XMLHelper.INSTANCE.save(company, SdoSampleConstants.COMPANY_NAMESPACE, "company");
+ System.out.println("Company data object xml representation: ");
+ System.out.println(generatedXml);
+ System.out.println();
+
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error encountered " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingDataObjectsViaPropertyIndex.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingDataObjectsViaPropertyIndex.java
new file mode 100644
index 0000000000..f2e2b8f2d7
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingDataObjectsViaPropertyIndex.java
@@ -0,0 +1,232 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specExampleSection;
+
+import java.io.InputStream;
+import java.util.List;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import commonj.sdo.DataObject;
+
+/**
+ * Demonstrates accessing the properties of a DataObject using property indices.
+ *
+ * The following sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiment with SDO. It is based upon example code contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ * <br>
+ * The following sample is based upon the 'Accessing DataObjects via Property Index'
+ * example from the Examples section of the SDO specification. It shows the use of
+ * DataObjects and the XMLHelper amd demonstrates accessing the properties of a
+ * DataObject using property indices. <br>
+ * <br>
+ * The following example has the same effect as
+ * {@link org.apache.tuscany.samples.sdo.specExampleSection.AccessDataObjectsUsingXPath}.
+ * The indexes for the properties are defined as constants in this class. <br>
+ * <br>
+ * This sample reads an xml file representing a DataObject of a company. In order to
+ * create a DataObject or DataGraph this sample relies upon XMLHelper class which is
+ * essentially example of a XML DAS implementation. The code shown here would work
+ * just as well against an equivalent DataObject that was provided by any DAS. <br>
+ * <br>
+ * To define the correct Types for each DataObject ( CompanyType, DepartmentType etc )
+ * this sample relies upon
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#COMPANY_XSD} which is
+ * provided in the resources directory of these samples <br>
+ * The xml file
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#COMPANY_DATAOBJECT_XML}
+ * used to load the DataObject is also located in this resources directory. To help
+ * ensure consistancy with the xsd the xml was generated by
+ * {@link org.apache.tuscany.samples.sdo.otherSources.CreateCompany} which is a good
+ * resource for populating DataObjects, or creating DataGraphs dynamically.
+ * <br><br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specExampleSection.AccessingDataObjectsViaPropertyIndex </LI>
+ * </UL>
+ *
+ * Note: when reading this source code in conjunction with the specification there is scope for confusion
+ * over what the values of the numeric indices ought to be, depending on your understanding of the
+ * word "lexical" in the context of sequencing. So the SDO specification at the 2.0.1 level (and before) says
+ * <i>The order of Properties in Type.getDeclaredProperties() is the lexical order of declarations in the
+ * XML Schema ComplexType.</i>. So if your background is in computing and in particular in lexical parsing
+ * then you would understand this to mean "the sequence of tokens produced by the lexical analyis (first) phase of a compiler"
+ * and if you ran a lexical parser against the complex type then you would see the elements emerege in the order they were
+ * written down. If however you think more in terms of lexical in the context of lexicons or dictionaries, you may
+ * expect the indices to correspond to the lexically sorted (essentially alphabetically sorted) list of element names.
+ * After some investigation it is understood that the intention of the spec is to convey the first of these meanings.
+ * A clarification in the SDO 2.1 spec is being sought, but may not make it into that document in time.
+ *
+ * @author Robbie Minshall
+ */
+
+public class AccessingDataObjectsViaPropertyIndex {
+
+ /**
+ * Predefine the property indexes.
+ *
+
+ */
+
+ private static final int COMPANY_DEPARTMENT = 0;
+
+ private static final int COMPANY_EMPLOYEE_OF_MONTH = 1;
+
+ private static final int COMPANY_NAME = 2;
+
+ private static final int DEPARTMENT_EMPLOYEES = 0;
+
+ private static final int EMPLOYEE_NAME = 0;
+ private static final int EMPLOYEE_SN = 1;
+ private static final int EMPLOYEE_MANAGER = 2;
+
+
+ /**
+ * Execute this method in order to run the sample.
+ *
+ * @param args
+ */
+ public static void main(String[] args) {
+ System.out.println("***************************************");
+ System.out.println("SDO Sample AccessingDataObjectsViaPropertyIndex");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates accessing the properties of a DataObject using property indices.");
+ System.out.println("***************************************");
+
+ // TODO: what happens if not type is defined
+ try {
+ System.out.println("Defining Types using XSD");
+ InputStream is = null;
+ is = ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_XSD);
+ XSDHelper.INSTANCE.define(is, null);
+ is.close();
+ System.out.println("Type definition completed");
+ } catch (Exception e) {
+ System.out.println("Exception caught defining types " + e.toString());
+ e.printStackTrace();
+ }
+
+ try {
+
+ /**
+ * In this example simply use a company DataObject read in from xml
+ */
+
+ // For DataObjects
+ // Obtain the RootObject from the XMLDocument obtained from the XMLHelper
+ // instance.
+ // In this case the root object is a DataObject representing a company
+ DataObject company = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_DATAOBJECT_XML))
+ .getRootObject();
+
+ // print out some information to show the user what the objects look like
+ System.out.println("Company DataObject:");
+ System.out.println(company);
+
+ String generatedXml = XMLHelper.INSTANCE.save(company, SdoSampleConstants.COMPANY_NAMESPACE, "company");
+ System.out.println("Company data object xml representation: ");
+ System.out.println(generatedXml);
+
+ System.out.println("Setting name of company to MegaCorp");
+ // Set the "name" property for the company
+ company.setString(COMPANY_NAME, "MegaCorp");
+ // Get the list of departments
+ List departments = company.getList(COMPANY_DEPARTMENT);
+ // Get the department at index 0 on the list
+ DataObject department = (DataObject) departments.get(0);
+ // Get the list of employees for the department
+ List employees = department.getList(DEPARTMENT_EMPLOYEES);
+ // Get the employee at index 1 on the list
+ DataObject employeeFromList = (DataObject) employees.get(2);
+
+ // remove the employee from the graph
+ System.out.println("Removing employee " + employeeFromList.getString("name") + " from list of employees");
+ employeeFromList.detach();
+
+ // create a new employee
+ System.out.println("Creating new employee (manager) Al Smith and adding to list");
+ DataObject newEmployee = department.createDataObject(DEPARTMENT_EMPLOYEES);
+
+ /**
+ * Properties from Type.getDeclaredProperties, or Type.getProperties
+ * should be in lexical (alphanumerical) ordering of the xml schema
+ * complex type.
+ */
+
+ newEmployee.set(EMPLOYEE_NAME, "Al Smith");
+ newEmployee.set(EMPLOYEE_SN, "E0005");
+ newEmployee.setBoolean(EMPLOYEE_MANAGER, true);
+
+ System.out.println("setting employee of the month to new employee");
+ company.set(COMPANY_EMPLOYEE_OF_MONTH, newEmployee.get(EMPLOYEE_SN));
+
+ // print out some information to show the user what the objects look like
+ System.out.println("The modified company DataObject :");
+ System.out.println(company);
+
+ generatedXml = XMLHelper.INSTANCE.save(company, SdoSampleConstants.COMPANY_NAMESPACE, "company");
+ System.out.println("Company data object xml representation: ");
+ System.out.println(generatedXml);
+
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error encountered " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingTheContentsOfASequence.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingTheContentsOfASequence.java
new file mode 100644
index 0000000000..04ef5e704f
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingTheContentsOfASequence.java
@@ -0,0 +1,194 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specExampleSection;
+
+
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import commonj.sdo.DataObject;
+import commonj.sdo.Sequence;
+import commonj.sdo.Property;
+
+/**
+ * Demonstrates accessing the sequence from a DataObject containing mixed content.
+ *
+ * The following sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon example code contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ *
+ * Specifically the following sample is based upon the 'Accessing the contents of a Sequence' example from the Examples section of the SDO specification. <br>
+ * <br>
+ * The following code uses the Sequence interface to analyze the contents of a DataObject that conforms to teh Letter model defined in
+ * {@link #LETTER_XSD}. This code first goes through the Sequence looking for unformatted text entrires and prints them out. Then the code checks to
+ * verify that the contents of teh "lastName" property of the DataObject matches the contents of the same property of the Sequence. <br>
+ * <br>
+ * This sample reads an xml file (@link #LETTER_XML} representing a DataObject of a letter. In order to create a DataObject or DataGraph this sample
+ * relies upon XMLHelper class which is an example of a XML DAS. The code shown here would work just as well against an equivalent DataObject that was
+ * provided by any DAS. <br>
+ *
+ * The XSD for {@link #LETTER_XSD} defines the FormLetter complex type as mixed. Mixed content and sequences are often used for semi structured business data.
+ * <br><br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specExampleSection.AccessingTheContentsOfASequence</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ */
+
+public class AccessingTheContentsOfASequence {
+
+ /**
+ * previously created XSD file used
+ */
+ public static final String LETTER_XSD = "letter.xsd";
+
+ /**
+ * previously created XML file used
+ */
+ public static final String LETTER_XML = "letter.xml";
+
+ /**
+ * Execute this method in order to run the sample.
+ *
+ * @param args
+ */
+ public static void main(String[] args) {
+ System.out.println("***************************************");
+ System.out.println("SDO Sample AccessingTheContentsOfASequence");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates accessing the sequence from a DataObject containing mixed content.");
+ System.out.println("***************************************");
+
+ try {
+ // define model
+ System.out.println("Defining Types using XSD");
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(LETTER_XSD), null);
+
+ // define letter data object
+ System.out.println("Loading Letter DataObject from XML");
+ DataObject letter = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(LETTER_XML)).getRootObject();
+
+ // print letter sequence
+ System.out.println("Printing sequence for " + letter);
+ System.out.println(XMLHelper.INSTANCE.save(letter, "letter.xsd", "letter"));
+ printSequence(letter);
+
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error encountered " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+
+ }
+
+ /**
+ * Uses the Sequence interface to analyze the contents of a DataObject that conforms to teh Letter model defined in
+ * {@link #LETTER_XSD}. This code first goes through the Sequence looking for unformatted text entrires and prints them out. Then the code checks
+ * to verify that the contents of the "lastName" property of the DataObject matches the contents of the same property of the Sequence. <br>
+ *
+ * @param letter. Letter DataObject conforming to {@link #LETTER_XSD}
+ */
+ public static void printSequence(DataObject letter) {
+ // Access the Sequence of the FormLetter
+ System.out.println("The type is for letter dataObject is mixed " + XSDHelper.INSTANCE.isMixed(letter.getType()));
+
+ Sequence letterSequence = letter.getSequence();
+ // Print out all the settings that contain unstructured text
+ System.out.println("Unstructured text:");
+ for (int i = 0; i < letterSequence.size(); i++) {
+
+ /*
+ * Please note that the following line is a correction to the 2.0 specification which incorrectly calls:
+ *
+ * String propertyName = ((Property) letterSequence.getProperty(i)).getName();
+ *
+ * According to the SDO API sequence.getProperty will return null if the content is mixed, in this case
+ * we want to print it out as unstructured text
+ */
+ Property prop = letterSequence.getProperty(i);
+ if (prop == null) {
+ String text = (String) letterSequence.getValue(i);
+ System.out.println("\t(" + text + ")");
+ }
+
+ }
+
+ /*
+ * Please note that the following line is a correction to the 2.0 Specification which incorrectly uses letterDataObject variable rather than
+ * simply letter
+ */
+ // Verify that the lastName property of the DataObject has the same
+ // value as the lastName property for the Sequence.
+ String dataObjectLastName = letter.getString("lastName");
+ for (int i = 0; i < letterSequence.size(); i++) {
+
+ /*
+ * The following line has been corrected from the 2.0 specification
+ * According to the SDO API sequence.getProperty will return null if the content is mixed.
+ * We want to check that the content is not mixed, and then check that it it is the property which
+ * we are looking for
+ */
+ Property property = letterSequence.getProperty(i);
+
+ if ( (property != null) && ("lastName".equals(property.getName()))) {
+ String sequenceLastName = (String) letterSequence.getValue(i);
+ if (dataObjectLastName == sequenceLastName)
+ System.out.println("Last Name property matches");
+ break;
+ }
+ }
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java
new file mode 100644
index 0000000000..53fc82e06d
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java
@@ -0,0 +1,136 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specExampleSection;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+/**
+ * Demonstrates creating a DataObject from an XML String.
+ * <br>
+ * The following sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon example code contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ * <br>
+ * Specifically this sample is based upon the 'Creating DataObject Trees from XML
+ * documents' example from the Examples section of the SDO Specification. It is
+ * possible to convert to and from XML documents to build DataObject trees, which is
+ * useful when assembling DataObjects from serval data sources. This sample uses the
+ * XMLHelper to convert xml Strings to DataObjects. <br>
+ * <br>
+ * Uses previously defined model
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} to
+ * define types then generates a purchase order DataObject and persists to
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_GENERATED}
+ * <br><br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specExampleSection.CreatingDataObjectTreesFromXMLDocuments</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ * @see {@link org.apache.tuscany.samples.sdo.otherSources.CreatePurchaseOrder}
+ */
+
+public class CreatingDataObjectTreesFromXMLDocuments {
+
+ /**
+ * Drives sample
+ *
+ * @param args.
+ * none required.
+ */
+ public static void main(String[] args) {
+ try {
+
+ System.out.println("***************************************");
+ System.out.println("SDO Sample CreatingDataObjectTreesFromXMLDocuments");
+ System.out.println("***************************************");
+
+ // use xsd to define purchase order types
+ System.out.println("Definging purchase order types using " + SdoSampleConstants.PO_XSD_RESOURCE);
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null);
+ System.out.println("Defined Types using xsd");
+
+ System.out.println("Creating purchase order DataObject using previously created " + SdoSampleConstants.PO_XML_RESOURCE);
+ DataObject purchaseOrder = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XML_RESOURCE))
+ .getRootObject();
+
+ // To create the shipTo DataObject from XML:
+ String shipToXML = "<shipTo country='US'>" + " <name>Alice Smith</name>" + " <street>123 Maple Street</street>"
+ + " <city>Mill Valley</city>" + " <state>PA</state>" + " <zip>90952</zip>" + "</shipTo>";
+ DataObject shipTo = XMLHelper.INSTANCE.load(shipToXML).getRootObject();
+
+ // Modify shipTo DataObject on purchaseOrder to value created from a
+ // String
+ System.out.println("Modifying shipTo DataObject on purchaseOrder to value created from a String");
+ purchaseOrder.set("shipTo", shipTo);
+
+ // Obtain and convert the billTo DataObject to XML
+ System.out.println("Obtaining billTo dataObject from purchase order and converting to XML");
+ DataObject billTo = purchaseOrder.getDataObject("billTo");
+ String billToXML = XMLHelper.INSTANCE.save(billTo, null, "billTo");
+ System.out.println("billTo DataObject:");
+ System.out.println(billToXML);
+
+ } catch (Exception e) {
+ System.out.println("Sorry an error occured " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingXmlFromDataObjects.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingXmlFromDataObjects.java
new file mode 100644
index 0000000000..3c5e147677
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingXmlFromDataObjects.java
@@ -0,0 +1,160 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specExampleSection;
+
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.DataFactory;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+/**
+ * Demonstrates programmatically creating a DataObject and generating an XML String.
+ * <br>
+ * The following sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon example code contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ * <br>
+ * Specifically this sample is based upon the 'Creating XML from DataObjects' example
+ * from the SDO specification's Examples section. <br>
+ * <br>
+ * Uses previously defined model
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} to
+ * define types then generates a purchase order DataObject and persists to
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_GENERATED}
+ * <br><br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specExampleSection.CreatingXmlFromDataObjects</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ * @see {@link org.apache.tuscany.samples.sdo.otherSources.CreatePurchaseOrder}
+ */
+
+public class CreatingXmlFromDataObjects {
+
+ /**
+ * Drives sample
+ *
+ * @param args.
+ * none required.
+ */
+ public static void main(String[] args) {
+ try {
+
+ System.out.println("***************************************");
+ System.out.println("SDO Sample CreatingXmlFromDataObjects");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates programmatically creating a DataObject and generating an XML String");
+ System.out.println("***************************************");
+
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null);
+ System.out.println("Defined Types using xsd");
+ DataObject purchaseOrder = DataFactory.INSTANCE.create(SdoSampleConstants.PO_NAMESPACE, "PurchaseOrderType");
+ System.out.println("Created DataObject using DataFactory");
+
+ System.out.println("Populating purchase order data object");
+
+ purchaseOrder.setString("orderDate", "1999-10-20");
+
+ DataObject shipTo = purchaseOrder.createDataObject("shipTo");
+ shipTo.set("country", "US");
+ shipTo.set("name", "Alice Smith");
+ shipTo.set("street", "123 Maple Street");
+ shipTo.set("city", "Mill Valley");
+ shipTo.set("state", "CA");
+ shipTo.setString("zip", "90952");
+ DataObject billTo = purchaseOrder.createDataObject("billTo");
+ billTo.set("country", "US");
+ billTo.set("name", "Robert Smith");
+ billTo.set("street", "8 Oak Avenue");
+ billTo.set("city", "Mill Valley");
+ billTo.set("state", "PA");
+ billTo.setString("zip", "95819");
+ purchaseOrder.set("comment", "Hurry, my lawn is going wild!");
+
+ DataObject items = purchaseOrder.createDataObject("items");
+
+ DataObject item1 = items.createDataObject("item");
+ item1.set("partNum", "872-AA");
+ item1.set("productName", "Lawnmower");
+ item1.setInt("quantity", 1);
+ item1.setString("price", "148.95");
+
+ item1.set("comment", "Confirm this is electric");
+
+ DataObject item2 = items.createDataObject("item");
+ item2.set("partNum", "926-AA");
+ item2.set("productName", "Baby Monitor");
+ item2.setInt("quantity", 1);
+ item2.setString("price", "39.98");
+ item2.setString("shipDate", "1999-05-21");
+
+ OutputStream stream = new FileOutputStream(SdoSampleConstants.PO_XML_GENERATED);
+ XMLHelper.INSTANCE.save(purchaseOrder, SdoSampleConstants.PO_NAMESPACE, "purchaseOrder", stream);
+ System.out.println("Created file " + SdoSampleConstants.PO_XML_GENERATED);
+ System.out.println("Populated with :");
+ System.out.println(XMLHelper.INSTANCE.save(purchaseOrder, SdoSampleConstants.PO_NAMESPACE, "purchaseOrder"));
+ } catch (Exception e) {
+ System.out.println("Sorry an error occured " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/SerializingDesearializingADataObject.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/SerializingDesearializingADataObject.java
new file mode 100644
index 0000000000..97772eaf33
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/SerializingDesearializingADataObject.java
@@ -0,0 +1,183 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specExampleSection;
+
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import commonj.sdo.helper.EqualityHelper;
+import commonj.sdo.DataObject;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.io.ObjectInputStream;
+import java.io.FileOutputStream;
+import java.io.ObjectOutputStream;
+import java.io.IOException;
+
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+/**
+ * Demonstrates serializing and deserializing a DataObject to disk.
+ *
+ * The following sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon example code contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ * <br>
+ * The following sample is based upon the 'Serializing/Deserializing a DataGraph or DataObject' example from the Examples section of the SDO
+ * specification. <br>
+ * <br>
+ * The DataObject and DataGraph interfaces extend java.io.Serializable, so any DataObject
+ * or DataGraph be serialized. The following code serializes the company dataObject to a file
+ * of the user's choosing. The file is not deleted after execution.<br>
+ * <br><br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specExampleSection.SerializingDesearializingADataObject</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ */
+
+public class SerializingDesearializingADataObject {
+
+ /**
+ * Default fileName to serialize DataObject to
+ */
+ public static final String DEFAULT_FILE_NAME = "temporarySerializedDataObject.xml";
+
+ public static void serializeDO(DataObject DataObject, String fileName) throws IOException {
+ // serialize data object
+ FileOutputStream fos = new FileOutputStream(fileName);
+ ObjectOutputStream out = new ObjectOutputStream(fos);
+ out.writeObject(DataObject);
+ out.close();
+ }
+
+ /**
+ * Execute this method in order to run the sample.
+ *
+ * @param args
+ */
+ public static void main(String[] args) {
+ System.out.println("***************************************");
+ System.out.println("SDO Sample AccessingTheContentsOfASequence");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates serializing and deserializing a DataObject to disk.");
+ System.out.println("***************************************");
+
+
+ try {
+ // define Types using XSDHelper and predefined xsd file
+ System.out.println("Defining Types using XSD");
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_XSD), null);
+ System.out.println("Type definition completed");
+
+ // create company DataObject using XMLHelper which is an example of a XML DAS
+ System.out.println("Creating company DataObject");
+ DataObject company = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_DATAOBJECT_XML))
+ .getRootObject();
+
+ // prompt user for a fileName
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ System.out.print("Please enter file name, enter for default :");
+ String fileName = in.readLine();
+ if ( (fileName == null) || (fileName.equals(""))){
+ fileName = DEFAULT_FILE_NAME;
+ }
+
+ // serialize dataObject out
+ System.out.println("Serializing DataObject to " + fileName);
+ FileOutputStream fos = new FileOutputStream(fileName);
+ ObjectOutputStream out = new ObjectOutputStream(fos);
+ out.writeObject(company);
+ out.close();
+
+ // read in DataObject
+ System.out.println("Deserialize DataObject from file");
+ FileInputStream fis = new FileInputStream(fileName);
+ ObjectInputStream input = new ObjectInputStream(fis);
+ DataObject newDataObject = (DataObject) input.readObject();
+ input.close();
+
+ /**
+ * Compare DataObjects
+ */
+
+ System.out.println("Comparing original and deserialized DataObject");
+ //determine if two graphs of DataObjects are equal
+ System.out.println("DataObjects are equal: " + EqualityHelper.INSTANCE.equal(company, newDataObject));
+
+ //determine if two DataObjects have the same values for their datatype properties
+ System.out.println("DataObjects have same values for their datatype properties : " + EqualityHelper.INSTANCE.equalShallow(company, newDataObject));
+
+ //print out xml representation
+ System.out.println();
+ System.out.println("Original company DataObject:");
+ System.out.println(XMLHelper.INSTANCE.save(company, SdoSampleConstants.COMPANY_NAMESPACE, "company"));
+
+ System.out.println();
+ System.out.println("Deserialized company DataObject:");
+ System.out.println(XMLHelper.INSTANCE.save(newDataObject, SdoSampleConstants.COMPANY_NAMESPACE, "company"));
+
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error encountered " + e.toString());
+ e.printStackTrace();
+ }
+
+ System.out.println("GoodBye");
+
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java
new file mode 100644
index 0000000000..f93b716fb5
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java
@@ -0,0 +1,190 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.samples.sdo.specExampleSection;
+
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import commonj.sdo.DataObject;
+import commonj.sdo.Type;
+import commonj.sdo.Property;
+
+import java.util.List;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+/**
+ * Demonstrates the use of Type and Property to display and the META-DATA for and access Properties of a DataObject.
+ * <br>
+ * The following sample is from the <a href="http://incubator.apache.org/tuscany"
+ * target="_blank"> Apache Tuscany</a> project. It was written to help users
+ * understand and experiement with SDO. It is based upon example code contained
+ * within, and is meant for use with, and reference to the <a
+ * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1"
+ * target="_bank">SDO Specification</a>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ * <br>
+ * The following sample is based upon the 'Using Type and Property with DataObjects' example from the Examples section of the SDO specification. <br>
+ * <br>
+ * The Type interface provides access to the metadata for DataObjects. The methods on Type and Property provide information that describes the
+ * properties of a DataObject. To obtain the Type for a DataObject use the getType() method. This example prints out the contents of a DataObject,
+ * each property has it's meta-data displayed, accessed dynamically using Type and Property.<br>
+ * <br><br>
+ * <b>Usage:</b> <br>
+ * This sample can easily be run from within Eclipse as a Java Application if tuscany or
+ * the sample-sdo project is imported into Eclipse as an existing project.
+ * <br><br>
+ * If executing as a standalone application please do the following:
+ * <br>
+ * <UL>
+ * <LI>Include the following jar files on your classpath :
+ * <UL>
+ * <LI>SDO API and Tuscany Implementation
+ * <UL>
+ * <LI>sdo-api-{version}.jar - SDO API
+ * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation
+ * </UL>
+ * </LI>
+ * <LI>EMF dependencies.
+ * <UL>
+ * <LI>emf-common-{version}.jar - some common framework utility and base classes
+ * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel)
+ * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework
+ * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader
+ * <LI>xsd-{version}.jar - the XML Schema model
+ * </UL>
+ * </LI>
+ * </UL>
+ *
+ * These jar files can be obtained from directly from Tuscany and EMF projects or from <a
+ * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies"
+ * target="_bank">SDO Execution Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specExampleSection.UsingTypeAndPropertyWithDataObjects</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ * @see org.apache.tuscany.samples.sdo.specCodeSnippets.PrintPropertiesOfDataObject
+ */
+
+public class UsingTypeAndPropertyWithDataObjects {
+
+ /**
+ * Execute this method in order to run the sample.
+ *
+ * @param args
+ */
+ public static void main(String[] args) {
+ System.out.println("***************************************");
+ System.out.println("SDO Sample UsingTypeAndPropertyWithDataObjects");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates the use of Type and Property to display and the META-DATA for and access Properties of a DataObject.");
+ System.out.println("***************************************");
+
+ try {
+ // define Types using XSDHelper and predefined xsd file
+ System.out.println("Defining company Types using XSD");
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_XSD), null);
+ System.out.println("Type definition completed");
+
+ // create company DataObject using XMLHelper which is an example of a XML DAS
+ System.out.println("Creating company DataObject");
+ DataObject company = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_DATAOBJECT_XML))
+ .getRootObject();
+
+ printDataObject(company, 0);
+
+ System.out.println("Defining purchase order types");
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null);
+ System.out.println("Type definition completed");
+
+ // create company DataObject using XMLHelper which is an example of a XML DAS
+ System.out.println("Creating purchase order DataObject");
+ DataObject po = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XML_RESOURCE))
+ .getRootObject();
+
+ printDataObject(po, 0);
+
+
+
+
+
+
+
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error encountered " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+ }
+
+ /**
+ * Prints out the contents of a DataObject, each property has it's meta-data displayed, accessed dynamically using Type and Property.
+ * @param dataObject. DataObject to be printed.
+ * @param indent. For formatting
+ */
+ public static void printDataObject(DataObject dataObject, int indent) {
+ // For each Property
+ List properties = dataObject.getInstanceProperties();
+ for (int p = 0, size = properties.size(); p < size; p++) {
+ if (dataObject.isSet(p)) {
+ Property property = (Property) properties.get(p);
+ if (property.isMany()) {
+ // For many-valued properties, process a list of values
+ List values = dataObject.getList(p);
+ for (int v = 0, count = values.size(); v < count; v++) {
+ printValue(values.get(v), property, indent);
+ }
+ } else {
+ // For single-valued properties, print out the value
+ printValue(dataObject.get(p), property, indent);
+ }
+ }
+ }
+ }
+
+ /**
+ * Prints the value of a property
+ * @param value. Value of Property to be printed
+ * @param property. Property to be printed
+ * @param indent. For formatting
+ */
+ public static void printValue(Object value, Property property, int indent) {
+ // Get the name of the property
+ String propertyName = property.getName();
+ // Construct a string for the proper indentation
+ String margin = "";
+ for (int i = 0; i < indent; i++)
+ margin += "\t";
+ if (value != null && property.isContainment()) {
+ // For containment properties, display the value
+ // with printDataObject
+ Type type = property.getType();
+ String typeName = type.getName();
+ System.out.println(margin + propertyName + " (" + typeName + "):");
+ printDataObject((DataObject) value, indent + 1);
+ } else {
+ // For non-containment properties, just print the value
+ System.out.println(margin + propertyName + ": " + value);
+ }
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/package.html b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/package.html
new file mode 100644
index 0000000000..0aa2a9bcb2
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/package.html
@@ -0,0 +1,40 @@
+<html>
+<!--
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * -->
+<head>
+ <title>package information</title>
+</head>
+<body>
+
+Provides samples from the Examples section of the SDO Specification. In general these samples attempt to use the
+code and comments contained within the specification; exceptions to this are noted in comments.
+ The user is encouraged to modify and execute these code examples.
+
+
+
+<h2>Related Documentation</h2>
+For overviews, tutorials, examples, guides, and tool documentation, please see the
+<a href="http://incubator.apache.org/tuscany/java_sdo_overview.html"> SDO Java Overview
+</a>
+section of the Tuscany website.
+
+
+</body>
+</html>