예제 #1
0
 public FakeItem WithPublishing(ItemPublishing publishing)
 {
     Item.Publishing.Returns(publishing);
     return(this);
 }
        /// <summary>
        /// This function checks if a schedule item has to be created / updated / deleted
        /// </summary>
        /// <param name="savedItem">the original saved item</param>
        /// <param name="lang">Language of the original saved item</param>
        /// <param name="db">Database where the item is stored</param>
        /// <param name="bPublishAndNotUnpublish">
        /// <b>true</b>: Publish date will be checked <br/>
        /// <b>false</b>: Unpublish date will be checked
        /// </param>
        public void CreateItemPublishSchedule(Item savedItem, Language lang, Database db, bool bPublishAndNotUnpublish)
        {
            const string strFunc = "Sitecore.Modules.AutomatedPublisher.TaskBuilder.CreateItemPublishSchedule";

            if (savedItem == null)
            {
                throw new ArgumentNullException("savedItem",
                                                string.Format(System.Globalization.CultureInfo.CurrentCulture, "Function '{0}' Parameter '{1}' is null.",
                                                              strFunc, "savedItem"));
            }
            if (lang == null)
            {
                throw new ArgumentNullException("lang",
                                                string.Format(System.Globalization.CultureInfo.CurrentCulture, "Function '{0}' Parameter '{1}' is null.",
                                                              strFunc, "lang"));
            }
            if (db == null)
            {
                throw new ArgumentNullException("db",
                                                string.Format(System.Globalization.CultureInfo.CurrentCulture, "Function '{0}' Parameter '{1}' is null.",
                                                              strFunc, "db"));
            }

            string strItemName         = Regex.Replace(savedItem.ID.ToString(), @"[{}]", String.Empty);
            string strScheduleItemName = string.Format(Constants.AutomatedPublisherItemNameFormat, strItemName);

            if (bPublishAndNotUnpublish == false)
            {
                strScheduleItemName = string.Format(Constants.AutomatedUNPublisherItemNameFormat, strItemName);
            }
            string strScheduleItemNameAndPath = string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}/{1}", Constants.SchedulesFolder, strScheduleItemName);

            // search for existing schedules with this item and remove it if it exists
            Item existingScheduleItem = db.GetItem(strScheduleItemNameAndPath, lang);

            //this will not create tasks for items with publishing dates prior to now.
            ItemPublishing ip = savedItem.Publishing;

            if ((ip != null) && (ip.NeverPublish == false) &&
                (((ip.PublishDate > DateTime.UtcNow) && (bPublishAndNotUnpublish == true)) ||
                 ((ip.UnpublishDate > DateTime.UtcNow) && (ip.UnpublishDate != DateTime.MaxValue) && (bPublishAndNotUnpublish == false))))
            {
                DateTime dtSchedulerStartDate = ip.PublishDate;
                if (bPublishAndNotUnpublish == false)
                {
                    dtSchedulerStartDate = ip.UnpublishDate;
                }
                CreateScheduleItem(db, savedItem.ID, existingScheduleItem, strScheduleItemName, dtSchedulerStartDate, lang);
            }
            else if (existingScheduleItem != null)
            {
                using (new SecurityDisabler())
                {
                    try
                    {
                        existingScheduleItem.Delete();
                    }
                    catch (Exception ex)
                    {
                        Log.Error("Error Deleting old scheduler item", ex, this);
                    }
                }
            }
        }