private string GetImageDescription(string imageNumber, Entities.SdkMessageProcessingStepImage image)
        {
            StringBuilder result = new StringBuilder();

            result.AppendFormat("{0}. ", imageNumber);

            if (image.ImageType != null)
            {
                if (image.ImageType.Value == 0)
                {
                    result.Append("PreImage");
                }
                else if (image.ImageType.Value == 1)
                {
                    result.Append("PostImage");
                }
                else if (image.ImageType.Value == 2)
                {
                    result.Append("BothImage");
                }
            }

            if (!string.IsNullOrEmpty(image.EntityAlias))
            {
                result.AppendFormat(",   EntityAlias '{0}'", image.EntityAlias);
            }

            if (!string.IsNullOrEmpty(image.Name))
            {
                result.AppendFormat(",   Name '{0}'", image.EntityAlias);
            }

            result.AppendLine();

            if (!string.IsNullOrEmpty(image.Attributes1))
            {
                result.AppendFormat("Attributes: {0}", image.Attributes1StringsSorted).AppendLine();
            }
            else
            {
                result.AppendLine("Attributes: All");
            }

            return result.ToString();
        }
        public static string GetImageDescription(int?numberImage, Entities.SdkMessageProcessingStepImage image)
        {
            StringBuilder result = new StringBuilder();

            string imageType = "Image";

            if (image.ImageType != null)
            {
                if (image.ImageType.Value == 0)
                {
                    imageType = "PreImage";
                }
                else if (image.ImageType.Value == 1)
                {
                    imageType = "PostImage";
                }
                else if (image.ImageType.Value == 2)
                {
                    imageType = "BothImage";
                }
            }

            if (numberImage.HasValue)
            {
                result.AppendFormat("{0}. ", numberImage);
            }

            result.AppendFormat("{0}", imageType).AppendLine();

            {
                StringBuilder temp = new StringBuilder();

                if (!string.IsNullOrEmpty(image.EntityAlias))
                {
                    if (temp.Length > 0)
                    {
                        temp.Append(",   ");
                    }

                    temp.AppendFormat("EntityAlias '{0}'", image.EntityAlias);
                }

                if (!string.IsNullOrEmpty(image.Name))
                {
                    if (temp.Length > 0)
                    {
                        temp.Append(",   ");
                    }

                    temp.AppendFormat("Name '{0}'", image.EntityAlias);
                }

                if (temp.Length > 0)
                {
                    result.AppendLine(temp.ToString());
                }
            }

            if (!string.IsNullOrEmpty(image.Attributes1))
            {
                result.AppendFormat("Attributes: {0}", image.Attributes1StringsSorted).AppendLine();
            }
            else
            {
                result.AppendLine("Attributes: All");
            }

            //{
            //    string dateStr = GetDateString(image.CreatedBy, image.CreatedOn, image.ModifiedBy, image.ModifiedOn, image.OverwriteTime);

            //    if (!string.IsNullOrEmpty(dateStr))
            //    {
            //        result.AppendLine(dateStr);
            //    }
            //}

            return(result.ToString());
        }
        public void Registre(string assemblyFilePath)
        {
            var code = System.IO.File.ReadAllBytes(assemblyFilePath);

            var assm = System.Reflection.Assembly.Load(code);

            var publickeytoken = PluginRegistrationService.GetPublicKeyTokenFromAssembly(assm);

            //FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assm.Location);
            //var tmp = fvi.FileVersion.Split('.');
            //var version = tmp[0] + "." + tmp[1];

            var file = new System.IO.FileInfo(assemblyFilePath);
            var name = file.Name.Substring(0, file.Name.Length - 4);
            var ns   = name + ".Attributes.";

            var solutionAttributeType = assm.GetType(ns + "SolutionAttribute");
            var stepAttributeType     = assm.GetType(ns + "StepAttribute");


            var pluginType = typeof(Microsoft.Xrm.Sdk.IPlugin);
            var classes    = (from cl in assm.GetTypes() where pluginType.IsAssignableFrom(cl) && !cl.IsAbstract select cl).ToArray();
            var steps      = new List <Model.Step>();

            var plugin = (from pl in uow.PluginAssemblies.GetQuery()
                          where pl.Name == name
                          select pl).SingleOrDefault();

            if (plugin == null)
            {
                plugin = new Kipon.PluginRegistration.Entities.PluginAssembly
                {
                    PluginAssemblyId = Guid.NewGuid(),
                    Content          = System.Convert.ToBase64String(code),
                    Description      = name,
                    IsolationMode    = new Microsoft.Xrm.Sdk.OptionSetValue(2),
                    Name             = name,
                    SourceType       = new Microsoft.Xrm.Sdk.OptionSetValue(0),
                    Culture          = "neutral",
                    PublicKeyToken   = publickeytoken,
                    Version          = "1.0"
                };
                uow.Create(plugin);
            }

            var pluginTypes = (from pl in uow.PluginTypes.GetQuery()
                               where pl.PluginAssemblyId.Id == plugin.PluginAssemblyId
                               select pl).ToArray().ToDictionary(t => t.TypeName);

            var stepindex = (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);

            foreach (var cl in classes)
            {
                var solution  = cl.GetCustomAttributes(solutionAttributeType, true).SingleOrDefault();
                var assmSteps = cl.GetCustomAttributes(stepAttributeType, false).ToArray();

                if (solution != null && assmSteps.Length > 0)
                {
                    foreach (var assStep in assmSteps)
                    {
                        var step = new Model.Step
                        {
                            Class               = cl,
                            EventType           = (Model.CrmEventType)Enum.Parse(typeof(Model.CrmEventType), stepAttributeType.GetProperty("EventType").GetValue(assStep).ToString()),
                            ExecutionOrder      = (int)stepAttributeType.GetProperty("ExecutionOrder").GetValue(assStep),
                            FilteringAttributes = (string[])stepAttributeType.GetProperty("FilteringAttributes").GetValue(assStep),
                            Preimage            = (bool)stepAttributeType.GetProperty("Preimage").GetValue(assStep),
                            PreimageAttributes  = (string[])stepAttributeType.GetProperty("PreimageAttributes").GetValue(assStep),
                            PrimaryEntity       = (string)stepAttributeType.GetProperty("PrimaryEntity").GetValue(assStep),
                            SecondaryEntity     = (string)stepAttributeType.GetProperty("SecondaryEntity").GetValue(assStep),
                            Solution            = new Model.Solution {
                                Name = (string)solutionAttributeType.GetProperty("Name").GetValue(solution)
                            },
                            Stage   = (Model.StageEnum)Enum.Parse(typeof(Model.StageEnum), stepAttributeType.GetProperty("Stage").GetValue(assStep).ToString()),
                            Offline = (bool)stepAttributeType.GetProperty("Offline").GetValue(assStep)
                        };

                        if (step.ExecutionOrder == 0)
                        {
                            step.ExecutionOrder = 1;
                        }
                        steps.Add(step);
                    }
                }
                else
                {
                    Console.WriteLine(cl.FullName + " is missing step or solution");
                }
            }

            #region find steps no longer in the solution and delete them
            {
                var dubs = (from s in steps
                            group s by s.UniqueName into grp
                            select new
                {
                    Name = grp.Key,
                    Count = grp.Count()
                }).Where(r => r.Count > 1).ToArray();
                if (dubs.Length > 0)
                {
                    foreach (var dub in dubs)
                    {
                        Console.WriteLine("Dublicate workflow: " + dub.Name);
                    }
                    return;
                }

                var newstepindex = steps.ToDictionary(s => s.UniqueName);
                var gones        = (from s in stepindex.Values where !newstepindex.ContainsKey(s.UniqueName) select s).ToArray();
                foreach (var gon in gones)
                {
                    Console.WriteLine("Delete step: " + gon.Name);
                    uow.Delete(gon);
                    stepindex.Remove(gon.UniqueName);
                }
            }
            #endregion

            #region remove plugin types no longer represented in the assembly
            {
                var newtypeindex = (from s in steps select s.Class.FullName).Distinct().ToArray();
                var gones        = (from p in pluginTypes.Values where !newtypeindex.Contains(p.TypeName) select p).ToArray();
                foreach (var gon in gones)
                {
                    Console.WriteLine("Delete plugin: " + gon.TypeName);
                    uow.Delete(gon);
                    pluginTypes.Remove(gon.TypeName);
                }
            }
            #endregion

            #region upload new assembly
            {
                var clean = uow.PluginAssemblies.Clean(plugin);
                clean.Content = System.Convert.ToBase64String(code);
                uow.Update(clean);
                Console.WriteLine("Assembly code updated");
            }
            #endregion

            #region find missing plugin types a registre these
            {
                var missings = (from s in steps where !pluginTypes.ContainsKey(s.Class.FullName) select s.Class.FullName).Distinct();
                foreach (var missing in missings)
                {
                    var type = new Entities.PluginType
                    {
                        PluginTypeId     = Guid.NewGuid(),
                        TypeName         = missing,
                        FriendlyName     = Guid.NewGuid().ToString().ToLower(),
                        Name             = missing,
                        PluginAssemblyId = plugin.ToEntityReference(),
                    };
                    uow.Create(type);
                    pluginTypes.Add(missing, type);
                    Console.WriteLine("Added plugin type " + type.TypeName);
                }
            }
            #endregion

            #region fetch sdk messages
            var sdkMessageIndex = (from s in uow.SdkMessages.GetQuery() select s).ToArray().ToDictionary(s => s.Name);
            #endregion

            #region find missing steps and registre these
            {
                var missings = (from s in steps where !stepindex.ContainsKey(s.UniqueName) select s).ToArray();
                foreach (var missing in missings)
                {
                    var SdkMessage = sdkMessageIndex[missing.EventType.ToString()];

                    Kipon.PluginRegistration.Entities.SdkMessageFilter filter = null;

                    if (string.IsNullOrEmpty(missing.SecondaryEntity))
                    {
                        filter = (from f in uow.SdkMessageFilters.GetQuery()
                                  where f.SdkMessageId.Id == SdkMessage.SdkMessageId &&
                                  f.PrimaryObjectTypeCode == missing.PrimaryEntity
                                  select f).SingleOrDefault();
                    }
                    else
                    {
                        filter = (from f in uow.SdkMessageFilters.GetQuery()
                                  where f.SdkMessageId.Id == SdkMessage.SdkMessageId &&
                                  f.PrimaryObjectTypeCode == missing.PrimaryEntity &&
                                  f.SecondaryObjectTypeCode == missing.SecondaryEntity
                                  select f).SingleOrDefault();
                    }

                    var deployment = 0;
                    if (missing.Offline)
                    {
                        deployment = 2;
                    }

                    var step = new Entities.SdkMessageProcessingStep
                    {
                        SdkMessageProcessingStepId = Guid.NewGuid(),
                        Name  = missing.Name,
                        Mode  = new Microsoft.Xrm.Sdk.OptionSetValue(missing.Async),
                        Rank  = missing.ExecutionOrder,
                        Stage = new Microsoft.Xrm.Sdk.OptionSetValue(missing.StageValue),
                        SupportedDeployment = new Microsoft.Xrm.Sdk.OptionSetValue(deployment),
                        EventHandler        = pluginTypes[missing.Class.FullName].ToEntityReference(),
                        SdkMessageId        = sdkMessageIndex[missing.EventType.ToString()].ToEntityReference(),
                        SdkMessageFilterId  = filter != null?filter.ToEntityReference() : null,
                    };

                    if (missing.Stage == Model.StageEnum.PostOperationAsyncWithDelete)
                    {
                        step.AsyncAutoDelete = true;
                    }


                    uow.Create(step);
                    stepindex.Add(step.UniqueName, step);
                    Console.WriteLine("Added step " + step.Name);
                }
                {
                    var edits = (from s in steps where stepindex.ContainsKey(s.UniqueName) select s).ToArray();
                    foreach (var edit in edits)
                    {
                        var step       = stepindex[edit.UniqueName];
                        var deployment = 0;

                        if (edit.Offline)
                        {
                            deployment = 2;
                        }

                        if (step.SupportedDeployment.Value != deployment)
                        {
                            var clean = uow.SdkMessageProcessingSteps.Clean(step);
                            clean.SupportedDeployment = new Microsoft.Xrm.Sdk.OptionSetValue(deployment);
                            uow.Update(clean);
                            Console.WriteLine("Changed supported deployment for " + edit.Name + " > " + deployment);
                        }

                        if (edit.Stage == Model.StageEnum.PostOperationAsyncWithDelete && !(step.AsyncAutoDelete ?? false))
                        {
                            var clean = uow.SdkMessageProcessingSteps.Clean(step);
                            clean.AsyncAutoDelete = true;
                            uow.Update(clean);
                            Console.WriteLine("Changed async delete policy deployment for " + edit.Name + " > " + deployment);
                        }

                        if (edit.Stage == Model.StageEnum.PostOperationAsyncWithoutDelete && (step.AsyncAutoDelete ?? false))
                        {
                            var clean = uow.SdkMessageProcessingSteps.Clean(step);
                            clean.AsyncAutoDelete = false;
                            uow.Update(clean);
                            Console.WriteLine("Changed async deployment for " + edit.Name + " > " + deployment);
                        }
                    }
                }
            }
            #endregion

            #region fetch images
            var images = (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 &&
                          im.Name == "preimage"
                          select im).ToArray();
            {
                var needImages = (from s in steps where s.Preimage == true select s).ToArray();
                foreach (var needImage in needImages)
                {
                    var xrmStep = stepindex[needImage.UniqueName];
                    var image   = (from im in images where im.SdkMessageProcessingStepId.Id == xrmStep.SdkMessageProcessingStepId.Value && im.Name == "preimage" select im).SingleOrDefault();
                    if (image == null)
                    {
                        image = new Entities.SdkMessageProcessingStepImage
                        {
                            SdkMessageProcessingStepImageId = Guid.NewGuid(),
                            SdkMessageProcessingStepId      = xrmStep.ToEntityReference(),
                            Name                = "preimage",
                            EntityAlias         = "preimage",
                            MessagePropertyName = needImage.MessagePropertyName,
                            ImageType           = new Microsoft.Xrm.Sdk.OptionSetValue(0),
                            Description         = "preimage",
                            Relevant            = true,
                            Attributes1         = needImage.PreimageAttributes != null && needImage.PreimageAttributes.Length > 0 ? string.Join(",", needImage.PreimageAttributes) : null
                        };
                        uow.Create(image);
                        Console.WriteLine("Pre image created " + needImage.Name);
                    }
                    else
                    {
                        var clean = uow.SdkMessageProcessingStepImages.Clean(image);

                        string preAtr = needImage.PreimageAttributes == null || needImage.PreimageAttributes.Length == 0 ? null : string.Join(",", needImage.PreimageAttributes);

                        if (preAtr != image.Attributes1)
                        {
                            clean.Attributes1 = preAtr;
                            uow.Update(clean);
                            Console.WriteLine("Pre image updated " + needImage.Name + " :" + preAtr);
                        }
                        image.Relevant = true;
                    }
                }
                var notNeededs = (from im in images where im.Relevant == false select im).ToArray();
                foreach (var notNeeded in notNeededs)
                {
                    uow.Delete(notNeeded);
                    Console.WriteLine("Preimage deleted for " + notNeeded.Name);
                }
            }
            #endregion
        }
