如何停止Sitecore中引用项目的归档(计划)?

时间:2013-01-25 22:23:31

标签: sitecore sitecore6

当我手动存档其他项目引用的项目时,Sitecore弹出对话框包含操作 - 如何处理链接。

如果项目配置为使用“设置存档日期”自动存档并且存档似乎Sitecore默认选择“离开链接”操作,则所有指向已存档项目的链接都将被破坏。

如何/我可以在哪里连接以停止归档其他项目引用的项目(预定归档)?我想停止存档,并建立一种存档不成功的关系。

1 个答案:

答案 0 :(得分:3)

为了防止Sitecore归档链接的项目,您需要覆盖2个类。 首先是 ArchiveItem ,以便在归档之前检查项目是否已链接:

namespace My.Assembly.And.Namespace
{
    public class MyArchiveItem : Sitecore.Tasks.ArchiveItem
    {
        public MyArchiveItem(System.DateTime taskDate) : base(taskDate)
        {
        }

        public override void Execute()
        {
            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                lock (SyncRoot)
                {
                    Sitecore.Data.Items.Item item = GetItem();
                    if (item != null && HasLink(Sitecore.Globals.LinkDatabase, item))
                    {
                        Sitecore.Diagnostics.Log.Error(string.Format(
                            "Item {0} or one of its descendants are linked from other items. "
                            + "Remove link before scheduling archive.", item.Paths.FullPath), this);
                        // uncomment next line if you don't want to retry archiving attempt
                        //Sitecore.Globals.TaskDatabase.Remove(this);
                        return;
                    }
                }
            }
            base.Execute();
        }

        private static bool HasLink(Sitecore.Links.LinkDatabase linkDatabase, Sitecore.Data.Items.Item item)
        {
            Sitecore.Links.ItemLink[] referrers = linkDatabase.GetReferrers(item);
            if (referrers.Length > 0)
            {
                if (referrers.Any(link => link.SourceFieldID != Sitecore.FieldIDs.Source))
                {
                    return true;
                }
            }
            foreach (Sitecore.Data.Items.Item item2 in item.Children)
            {
                if (HasLink(linkDatabase, item2))
                {
                    return true;
                }
            }
            return false;
        }
    }
}

您需要覆盖的第二个类是 SqlServerTaskDatabase ,因此它会调度覆盖 MyArchiveItem 任务而不是原始Sitecore ArchiveItem

namespace My.Assembly.And.Namespace
{
    public class MySqlServerTaskDatabase : Sitecore.Data.SqlServer.SqlServerTaskDatabase
    {
        public MySqlServerTaskDatabase(string connectionString) : base(connectionString)
        {
        }

        public override void UpdateItemTask(Sitecore.Tasks.Task task, bool insertIfNotFound)
        {
            Sitecore.Data.Sql.SqlBatch batch = new Sitecore.Data.Sql.SqlBatch(true);
            BindTaskData(task, batch);
            string sql = GetUpdateSql() + 
                " WHERE [ItemID] = @itemID AND [Database] = @databaseName AND [taskType] = @taskType";
            batch.AddSql(sql);
            if (insertIfNotFound)
            {
                AddInsertTask(batch, true);
            }
            batch.Execute(ConnectionString);
        }

        protected new virtual void BindTaskData(Sitecore.Tasks.Task task, 
            Sitecore.Data.Sql.SqlBatch batch)
        {
            System.DateTime taskDate = task.TaskDate;
            if (taskDate == System.DateTime.MinValue)
            {
                taskDate = (System.DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
            }
            batch.AddParameter("taskID", task.ID);
            batch.AddParameter("nextRun", taskDate);
            if (task is Sitecore.Tasks.ArchiveItem)
            {
                batch.AddParameter("taskType",
                    Sitecore.Reflection.ReflectionUtil.GetTypeString(typeof(MyArchiveItem)));
            }
            else
            {
                batch.AddParameter("taskType", ReflectionUtil.GetTypeString(task.GetType()));
            }
            batch.AddParameter("parameters", task.Parameters);
            batch.AddParameter("recurrence", task.RecurrencePattern);
            batch.AddParameter("itemID", task.ItemID);
            batch.AddParameter("databaseName", task.DatabaseName);
            if (string.IsNullOrEmpty(task.InstanceName))
            {
                batch.AddParameter("instanceName", System.DBNull.Value);
            }
            else
            {
                batch.AddParameter("instanceName", task.InstanceName);
            }
        }
    }
}

您需要做的最后一件事是将Sitecore配置更新为指向 MySqlServerTaskDatabase

<TaskDatabase type="My.Assembly.And.Namespace.MySqlServerTaskDatabase, My.Assembly">
    <param connectionStringName="core"/>
</TaskDatabase>

有关失败的归档尝试的信息将存储在日志文件中。您可能希望更新此部件以将其存储在自定义报告中。


下面提供了原始问题无法使用的其他信息。

您也可以在按照以下说明设置时间表之前进行挂钩,以通知用户该项目不会被存档。

首先创建将覆盖 ArchiveDateForm 类的类:

namespace My.Assembly.And.Namespace
{
    public class MyArchiveDateForm
        : Sitecore.Shell.Applications.Dialogs.ArchiveDate.ArchiveDateForm
    {
        protected override bool SetItemArchiveDate
            (Sitecore.Data.Items.Item item, string value)
        {
            if (HasLink(Sitecore.Globals.LinkDatabase, item))
            {
                Sitecore.Web.UI.Sheer.SheerResponse.Alert(
                    "Item or one of its descendants are linked from other items. "
                    + "Remove link before scheduling archive.", new string[0]);
                return false;
            }
            return base.SetItemArchiveDate(item, value);
        }

        private static bool HasLink(Sitecore.Links.LinkDatabase linkDatabase,
            Sitecore.Data.Items.Item item)
        {
            Sitecore.Links.ItemLink[] referrers = 
                linkDatabase.GetReferrers(item);
            if (referrers.Length > 0)
            {
                if (referrers.Any(
                    link => link.SourceFieldID != Sitecore.FieldIDs.Source))
                {
                    return true;
                }
            }
            foreach (Sitecore.Data.Items.Item item2 in item.Children)
            {
                if (HasLink(linkDatabase, item2))
                {
                    return true;
                }
            }
            return false;
        }

    }
}

然后找到文件 / sitecore / shell / applications / dialogs / archive item / archive date.xml 。将第6行更改为指向新类:

<CodeBeside Type="My.Assembly.And.Namespace.MyArchiveDateForm,My.Assembly" />

就是这样。每当人们尝试安排链接项目的归档时,Sitecore将显示该项目无法归档的信息。