-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuditClass.xml
executable file
·228 lines (190 loc) · 8.32 KB
/
AuditClass.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="24" zv="Cache for UNIX (IBM AIX for System P5-64) 2010.1.4 (Build 803U)" ts="2011-06-28 08:58:35">
<Class name="Spectrum.Util.AuditClass">
<Super>%Persistent</Super>
<TimeChanged>62270,32163.631311</TimeChanged>
<TimeCreated>62180,62915.069152</TimeCreated>
<UDLText name="T">
<Content><![CDATA[
/*
Author: Dan McCracken
Date: 4/14/2011 Moved to Prod: 5/4/2011
Version: 1.0
Class that will audit any persistent class that extends it, and store the data in Spectrum.Util.AuditStorageClass.
Author: Dan McCracken
Date: 5/31/2011 Moved to Prod: 6/28/2011
Version: 1.1
Updated %OnBeforeSave to check and make sure the root global node of a new class has been built..
If it hasn't, set the maxID = 0 and add 1 so that it's assigned ID value #1.
*/
]]></Content>
</UDLText>
<Method name="%OnBeforeSave">
<Description><![CDATA[
This callback method is invoked by the <METHOD>%Save</METHOD> method to
provide notification that the object is being saved. It is called after
the object's data has been successfully written to disk.
<P><VAR>insert</VAR> will be set to 1 if this object is being saved for the first time.
<P>If this method returns an error then the call to <METHOD>%Save</METHOD> will fail.]]></Description>
<CodeMode>objectgenerator</CodeMode>
<FormalSpec>insert:%Boolean</FormalSpec>
<Private>1</Private>
<ReturnType>%Status</ReturnType>
<ServerOnly>1</ServerOnly>
<Implementation><![CDATA[
// %OnBeforeSave - check to see if the ..%Id() has value. If it does not, then it's an INSERT. Else its an UPDATE.
// If its an UPDATE, audit each field to see if the value changed. Do a lookup off OrefId and embedded SQL..
// If it did change, then log that to the Dump field as a HashPair of some kind: "DepAlias=>From PDCL To PDOD"
Do %code.WriteLine(" Set saveAudit = 1")
Do %code.WriteLine(" Set obj = ##class(Spectrum.Util.AuditClassStorage).%New()")
Do %code.WriteLine(" Set obj.OrefId = ..%Id()")
Do %code.WriteLine(" Set obj.ClassName = ..%ClassName(1)")
Do %code.WriteLine(" Set obj.UserName = $USERNAME")
Do %code.WriteLine(" If (insert) {")
// Get next ID value for this class, and assign that as the OrefId
// Take the largest ID from the audited table and assign the next one to a new add
Do %code.WriteLine(" Set maxID = """"")
Do %code.WriteLine(" Set obj.AuditType = ""INSERT""")
// Find the global name the persistent class being extended, so we can do a %code.WriteLine with it to get the next ID subscript
// Open dictionary access for class being extended.
S class = ##class(%Dictionary.ClassDefinition).%OpenId(%compiledclass.Name)
// Check the storage definition profile name.. if null, use "Default"
S storageName = class.StorageStrategy
If (storageName = "") {
S storageName = "Default"
}
// Open the storage definition class for the class/def combo
S storageDefinition = ##class(%Dictionary.StorageDefinition).%OpenId(%compiledclass.Name_"||"_storageName)
// Access the DataLocation property, which is the global name being used by the persistent class
S globalName = storageDefinition.DataLocation
// Check for no root node created yet, and set maxID = 0 if it's not.
// This will prevent an undefined error when a class is first created with the auditor extended.
// 5/31/2011 -DM
Do %code.WriteLine(" If $Data("_globalName_") {")
Do %code.WriteLine(" Set maxID = "_globalName)
Do %code.WriteLine(" }")
Do %code.WriteLine(" Else {")
Do %code.WriteLine(" Set maxID = 0")
Do %code.WriteLine(" }")
Do %code.WriteLine(" Set obj.OrefId = maxID + 1")
Do %code.WriteLine(" }")
Do %code.WriteLine(" Else {")
// Do not save the audit object if it was not modified.
Do %code.WriteLine(" If (..%ObjectModified()) { Set obj.AuditType = ""UPDATE"" }")
Do %code.WriteLine(" Else { Set saveAudit = 0 }")
Do %code.WriteLine(" }")
Do %code.WriteLine(" If (saveAudit) {")
// Rip through each property of the class.. that does not start with a %
For i = 1:1:%compiledclass.Properties.Count() {
Set prop = %compiledclass.Properties.GetAt(i).Name
IF ($EXTRACT(prop) '= "%") {
Do %code.WriteLine(" Set sc = obj.AppendHashPair("""_prop_""",.."_prop_")")
}
}
Do %code.WriteLine(" Do obj.%Save()")
Do %code.WriteLine(" }")
Do %code.WriteLine(" Quit $$$OK")
Quit $$$OK
]]></Implementation>
</Method>
<Method name="%OnDelete">
<Description><![CDATA[
This callback method is invoked by the <METHOD>%Delete</METHOD> method to
provide notification that the object specified by <VAR>oid</VAR> is being deleted.
<P>If this method returns an error then the object will not be deleted.]]></Description>
<ClassMethod>1</ClassMethod>
<CodeMode>objectgenerator</CodeMode>
<FormalSpec>oid:%ObjectIdentity</FormalSpec>
<Private>1</Private>
<ReturnType>%Status</ReturnType>
<ServerOnly>1</ServerOnly>
<Implementation><![CDATA[
// Open the instance of the soon to be deleted object from the oid.
Do %code.WriteLine(" Set deleteMe = ..%Open(oid)")
Do %code.WriteLine(" Set obj = ##class(Spectrum.Util.AuditClassStorage).%New()")
Do %code.WriteLine(" Set obj.OrefId = deleteMe.%Id()")
Do %code.WriteLine(" Set obj.ClassName = deleteMe.%ClassName(1)")
Do %code.WriteLine(" Set obj.UserName = $USERNAME")
Do %code.WriteLine(" Set obj.AuditType = ""DELETE""")
// Rip through each property of the class.. that does not start with a %
For i = 1:1:%compiledclass.Properties.Count() {
Set prop = %compiledclass.Properties.GetAt(i).Name
IF ($EXTRACT(prop) '= "%") {
Do %code.WriteLine(" Set sc = obj.AppendHashPair("""_prop_""",deleteMe."_prop_")")
}
}
Do %code.WriteLine(" Do obj.%Save()")
Do %code.WriteLine(" Quit $$$OK")
Quit $$$OK
]]></Implementation>
</Method>
<Method name="TakeAuditSnapshot">
<Description>
Initialize the auditStorageClass with current state values.
Open all records, and save a SNAPSHOT of each row in the AuditStorageClass.</Description>
<ClassMethod>1</ClassMethod>
<CodeMode>objectgenerator</CodeMode>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
// %compiledclass.SqlQualifiedNameQ
// %compiledclass.Name
Do %code.WriteLine(" Set tStatement = ##class(%SQL.Statement).%New()")
Do %code.WriteLine(" Set tSQL = 2")
Do %code.WriteLine(" Set tSQL(1) = ""SELECT %ID FROM " _ %compiledclass.SqlQualifiedNameQ _ """")
Do %code.WriteLine(" Set tSQL(2) = ""Order By %ID ASC""")
Do %code.WriteLine(" Set sc = tStatement.%Prepare(.tSQL)")
Do %code.WriteLine(" Set result = tStatement.%Execute()")
// If sqlcode is a negative number, quit.. the statement didn't run right.
Do %code.WriteLine(" QUIT:(result.%SQLCODE < 0)")
Do %code.WriteLine(" While (result.%Next()) {")
Do %code.WriteLine(" Set currentID = result.%Get(""ID"")")
Do %code.WriteLine(" Set tObj = ##class(" _ %compiledclass.Name _ ").%OpenId(currentID)")
Do %code.WriteLine(" Set auditObj = ##class(Spectrum.Util.AuditClassStorage).%New()")
Do %code.WriteLine(" Set auditObj.OrefId = tObj.%Id()")
Do %code.WriteLine(" Set auditObj.ClassName = tObj.%ClassName(1)")
Do %code.WriteLine(" Set auditObj.UserName = $USERNAME")
Do %code.WriteLine(" Set auditObj.AuditType = ""SNAPSHOT""")
// Rip through each property of the class.. that does not start with a %
For i = 1:1:%compiledclass.Properties.Count() {
Set prop = %compiledclass.Properties.GetAt(i).Name
IF ($EXTRACT(prop) '= "%") {
Do %code.WriteLine(" Set sc = auditObj.AppendHashPair("""_prop_""",tObj."_prop_")")
}
}
Do %code.WriteLine(" Do auditObj.%Save()")
Do %code.WriteLine(" }")
Do %code.WriteLine(" Quit sc")
]]></Implementation>
</Method>
<Storage name="Default">
<Type>%Library.CacheStorage</Type>
<DataLocation>^Spectrum.Util.AuditClassD</DataLocation>
<DefaultData>AuditClassDefaultData</DefaultData>
<IdLocation>^Spectrum.Util.AuditClassD</IdLocation>
<IndexLocation>^Spectrum.Util.AuditClassI</IndexLocation>
<StreamLocation>^Spectrum.Util.AuditClassS</StreamLocation>
<Data name="AuditClassDefaultData">
<Structure>listnode</Structure>
<Subscript/>
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>Object</Value>
</Value>
<Value name="3">
<Value>TimeStamp</Value>
</Value>
<Value name="4">
<Value>Dump</Value>
</Value>
<Value name="5">
<Value>ClassName</Value>
</Value>
<Value name="6">
<Value>OrefId</Value>
</Value>
</Data>
</Storage>
</Class>
</Export>