Beispiel #4
0
        public void Import(string filename)
        {
            var ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Models.PluginAssembly));

            using (var file = new System.IO.FileStream(filename, System.IO.FileMode.Open))
            {
                var importModel = (Models.PluginAssembly)ser.ReadObject(file);
                var crmModel    = this.Get(importModel.Name);

                var sdkmessageIndex = (from s in uow.SdkMessages.GetQuery()
                                       select s).ToDictionary(r => r.Name);

                foreach (var type in importModel.PluginTypes)
                {
                    var crmType = crmModel.PluginTypes.Where(r => r.Name == type.Name).SingleOrDefault();
                    if (crmType != null)
                    {
                        foreach (var step in type.PluginSteps)
                        {
                            var crmStep = (from s in crmType.PluginSteps
                                           where s.Stage == step.Stage &&
                                           s.Mode == step.Mode &&
                                           s.Message == step.Message
                                           select s).SingleOrDefault();

                            if (crmStep == null)
                            {
                                var trans = new Services.CrmTransaction();

                                var newStep = new Entities.SdkMessageProcessingStep
                                {
                                    SdkMessageProcessingStepId = step.Id,
                                    Name  = step.Name,
                                    Mode  = new Microsoft.Xrm.Sdk.OptionSetValue(step.Mode),
                                    Rank  = step.Rank,
                                    Stage = new Microsoft.Xrm.Sdk.OptionSetValue(step.Stage),
                                    SupportedDeployment = new Microsoft.Xrm.Sdk.OptionSetValue(0),
                                    EventHandler        = new Microsoft.Xrm.Sdk.EntityReference(Entities.PluginType.EntityLogicalName, crmType.Id),
                                    SdkMessageId        = sdkmessageIndex[step.Message].ToEntityReference(),
                                    FilteringAttributes = step.FilteringAttributes
                                };

                                if (step.PrimaryEntityLogicalName != null)
                                {
                                    newStep.SdkMessageFilterId = this.stepService.GetFilterFor(sdkmessageIndex[step.Message], step.PrimaryEntityLogicalName);
                                }

                                if (newStep.Mode.Value == 1)
                                {
                                    newStep.AsyncAutoDelete = true;
                                }


                                trans.Create(newStep);

                                if (step.Images != null && step.Images.Length > 0)
                                {
                                    foreach (var image in step.Images)
                                    {
                                        var newImage = new Entities.SdkMessageProcessingStepImage
                                        {
                                            SdkMessageProcessingStepImageId = image.Id,
                                            SdkMessageProcessingStepId      = new Microsoft.Xrm.Sdk.EntityReference(Entities.SdkMessageProcessingStep.EntityLogicalName, newStep.SdkMessageProcessingStepId.Value),
                                            Name                = image.Name,
                                            EntityAlias         = image.EntityAlias,
                                            Description         = image.Description,
                                            ImageType           = new Microsoft.Xrm.Sdk.OptionSetValue(image.ImageType),
                                            MessagePropertyName = image.MessagePropertyName,
                                        };

                                        if (!string.IsNullOrEmpty(image.Attributes1))
                                        {
                                            newImage.Attributes1 = image.Attributes1;
                                        }
                                        trans.Create(newImage);
                                    }
                                }

                                trans.Commit(this.uow);
                                Console.WriteLine($"Step: { step.Name } for type: {crmType.Name} was created");
                            }
                            else
                            {
                                Console.WriteLine($"Step: { step.Name } for Type: { crmType.Name } already exists");
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine($"Type: { type.Name } does not exists in target environment");
                    }
                }
            }
        }