/// <summary>
        /// GetPublishInfoData - Creates a PublishInfoData which is used to obtain a list of publishable items.
        /// </summary>
        /// <param name="parameters">PagePublisherParameters</param>
        /// <returns>GetPublishInfoData object</returns>
        private PublishInstructionData GetPublishInstructionData(PagePublisherParameters parameters)
        {
            ResolveInstructionData resolveInstruction = new ResolveInstructionData();

            resolveInstruction.IncludeChildPublications = parameters.PublishChildren;
            resolveInstruction.IncludeComponentLinks    = parameters.IncludeComponentLinks ? true : false;
            resolveInstruction.StructureResolveOption   = parameters.IncludeStructureGroups ? StructureResolveOption.ItemsAndStructure : StructureResolveOption.OnlyItems;
            resolveInstruction.IncludeWorkflow          = parameters.IncludeWorkflow ? true : false;
            resolveInstruction.Purpose = parameters.Republish ? ResolvePurpose.RePublish : ResolvePurpose.Publish;

            RenderInstructionData renderInstruction = new RenderInstructionData();

            renderInstruction.RenderMode = RenderMode.Publish;


            PublishInstructionData instruction = new PublishInstructionData();

            instruction.RollbackOnFailure             = true;
            instruction.MaximumNumberOfRenderFailures = 1;
            instruction.ResolveInstruction            = resolveInstruction;
            instruction.RenderInstruction             = renderInstruction;
            if (!string.IsNullOrEmpty(parameters.SchedulePublish))
            {
                instruction.StartAt = DateTime.Parse(parameters.SchedulePublish);
            }
            return(instruction);
        }
        public override void Process(ServiceProcess process, object arguments)
        {
            PagePublisherParameters parameters = (PagePublisherParameters)arguments;

            process.SetCompletePercentage(0);
            process.SetStatus("Initializing");

            using (var coreService = Client.GetCoreService())
            {
                _pagePublisherData = new PagePublisherData();
                // get a list of the items from the core service
                ItemsFilterData filter  = GetFilter(parameters);
                XElement        listXml = coreService.GetListXml(parameters.LocationId, filter);

                // Get the page id's that will be published
                string[] pageIds      = GetPageIds(listXml);
                int      batchSize    = 5;
                int      currentBatch = 0;

                // Publish pages
                try
                {
                    double ratio      = pageIds.Count() / batchSize;
                    double percentage = 100 / ratio;
                    double currperc   = 0;
                    while (currentBatch * batchSize < pageIds.Count())
                    {
                        string[] nextBatch = pageIds.Skip(currentBatch * batchSize)
                                             .Take(batchSize).ToArray();
                        coreService.Publish(nextBatch, GetPublishInstructionData(parameters), parameters.TargetUri, parameters.Priority, new ReadOptions());
                        currentBatch++;
                        currperc += percentage;
                        if (currperc >= 1)
                        {
                            process.IncrementCompletePercentage();
                            currperc = 0;
                        }
                    }
                    _pagePublisherData.SuccessMessage = string.Format("{0} Pages published successfully", pageIds.Length.ToString());
                }
                catch (Exception ex)
                {
                    //process.Complete(string.Format("Failed to publish, reason: {0}", ex.Message));
                    _pagePublisherData.FailedMessage = string.Format("Page publishing failed, reason {0}", ex.Message);
                }

                process.Complete("done");
            }
        }
        /// <summary>
        /// GetFilter - Creates a ItemFilter which is used to obtain a list of publishable items.
        /// </summary>
        /// <param name="parameters">PagePublisherParameters</param>
        /// <returns>ItemsFilterData object</returns>
        private ItemsFilterData GetFilter(PagePublisherParameters parameters)
        {
            ItemsFilterData filter = null;

            if (parameters.LocationId.EndsWith("-1")) // is Publication
            {
                filter = new RepositoryItemsFilterData();
            }
            else // is Folder or Structure Group
            {
                filter = new OrganizationalItemItemsFilterData();
            }

            filter.Recursive = parameters.Recursive;
            List <ItemType> itemTypesList = new List <ItemType>();

            itemTypesList.Add(ItemType.Page);
            filter.ItemTypes = itemTypesList.ToArray();
            return(filter);
        }
        public ServiceProcess Execute(string locationId, string[] targetUri, bool recursive, bool republish, int priority, bool publishChildren, bool includeComponentLinks, bool includeStructureGroups, bool includeWorkflow)
        {
            if (string.IsNullOrEmpty(locationId))
            {
                throw new ArgumentNullException("locationId is null");
            }

            PagePublisherParameters arguments = new PagePublisherParameters
            {
                LocationId = locationId,
                TargetUri = targetUri,
                Recursive = recursive,
                Republish = republish,
                Priority = (PublishPriority)priority,
                PublishChildren = publishChildren,
                IncludeComponentLinks = includeComponentLinks,
                IncludeStructureGroups = includeStructureGroups,
                IncludeWorkflow = includeWorkflow
            };
            return ExecuteAsync(arguments);
        }
        public ServiceProcess Execute(string locationId, string[] targetUri, bool recursive, bool republish, int priority, bool publishChildren, bool includeComponentLinks, bool includeStructureGroups, bool includeWorkflow, string schedulePublish)
        {
            if (string.IsNullOrEmpty(locationId))
            {
                throw new ArgumentNullException("locationId is null");
            }

            PagePublisherParameters arguments = new PagePublisherParameters
            {
                LocationId             = locationId,
                TargetUri              = targetUri,
                Recursive              = recursive,
                Republish              = republish,
                Priority               = (PublishPriority)priority,
                PublishChildren        = publishChildren,
                IncludeComponentLinks  = includeComponentLinks,
                IncludeStructureGroups = includeStructureGroups,
                IncludeWorkflow        = includeWorkflow,
                SchedulePublish        = schedulePublish
            };

            return(ExecuteAsync(arguments));
        }
        /// <summary>
        /// GetPublishInfoData - Creates a PublishInfoData which is used to obtain a list of publishable items.
        /// </summary>
        /// <param name="parameters">PagePublisherParameters</param>
        /// <returns>GetPublishInfoData object</returns>
        private PublishInstructionData GetPublishInstructionData(PagePublisherParameters parameters)
        {
            ResolveInstructionData resolveInstruction = new ResolveInstructionData();
            resolveInstruction.IncludeChildPublications = parameters.PublishChildren;
            resolveInstruction.IncludeComponentLinks = parameters.IncludeComponentLinks ? true : false;
            resolveInstruction.StructureResolveOption = parameters.IncludeStructureGroups ? StructureResolveOption.ItemsAndStructure : StructureResolveOption.OnlyItems;
            resolveInstruction.IncludeWorkflow = parameters.IncludeWorkflow ? true : false;
            resolveInstruction.Purpose = parameters.Republish ? ResolvePurpose.RePublish : ResolvePurpose.Publish;

            RenderInstructionData renderInstruction = new RenderInstructionData();
            renderInstruction.RenderMode = RenderMode.PreviewDynamic;

            PublishInstructionData instruction = new PublishInstructionData();
            instruction.RollbackOnFailure = true;
            instruction.MaximumNumberOfRenderFailures = 1;
            instruction.ResolveInstruction = resolveInstruction;
            instruction.RenderInstruction = renderInstruction;
            return instruction;
        }
        /// <summary>
        /// GetFilter - Creates a ItemFilter which is used to obtain a list of publishable items.
        /// </summary>
        /// <param name="parameters">PagePublisherParameters</param>
        /// <returns>ItemsFilterData object</returns>
        private ItemsFilterData GetFilter(PagePublisherParameters parameters)
        {
            ItemsFilterData filter = null;
            if (parameters.LocationId.EndsWith("-1")) // is Publication
            {
                filter = new RepositoryItemsFilterData();
            }
            else // is Folder or Structure Group
            {
                filter = new OrganizationalItemItemsFilterData();
            }

            filter.Recursive = parameters.Recursive;
            List<ItemType> itemTypesList = new List<ItemType>();
            itemTypesList.Add(ItemType.Page);
            filter.ItemTypes = itemTypesList.ToArray();
            return filter;
        }
        /// <summary>
        /// GetPublishInfoData - Creates a PublishInfoData which is used to obtain a list of publishable items.
        /// </summary>
        /// <param name="parameters">PagePublisherParameters</param>
        /// <returns>GetPublishInfoData object</returns>
        private PublishInstructionData GetPublishInstructionData(PagePublisherParameters parameters)
        {
            ResolveInstructionData resolveInstruction = new ResolveInstructionData();
            resolveInstruction.IncludeChildPublications = parameters.PublishChildren;
            resolveInstruction.IncludeComponentLinks = parameters.IncludeComponentLinks ? true : false; 
            resolveInstruction.StructureResolveOption = parameters.IncludeStructureGroups ? StructureResolveOption.ItemsAndStructure : StructureResolveOption.OnlyItems;
            resolveInstruction.IncludeWorkflow = parameters.IncludeWorkflow ? true : false;
            resolveInstruction.Purpose = parameters.Republish ? ResolvePurpose.RePublish : ResolvePurpose.Publish;

            RenderInstructionData renderInstruction = new RenderInstructionData();
            renderInstruction.RenderMode = RenderMode.Publish;
 

            PublishInstructionData instruction = new PublishInstructionData();
            instruction.RollbackOnFailure = true;
            instruction.MaximumNumberOfRenderFailures = 1;
            instruction.ResolveInstruction = resolveInstruction;
            instruction.RenderInstruction = renderInstruction;
            if (!string.IsNullOrEmpty(parameters.SchedulePublish))
            {
                instruction.StartAt = DateTime.Parse(parameters.SchedulePublish);
            }
            return instruction;
        }