Package org.apache.tuscany.samples.sdo.specExampleSection

Source Code of org.apache.tuscany.samples.sdo.specExampleSection.AccessDataObjectsUsingXPath

/**
*
*  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");
    }
}
TOP

Related Classes of org.apache.tuscany.samples.sdo.specExampleSection.AccessDataObjectsUsingXPath

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.