summaryrefslogtreecommitdiffstats
path: root/sdo-cpp/branches/sdo-cpp-pre2.1/samples/misc/ObjectCreation.cpp
blob: 0db919cc6924d84fab4513632f73b53e3ae23593 (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
250
251
/*
 * 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.
 */

/* $Rev$ $Date$ */

#include "samples.h"
using namespace std;

void ObjectCreation::sample()
{


    cout << " ********** Sample ObjectCreation **********" << endl;

    try {


        /** 
         * Get a data factory. With it we can either create metadata
         * or load it from an XSD.
         */

        DataFactoryPtr mdg  = DataFactory::getDataFactory();
 
        /**
         * Add some Types to the data factory..
         * The booleans at on addType are:
         * "isSequenced", "isOpen" "isAbstract" and "isDataType"
         */

        mdg->addType("myspace","Company");

        mdg->addType("myspace","Department");
        
        /**
         * Manager is going to be a sequenced type...
         */
        
        mdg->addType("myspace","Manager", true, false);

        mdg->addType("myspace","Employee");


        /**
         * We will make employee and manager sub-types of 'person'
         */

        mdg->addType("myspace","Person", true, false);


        /**
         * having all the types defined, we can now define the tree
         * by giving properties to the types.
         */

        /** 
         * We could use the API passing in name and URI for each
         * Type , or get the Types back and use them directly.
         * Here we get back the types to use...
         */

        const Type& tc = mdg->getType("myspace","Company");
        const Type& ts = mdg->getType("commonj.sdo","String");
        const Type& ti = mdg->getType("commonj.sdo","Integer");
        const Type& tm = mdg->getType("myspace","Manager");
        const Type& td = mdg->getType("myspace","Department");
        const Type& te = mdg->getType("myspace","Employee");
        const Type& tp = mdg->getType("myspace","Person");


        /**
         * Example 1 - add a property of type String to type company
         */

        mdg->addPropertyToType(tc,"name",ts);

        /** 
         * Example 2 - add using the name of the company instead of the
         * type...
         */

        mdg->addPropertyToType("myspace","Company","address",ts);

        /**
         * Example 3 - add a many valued property 
         */

        mdg->addPropertyToType(tc,"departments", "myspace","Department",
        true);


        /**
         * Example 4 - add a reference property
         */

        mdg->addPropertyToType(tc,"employee of the month", "myspace",
             "Employee",false, false, false);


        /**
         * Add other department properties...
         */

        mdg->addPropertyToType(td,"name", ts);
        mdg->addPropertyToType(td,"id", ti);
        mdg->addPropertyToType(td,"manager", tm);
        mdg->addPropertyToType(td,"employees",te,true,false,true);

        /**
         * Add a name to the person
         */

        mdg->addPropertyToType(tp,"name", ts);



        /**
         * Make employees and mamagers both substypes of person
         */

        mdg->setBaseType(te,tp);
          mdg->setBaseType(tm,tp);
  
        /**
         * And give them different properties of their own.
         */

        mdg->addPropertyToType(tm,"officeid", ts);
        mdg->addPropertyToType(te,"cubelocation", ts);

        
      
        /**
         * The data structure looks like this:
     
         *  Company
         *  ----name (String)
         *  ----address *String)
         *  ----departments (Department, many valued)
         *  ----employee of the month ( Employee - reference)
     
         * Person
         * ----name (String)

         * Employee
         * ----name (String - inherited from Person)
         * ----cubelocation (String)

         * Manager
         * ----name (String - inherited from Person)
         * ----officeid (String)

         * Department
         * ----name (String)
         * ----id (Integer)
         * ---- manager (Manager)
         * ---- employees (Employee - many valued)

    
        /** 
         * create an object of type Company using the DataFactory
         */

        DataObjectPtr dor = mdg->create((Type&)tc);

        /**
         * Set the company name to Acme
         */

        dor->setCString("name","Acme");

        /**
         * Set up the two departments  - using the 
         * DataObject createDataObject API
         */

        DataObjectPtr dep1 = dor->createDataObject("departments");
        dep1->setCString("name","Development");
        dep1->setInteger("id",100);

        DataObjectPtr man1 = dep1->createDataObject("manager");
        man1->setCString("name","Herve Jones");

        DataObjectPtr dep2= dor->createDataObject("departments");
        dep2->setCString("name","Marketing");
        dep2->setInteger("id",200);

        DataObjectPtr man2 = dep2->createDataObject("manager");
        man1->setCString("name","August Phan");

        /**
         * Give the departments some employees
         */

        DataObjectPtr emp1 = dep1->createDataObject("employees");
        emp1->setCString("name","Fred Appleby");
        emp1->setCString("cubelocation","100-A");

        DataObjectPtr emp2 = dep1->createDataObject("employees");
        emp2->setCString("name","Jane Bloggs");
        emp2->setCString("cubelocation","100-B");

        DataObjectPtr emp3 = dep2->createDataObject("employees");
        emp3->setCString("name","Robin Corbet");
        emp3->setCString("cubelocation","200-A");

        DataObjectPtr emp4 = dep2->createDataObject("employees");
        emp4->setCString("name","Martha Denby");
        emp4->setCString("cubelocation","200-B");

        cout << "Company Name:" << dor->getCString("name") << endl;
        
        DataObjectList& depts = dor->getList("departments");
        for (int i=0;i<depts.size();i++)
        {
            cout << "  Department Name:" << depts[i]->getCString("name") << endl;
            
            DataObjectList& emps = depts[i]->getList("employees");
 
            for (int j=0;j<emps.size();j++)
            {
                cout << "    Employee Name:" << emps[j]->getCString("name") << endl;
            }
        }
    }
    catch (SDORuntimeException e)
    {
        cout << "Exception in ObjectCreation" <<endl;
        cout << e;
    }
    cout << " ********** Sample ends ********************" << endl;
    return;
}