private void Update(Models.Plugin plugin, Models.Step step, Entities.SdkMessageProcessingStep crmStep)
        {
            var updated = false;
            var clean   = new Entities.SdkMessageProcessingStep
            {
                SdkMessageProcessingStepId = crmStep.SdkMessageProcessingStepId
            };

            if (crmStep.FilteringAttributes != null && step.FilteringAttributesString == null)
            {
                clean.FilteringAttributes = null;
                updated = true;
            }

            if (step.FilteringAttributesString != null && step.FilteringAttributesString != crmStep.FilteringAttributes)
            {
                clean.FilteringAttributes = step.FilteringAttributesString;
                updated = true;
            }

            if (updated)
            {
                uow.Update(clean);
                this.messageService.Inform($"Updated step {crmStep.Name} on {crmStep.LogicalName}.");
            }

            this.UpdateImage(crmStep, 1, step.Stage, step.IsAsync, step.Message, step.PreImage);
            this.UpdateImage(crmStep, 2, step.Stage, step.IsAsync, step.Message, step.PostImage);
        }
        public Plugin[] ForAssembly(Assembly assembly)
        {
            this.types = Kipon.Tools.Xrm.Reflection.Types.Instance;
            types.SetAssembly(assembly);
            this.pluginMethodCache = new Kipon.Tools.Xrm.Reflection.PluginMethod.Cache(assembly);

            var plugins = assembly.GetTypes().Where(r => r.BaseType == types.BasePlugin).ToArray();

            this.messageService.Inform($"Kipon.Xrm.Tools, plugin deployment found {plugins.Length} plugins in {assembly.FullName}.");

            List <Plugin> result = new List <Plugin>();

            this.entityLogicalNames = assembly.GetTypes().
                                      Where(r => r.BaseType == typeof(Microsoft.Xrm.Sdk.Entity))
                                      .Select(r => ((Microsoft.Xrm.Sdk.Entity)System.Activator.CreateInstance(r)).LogicalName)
                                      .Distinct()
                                      .ToArray();

            this.messageService.Inform($"Found {this.entityLogicalNames.Length} entities.");

            foreach (var pluginType in plugins)
            {
                var typeName = pluginType.FullName;
                var next     = new Models.Plugin(pluginType);
                result.Add(next);
                foreach (var stage in this.stages)
                {
                    foreach (var message in this.messages)
                    {
                        string[] handleEntities = this.entityLogicalNames;

                        if (Kipon.Tools.Xrm.Reflection.Types.MESSAGE_WITHOUT_PRIMARY_ENTITY.Contains(message))
                        {
                            handleEntities = new string[] { null };
                        }

                        foreach (var logicalname in handleEntities)
                        {
                            var methods = this.pluginMethodCache.ForPlugin(pluginType, stage, message, logicalname, false, false);
                            if (methods != null && methods.Length > 0)
                            {
                                next.AddStep(stage, message, logicalname, false, methods);
                            }
                            if (stage == 40)
                            {
                                methods = this.pluginMethodCache.ForPlugin(pluginType, stage, message, logicalname, true, false);
                                if (methods != null)
                                {
                                    next.AddStep(stage, message, logicalname, true, methods);
                                }
                            }
                        }
                    }
                }
            }
            return(result.ToArray());
        }
        public Models.Responses.Control GetPlugin(Models.Plugin Plugin)
        {
            try
            {
                // Get Plugin Type
                Manager.ControlType plugintype = this.Session.Database.Server.ControlType(Plugin.Name);

                // Get Plugin Control
                ViewModel.Control plugincontrol = this.Session.Plugin(plugintype, Plugin.Context);

                return(new Models.Responses.Control(this.Session, plugincontrol));
            }
            catch (Exception e)
            {
                throw this.ProcessException(e);
            }
        }
        private Entities.SdkMessageProcessingStep Create(Models.Plugin plugin, Models.Step step, string name)
        {
            var next = new Entities.SdkMessageProcessingStep
            {
                SdkMessageProcessingStepId = Guid.NewGuid(),
                Name  = name,
                Mode  = step.IsAsync ? new Microsoft.Xrm.Sdk.OptionSetValue(1) : new Microsoft.Xrm.Sdk.OptionSetValue(0),
                Rank  = step.ExecutionOrder <= 0 ? 1 : step.ExecutionOrder,
                Stage = new Microsoft.Xrm.Sdk.OptionSetValue(step.Stage),
                SupportedDeployment = new Microsoft.Xrm.Sdk.OptionSetValue(0),
                EventHandler        = new Microsoft.Xrm.Sdk.EntityReference(Entities.PluginType.EntityLogicalName, plugin.CurrentCrmInstance.PluginTypeId.Value),
                SdkMessageId        = this.GetSdkMessage(step.Message).ToEntityReference(),
                SdkMessageFilterId  = this.GetFilterFor(this.GetSdkMessage(step.Message), step.PrimaryEntityLogicalName)
            };

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

            if (step.FilteringAttributes != null && step.FilteringAttributes.Length > 0)
            {
                next.FilteringAttributes = string.Join(",", step.FilteringAttributes);
            }

            uow.Create(next);
            this.messageService.Inform($"Created step: {next.Name}");

            if (step.PreImage != null)
            {
                CreateImage(next, 1, step.Stage, step.IsAsync, step.Message, step.PreImage);
            }

            if (step.PostImage != null)
            {
                CreateImage(next, 2, step.Stage, step.IsAsync, step.Message, step.PostImage);
            }
            return(next);
        }
