summaryrefslogtreecommitdiffstats
path: root/sandbox/jboynes/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/otherSources/CreateCompany.java
blob: c4eddabb6b083cade0285e3aabef553747d1c843 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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;
        }
    }


}