forked from ARSBlue/ToolBox-4-Iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PersistentEvent.cls
156 lines (142 loc) · 5.88 KB
/
PersistentEvent.cls
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
/// This class contains all information about persistent events
///
/// ARSBlue ToolBox-4-Iris
/// Copyright © 2019 ARS Blue GmbH
/// http://www.ars-blue.at
Class arsblue.event.PersistentEvent Extends arsblue.event.Event
{
/// The object identity (%Oid=$LB(id,classname))
Property Oid As %ObjectIdentity;
/// The event action (one of INSERT, UPDATE or DELETE)
Property Action As %EnumString(VALUELIST = ",INSERT,UPDATE,DELETE");
/// A JSON object with the event action details.
/// On INSERT the new object will be JSON serialized.
/// On UPDATE a JSON object with all modifications: { propertyDataType: [oldValue, newValue], propertyObject: { propertyDataType: [oldValue, newValue], ... }, ... }.
/// On DELETE the old object will be JSON serialized.
Property ActionDetails As %DynamicObject;
/// This method adds a persistent event listener.
/// <ul>
/// <li>EventListener...the created persistent event listener.</li>
/// <li>EventTarget...the event target.</li>
/// <li>EventDeamon...the event listener deamon.</li>
/// <li>EventQueue...the event listener queue.</li>
/// <li>ClassName...the classname to monitor.</li>
/// <li>Actions...the actions to monitor (see <code>arsblue.event.PersistentEventListener.SetActions</code> method for more details).</li>
/// </ul>
/// Returns status OK if successfully created a persistent event listener, any other status signals failure and the listener will be set to null!
ClassMethod AddEventListener(ByRef EventListener As EventListener = {$$$NULLOREF}, EventTarget As %String = "", EventDeamon As %Integer, EventQueue As arsblue.event.EventQueue, ClassName As %String = "", Actions As %String) As %Status
{
#dim status as %Status = $$$OK
try
{
new %objlasterror
set:('$$$TypeOf(EventListener,..%ClassName(1)_"Listener")) EventListener=$ClassMethod(..%ClassName(1)_"Listener","%New") $$$Throw($G(%objlasterror,$$$OK))
set:($$$ISNULL(EventListener.ClassName)||'$$$ISNULL(ClassName)) EventListener.ClassName=ClassName $$$Throw($G(%objlasterror,$$$OK))
if ($D(Actions)) $$$ThrowOnError(EventListener.SetActions(Actions))
}
catch (exc)
{
set status=exc.AsStatus()
}
if ($$$ISERR(status))
{
set EventListener=$$$NULLOREF
quit status
}
else
{
quit ##super(.EventListener,EventTarget,.EventDeamon,.EventQueue)
}
}
/// This method gets a persistent event listener.
/// <ul>
/// <li>EventListener...the created persistent event listener.</li>
/// <li>EventTarget...the event target.</li>
/// <li>EventDeamon...the event listener deamon.</li>
/// <li>EventQueue...the event listener queue.</li>
/// <li>ClassName...the classname to monitor.</li>
/// <li>Actions...the actions to monitor (see <code>arsblue.event.PersistentEventListener.SetActions</code> method for more details).</li>
/// </ul>
/// Returns the persistent event listener or null on any failure!
ClassMethod GetEventListener(EventTarget As %String = "", EventDeamon As %Integer, EventQueue As arsblue.event.EventQueue, ClassName As %String = "", Actions As %String) As EventListener
{
do ..GetEventListenerSQL(.sql,.params,EventTarget,.EventDeamon,.EventQueue,ClassName,.Actions)
set stmt=##class(%SQL.Statement).%New()
quit:($$$ISERR(stmt.%Prepare(.sql))) $$$NULLOREF
set rs=stmt.%Execute(params...)
set eventListener=$$$NULLOREF
set:(rs.%Next()) eventListener=##class(arsblue.event.EventListener).%OpenId(rs.ID)
quit eventListener
}
/// This method gets the SQL statement to get a persistent event listener.
/// This method will be overwritten by derived events specifying the event parameters.
/// NOTE: Don't forget to call super class first!
/// <ul>
/// <li>sql...the SQL statement (by reference).</li>
/// <li>params...the SQL statement parameters (by reference).</li>
/// <li>EventTarget...the event target classname to call.</li>
/// <li>EventDeamon...the event listener deamon.</li>
/// <li>EventQueue...the event listener queue.</li>
/// <li>ClassName...the classname to monitor.</li>
/// <li>Actions...the actions to monitor (see <code>arsblue.event.PersistentEventListener.SetActions</code> method for more details).</li>
/// </ul>
/// Returns the persistent event listener SQL statement.
ClassMethod GetEventListenerSQL(ByRef sql, ByRef params, EventTarget As %String = "", EventDeamon As %Integer, EventQueue As arsblue.event.EventQueue, ClassName As %String = "", Actions As %String) [ Private ]
{
do ##super(.sql,.params,EventTarget,.EventDeamon,.EventQueue)
set:('$$$ISNULL(ClassName)) sql($I(sql))="and ClassName=?",params($I(params))=ClassName
if ($D(Actions))
{
set actions=","_$ZCVT(Actions,"U")_","
for action="INSERT","UPDATE","DELETE"
{
if ($F(actions,","_action_",") || $F(actions,","_action_"=1,"))
{
set sql($I(sql))="and Action"_$ZCVT(action,"W")_"=?",params($I(params))=1
}
elseif ($F(actions,","_action_"=2,"))
{
set sql($I(sql))="and Action"_$ZCVT(action,"W")_"=?",params($I(params))=2
}
else
{
set sql($I(sql))="and Action"_$ZCVT(action,"W")_"=?",params($I(params))=0
}
}
}
}
/// This method fires the persistent event.
/// Returns status OK if successfully processed event, any other status signals failure!
Method FireEvent() As %Status
{
$$$QuitOnError(##super())
#dim status as %Status = $$$OK
try {
set status=$ClassMethod(..EventListener.EventTarget,"OnPersistentEvent",$this)
}
catch (exc)
{
set status=exc.AsStatus()
}
quit status
}
Storage Default
{
<Data name="ActionDetails">
<Attribute>ActionDetails</Attribute>
<Structure>node</Structure>
<Subscript>"arsblue.event.PersistentEvent.ActionDetails"</Subscript>
</Data>
<Data name="PersistentEventDefaultData">
<Subscript>"PersistentEvent"</Subscript>
<Value name="1">
<Value>Oid</Value>
</Value>
<Value name="2">
<Value>Action</Value>
</Value>
</Data>
<DefaultData>PersistentEventDefaultData</DefaultData>
<Type>%Storage.Persistent</Type>
}
}