Beispiel #5
0
        public Plugin[] ForAssembly(Assembly assembly)
        {
            var customActions = (from sm in uow.SdkMessages.GetQuery()
                                 join wf in uow.Workflows.GetQuery() on sm.SdkMessageId equals wf.SdkMessageId.Id
                                 select sm.Name).Distinct().ToArray();

            uow.ClearContext();

            var unbounds = (from sm in uow.SdkMessages.GetQuery()
                            join wf in uow.Workflows.GetQuery() on sm.SdkMessageId equals wf.SdkMessageId.Id
                            where wf.PrimaryEntity == "none" &&
                            sm.Name != null &&
                            sm.Name != ""
                            select sm.Name).Distinct().ToArray().OrderBy(r => r).ToArray();

            uow.ClearContext();

            this.entityLogicalNames = assembly.GetTypes().
                                      Where(r => r.BaseType == typeof(Microsoft.Xrm.Sdk.Entity))
                                      .Select(r => ((Microsoft.Xrm.Sdk.Entity)System.Activator.CreateInstance(r)).LogicalName)
                                      .Distinct()
                                      .ToArray();

            this.messageService.Inform($"Found {this.entityLogicalNames.Length} entities.");


            var x = this.entityLogicalNames;

            var tmp = (from sm in uow.SdkMessages.GetQuery()
                       join wf in uow.Workflows.GetQuery() on sm.SdkMessageId equals wf.SdkMessageId.Id
                       where wf.PrimaryEntity != "none" &&
                       sm.Name != null &&
                       sm.Name != ""
                       select new
            {
                Name = sm.Name,
                PrimaryEntity = wf.PrimaryEntity
            }).Distinct()
                      .ToArray();

            var bounds = tmp.Where(r => this.entityLogicalNames != null && this.entityLogicalNames.Length > 0 && this.entityLogicalNames.Contains(r.PrimaryEntity))
                         .ToDictionary(r => r.Name, v => v.PrimaryEntity);

            bounds["QualifyLead"] = "lead";

            uow.ClearContext();

            var allActions = new List <string>(messages);

            allActions.AddRange(customActions);

            messages = allActions.ToArray();

            this.types = Kipon.Tools.Xrm.Reflection.Types.Instance;
            types.SetAssembly(assembly);
            this.pluginMethodCache = new Kipon.Tools.Xrm.Reflection.PluginMethod.Cache(assembly);

            var plugins  = assembly.GetTypes().Where(r => r.BaseType == types.BasePlugin).ToArray();
            var vPlugins = assembly.GetTypes().Where(r => r.BaseType == types.VirtualEntityPlugin).ToArray();

            this.messageService.Inform($"Kipon.Xrm.Tools, plugin deployment found {plugins.Length} plugins, virtual entityplugins: {vPlugins.Length} in {assembly.FullName}");

            List <Plugin> result = new List <Plugin>();

            if (vPlugins != null && vPlugins.Length > 0)
            {
                foreach (var s in vPlugins)
                {
                    var next = new Models.Plugin(s, true);
                    result.Add(next);
                }
            }

            foreach (var pluginType in plugins)
            {
                var typeName = pluginType.FullName;
                var next     = new Models.Plugin(pluginType);
                result.Add(next);
                foreach (var stage in this.stages)
                {
                    foreach (var message in this.messages)
                    {
                        string[] handleEntities = this.entityLogicalNames;

                        if (Kipon.Tools.Xrm.Reflection.Types.MESSAGE_WITHOUT_PRIMARY_ENTITY.Contains(message))
                        {
                            handleEntities = new string[] { null };
                        }
                        else
                        {
                            if (unbounds.Contains(message))
                            {
                                handleEntities = new string[] { null };
                            }
                        }

                        if (bounds.ContainsKey(message))
                        {
                            handleEntities = new string[] { bounds[message] };
                        }

                        foreach (var logicalname in handleEntities)
                        {
                            var methods = this.pluginMethodCache.ForPlugin(pluginType, stage, message, logicalname, false, false);
                            if (methods != null && methods.Length > 0)
                            {
                                next.AddStep(stage, message, logicalname, false, methods);
                            }
                            if (stage == 40)
                            {
                                methods = this.pluginMethodCache.ForPlugin(pluginType, stage, message, logicalname, true, false);
                                if (methods != null)
                                {
                                    next.AddStep(stage, message, logicalname, true, methods);
                                }
                            }
                        }
                    }
                }
            }
            return(result.ToArray());
        }