-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginDeployer.cs
1054 lines (902 loc) · 44.5 KB
/
PluginDeployer.cs
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Activities;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Xml;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using PowerArgs;
using Xrm.PluginDeployer.Entities;
using Xrm.PluginDeployer.StepModel;
using Xrm.PluginDeployer.UnitOfWork;
using Xrm.PluginDeployer.Utility;
namespace Xrm.PluginDeployer
{
public class PluginDeployer
{
private readonly ConsoleLogger logger;
private readonly IOrganizationService sourceSystem;
private readonly IOrganizationService destinationSystem;
private readonly string solutionPreFix;
private string SolutionName { get; set; }
/// <summary>
/// Plugin Assembly
/// </summary>
public Assembly AssemblyPlugin { get; private set; }
/// <summary>
/// XmlDocument containing the assembly documentation
/// </summary>
private XmlDocument CommentXml { get; set; }
/// <summary>
/// Constructor
/// </summary>
public PluginDeployer(IOrganizationService sourceSystem,
IOrganizationService destinationSystem,
string solutionPreFix,
ConsoleLogger logger)
{
this.sourceSystem = sourceSystem;
this.destinationSystem = destinationSystem;
this.logger = logger;
this.solutionPreFix = solutionPreFix;
}
/// <summary>
/// Load the assembly and generate a solutionName
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public bool LoadAssembly(string filePath)
{
if (!File.Exists(filePath))
{
logger.Log($"Could not find the specified assembly at {filePath}");
return false;
}
GenerateSolutionName(filePath);
logger.Log("Assembly loaded");
var xmlPath = filePath.Replace(".dll", ".xml");
if (!File.Exists(xmlPath))
{
logger.Log($"Could not find the corresponding xml file at {xmlPath}");
return false;
}
CommentXml = new XmlDocument();
CommentXml.Load(xmlPath);
return true;
}
private void GenerateSolutionName(string filePath)
{
AssemblyPlugin = Assembly.LoadFrom(filePath);
SolutionName = solutionPreFix + AssemblyPlugin.GetName().Name.Replace(".", "").Replace("_", "").Replace("-", "");
if (SolutionName.Length > 50)
{
SolutionName = SolutionName.Substring(0, 50);
}
}
/// <summary>
/// Create or update the PluginAssembly depending on the given pluginAssemblyEntity
/// </summary>
/// <param name="pluginAssembly">PluginAssembly or null</param>
/// <returns></returns>
private PluginAssembly UploadPluginAssemblyToDestination(PluginAssembly pluginAssembly)
{
if (AssemblyPlugin == null)
{
throw new InvalidOperationException("Load Assembly first");
}
var dllContentBytes = File.ReadAllBytes(AssemblyPlugin.Location);
var dllContent = Convert.ToBase64String(dllContentBytes);
if (pluginAssembly == null)
{
return CreatePluginAssembly(destinationSystem, AssemblyPlugin, dllContent);
}
UpdatePluginAssembly(destinationSystem, pluginAssembly, dllContent);
return pluginAssembly;
}
/// <summary>
/// Create PluginType Entries in the destination system for all pluginTypes and workflows
/// which are found in the assembly but are not registered in the destination system yet
///
/// Workflows are a special type of PluginType
/// </summary>
/// <param name="pluginAssembly">PluginAssembly from the destination system</param>
private void CreateNewPluginTypesInDestination(Entity pluginAssembly)
{
var foundPlugins = SearchForPlugins().ToList();
var registeredPluginTypes = RetrievePluginTypes(destinationSystem, pluginAssembly.Id).ToList();
// Create new pluginTypes
foreach (var type in foundPlugins)
{
var pluginType = registeredPluginTypes.FirstOrDefault(registeredPluginType =>
registeredPluginType.TypeName.Equals(type.FullName));
if (pluginType != null)
{
UpdatePluginType(destinationSystem, pluginType, type);
}
else
{
CreatePluginType(destinationSystem, pluginAssembly.Id, type);
}
}
// Create new workflows
var foundWorkflows = SearchForWorkflows().ToList();
foreach (var type in foundWorkflows)
{
if (!registeredPluginTypes.Any(registeredPluginType =>
registeredPluginType.TypeName.Equals(type.FullName)))
{
CreatePluginType(destinationSystem, pluginAssembly.Id, type);
}
}
}
private void DeleteStepsByPluginType(Entity pluginType)
{
var dependentObjects = RetrieveSdkStepsByPluginType(destinationSystem, pluginType);
DeleteDependentObjects(destinationSystem, dependentObjects);
}
private void DeleteDependentObjects(IOrganizationService service, IEnumerable<Dependency> dependentObjects)
{
foreach (var d in dependentObjects)
{
if (d.DependentComponentType.Value ==
(int) SolutionComponent.OptionSet.ComponentType.SdkMessageProcessingStep)
{
if (d.DependentComponentObjectId == null)
{
continue;
}
service.Delete(SdkMessageProcessingStep.EntityLogicalName, d.DependentComponentObjectId.Value);
logger.Log($"SdkMessageProcessingStep '{d.DependentComponentObjectId.Value}' deleted successfully");
}
else
{
throw new InvalidOperationException(
$"Unknown DependentComponentValue: {d.DependentComponentType.Value}");
}
}
}
/// <summary>
/// Creates solution with all components of the pluginAssembly in the source system. Then Imports the solution to the destination system
/// </summary>
private void SyncPluginStepsViaSolution(Entity sourcePluginAssembly,
Entity destPluginAssembly,
string publisher)
{
var solutions = RetrieveSpecificSolution(sourceSystem).ToList();
if (solutions.Any())
{
DeleteSolution(sourceSystem, solutions.First());
}
CreateSolution(sourceSystem, publisher);
var registeredDestPluginTypes = RetrievePluginTypes(destinationSystem, destPluginAssembly.Id).ToList();
var registeredSourcePluginTypes = RetrievePluginTypes(sourceSystem, sourcePluginAssembly.Id).ToList();
var itemsToAddToSolution = new List<PluginType>();
registeredDestPluginTypes.ForEach(destType =>
{
var intersectcandidate =
registeredSourcePluginTypes.FirstOrDefault(sourceType => sourceType.TypeName == destType.TypeName);
if (intersectcandidate != null)
{
itemsToAddToSolution.Add(intersectcandidate);
}
});
AddItemsToSolution(sourceSystem, itemsToAddToSolution);
var solution = ExportSolution(sourceSystem);
ImportSolutionInDestination(solution);
}
/// <summary>
/// Delete all registered plugintypes+steps and workflows which are not in the assembly anymore
/// </summary>
/// <param name="pluginAssembly"></param>
private void DeleteOldPluginTypeStepsOfAssembly(Entity pluginAssembly)
{
if (pluginAssembly == null)
{
return;
}
var foundPlugins = SearchForPlugins().ToList();
var registeredPluginTypes = RetrievePluginTypes(destinationSystem, pluginAssembly.Id).ToList();
// Delete old pluginTypes
foreach (var pluginType in registeredPluginTypes.Where(pt =>
pt.IsWorkflowActivity.HasValue && !pt.IsWorkflowActivity.Value))
{
if (!foundPlugins.Any(ft => ft.FullName != null && ft.FullName.Equals(pluginType.TypeName)))
{
DeleteStepsByPluginType(pluginType);
DeletePluginType(pluginType);
}
}
var foundWorkflows = SearchForWorkflows().ToList();
// Delete old workflows
foreach (var pluginType in registeredPluginTypes.Where(pt =>
pt.IsWorkflowActivity.HasValue && pt.IsWorkflowActivity.Value))
{
if (!foundWorkflows.Any(ft => ft.FullName != null && ft.FullName.Equals(pluginType.TypeName)))
{
DeletePluginType(pluginType);
}
}
}
/// <summary>
/// Delete all registered plugintypes+steps and workflows which are not in the assembly anymore
/// </summary>
/// <param name="pluginAssembly"></param>
private void DeleteAllPluginTypeStepsOfAssembly(Entity pluginAssembly)
{
if (pluginAssembly == null)
{
return;
}
var registeredPluginTypes = RetrievePluginTypes(destinationSystem, pluginAssembly.Id).ToList();
// Delete old pluginTypes
foreach (var pluginType in registeredPluginTypes.Where(pt =>
pt.IsWorkflowActivity.HasValue && !pt.IsWorkflowActivity.Value))
{
DeleteStepsByPluginType(pluginType);
DeletePluginType(pluginType);
}
// Delete old workflows
foreach (var pluginType in registeredPluginTypes.Where(pt =>
pt.IsWorkflowActivity.HasValue && pt.IsWorkflowActivity.Value))
{
DeletePluginType(pluginType);
}
}
private void UpdatePluginAssembly(IOrganizationService service, PluginAssembly pluginAssembly, string content)
{
var updatedAssembly = new PluginAssembly
{
LogicalName = PluginAssembly.EntityLogicalName,
Id = pluginAssembly.Id,
Content = content
};
service.Update(updatedAssembly);
// Necessary because otherwise further steps will fail, because the new plugin content is not
// properly loaded in the system.
PluginAssembly newPluginAssembly;
var count = 10;
do
{
Thread.Sleep(100);
newPluginAssembly = service.Retrieve(PluginAssembly.EntityLogicalName,
pluginAssembly.Id,
new ColumnSet(PluginAssembly.PropertyNames.VersionNumber)).ToEntity<PluginAssembly>();
count--;
logger.Log(
$"Retry #{10 - count}: Old VersionNumber: {pluginAssembly.VersionNumber}, New VersionNumber: {newPluginAssembly.VersionNumber}");
} while (pluginAssembly.VersionNumber == newPluginAssembly.VersionNumber && count >= 0);
logger.Log("Successfully updated PluginAssembly");
}
/// <summary>
/// Find all classes of the assembly via reflection which implement the interface IPlugin
/// </summary>
/// <returns></returns>
private IEnumerable<Type> SearchForPlugins()
{
return AssemblyPlugin.ExportedTypes.Where(
t => t.IsClass
&& !t.IsAbstract
&& t.GetInterface(typeof(IPlugin).FullName) != null
);
}
/// <summary>
/// Find all classes of the assembly via reflection which directly inherit from CodeActivity
/// </summary>
/// <returns></returns>
private IEnumerable<Type> SearchForWorkflows()
{
return AssemblyPlugin.ExportedTypes.Where(
t => t.IsClass
&& !t.IsAbstract
&& t.BaseType == typeof(CodeActivity)
);
}
private PluginAssembly CreatePluginAssembly(IOrganizationService service, Assembly assembly, string content)
{
var bytes = assembly.GetName().GetPublicKeyToken();
var publicKeyToken = BitConverter.ToString(bytes).Replace("-", "");
var pluginAssembly = new PluginAssembly
{
LogicalName = PluginAssembly.EntityLogicalName,
Content = content,
Name = assembly.GetName().Name,
Culture = assembly.GetName().CultureName,
Version = assembly.GetName().Version.ToString(),
PublicKeyToken = publicKeyToken,
SourceType = new OptionSetValue((int) PluginAssembly.OptionSet.SourceType.Database),
IsolationMode = new OptionSetValue((int) PluginAssembly.OptionSet.IsolationMode.None)
};
var id = service.Create(pluginAssembly);
logger.Log($"Successfully created PluginAssembly '{assembly.GetName().Name}' with id {id}");
pluginAssembly.Id = id;
return pluginAssembly;
}
private void CreatePluginType(IOrganizationService service, Guid pluginAssemblyId, Type type)
{
var pluginType = CreatePluginTypeEntity(pluginAssemblyId, type);
var id = service.Create(pluginType);
logger.Log($"Successfully created PluginType '{type.FullName}' with id {id}");
}
private void UpdatePluginType(IOrganizationService service, PluginType existingPluginType, Type type)
{
var description = GetDescription(type);
if (existingPluginType.Description == description) return;
var updatePluginType = new PluginType
{
Id = existingPluginType.Id,
Description = description
};
service.Update(updatePluginType);
}
private PluginType CreatePluginTypeEntity(Guid pluginAssemblyId, Type type)
{
var description = GetDescription(type);
var pluginType = new PluginType
{
LogicalName = PluginType.EntityLogicalName,
PluginAssemblyId = new EntityReference(PluginAssembly.EntityLogicalName, pluginAssemblyId),
TypeName = type.FullName,
FriendlyName = Guid.NewGuid().ToString(),
Name = type.FullName,
Description = description
};
return pluginType;
}
private string GetDescription(Type type)
{
var descriptionNode =
CommentXml.SelectSingleNode($"doc/members/member[@name=\"T:{type.FullName}\"]/summary");
var description = "No description";
if (descriptionNode != null)
{
description = descriptionNode.InnerText.Trim(' ', '\r', '\n').Replace("\r\n ", " ");
if (description.Length > 256) description = description.Substring(0, 256);
}
return description;
}
private void DeletePluginType(PluginType pluginType)
{
destinationSystem.Delete(PluginType.EntityLogicalName, pluginType.Id);
logger.Log($"Successfully deleted PluginType '{pluginType.TypeName}'");
}
private void DeleteSolution(IOrganizationService service, Solution solution)
{
service.Delete("solution", solution.Id);
logger.Log($"Successfully deleted Solution '{solution.UniqueName}'");
}
private void CreateSolution(IOrganizationService service, string publisher)
{
var solution = new Solution
{
LogicalName = Solution.EntityLogicalName,
FriendlyName = SolutionName,
UniqueName = SolutionName,
Version = "1.0.0.0",
PublisherId = RetrievePublisher(service, publisher).ToEntityReference()
};
var id = service.Create(solution);
logger.Log($"Successfully created Solution '{solution.UniqueName}' with id {id}");
}
private void AddItemsToSolution(IOrganizationService service, List<PluginType> pluginTypes)
{
var executeMultipleRequest =
new ExecuteMultipleRequest
{
Settings = new ExecuteMultipleSettings
{
ContinueOnError = true,
// We don't need responses for succeeded requests, failed requests always return responses
ReturnResponses = false
},
Requests = new OrganizationRequestCollection()
};
pluginTypes.ForEach(pluginType =>
{
foreach (var d in RetrieveSdkStepsByPluginType(service, pluginType))
{
if (d.DependentComponentType.Value ==
(int) SolutionComponent.OptionSet.ComponentType.SdkMessageProcessingStep
|| d.DependentComponentType.Value ==
(int) SolutionComponent.OptionSet.ComponentType.Workflow)
{
if (d.DependentComponentObjectId != null)
executeMultipleRequest.Requests.Add(new AddSolutionComponentRequest
{
AddRequiredComponents = false,
ComponentId = d.DependentComponentObjectId.Value,
ComponentType = d.DependentComponentType.Value,
SolutionUniqueName = SolutionName
});
executeMultipleRequest.Requests.AddRange(RetrieveDependentComponents(service, d));
}
else
{
throw new InvalidOperationException(
$"Unknown DependentComponentValue: {d.DependentComponentType.Value}");
}
}
}
);
if (!executeMultipleRequest.Requests.Any()) return;
var executeMultipleResponse = (ExecuteMultipleResponse) service.Execute(executeMultipleRequest);
if (executeMultipleResponse.IsFaulted)
{
var errorMessages = executeMultipleResponse.Responses
.Where(r => r.Fault != null)
.Select(r => r.Fault.Message);
logger.Error($"Errors occured:\n{string.Join(Environment.NewLine, errorMessages)}");
}
else
{
logger.Log($"Successfully added {executeMultipleRequest.Requests.Count} components to solution");
}
}
private byte[] ExportSolution(IOrganizationService service)
{
var exportSolutionRequest = new ExportSolutionRequest
{
Managed = false,
SolutionName = SolutionName
};
var result = (ExportSolutionResponse) service.Execute(exportSolutionRequest);
return result.ExportSolutionFile;
}
private void ImportSolutionInDestination(byte[] solutionContent)
{
var importSolutionRequest = new ImportSolutionRequest
{
CustomizationFile = solutionContent,
PublishWorkflows = true
};
destinationSystem.Execute(importSolutionRequest);
}
/// <summary>
/// Updates destination system according to arguments
/// Could delete plugins / steps / images
/// </summary>
/// <param name="parsedArgs"></param>
/// <param name="destPluginAssembly"></param>
public void UpdateSystem(CmdArgs parsedArgs, PluginAssembly destPluginAssembly)
{
if (!parsedArgs.Sync)
{
try
{
// Update Assembly
destPluginAssembly = UploadPluginAssemblyToDestination(destPluginAssembly);
// Create NEW PluginTypes/Workflows
CreateNewPluginTypesInDestination(destPluginAssembly);
}
catch (Exception exception)
{
logger.Error("Exception on Update of Assembly occured:\n", exception);
logger.Error(
"Your Assembly differs from Assembly on Destination-System: PluginTypes or Steps do not fit. Please contact colleagues or try Sync Option");
}
}
else
{
if (parsedArgs.SourceSystem == null)
{
// Deletion of old PluginSteps and PluginTypes
DeleteOldPluginTypeStepsOfAssembly(destPluginAssembly);
// Assembly Update
destPluginAssembly = UploadPluginAssemblyToDestination(destPluginAssembly);
// Create NEW PluginTypes/Workflows
CreateNewPluginTypesInDestination(destPluginAssembly);
}
else
{
var sourcePluginAssembly = RetrievePluginAssembly(sourceSystem, AssemblyPlugin.GetName().Name);
logger.Log($"SourceSystem is \'{parsedArgs.SourceSystem}\'");
// Delete all Steps in Destination
DeleteOldPluginTypeStepsOfAssembly(destPluginAssembly);
// Assembly Update or Create
destPluginAssembly = UploadPluginAssemblyToDestination(destPluginAssembly);
// Create NEW PluginTypes/Workflows
CreateNewPluginTypesInDestination(destPluginAssembly);
// Sync Steps from Source to Destination
SyncPluginStepsViaSolution(sourcePluginAssembly, destPluginAssembly, parsedArgs.Publisher);
logger.Log("Successfully imported solution");
}
}
}
/// <summary>
/// Create plugins / steps / images of an assembly from scratch
/// Based on VS project
/// </summary>
/// <param name="parsedArgs"></param>
/// <param name="destPluginAssembly"></param>
public void CreateFromScratch(CmdArgs parsedArgs, PluginAssembly destPluginAssembly)
{
var uow = new CrmUnitOfWork(destinationSystem);
// Delete destination assembly
DeleteAllPluginTypeStepsOfAssembly(destPluginAssembly);
// Create / Update assembly
destPluginAssembly = UploadPluginAssemblyToDestination(destPluginAssembly);
// Create NEW PluginTypes/Workflows
CreateNewPluginTypesInDestination(destPluginAssembly);
// Create Steps
var file = new FileInfo(parsedArgs.AssemblyPath);
var assemblyName = file.Name.Substring(0, file.Name.Length - 4);
var plugin = (from pl in uow.PluginAssemblies.GetQuery()
where pl.Name == assemblyName
select pl).SingleOrDefault();
var steps = (from st in uow.SdkMessageProcessingSteps.GetQuery()
join pt in uow.PluginTypes.GetQuery() on st.EventHandler.Id equals pt.PluginTypeId
where pt.PluginAssemblyId.Id == plugin.PluginAssemblyId
select st).ToArray().ToDictionary(s => s.UniqueName);
var stepsToCreate = CreateStepsModel(assemblyName);
CreateSteps(uow, plugin, steps, stepsToCreate);
// Create Images
var allImages = (from im in uow.SdkMessageProcessingStepImages.GetQuery()
join st in uow.SdkMessageProcessingSteps.GetQuery() on im.SdkMessageProcessingStepId.Id equals st.SdkMessageProcessingStepId
join pl in uow.PluginTypes.GetQuery() on st.EventHandler.Id equals pl.PluginTypeId
where pl.PluginAssemblyId.Id == plugin.PluginAssemblyId
select im).ToList();
CreateImages(uow, stepsToCreate, allImages, steps);
logger.Log(
$"Successfully created steps and images of Plugin '{AssemblyPlugin.GetName().Name}'");
}
private List<Step> CreateStepsModel(string assemblyName)
{
var plugins = SearchForPlugins();
var workflows = SearchForWorkflows();
var classes = plugins.Concat(workflows);
var stepsToCreate = new List<Step>();
classes.ForEach(cl =>
{
var assmSteps = cl.GetCustomAttributes(false).ToArray();
if (assmSteps.Length > 0)
{
foreach (var assStep in assmSteps)
{
var stepToCreate = new Step
{
Class = cl,
EventType = (CrmEventType) Enum.Parse(typeof(CrmEventType), assStep.GetType().GetProperty(nameof(Step.EventType))?.GetValue(assStep).ToString()),
PrimaryEntity = (string) assStep.GetType().GetProperty(nameof(Step.PrimaryEntity))?.GetValue(assStep),
SecondaryEntity = (string) assStep.GetType().GetProperty(nameof(Step.SecondaryEntity))?.GetValue(assStep),
ExecutionOrder = (int) assStep.GetType().GetProperty(nameof(Step.ExecutionOrder))?.GetValue(assStep),
FilteringAttributes = (string[]) assStep.GetType().GetProperty(nameof(Step.FilteringAttributes))?.GetValue(assStep),
PreImageName = (string) assStep.GetType().GetProperty(nameof(Step.PreImageName))?.GetValue(assStep),
PostImageName = (string) assStep.GetType().GetProperty(nameof(Step.PostImageName))?.GetValue(assStep),
PreImageAttributes = (string[]) assStep.GetType().GetProperty(nameof(Step.PreImageAttributes))?.GetValue(assStep),
PostImageAttributes = (string[]) assStep.GetType().GetProperty(nameof(Step.PostImageAttributes))?.GetValue(assStep),
Offline = (bool) assStep.GetType().GetProperty(nameof(Step.Offline))?.GetValue(assStep),
Stage = (StageEnum) assStep.GetType().GetProperty(nameof(Step.Stage))?.GetValue(assStep)
};
if (stepToCreate.ExecutionOrder == 0)
{
stepToCreate.ExecutionOrder = 1;
}
stepsToCreate.Add(stepToCreate);
}
}
else
{
logger.Error(cl.FullName + " is missing step or solution");
}
});
return stepsToCreate;
}
private void CreateSteps(UnitOfWork.UnitOfWork uow,
PluginAssembly plugin,
IDictionary<string, SdkMessageProcessingStep> steps,
IReadOnlyCollection<Step> stepsToCreate)
{
var pluginTypes = (from pl in uow.PluginTypes.GetQuery()
where pl.PluginAssemblyId.Id == plugin.PluginAssemblyId
select pl).ToArray().ToDictionary(t => t.TypeName);
var sdkMessageIndex = (from s in uow.SdkMessages.GetQuery() select s).ToArray().ToDictionary(s => s.Name);
foreach (var step in stepsToCreate)
{
var sdkMessage = sdkMessageIndex[step.EventType.ToString()];
SdkMessageFilter filter;
if (string.IsNullOrEmpty(step.SecondaryEntity))
{
filter = (from f in uow.SdkMessageFilters.GetQuery()
where f.SdkMessageId.Id == sdkMessage.SdkMessageId
&& f.PrimaryObjectTypeCode == step.PrimaryEntity
select f).SingleOrDefault();
}
else
{
filter = (from f in uow.SdkMessageFilters.GetQuery()
where f.SdkMessageId.Id == sdkMessage.SdkMessageId
&& f.PrimaryObjectTypeCode == step.PrimaryEntity
&& f.SecondaryObjectTypeCode == step.SecondaryEntity
select f).SingleOrDefault();
}
var deployment = 0;
if (step.Offline)
{
deployment = 2;
}
var newStep = new SdkMessageProcessingStep
{
SdkMessageProcessingStepId = Guid.NewGuid(),
Name = step.Name,
Mode = new OptionSetValue(step.Async),
Rank = step.ExecutionOrder,
Stage = new OptionSetValue(step.StageValue),
SupportedDeployment = new OptionSetValue(deployment),
EventHandler = pluginTypes[step.Class.FullName].ToEntityReference(),
SdkMessageId = sdkMessageIndex[step.EventType.ToString()].ToEntityReference(),
SdkMessageFilterId = filter?.ToEntityReference(),
FilteringAttributes = step.FilteringAttributes != null ? step.FilteringAttributes.Length > 1 ? string.Join(",", step.FilteringAttributes) : step.FilteringAttributes[0] : null
};
if (step.Stage == StageEnum.PostOperationAsyncWithDelete)
{
newStep.AsyncAutoDelete = true;
}
uow.Create(newStep);
steps.Add(newStep.UniqueName, newStep);
logger.Log("Added step: " + newStep.Name);
}
var edits = (from s in stepsToCreate where steps.ContainsKey(s.UniqueName) select s).ToArray();
foreach (var edit in edits)
{
var step = steps[edit.UniqueName];
var deployment = 0;
if (edit.Offline)
{
deployment = 2;
}
if (step.SupportedDeployment.Value != deployment)
{
var clean = uow.SdkMessageProcessingSteps.Clean(step);
clean.SupportedDeployment = new OptionSetValue(deployment);
uow.Update(clean);
logger.Log("Changed supported deployment for " + edit.Name + " > " + deployment);
}
switch (edit.Stage)
{
case StageEnum.PostOperationAsyncWithDelete when !(step.AsyncAutoDelete ?? false):
{
var clean = uow.SdkMessageProcessingSteps.Clean(step);
clean.AsyncAutoDelete = true;
uow.Update(clean);
logger.Log("Changed async delete policy deployment for " + edit.Name + " > " + deployment);
break;
}
case StageEnum.PostOperationAsyncWithoutDelete when (step.AsyncAutoDelete ?? false):
{
var clean = uow.SdkMessageProcessingSteps.Clean(step);
clean.AsyncAutoDelete = false;
uow.Update(clean);
logger.Log("Changed async deployment for " + edit.Name + " > " + deployment);
break;
}
}
}
}
private void CreateImages(UnitOfWork.UnitOfWork uow,
IEnumerable<Step> stepsToCreate,
IReadOnlyCollection<SdkMessageProcessingStepImage> allImages,
IReadOnlyDictionary<string, SdkMessageProcessingStep> stepindex)
{
foreach (var step in stepsToCreate)
{
var xrmStep = stepindex[step.UniqueName];
var images = (
from im in allImages
where xrmStep.SdkMessageProcessingStepId != null && im.SdkMessageProcessingStepId.Id == xrmStep.SdkMessageProcessingStepId.Value
select im);
var preImageDefined = !string.IsNullOrEmpty(step.PreImageName);
var sdkMessageProcessingStepImages = images as SdkMessageProcessingStepImage[] ?? images.ToArray();
if (sdkMessageProcessingStepImages.Any())
{
sdkMessageProcessingStepImages.ForEach(image =>
{
if (image == null)
{
var attributes = preImageDefined
? step.PreImageAttributes != null && step.PreImageAttributes.Length > 0
? string.Join(",", step.PreImageAttributes)
: null
: step.PostImageAttributes != null && step.PostImageAttributes.Length > 0
? string.Join(",", step.PostImageAttributes)
: null;
var imageToCreate = new SdkMessageProcessingStepImage
{
SdkMessageProcessingStepImageId = Guid.NewGuid(),
SdkMessageProcessingStepId = xrmStep.ToEntityReference(),
Name = preImageDefined ? step.PreImageName : step.PostImageName,
EntityAlias = preImageDefined ? step.PreImageName : step.PostImageName,
MessagePropertyName = step.MessagePropertyName,
ImageType = preImageDefined ? new OptionSetValue(0) : new OptionSetValue(1),
Description = preImageDefined ? step.PreImageName : step.PostImageName,
Relevant = true,
Attributes1 = attributes
};
uow.Create(imageToCreate);
logger.Log(preImageDefined ? $"Pre Image '{step.PreImageName}' created for '{step.Name}'" : $"Post Image '{step.PostImageName}' created for '{step.Name}'");
}
else
{
var clean = uow.SdkMessageProcessingStepImages.Clean(image);
var atr = preImageDefined ? step.PreImageAttributes == null || step.PreImageAttributes.Length == 0 ? null :
string.Join(",", step.PreImageAttributes) :
step.PostImageAttributes == null || step.PostImageAttributes.Length == 0 ? null :
string.Join(",", step.PostImageAttributes);
if (atr != image.Attributes1)
{
clean.Attributes1 = atr;
uow.Update(clean);
if (preImageDefined)
{
logger.Log("Pre image updated " + step.Name + " :" + atr);
}
else
{
logger.Log("Post image updated " + step.Name + " :" + atr);
}
}
image.Relevant = true;
}
});
}
else
{
var attributes = preImageDefined
? step.PreImageAttributes != null && step.PreImageAttributes.Length > 0
? string.Join(",", step.PreImageAttributes)
: null
: step.PostImageAttributes != null && step.PostImageAttributes.Length > 0
? string.Join(",", step.PostImageAttributes)
: null;
var imageToCreate = new SdkMessageProcessingStepImage
{
SdkMessageProcessingStepImageId = Guid.NewGuid(),
SdkMessageProcessingStepId = xrmStep.ToEntityReference(),
Name = preImageDefined ? step.PreImageName : step.PostImageName,
EntityAlias = preImageDefined ? step.PreImageName : step.PostImageName,
MessagePropertyName = step.MessagePropertyName,
ImageType = preImageDefined ? new OptionSetValue(0) : new OptionSetValue(1),
Description = preImageDefined ? step.PreImageName : step.PostImageName,
Relevant = true,
Attributes1 = attributes
};
uow.Create(imageToCreate);
if (preImageDefined)
{
logger.Log($"Pre Image {step.PreImageName} created " + step.Name);
}
else
{
logger.Log($"Post Image {step.PostImageName} created " + step.Name);
}
}
}
var notNeededs = (from im in allImages where im.Relevant == false select im).ToArray();
foreach (var notNeeded in notNeededs)
{
uow.Delete(notNeeded);
logger.Log("Image deleted for " + notNeeded.Name);
}
}
/// <summary>
/// Fetch the pluginassembly from the given service
/// </summary>
/// <param name="service"></param>
/// <param name="assemblyName"></param>
/// <returns></returns>
public PluginAssembly RetrievePluginAssembly(IOrganizationService service, string assemblyName)
{
var fetchXml =
$@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' no-lock='true'>
<entity name='{PluginAssembly.EntityLogicalName}'>
<attribute name='{PluginAssembly.PropertyNames.PluginAssemblyId}' />
<attribute name='{PluginAssembly.PropertyNames.VersionNumber}' />
<filter type='and'>
<condition attribute='{PluginAssembly.PropertyNames.ComponentState}' operator='eq' value='{(int) PluginAssembly.OptionSet.ComponentState.Published}'/>
<condition attribute='{PluginAssembly.PropertyNames.Name}' operator='eq' value='{assemblyName}'/>
</filter>
</entity>
</fetch>";
var result = service.RetrieveMultiple(new FetchExpression(fetchXml));
if (result.Entities.Count != 1)
{
return null;
}
var pluginAssembly = result.Entities.First().ToEntity<PluginAssembly>();
logger.Log($"Found assembly with id {pluginAssembly.Id}");
return pluginAssembly;
}
private IEnumerable<PluginType> RetrievePluginTypes(IOrganizationService service, Guid pluginAssemblyId)
{
var fetchXml =
$@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' no-lock='true'>
<entity name='{PluginType.EntityLogicalName}'>
<attribute name='{PluginType.PropertyNames.PluginTypeId}' />
<attribute name='{PluginType.PropertyNames.TypeName}' />
<attribute name='{PluginType.PropertyNames.IsWorkflowActivity}' />
<attribute name='{PluginType.PropertyNames.Description}' />
<filter type='and'>
<condition attribute='{PluginType.PropertyNames.PluginAssemblyId}' operator='eq' value='{pluginAssemblyId}'/>
<condition attribute='{PluginType.PropertyNames.ComponentState}' operator='eq' value='{(int) PluginType.OptionSet.ComponentState.Published}'/>
</filter>
</entity>
</fetch>";
var response = service.RetrieveMultiple(new FetchExpression(fetchXml));
return response.Entities.Select(e => e.ToEntity<PluginType>());
}
private IEnumerable<Solution> RetrieveSpecificSolution(IOrganizationService service)
{
var fetchXml =
$@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' no-lock='true'>
<entity name='{Solution.EntityLogicalName}'>
<attribute name='{Solution.PropertyNames.SolutionId}' />
<attribute name='{Solution.PropertyNames.UniqueName}' />
<filter type='and'>
<condition attribute='{Solution.PropertyNames.UniqueName}' operator='eq' value='{SolutionName}'/>
</filter>
</entity>
</fetch>";
var response = service.RetrieveMultiple(new FetchExpression(fetchXml));
return response.Entities.Select(e => e.ToEntity<Solution>());
}
private Publisher RetrievePublisher(IOrganizationService service, string publisher)
{
var fetchXml =
$@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' no-lock='true'>
<entity name='{Publisher.EntityLogicalName}'>
<attribute name='{Publisher.PropertyNames.PublisherId}' />
<attribute name='{Publisher.PropertyNames.UniqueName}' />
<filter type='and'>
<condition attribute='{Publisher.PropertyNames.UniqueName}' operator='eq' value='{publisher}'/>
</filter>
</entity>
</fetch>";
var response = service.RetrieveMultiple(new FetchExpression(fetchXml));