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); } }
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; } } } }