Apache sdo学习笔记(五)——变更摘要

1:schema文件company.xsd

<?xml version="1.0" encoding="UTF-8"?>
 <xsd:schema xmlns:company="company.ns" 
             xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
             targetNamespace="company.ns">
    <xsd:element name="company" type="company:CompanyType"/>
    <xsd:complexType name="CompanyType">
	    <xsd:sequence>
		    <xsd:element name="departments" type="company:DepartmentType" />
	    </xsd:sequence>
	    <xsd:attribute name="name" type="xsd:string"/>
	    <xsd:attribute name="employeeOfTheMonth" type="xsd:string"/>
    </xsd:complexType>
    <xsd:complexType name="DepartmentType">
	    <xsd:sequence>
    		<xsd:element name="employees" type="company:EmployeeType"   maxOccurs="unbounded"/>
	    </xsd:sequence>
	    <xsd:attribute name="name" type="xsd:string"/>
	    <xsd:attribute name="location" type="xsd:string"/>
    	<xsd:attribute name="number" type="xsd:int"/>
    </xsd:complexType>
    <xsd:complexType name="EmployeeType">
	    <xsd:attribute name="name" type="xsd:string"/>    
	    <xsd:attribute name="SN" type="xsd:ID"/>
	    <xsd:attribute name="manager" type="xsd:boolean"/>
    </xsd:complexType>
 </xsd:schema>


2:java代码

package com.waysoft.intergration.dao;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.tuscany.sdo.api.SDOUtil;

import commonj.sdo.DataGraph;
import commonj.sdo.DataObject;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.XSDHelper;
import commonj.sdo.impl.HelperProvider;

public class Test2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			HelperContext helperContext = HelperProvider.getDefaultContext();
			XSDHelper xsdHelper = helperContext.getXSDHelper();
			URL url = new File(Test2.class.getResource("/").getPath().substring(1)+"company.xsd").toURI().toURL();
			xsdHelper.define(url.openStream(), url.toString());

			DataGraph graph = SDOUtil.createDataGraph();
			DataObject company = graph.createRootObject("company.ns","CompanyType");
			populateGraph(company);


			Map<String, String> options = new HashMap<String, String>();

			DataObject root = graph.getRootObject();
			System.out.println("+++++++++++++++++++++++初始值++++++++++++++++++++++++++++++++++");
			System.out.println("company name:"+root.getString("name"));
			System.out.println("company employeeOfTheMonth:"+root.getString("employeeOfTheMonth"));
			SDOUtil.saveDataGraph(graph, System.out, options);
			
			
			System.out.println("+++++++++++++++++++++++改变后++++++++++++++++++++++++++++++++++");
			
			graph.getChangeSummary().beginLogging();
			root = graph.getRootObject();
			root.setString("name", "bangde");
			root.setString("employeeOfTheMonth", "avc");
			System.out.println("company name:"+root.getString("name"));
			System.out.println("company employeeOfTheMonth:"+root.getString("employeeOfTheMonth"));
			SDOUtil.saveDataGraph(graph, System.out, options);
			
			
			System.out.println("+++++++++++++++++++++++恢复++++++++++++++++++++++++++++++++++");
			graph.getChangeSummary().undoChanges();
			root = graph.getRootObject();
			System.out.println("company name:"+root.getString("name"));
			System.out.println("company employeeOfTheMonth:"+root.getString("employeeOfTheMonth"));
			SDOUtil.saveDataGraph(graph, System.out, options);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	private static  void populateGraph(DataObject company) {
		company.setString("name", "ACME");
		company.setString("employeeOfTheMonth", "E001");

		DataObject departments = company.createDataObject("departments");
		departments.setString("name", "Advanced Technologies");
		departments.setString("location", "NS");
		departments.setString("number", "123");
		
		DataObject johnJones = departments.createDataObject("employees");
		johnJones.setString("name", "John Jones");
		johnJones.setString("SN", "E002");

		DataObject janeDoe = departments.createDataObject("employees");
		janeDoe.setString("name", "Jane Doe");
		janeDoe.setString("SN", "E003");

		DataObject fred = departments.createDataObject("employees");
		fred.setString("name", "Fred");
		fred.setString("SN", "E004");
		fred.setString("manager", "true");           
	}

}


3:运行结果

+++++++++++++++++++++++初始值++++++++++++++++++++++++++++++++++
company name:ACME
company employeeOfTheMonth:E001
<?xml version="1.0" encoding="ASCII"?>
<sdo:datagraph xmlns:company="company.ns" xmlns:sdo="commonj.sdo">
  <company:CompanyType name="ACME"
      employeeOfTheMonth="E001">
    <departments name="Advanced Technologies" location="NS" number="123">
      <employees name="John Jones" SN="E002"/>
      <employees name="Jane Doe" SN="E003"/>
      <employees name="Fred" SN="E004" manager="true"/>
    </departments>
  </company:CompanyType>
</sdo:datagraph>
+++++++++++++++++++++++改变后++++++++++++++++++++++++++++++++++
company name:bangde
company employeeOfTheMonth:avc
<?xml version="1.0" encoding="ASCII"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:company="company.ns"
    xmlns:sdo="commonj.sdo" xmlns:sdo_1="http://www.apache.org/tuscany/2005/SDO">
  <changeSummary xmlns=""
      logging="true">
    <objectChanges key="#//@eRootObject">
      <value xsi:type="sdo_1:ChangeSummarySetting" featureName="name" dataValue="ACME"/>
      <value xsi:type="sdo_1:ChangeSummarySetting" featureName="employeeOfTheMonth"
          dataValue="E001"/>
    </objectChanges>
  </changeSummary>
  <company:CompanyType name="bangde"
      employeeOfTheMonth="avc">
    <departments name="Advanced Technologies" location="NS" number="123">
      <employees name="John Jones" SN="E002"/>
      <employees name="Jane Doe" SN="E003"/>
      <employees name="Fred" SN="E004" manager="true"/>
    </departments>
  </company:CompanyType>
</sdo:datagraph>
+++++++++++++++++++++++恢复++++++++++++++++++++++++++++++++++
company name:ACME
company employeeOfTheMonth:E001
<?xml version="1.0" encoding="ASCII"?>
<sdo:datagraph xmlns:company="company.ns" xmlns:sdo="commonj.sdo">
  <changeSummary xmlns=""
      logging="true"/>
  <company:CompanyType name="ACME"
      employeeOfTheMonth="E001">
    <departments name="Advanced Technologies" location="NS" number="123">
      <employees name="John Jones" SN="E002"/>
      <employees name="Jane Doe" SN="E003"/>
      <employees name="Fred" SN="E004" manager="true"/>
    </departments>
  </company:CompanyType>
</sdo:datagraph>


从红色部分我们可以对比分析变更摘要的含义,这里就不多废话了。

你可能感兴趣的:(sdo)