private void ValidateJobParams(CustomJob postImage)
        {
            postImage.Require(nameof(postImage));

            if (postImage.ActionName != null && postImage.Workflow != null && postImage.URL.IsFilled())
            {
                throw new InvalidPluginExecutionException("Either an action or workflow or URL can be specified.");
            }

            if (string.IsNullOrEmpty(postImage.ActionName) && postImage.Workflow == null && postImage.URL.IsEmpty())
            {
                throw new InvalidPluginExecutionException("An action or workflow or URL must be specified.");
            }

            if (postImage.TargetID != null && postImage.TargetXML != null)
            {
                throw new InvalidPluginExecutionException("Either a target ID or XML can be specified.");
            }

            if ((postImage.TargetID != null || postImage.TargetXML != null) &&
                string.IsNullOrEmpty(postImage.TargetLogicalName))
            {
                throw new InvalidPluginExecutionException("Target logical name must be specified.");
            }
        }
        private void ProcessQueuedJob(CustomJob postImage)
        {
            postImage.Require(nameof(postImage));

            var isRetry = Context.PreEntityImages.FirstOrDefault().Value?.ToEntity <CustomJob>().StatusReason
                          == CustomJob.StatusReasonEnum.Retry;

            if (isRetry)
            {
                Log.Log("Retrying ...");
            }

            Log.Log("Setting job to 'running' ...");
            Service.Update(
                new CustomJob
            {
                Id           = postImage.Id,
                StatusReason = CustomJob.StatusReasonEnum.Running
            });
            Log.Log("Set job to 'running'.");

            var run = JobRunFactory.GetJobRun(postImage, isRetry, Service, ServiceFactory, Log);

            run.Process();
        }
        private string BuildJobName(CustomJob customJob)
        {
            customJob.Require(nameof(customJob));

            CustomJob preImage = null;

            if (Context.MessageName == "Update")
            {
                preImage = Context.PreEntityImages?.FirstOrDefault().Value?.ToEntity <CustomJob>();

                if (preImage == null)
                {
                    throw new InvalidPluginExecutionException("A full pre image must be registered on this plugin step.");
                }
            }

            if (Context.MessageName == "Create" && !string.IsNullOrEmpty(customJob.Name))
            {
                Log.Log("'Create' message and name is filled; using the custom name.");
                return(customJob.Name);
            }

            var customJobTemp =
                new CustomJob
            {
                Id         = customJob.Id,
                ActionName = customJob.ActionName,
                Workflow   = customJob.Workflow
            };

            Log.Log("Loading lookup labels ...");
            customJobTemp.LoadLookupLabels(Service);
            Log.Log("Loaded lookup labels.");

            var label   = customJobTemp.WorkflowLabels?.FirstOrDefault(p => p.Key == 1033).Value;
            var newName = $"{customJob.TargetLogicalName}" +
                          " (" + (string.IsNullOrEmpty(customJob.TargetID)
                                        ? (string.IsNullOrEmpty(customJob.TargetXML) ? "no target" : "multi-target")
                                        : customJob.TargetID) + ")" +
                          $"{(string.IsNullOrEmpty(customJobTemp.ActionName) ? "" : " : " + customJobTemp.ActionName)}" +
                          $"{(string.IsNullOrEmpty(label) ? "" : " : " + label)}";

            Log.Log($"Assumed new name: {newName}.");

            if (Context.MessageName == "Update" && preImage != null)
            {
                Log.Log($"Updating message; comparing updated name.");
                customJobTemp =
                    new CustomJob
                {
                    Id         = customJob.Id,
                    ActionName = preImage.ActionName,
                    Workflow   = preImage.Workflow
                };

                Log.Log("Loading lookup labels of pre-image ...");
                customJobTemp.LoadLookupLabels(Service);
                Log.Log("Loaded lookup labels of pre-image.");

                var preLabel = customJobTemp.WorkflowLabels?.FirstOrDefault(p => p.Key == 1033).Value;
                var preName  = $"{preImage.TargetLogicalName}" +
                               " (" + (string.IsNullOrEmpty(preImage.TargetID)
                                                ? (string.IsNullOrEmpty(preImage.TargetXML) ? "no target" : "multi-target")
                                                : preImage.TargetID) + ")" +
                               $"{(string.IsNullOrEmpty(customJobTemp.ActionName) ? "" : " : " + customJobTemp.ActionName)}" +
                               $"{(string.IsNullOrEmpty(preLabel) ? "" : " : " + preLabel)}";
                Log.Log($"Pre-image name: {preName}.");

                var existingName = customJob.Name;
                newName = (string.IsNullOrEmpty(existingName) || preName == existingName) ? newName : existingName;
                Log.Log($"Final new name: {newName}.");
            }

            return(newName.Trim(' ').Trim(':').Trim(' '));
        }