Example #1
0
        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);
            }
        }