diff options
Diffstat (limited to 'sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate')
7 files changed, 900 insertions, 0 deletions
diff --git a/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/AccessDataObjectUsingValidXPath.java b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/AccessDataObjectUsingValidXPath.java new file mode 100644 index 0000000000..56bc68340f --- /dev/null +++ b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/AccessDataObjectUsingValidXPath.java @@ -0,0 +1,129 @@ +/** + * + * 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.intermediate; + +import java.util.List; + +import org.apache.tuscany.samples.sdo.SampleBase; +import org.apache.tuscany.samples.sdo.internal.SampleInfrastructure; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; + +/** + * Demonstrates accessing a created DataObject's properties using the SDO XPath like syntax. + * <p> + * <h3>Running this Sample</h3> See <A HREF="../../../../../../index.html" + * target="_top">the main overview</A> for instructions on how to run this + * sample. + */ +public class AccessDataObjectUsingValidXPath extends SampleBase { + HelperContext scope; + + public AccessDataObjectUsingValidXPath(Integer userLevel) { + super(userLevel, SampleInfrastructure.SAMPLE_LEVEL_INTERMEDIATE); + } + + + /** + * Accesses and modifies properties of a purchase order DataObject using xPath( + * properties are defined in the xsd + * {@link org.apache.tuscany.samples.sdo.internal.SampleInfrastructure#PO_XSD_RESOURCE} and + * populated by xml + * {@link org.apache.tuscany.samples.sdo.internal.SampleInfrastructure#PO_XML_RESOURCE} ) + * + * @param args. + * No parameters required. + */ + public static void main(String[] args) { + AccessDataObjectUsingValidXPath sample = new AccessDataObjectUsingValidXPath(COMMENTARY_FOR_NOVICE); + sample.run(); + + } + + /* + * metadata for the sample documenting the areas of SDO that are explored + */ + public static int [] CORE_FUNCTION = { + SDOFacets.GET_SET_PROPERTIES_OF_DATAOBJECT_BY_XPATH + }; + + + public void runSample () throws Exception { + + commentary("Demonstrates accessing a DataObject's properties using the XPath style getter/setter methods"); + + + HelperContext scope = createScopeForTypes(); + + + commentary( + "First we create the type system using an XML Schema file and then create\n"+ + "A DataObject using an XML document for convenience"); + + loadTypesFromXMLSchemaFile(scope, SampleInfrastructure.PO_XSD_RESOURCE); + DataObject purchaseOrder = getDataObjectFromFile(scope, SampleInfrastructure.PO_XML_RESOURCE); + + + + commentary( + "Accessing data from the purchase order using the DataObjects XPath style methods\n"); + + + System.out.println("First we use the simplest kind of path\n" + + "purchaseOrder.getString(\"billTo/name\")\n" + + "The purchase is to be paid for by .... " + + purchaseOrder.getString("billTo/name")); + + + System.out.println("\nThen we use indexing by integer starting from 1\n" + + "purchaseOrder.getString(\"items/item[1]/productName\")\n" + + "The first item in the order is a ... " + + purchaseOrder.getString("items/item[1]/productName")); + + + System.out.println("\nThe alternative style of indexing uses a . notation and starts from 0\n"+ + "purchaseOrder.getFloat(\"items/item.0/price\")\n" + + "The price of this item is ... " + + purchaseOrder.getFloat("items/item.0/price")); + + + System.out.println("\nDataObjects can be looked up by supplying the value of one of the contained simple valued Properties\n"+ + "DataObject babyMonitorItem = purchaseOrder.getDataObject(\"items/item[productName=\\\"Baby Monitor\\\"]"); + + DataObject babyMonitorItem = purchaseOrder.getDataObject("items/item[productName=\"Baby Monitor\"]"); + System.out.println("The price of the Baby Monitor is .... " + + babyMonitorItem.getFloat("price")); + + + System.out.println("\nA parent DataObject can be accessed with the .. notation\n"+ + "List onlyIfBuyingGrassSeed = purchaseOrder.getList(\"items/item[productName=GrassSeed]/../item\");"); + List onlyIfBuyingGrassSeed = purchaseOrder.getList("items/item[productName=GrassSeed]/../item"); + if(onlyIfBuyingGrassSeed != null) { + System.out.println("The purchase order included grass seed and " + new Integer(onlyIfBuyingGrassSeed.size()-1) + " other items"); + } else { + System.out.println("The purchase order did not include GrassSeed"); + } + + + } + +} diff --git a/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/AccessingDataObjectsViaPropertyIndex.java b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/AccessingDataObjectsViaPropertyIndex.java new file mode 100644 index 0000000000..98a63b5654 --- /dev/null +++ b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/AccessingDataObjectsViaPropertyIndex.java @@ -0,0 +1,164 @@ +/** + * + * 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.intermediate; + +import java.util.List; + +import org.apache.tuscany.samples.sdo.SampleBase; +import org.apache.tuscany.samples.sdo.advanced.PrintDataGraph; +import org.apache.tuscany.samples.sdo.internal.SampleInfrastructure; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; + +/** + * Demonstrates accessing the properties of a DataObject using integer property indices. + * <p> + * <h3>Running this Sample</h3> See <A HREF="../../../../../../index.html" + * target="_top">the main overview</A> for instructions on how to run this + * sample. + */ + +public class AccessingDataObjectsViaPropertyIndex extends SampleBase { + + HelperContext scope; + PrintDataGraph printer; + + public AccessingDataObjectsViaPropertyIndex(Integer userLevel) { + /* + * Classifying this sample as intermediate, not because it is complex + * but because it's a scenario not to be encouraged for use by + * people new to SDO. + */ + super(userLevel, SAMPLE_LEVEL_INTERMEDIATE); + printer = new PrintDataGraph(SAMPLE_LEVEL_ADVANCED); + } + + + /** + * Predefine the property indexes. + */ + + private static final int COMPANY_DEPARTMENT = 0; + private static final int COMPANY_NAME = 1; + private static final int COMPANY_EMPLOYEE_OF_MONTH = 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) { + AccessingDataObjectsViaPropertyIndex sample = + new AccessingDataObjectsViaPropertyIndex(COMMENTARY_FOR_NOVICE); + + sample.run(); + + } + + /* + * metadata for the sample documenting the areas of SDO that are explored + */ + public static int [] CORE_FUNCTION = { + SDOFacets.GET_SET_PROPERTIES_OF_DATAOBJECT_BY_INDEX + }; + + + public void runSample () throws Exception { + + + commentary( + "This sample demonstrates the use of integer index to get and set\n" + + "Property values of a DataObject. This approach is optimized for\n" + + "performance, but is fragile to changes to the Type system. It will be\n" + + "broken if someone alters the XML schema that is used to load the type system." + + "This kind of approach is well suited to a situation where the SDO code is being\n" + + "generated."); + + scope = createScopeForTypes(); + + loadTypesFromXMLSchemaFile(scope, SampleInfrastructure.COMPANY_XSD); + + DataObject company = getDataObjectFromFile(scope, SampleInfrastructure.COMPANY_DATAOBJECT_XML); + + commentary("We've loaded a data graph that looks like this ..."); + + printer.print(company); + System.out.println(printer.getBuf()); + printer.reset(); + + commentary("Here's how the data graph looks rendered in XML ..."); + System.out.println( + scope.getXMLHelper().save(company, SampleInfrastructure.COMPANY_NAMESPACE, "company")); + + commentary( + "This sample class has primitive int static constants defined for the Properties\n" + + "of the Types that the program is designed to work with, e.g.\n\n" + + "private static final int COMPANY_NAME = 2;\n\n" + + "The value of the integers is defined by the sequence the Properties\n" + + "appear in the List returned by dataObject.getType().getDeclaredProperties()\n" + + "For a type derived from an XML schema this will be the sequence they appeared in the\n" + + "schema document.\n" + + "We can use these integer values to get and set Properties on the company DataObject\n\n" + + "company.setString(COMPANY_NAME, \"MegaCorp\");\n" + + "List departments = company.getList(COMPANY_DEPARTMENT);\n" + + "List employees = department.getList(DEPARTMENT_EMPLOYEES);\n"); + + company.setString(COMPANY_NAME, "MegaCorp"); + + + + commentary("The sample continues, altering the data graph by using the getter\n" + + "and setter methods that take int arguments"); + + List departments = company.getList(COMPANY_DEPARTMENT); + DataObject department = (DataObject) departments.get(0); + List employees = department.getList(DEPARTMENT_EMPLOYEES); + DataObject employeeFromList = (DataObject) employees.get(2); + employeeFromList.detach(); + + DataObject newEmployee = department.createDataObject(DEPARTMENT_EMPLOYEES); + + newEmployee.set(EMPLOYEE_NAME, "Al Smith"); + newEmployee.set(EMPLOYEE_SN, "E0005"); + newEmployee.setBoolean(EMPLOYEE_MANAGER, true); + + company.set(COMPANY_EMPLOYEE_OF_MONTH, newEmployee.get(EMPLOYEE_SN)); + + commentary("After some more manipulation (take a look at the sample code to see the detail)\n" + + "we can examine the state of the modified graph"); + + printer.print(company); + System.out.println(printer.getBuf()); + printer.reset(); + + commentary("Or rendered in XML it now looks like this ..."); + System.out.println(scope.getXMLHelper().save(company, SampleInfrastructure.COMPANY_NAMESPACE, "company")); + + } +} diff --git a/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/CreateCompanyTuscanyAPI.java b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/CreateCompanyTuscanyAPI.java new file mode 100644 index 0000000000..097814d35f --- /dev/null +++ b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/CreateCompanyTuscanyAPI.java @@ -0,0 +1,114 @@ +/** + * + * 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.intermediate; + +import java.io.FileOutputStream; + +import org.apache.tuscany.samples.sdo.internal.SampleInfrastructure; +import org.apache.tuscany.sdo.api.SDOUtil; + +import commonj.sdo.DataGraph; +import commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; + +/** + * This sample is a variant of the CreateCompany sample where the graph of DataObjects + * is contained in an instance of DataGraph. + * <p> + * <h3>Running this Sample</h3> See <A HREF="../../../../../../index.html" + * target="_top">the main overview</A> for instructions on how to run this + * sample. + */ +public class CreateCompanyTuscanyAPI extends org.apache.tuscany.samples.sdo.basic.CreateCompany { + + public CreateCompanyTuscanyAPI(Integer commentaryLevel) { + super(commentaryLevel, SAMPLE_LEVEL_INTERMEDIATE); + } + + /** + * XML file generated for the company DataGraph + */ + private 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) { + /* + * this sample is best suited so someone with an intermediate level + * of understanding of SDO. Change the experience level argument to get more + * or less commentary. + */ + CreateCompanyTuscanyAPI sample = new CreateCompanyTuscanyAPI(COMMENTARY_FOR_INTERMEDIATE); + sample.run(); + + } + + public void runSample () throws Exception { + + banner('*', + " SDO Sample Create Company in a DataGraph \n\n"+ + "This is a variant of the simple CreateCompany sample, but\n"+ + "demonstrates how to create a DataGraph in the absence of a Data Access Service (DAS)\n"+ + "This is not typical of how an SDO user would code, because a DAS is usually\n"+ + "responsible for creating a DataGraph instance. This is why we must use a Tuscany API\n"+ + "to create the dataGraph in the first place\n"+ + "If you don't have a need to use a DataGraph instance then you may want to skip this\n"+ + "sample, or leave it until you have explored the DataObject interface fully\n"); + + commentary(COMMENTARY_FOR_INTERMEDIATE, + "As we are dealing with a DataGraph, the SDO API has some gray areas at the moment\n"+ + "in that the DataGraph API hasn't yet been developed to deal with scopes\n"+ + "other than the default scope. So here is an occasion where we must use "+ + "the default singleton scope"); + HelperContext scope = useDefaultScopeForTypes(); + loadTypesFromXMLSchemaFile(scope, SampleInfrastructure.COMPANY_XSD); + + commentary (COMMENTARY_FOR_INTERMEDIATE, + "Here is the use of the Tuscany API for creating a DataGraph instance\n\n"+ + "DataGraph dataGraph = SDOUtil.createDataGraph();"); + + DataGraph dataGraph = SDOUtil.createDataGraph(); + + commentary(COMMENTARY_FOR_INTERMEDIATE, + "Now we create a root object of a selected type for the DataGraph wrapper to contain.\n"+ + "This is an example of a DataGraph interface that currently forces us to use the default scope.\n\n"+ + "company = dataGraph.createRootObject(SampleInfrastructure.COMPANY_NAMESPACE, \"CompanyType\");"); + + DataObject company = dataGraph.createRootObject(SampleInfrastructure.COMPANY_NAMESPACE, "CompanyType"); + + populateGraph(scope, company); + + commentary(COMMENTARY_FOR_INTERMEDIATE, + "Since the SDO API doesn't currently have a method for serializing the DataGraph instance\n"+ + "we again use a Tuscany specific API\n\n"+ + "SDOUtil.saveDataGraph(dataGraph, fos, null);"); + + FileOutputStream fos = new FileOutputStream(COMPANY_DATAGRAPH_GENERATED_XML); + SDOUtil.saveDataGraph(dataGraph, fos, null); + + SDOUtil.saveDataGraph(dataGraph, System.out, null); + } + +} diff --git a/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/CreateDataObjectFromXmlString.java b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/CreateDataObjectFromXmlString.java new file mode 100644 index 0000000000..1ec920146d --- /dev/null +++ b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/CreateDataObjectFromXmlString.java @@ -0,0 +1,133 @@ +/** + * + * 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.intermediate; + +import java.util.List; + +import org.apache.tuscany.samples.sdo.SampleBase; + +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Sequence; +import commonj.sdo.helper.HelperContext; + +/** + * Demonstrates creating a DataObject from a String of XML without an explicit model. + * <p> + * <h3>Running this Sample</h3> See <A HREF="../../../../../../index.html" + * target="_top">the main overview</A> for instructions on how to run this + * sample. + */ +public class CreateDataObjectFromXmlString extends SampleBase { + HelperContext scope; + + + public CreateDataObjectFromXmlString(Integer userLevel) { + super(userLevel, SAMPLE_LEVEL_INTERMEDIATE); + } + + + /** + * 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>"; + + + public static void main(String[] args) { + + CreateDataObjectFromXmlString sample = new CreateDataObjectFromXmlString(COMMENTARY_FOR_NOVICE); + sample.run(); + + } + + /* + * metadata for the sample documenting the areas of SDO that are explored + */ + public static int [] CORE_FUNCTION = { + SDOFacets.USING_BUILT_IN_TYPES, + SDOFacets.LOADING_DATA_FROM_XML + }; + + public void runSample () throws Exception { + + + commentary("Demonstrates creating a DataObject from a String of XML,\n" + + "based upon section titled 'Creating DataObjects from XML documents'\n"+ + "This quite unassuming sample demonstrates the maxim of 'less is more'\n"+ + "in that it might look like other samples where the XML document was loaded\n"+ + "from a file or string, but note that we don't define any Types before loading\n"+ + "the document. Built in generic types are used to model the data graph"); + + + scope = createScopeForTypes(); + + commentary("don't define any types! Just go ahead and load from the String\n\n"+ + "DataObject purchaseOrder = scope.getXMLHelper().load(XML_STRING).getRootObject();"); + + DataObject purchaseOrder = scope.getXMLHelper().load(XML_STRING).getRootObject(); + + commentary("Note that the Type of the newly created DataObject has no Properties defined for it\n"+ + "but the type is said to be \"Open\", which means that an instance of the Type may make use of other Properties\n"+ + "defined elsewhere. The XMLHelper's load operation creates Properties on demand for the DataObject instance to use as it loads\n"+ + "the document\n\n" + + "purchaseOrder.getType().isOpen returns true\n" + + "purchaseOrder.getType().getProperty(\"shipTo\") returns null and\n" + + "purchaseOrder.getInstanceProperty(\"shipTo\") returns a Property"); + + System.out.println("DataObject's type is open?: " + purchaseOrder.getType().isOpen()); + System.out.println("Data Object's type has a property shipTo?: " + (purchaseOrder.getType().getProperty("shipTo") != null)); + System.out.println("Data Object instance has a property shipTo?: " + (purchaseOrder.getInstanceProperty("shipTo") != null)); + + commentary( + "Without a Type definition accessing a DataObject is slightly\n"+ + "different, since without a Type definition SDO does not know\n"+ + "the intended multiplicity of properties. So every Property is assumed to\n"+ + "be multi-valued, and must be accessed via a list\n\n"+ + "List shipToList = purchaseOrder.getList(\"shipTo\");\n"+ + "DataObject shipTo = (DataObject) shipToList.get(0);"); + + List shipToList = purchaseOrder.getList("shipTo"); + DataObject shipTo = (DataObject) shipToList.get(0); + + + + /* + * With no model around, this next operation 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 + Sequence s = name.getSequence(); + String actualName = (String) s.getValue(0); + System.out.println("Name being shipped to: " + actualName); + + } +} diff --git a/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/DynamicCustomerTypeSample.java b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/DynamicCustomerTypeSample.java new file mode 100644 index 0000000000..abe3434fab --- /dev/null +++ b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/DynamicCustomerTypeSample.java @@ -0,0 +1,193 @@ +/** + * + * 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.intermediate; + +import java.util.List; + +import org.apache.tuscany.samples.sdo.SampleBase; + +import commonj.sdo.DataObject; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; + +/** + * Demonstrates creating and using Types dynamically. + * <p> + * <h3>Running this Sample</h3> See <A HREF="../../../../../../index.html" + * target="_top">the main overview</A> for instructions on how to run this + * sample. + */ +public class DynamicCustomerTypeSample extends SampleBase { + HelperContext scope; + + public DynamicCustomerTypeSample(Integer userLevel) { + super(userLevel, SAMPLE_LEVEL_INTERMEDIATE); + } + + + /** + * 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"; + + /** + * Drives sample methods + * + * @param args + * no arguments required + */ + public static void main(String[] args) { + + DynamicCustomerTypeSample sample = + new DynamicCustomerTypeSample(COMMENTARY_FOR_INTERMEDIATE); + sample.run(); + + } + + /* + * metadata for the sample documenting the areas of SDO that are explored + */ + public static int [] CORE_FUNCTION = { + SDOFacets.CREATE_TYPES_USING_THE_SDO_API + }; + /* + * metadata for the sample documenting the areas of SDO that are explored + */ + public static int [] SIGNIFICANT_FUNCTION = { + SDOFacets.SET_PROPERTIES_OF_DATAOBJECT_BY_NAME, + SDOFacets.USING_BUILT_IN_TYPES, + SDOFacets.SAVING_DATA_TO_XML + }; + + public void runSample () throws Exception { + + commentary(COMMENTARY_ALWAYS, + "Demonstrates the use of the SDO API to build types dynamically\n"+ + "by building a data graph representing the type system and\n"+ + "submitting that graph to TypeHelper.define()"); + + + scope = createScopeForTypes(); + + + commentary("A TypeHelper is used for both looking up types, and later creating them\n\n"+ + "TypeHelper typeHelper = scope.getTypeHelper();"); + + TypeHelper typeHelper = scope.getTypeHelper(); + + commentary("We can look up existing types to use in the creation of Properties\n\n"+ + "Type intType = types.getType(\"commonj.sdo\", \"Int\");\n"+ + "Type stringType = types.getType(\"commonj.sdo\", \"String\");"); + + Type intType = typeHelper.getType("commonj.sdo", "Int"); + Type stringType = typeHelper.getType("commonj.sdo", "String"); + + commentary("To begin modeling the type system we create a DataObject with\n"+ + "Type \"commonj.sdo#Type\" and set the URI and name for that type\n\n"+ + + "DataObject customerType = scope.getDataFactory().create(\"commonj.sdo\", \"Type\");\n"+ + "customerType.set(\"uri\", \"http://example.com/customer\");\n"+ + "customerType.set(\"name\", \"Customer\");"); + + DataObject customerType = scope.getDataFactory().create("commonj.sdo", "Type"); + customerType.set("uri", "http://example.com/customer"); + customerType.set("name", "Customer"); + + commentary("Now we can create a model for the Properties for the Type\n"+ + "and set the name and Types of those Properties\n\n"+ + "DataObject custNumProperty = customerType.createDataObject(\"property\");\n"+ + "custNumProperty.set(\"name\", \"custNum\");\n"+ + "custNumProperty.set(\"type\", intType);" + ); + + DataObject custNumProperty = customerType.createDataObject("property"); + custNumProperty.set("name", "custNum"); + custNumProperty.set("type", intType); + + commentary("We continue in this manner until all the Types and their Properties are modeled"); + DataObject lastNameProperty = customerType.createDataObject("property"); + lastNameProperty.set("name", "lastName"); + lastNameProperty.set("type", stringType); + + DataObject firstNameProperty = customerType.createDataObject("property"); + firstNameProperty.set("name", "firstName"); + firstNameProperty.set("type", stringType); + + commentary("Now that our type is fully modeled we submit the model to the TypeHelper\n"+ + "The new Type instance is returned to us, but is also available for lookup within\n"+ + "the scope associated with the TypeHelper\n\n"+ + "Type t = typeHelper.define(customerType);"); + Type t = typeHelper.define(customerType); + + commentary("Here we see the newly created Type being accessed via the TypeHelper\n"+ + "along with a printout of the Type's Properties\n\n"+ + "Type testType = scope.getTypeHelper().getType(\"http://example.com/customer\", \"Customer\");"); + + Type testType = scope.getTypeHelper().getType("http://example.com/customer", "Customer"); + List props = testType.getProperties(); + for (int i = 0; i < props.size(); i++) { + System.out.println(props.get(i)); + } + + commentary("Now we can create an instance of the type using the DataFactory associated with the type scope\n\n"+ + "DataFactory factory = scope.getDataFactory();\n"+ + "DataObject customer1 = factory.create(\"http://example.com/customer\", \"Customer\");"); + + DataFactory factory = scope.getDataFactory(); + DataObject customer1 = factory.create("http://example.com/customer", "Customer"); + customer1.setInt("custNum", 1); + customer1.set("firstName", "John"); + customer1.set("lastName", "Adams"); + + commentary("Here's an XML String representing a DataObject we have created with the new type"); + String xmlDocString = scope.getXMLHelper().save(customer1, CUSTOMER_NAMESPACE, "customer"); + System.out.println(xmlDocString); + + + } + +} diff --git a/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/SerializingDeserializingADataObject.java b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/SerializingDeserializingADataObject.java new file mode 100644 index 0000000000..eaecf436bf --- /dev/null +++ b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/SerializingDeserializingADataObject.java @@ -0,0 +1,137 @@ +/** + * + * 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.intermediate; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import org.apache.tuscany.samples.sdo.SampleBase; +import org.apache.tuscany.samples.sdo.internal.SampleInfrastructure; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; + +/** + * Demonstrates serializing and deserializing a DataObject to disk + * using java serialization. + * <p> + * <h3>Running this Sample</h3> See <A HREF="../../../../../../index.html" + * target="_top">the main overview</A> for instructions on how to run this + * sample. + */ +public class SerializingDeserializingADataObject extends SampleBase { + HelperContext scope; + + public SerializingDeserializingADataObject(Integer userLevel) { + super(userLevel, SAMPLE_LEVEL_INTERMEDIATE); + } + + + public static void main(String[] args) { + + SerializingDeserializingADataObject sample = + new SerializingDeserializingADataObject(COMMENTARY_FOR_INTERMEDIATE); + sample.run(); + + } + + /* + * metadata for the sample documenting the areas of SDO that are explored + */ + public static int [] CORE_FUNCTION = { + SDOFacets.JAVA_SERIALIZATION_OF_DATA_GRAPH + }; + /* + * metadata for the sample documenting the areas of SDO that are explored + */ + public static int [] SIGNIFICANT_FUNCTION = { + SDOFacets.TESTING_FOR_GRAPH_EQUALITY + }; + + public void runSample () throws Exception { + + commentary( + "Demonstrates serializing and deserializing a DataObject\n" + + "to disk using Java serialization."); + + scope = useDefaultScopeForTypes(); + loadTypesFromXMLSchemaFile(scope, SampleInfrastructure.COMPANY_XSD); + DataObject company = getDataObjectFromFile(scope, SampleInfrastructure.COMPANY_DATAOBJECT_XML); + + + commentary("We've loaded a data graph 'company' from a file\n" + + "using XML schema for the model and XML for the graph in the usual manner\n"); + String fileName = "temporarySerializedDataObject.xml"; + commentary("We've loaded a data graph 'company' from a file\n" + + "using XML schema for the model and XML for the graph in the usual manner\n" + + "Now we are going to serialize it to, and read it from a temporary file: "+fileName); + + commentary( + "The following code, which doesn't use any SDO APIs, demonstrates the\n" + + "underlying SDO function of performing Java serialization on SDO objects\n\n" + + "FileOutputStream fos = new FileOutputStream(fileName);\n" + + "ObjectOutputStream out = new ObjectOutputStream(fos);\n" + + "out.writeObject(company);\n" + + "out.close();"); + + FileOutputStream fos = new FileOutputStream(fileName); + ObjectOutputStream out = new ObjectOutputStream(fos); + out.writeObject(company); + out.close(); + + // read in DataObject + commentary("Having written the data graph to the temporary file we\n" + + "can read it back\n\n" + + "FileInputStream fis = new FileInputStream(fileName);\n" + + "ObjectInputStream input = new ObjectInputStream(fis);\n" + + "DataObject newDataObject = (DataObject) input.readObject();\n" + + "input.close();"); + + FileInputStream fis = new FileInputStream(fileName); + ObjectInputStream input = new ObjectInputStream(fis); + DataObject newDataObject = (DataObject) input.readObject(); + input.close(); + + /** + * Compare data graphs + */ + + commentary("We can use the SDO EqualityHelper to check that we have got\n" + + "back an equivalent graph to the one we had originally\n\n" + + "boolean equal = scope.getEqualityHelper().equal(company, newDataObject);"); + + boolean equal = scope.getEqualityHelper().equal(company, newDataObject); + System.out.println("DataObjects are equal: " + equal); + + //print out xml representation + System.out.println(); + System.out.println("Original company DataObject:"); + System.out.println(scope.getXMLHelper().save(company, SampleInfrastructure.COMPANY_NAMESPACE, "company")); + + System.out.println(); + System.out.println("Deserialized company DataObject:"); + System.out.println(scope.getXMLHelper().save(newDataObject, SampleInfrastructure.COMPANY_NAMESPACE, "company")); + + + } +} diff --git a/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/package.html b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/package.html new file mode 100644 index 0000000000..59ea1c39e6 --- /dev/null +++ b/sdo-java/branches/sdo-1.1.1-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/package.html @@ -0,0 +1,30 @@ +<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>Intermediate SDO Samples</title>
+</head>
+<BODY>
+<h1>Intermediate Service Data Object (SDO) Sample Programs</h1>
+<h3>Running the Samples</h3> See <A HREF="../../../../../../index.html"
+target="_top">the main overview</A> for instructions on how to run these
+samples.
+</BODY>
+</HTML>
|