summaryrefslogtreecommitdiffstats
path: root/sca-cpp/branches/cpp-M1/sdo/samples/misc/ChangeSummarySave.cpp
blob: 5d65ec100952e2225106117318881df1c22e8444 (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
/*
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed 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: 2005/12/22 16:54:14 $ */

#include "samples.h"
using namespace std;

void ChangeSummarySave::sample()
{
    cout << " ********** ChangeSummarySave sample********" << endl;

    try {

        DataFactoryPtr mdg  = DataFactory::getDataFactory();

        XSDHelperPtr xsh = HelperProvider::getXSDHelper(mdg);
        xsh->defineFile("ChangeSummarySave.xsd");

        /**
         * Load the schema from ChangeSummarySave.xsd
         */

        const Type& tstring  = mdg->getType("commonj.sdo","String");
        const Type& tbool    = mdg->getType("commonj.sdo","Boolean");
        const Type& tcs      = mdg->getType("commonj.sdo","ChangeSummary");
        const Type& tcomp    = mdg->getType("companyNS","CompanyType");
        const Type& tdept    = mdg->getType("companyNS","DepartmentType");
        const Type& temp     = mdg->getType("companyNS","EmployeeType");

    
        /**
         * create a graph, set the change summary logging, modify the
         * data, then save it to a file
         */

        DataObjectPtr comp = mdg->create((Type&)tcomp);
        comp->setCString("name","ACME");

        DataObjectPtr dept = mdg->create((Type&)tdept);
        DataObjectList& dol = comp->getList("departments");
        dol.append(dept);

        dept->setCString("name","Advanced Technologies");
        dept->setCString("location","NY");
        dept->setCString("number","123");

        DataObjectPtr emp1 = mdg->create(temp);
        DataObjectPtr emp2 = mdg->create(temp);
        DataObjectPtr emp3 = mdg->create(temp);

        emp1->setCString("name","John Jones");
        emp1->setCString("SN","E0001");

        emp2->setCString("name","Mary Smith");
        emp2->setCString("SN","E0002");
        emp2->setBoolean("manager",true);

        emp3->setCString("name","Jane Doe");
        emp3->setCString("SN","E0003");

        DataObjectList& dol2 = dept->getList("employees");
        dol2.append(emp1);
        dol2.append(emp2);
        dol2.append(emp3);

        
        /**
         * Set the employee of the month - which is a reference, not
         * a containment value
         */

        comp->setDataObject("employeeOfTheMonth",emp2);

        /** 
         * The XSD defined the company type as having a change summary,
         * so we can get it...
         */
          
        ChangeSummaryPtr cs = comp->getChangeSummary();

        /**
         * And ask it to start logging...
         */

        cs->beginLogging();

        /**
         * With logging on, create a new employee
         */

        DataObjectPtr emp4 = mdg->create(temp);
        emp4->setCString("name","Al Smith");
        emp4->setCString("SN","E0004");
        emp4->setBoolean("manager",true);

        /**
         * The first recorded change happens now, as the employee is
         * added into the data graph. Emp4 (Al Smith) will appear in the
         * change summary as a creation.  There will also be a change
         * record for the list "employees" of this department, holding the
         * values before Al was added.
         */ 
        dol2.append(emp4);

        /** 
         * The second change is to remove element 1 from the
         * same list - Thats Mary Smith. 
         * Mary will appear as a deletion, but there will be no extra
         * change record for "employees", as its already been changed.
         * Mary was employee of the month, so that reference gets 
         * emptied, and a change record is set up for it, recording
         * Mary as the old value.
         */

        dol2.remove(1); // element 1  is Mary

        DataObjectPtr emp5 = mdg->create(temp);
        emp5->setCString("name","Bill Withers");
        emp5->setCString("SN","E0005");

        
        /**
         * The third change is to  append Bill to the same list.
         * Bill appears as a creation, but there is no change recorded to
         * the employees list.
         */

        dol2.append(emp5);


        /**
         * The company name is changed. A change record is set up for
         * the property "name" of this company. It stores the old value
         * "ACME"
         */

        comp->setCString("name","MegaCorp");

       /**
         * The company employee of the month is changed.  The old
         * value has already been changed from Mary to NULL, so no change
         * record is created here at all
         */

        comp->setDataObject("employeeOfTheMonth",emp4);

  
        /** 
         * Stop logging changes
         */

        cs->endLogging();


        XMLHelperPtr xmh = HelperProvider::getXMLHelper(mdg);
        XMLDocumentPtr doc = xmh->createDocument(comp,"companyNS","company");
        xmh->save(doc,"ChangeSummarySave-output.xml");

        /**
         * Have a look in the file and see if you can recognise the changes
         * above
         */

    }
    catch (SDORuntimeException e)
    {
        cout << "Exception in ChangeSummarySave" << endl;
        cout << e;
    }
    cout << " ********** Sample ends ********************" << endl;
}