-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathPluginTrigger.cs
More file actions
232 lines (202 loc) · 9.27 KB
/
PluginTrigger.cs
File metadata and controls
232 lines (202 loc) · 9.27 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Metadata;
using System.Runtime.ExceptionServices;
using XrmMockupShared.Plugin;
using XrmPluginCore.Enums;
using XrmPluginCore.Interfaces.Plugin;
using System.Diagnostics;
using DG.Tools.XrmMockup.Internal;
using DG.Tools.XrmMockup.Extensions;
namespace DG.Tools.XrmMockup
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
internal class PluginTrigger : IComparable<PluginTrigger>
{
public Action<MockupServiceProviderAndFactory> PluginExecute { get; }
internal string EntityName { get; }
internal string Operation { get; }
internal ExecutionStage Stage { get; }
internal ExecutionMode Mode { get; }
internal int Order { get; }
internal Guid? ImpersonatingUserId { get; }
readonly Dictionary<string, EntityMetadata> Metadata;
readonly HashSet<string> Attributes;
readonly IEnumerable<IImageSpecification> Images;
private string DebuggerDisplay => $"{Stage} {Operation} {EntityName} [{string.Join(", ", Attributes)}]";
public PluginTrigger(string operation, ExecutionStage stage,
Action<MockupServiceProviderAndFactory> pluginExecute, IPluginStepConfig pluginRegistration, Dictionary<string, EntityMetadata> metadata)
{
PluginExecute = pluginExecute;
EntityName = pluginRegistration.EntityLogicalName;
Operation = operation;
Stage = stage;
Mode = pluginRegistration.ExecutionMode;
Order = pluginRegistration.ExecutionOrder;
Images = pluginRegistration.ImageSpecifications;
Metadata = metadata;
ImpersonatingUserId = pluginRegistration.ImpersonatingUserId;
var attrs = pluginRegistration.FilteredAttributes ?? "";
Attributes = string.IsNullOrWhiteSpace(attrs) ? new HashSet<string>() : new HashSet<string>(attrs.Split(','));
}
public ExecutionMode GetExecutionMode()
{
return Mode;
}
public int GetExecutionOrder()
{
return Order;
}
// Saves "execution" for Async plugins to be executed after sync plugins.
public PluginExecutionProvider ToPluginExecution(object entityObject, Entity preImage, Entity postImage, PluginContext pluginContext, Core core)
{
var entity = entityObject as Entity;
var entityRef = entityObject as EntityReference;
var guid = (entity != null) ? entity.Id : entityRef.Id;
var logicalName = (entity != null) ? entity.LogicalName : entityRef.LogicalName;
if (VerifyPluginTrigger(entity, logicalName, guid, preImage, postImage, pluginContext))
{
// Create the plugin context
var thisPluginContext = CreatePluginContext(pluginContext, guid, logicalName, preImage, postImage);
return new PluginExecutionProvider(PluginExecute, new MockupServiceProviderAndFactory(core, thisPluginContext, core.TracingServiceFactory));
}
return null;
}
public void ExecuteIfMatch(object entityObject, Entity preImage, Entity postImage, PluginContext pluginContext, Core core)
{
// Check if it is supposed to execute. Returns preemptively, if it should not.
var entity = entityObject as Entity;
var entityRef = entityObject as EntityReference;
var entityCollection = entityObject as EntityCollection;
var guid =
entity != null
? entity.Id :
entityRef != null
? entityRef.Id
: Guid.Empty;
var logicalName =
entity != null
? entity.LogicalName :
entityRef != null
? entityRef.LogicalName
: entityCollection.EntityName;
if (VerifyPluginTrigger(entity, logicalName, guid, preImage, postImage, pluginContext))
{
var thisPluginContext = CreatePluginContext(pluginContext, guid, logicalName, preImage, postImage);
//Create Serviceprovider, and execute plugin
MockupServiceProviderAndFactory provider = new MockupServiceProviderAndFactory(core, thisPluginContext, core.TracingServiceFactory);
try
{
PluginExecute(provider);
}
catch (TargetInvocationException e)
{
ExceptionDispatchInfo.Capture(e.InnerException).Throw();
}
foreach (var parameter in thisPluginContext.SharedVariables)
{
pluginContext.SharedVariables[parameter.Key] = parameter.Value;
}
}
}
private void CheckInfiniteLoop(PluginContext pluginContext)
{
if (pluginContext.Depth > 8)
{
throw new FaultException(
"This workflow job was canceled because the workflow that started it included an infinite loop." +
" Correct the workflow logic and try again.");
}
}
private void CheckSpecialRequest()
{
if (EntityName != "" && (Operation.Matches(EventOperation.Associate) || Operation.Matches(EventOperation.Disassociate)))
{
throw new MockupException(
$"An {Operation} plugin step was registered for a specific entity, which can only be registered on AnyEntity");
}
}
private Entity AddPostImageAttributesToEntity(Entity entity, Entity preImage, Entity postImage)
{
if (Operation.Matches(EventOperation.Update) && Stage == ExecutionStage.PostOperation
&& preImage != null && postImage != null)
{
var shadowAddedAttributes = postImage.Attributes.Where(a => !preImage.Attributes.ContainsKey(a.Key) && !entity.Attributes.ContainsKey(a.Key));
entity = entity.CloneEntity();
entity.Attributes.AddRange(shadowAddedAttributes);
}
return entity;
}
private bool FilteredAttributesMatches(Entity entity)
{
if (!Operation.Matches(EventOperation.Update) || Attributes.Count == 0)
{
return true;
}
bool foundAttr = false;
foreach (var attr in entity.Attributes)
{
if (Attributes.Contains(attr.Key))
{
foundAttr = true;
break;
}
}
return foundAttr;
}
private bool VerifyPluginTrigger(Entity entity, string logicalName, Guid guid, Entity preImage, Entity postImage, PluginContext pluginContext)
{
if (EntityName != "" && EntityName != logicalName) return false;
if (entity != null && Metadata.GetMetadata(logicalName)?.PrimaryIdAttribute != null)
{
entity[Metadata.GetMetadata(logicalName).PrimaryIdAttribute] = guid;
}
CheckInfiniteLoop(pluginContext);
entity = AddPostImageAttributesToEntity(entity, preImage, postImage);
CheckSpecialRequest();
if (FilteredAttributesMatches(entity))
{
return true;
}
return false;
}
private PluginContext CreatePluginContext(PluginContext pluginContext, Guid guid, string logicalName, Entity preImage, Entity postImage)
{
var thisPluginContext = pluginContext.Clone();
thisPluginContext.Mode = (int)this.Mode;
thisPluginContext.Stage = (int)this.Stage;
if (thisPluginContext.PrimaryEntityId == Guid.Empty)
{
thisPluginContext.PrimaryEntityId = guid;
}
thisPluginContext.PrimaryEntityName = logicalName;
if (ImpersonatingUserId is Guid impersonatingUserId && impersonatingUserId != Guid.Empty)
{
thisPluginContext.UserId = impersonatingUserId;
}
foreach (var image in this.Images)
{
var imageType = image.ImageType;
var cols = !string.IsNullOrEmpty(image.Attributes) ? new ColumnSet(image.Attributes.Split(',')) : new ColumnSet(true);
if (postImage != null && Stage == ExecutionStage.PostOperation && (imageType == ImageType.PostImage || imageType == ImageType.Both))
{
thisPluginContext.PostEntityImages.Add(image.ImageName, postImage.CloneEntity(Metadata.GetMetadata(postImage.LogicalName), cols));
}
if (preImage != null && (imageType == ImageType.PreImage || imageType == ImageType.Both))
{
thisPluginContext.PreEntityImages.Add(image.ImageName, preImage.CloneEntity(Metadata.GetMetadata(preImage.LogicalName), cols));
}
}
return thisPluginContext;
}
public int CompareTo(PluginTrigger other)
{
return Order.CompareTo(other.Order);
}
}
}