summaryrefslogtreecommitdiffstats
path: root/sdo-java/branches/sdo-1.0-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/CreateCompanyTuscanyAPI.java
blob: da631227d60d176a161111a0cded390bbcbf23c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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);
    }

}