internal static PluginStep GetObject(
            Entities.SdkMessageProcessingStep entStep
            , Entities.SdkMessage entMessage
            , Entities.SdkMessageFilter entFilter
            , Entities.SdkMessageProcessingStepSecureConfig entSecure
            )
        {
            var result = new PluginStep();

            result.Id = entStep.Id;

            result.Message = entMessage.Name;

            result.Name = entStep.Name;

            result.Description = entStep.Description;

            if (entFilter != null)
            {
                result.PrimaryEntity   = entFilter.PrimaryObjectTypeCode;
                result.SecondaryEntity = entFilter.SecondaryObjectTypeCode;
            }
            else
            {
                result.PrimaryEntity   = "none";
                result.SecondaryEntity = "none";
            }

            result.StateCode  = entStep.StateCode.Value;
            result.StatusCode = entStep.StatusCode.Value;

            result.StateCodeName  = entStep.FormattedValues[Entities.SdkMessageProcessingStep.Schema.Attributes.statecode];
            result.StatusCodeName = entStep.FormattedValues[Entities.SdkMessageProcessingStep.Schema.Attributes.statuscode];

            result.Stage     = entStep.Stage.Value;
            result.StageName = entStep.FormattedValues[Entities.SdkMessageProcessingStep.Schema.Attributes.stage];

            result.ExecutionMode     = entStep.Mode.Value;
            result.ExecutionModeName = entStep.FormattedValues[Entities.SdkMessageProcessingStep.Schema.Attributes.mode];

            result.AsyncAutoDelete = entStep.AsyncAutoDelete;

            result.ExecutionOrder = entStep.Rank;

            var user = entStep.ImpersonatingUserId;

            if (user != null)
            {
                result.RunInUserContext = user.Name;
            }
            else
            {
                result.RunInUserContext = "Calling User";
            }

            result.SupportedDeployment     = entStep.FormattedValues[Entities.SdkMessageProcessingStep.Schema.Attributes.supporteddeployment];
            result.SupportedDeploymentCode = entStep.SupportedDeployment.Value;

            result.InvocationSource = entStep.FormattedValues[Entities.SdkMessageProcessingStep.Schema.Attributes.invocationsource];
#pragma warning disable CS0612 // 'SdkMessageProcessingStep.InvocationSource' is obsolete
            result.InvocationSourceCode = entStep.InvocationSource?.Value;
#pragma warning restore CS0612 // 'SdkMessageProcessingStep.InvocationSource' is obsolete

            result.CreatedBy = entStep.CreatedBy.Name;
            result.CreatedOn = entStep.CreatedOn;

            result.ModifiedBy = entStep.ModifiedBy.Name;
            result.ModifiedOn = entStep.ModifiedOn;

            result.ComponentState     = entStep.FormattedValues[Entities.SdkMessageProcessingStep.Schema.Attributes.componentstate];
            result.ComponentStateCode = entStep.ComponentState.Value;

            result.UnsecureConfiguration = entStep.Configuration;

            if (entSecure != null)
            {
                result.SecureConfiguration = entSecure.SecureConfig;
            }

            result.FilteringAttributes.AddRange(entStep.FilteringAttributesStrings);

            return(result);
        }
        private async Task <Model.Backup.PluginDescription> GetPluginDescription(IOrganizationServiceExtented service)
        {
            var repositoryAssembly = new PluginAssemblyRepository(service);
            var repositoryType     = new PluginTypeRepository(service);

            var repositoryMessage = new SdkMessageRepository(service);
            var repositoryFilter  = new SdkMessageFilterRepository(service);
            var repositorySecure  = new SdkMessageProcessingStepSecureConfigRepository(service);
            var repositoryImage   = new SdkMessageProcessingStepImageRepository(service);

            var repositoryStep = new SdkMessageProcessingStepRepository(service);

            var result = new Model.Backup.PluginDescription();

            result.CreatedOn = DateTime.Now;

            var listAssemblies = await repositoryAssembly.GetAllPluginAssemblisWithStepsAsync();

            var listMessage = await repositoryMessage.GetAllSdkMessageWithStepsAsync();

            var listFilter = await repositoryFilter.GetAllSdkMessageFilterWithStepsAsync();

            var listSecure = await repositorySecure.GetAllSdkMessageProcessingStepSecureConfigAsync();

            foreach (var entAssembly in listAssemblies)
            {
                var assembly = Model.Backup.PluginAssembly.GetObject(entAssembly);

                result.PluginAssemblies.Add(assembly);

                var listTypes = await repositoryType.GetPluginTypesAsync(entAssembly.Id);

                foreach (var entPluginType in listTypes)
                {
                    var pluginType = Model.Backup.PluginType.GetObject(entPluginType);

                    assembly.PluginTypes.Add(pluginType);

                    var listSteps = await repositoryStep.GetPluginStepsByPluginTypeIdAsync(entPluginType.Id);

                    var listStepsToAdd = new List <Model.Backup.PluginStep>();

                    foreach (var entStep in listSteps)
                    {
                        Entities.SdkMessage       entMessage = null;
                        Entities.SdkMessageFilter entFilter  = null;
                        Entities.SdkMessageProcessingStepSecureConfig entSecure = null;

                        var refMessage = entStep.SdkMessageId;
                        if (refMessage != null)
                        {
                            entMessage = listMessage.FirstOrDefault(m => m.SdkMessageId == refMessage.Id);
                        }

                        var refFilter = entStep.SdkMessageFilterId;
                        if (refFilter != null)
                        {
                            entFilter = listFilter.FirstOrDefault(f => f.SdkMessageFilterId == refFilter.Id);
                        }

                        var refSecure = entStep.SdkMessageProcessingStepSecureConfigId;
                        if (refSecure != null)
                        {
                            entSecure = listSecure.FirstOrDefault(s => s.SdkMessageProcessingStepSecureConfigId == refSecure.Id);
                        }

                        var step = Model.Backup.PluginStep.GetObject(entStep, entMessage, entFilter, entSecure);

                        listStepsToAdd.Add(step);

                        var listImages = await repositoryImage.GetStepImagesAsync(entStep.Id);

                        foreach (var entImage in listImages)
                        {
                            var image = Model.Backup.PluginImage.GetObject(entImage);

                            step.PluginImages.Add(image);
                        }
                    }

                    pluginType.PluginSteps.AddRange(
                        listStepsToAdd
                        .OrderBy(step => step.PrimaryEntity)
                        .ThenBy(step => step.SecondaryEntity)
                        .ThenBy(step => step.Message, MessageComparer.Comparer)
                        .ThenBy(step => step.Stage)
                        .ThenBy(step => step.ExecutionOrder)
                        .ThenBy(step => step.Name)
                        .ThenBy(step => step.CreatedOn)
                        );
                }
            }

            return(result);
        }