public override bool ChangeTemplate(
            [NotNull] ItemDefinition itemDefinition,
            [NotNull] TemplateChangeList changeList,
            [NotNull] CallContext context)
        {
            lock (this)
            {
                var changes = changeList.Changes;

                foreach (var change in changes)
                {
                    switch (change.Action)
                    {
                    case TemplateChangeAction.ChangeFieldID:
                        ChangeTemplate_ChangeFieldID(change, itemDefinition.ID);
                        ChangeTemplate_ChangeFieldSharing(change, itemDefinition.ID);
                        break;

                    case TemplateChangeAction.DeleteField:
                        ChangeTemplate_DeleteFieldID(change, itemDefinition.ID);
                        break;
                    }
                }

                ChangeTemplate_ChangeTemplateID(itemDefinition.ID, changeList.Target.ID);

                return(true);
            }
        }
    public void ChangeTemplateThrowsIfNoDbItemFound([Greedy]FakeDataProvider sut, ItemDefinition def, TemplateChangeList changes)
    {
      Action action = () => sut.ChangeTemplate(def, changes, null);

      action.ShouldThrow<InvalidOperationException>()
            .WithMessage("Unable to change item template. The item '{0}' is not found.".FormatWith(def.ID));
    }
        /// <summary>
        /// Responds to Sitecore item template changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args">
        /// Param index 0 contains the ID of the item being changed,
        /// Param index 1 contains the Instance of the datamanager class handling the template
        /// </param>
        protected void OnItemTemplateChanged(object sender, EventArgs args)
        {
            Assert.IsTrue(args != null, "args != null");
            if (args != null && AuditItemDeleting)
            {
                using (new SecurityDisabler())
                {
                    Item item = Event.ExtractParameter(args, 0) as Item;
                    TemplateChangeList change = Event.ExtractParameter(args, 1) as TemplateChangeList;

                    Assert.IsTrue(item != null, "item != null");

                    if (item != null && ShouldAudit(item) && change.Target.ID != change.Source.ID)
                    {
                        Log(string.Format("TEMPLATE CHANGE: {0}:{1}, target: {2}, source: {3}", item.Database.Name, item.Paths.Path, change.Target.Name, change.Source.Name));
                        foreach (TemplateChangeList.TemplateChange c in change.Changes)
                        {
                            if (c.Action == TemplateChangeAction.DeleteField)
                            {
                                Log(string.Format("** {0}: {1}", c.Action, c.SourceField.Name));
                            }
                        }
                    }
                }
            }
        }
        public void ChangeTemplate(ItemDefinition itemDefinition, TemplateChangeList changeList, CallContext context)
        {
            if (DisableSerialization)
            {
                return;
            }

            Assert.ArgumentNotNull(itemDefinition, nameof(itemDefinition));
            Assert.ArgumentNotNull(changeList, nameof(changeList));

            var sourceItem = GetSourceFromId(itemDefinition.ID);

            var existingSerializedItem = _targetDataStore.GetByPathAndId(sourceItem.Path, sourceItem.Id, sourceItem.DatabaseName);

            if (existingSerializedItem == null)
            {
                return;
            }

            var newItem = new ProxyItem(sourceItem);             // note: sourceItem gets dumped. Because it has field changes made to it.

            newItem.TemplateId = changeList.Target.ID.Guid;

            _targetDataStore.Save(newItem);

            _logger.SavedItem(_targetDataStore.FriendlyName, sourceItem, "TemplateChanged");
        }
    public void ChangeTemplateThrowsIfNoTargetTemplateFound([Greedy]FakeDataProvider sut, ItemDefinition def, TemplateChangeList changes, DbItem item)
    {
      sut.DataStorage.GetFakeItem(def.ID).Returns(item);

      Action action = () => sut.ChangeTemplate(def, changes, null);

      action.ShouldThrow<InvalidOperationException>()
            .WithMessage("Unable to change item template. The target template is not found.");
    }
Beispiel #6
0
        public override bool ChangeTemplate(ItemDefinition itemDefinition, TemplateChangeList changes, CallContext context)
        {
            this.Trace(true, null, itemDefinition, context);

            if (HeadProvider.ChangeTemplate(itemDefinition, changes, context))
            {
                return(true);
            }

            throw new NotImplementedException();
        }
    public override bool ChangeTemplate(ItemDefinition itemDefinition, TemplateChangeList changes, CallContext context)
    {
      Assert.ArgumentNotNull(itemDefinition, "itemDefinition");
      Assert.ArgumentNotNull(changes, "changes");

      var item = this.DataStorage().GetFakeItem(itemDefinition.ID);
      Assert.IsNotNull(item, "Unable to change item template. The item '{0}' is not found.", itemDefinition.ID);
      Assert.IsNotNull(changes.Target, "Unable to change item template. The target template is not found.");

      item.TemplateID = changes.Target.ID;
      return true;
    }
        public override bool ChangeTemplate(ItemDefinition itemDefinition, TemplateChangeList changes, CallContext context)
        {
            Assert.ArgumentNotNull(itemDefinition, "itemDefinition");
            Assert.ArgumentNotNull(changes, "changes");

            var item = this.DataStorage.GetFakeItem(itemDefinition.ID);

            Assert.IsNotNull(item, "Unable to change item template. The item '{0}' is not found.", itemDefinition.ID);
            Assert.IsNotNull(changes.Target, "Unable to change item template. The target template is not found.");

            item.TemplateID = changes.Target.ID;
            return(true);
        }
Beispiel #9
0
        protected bool ChangeTemplateIfNeeded(IItemData serializedItemData, Item targetItem)
        {
            if (targetItem.TemplateID.Guid == serializedItemData.TemplateId)
            {
                return(false);
            }

            var oldTemplate = targetItem.Template;
            var newTemplate = targetItem.Database.Templates[new ID(serializedItemData.TemplateId)];

            Assert.IsNotNull(newTemplate, "Cannot change template of {0} because its new template {1} does not exist!", targetItem.ID, serializedItemData.TemplateId);

            targetItem.Editing.BeginEdit();

            targetItem.RuntimeSettings.ReadOnlyStatistics = true;
            try
            {
                targetItem.ChangeTemplate(newTemplate);
            }
            catch
            {
                // this generally means that we tried to sync an item and change its template AND we already deleted the item's old template in the same sync
                // the Sitecore change template API chokes if the item's CURRENT template is unavailable, but we can get around that
                // issue reported to Sitecore Support (406546)
                lock (targetItem.SyncRoot)
                {
                    Template sourceTemplate = TemplateManager.GetTemplate(targetItem);
                    Template targetTemplate = TemplateManager.GetTemplate(newTemplate.ID, targetItem.Database);

                    Error.AssertNotNull(targetTemplate, "Could not get target in ChangeTemplate");

                    // this is probably true if we got here. This is the check the Sitecore API fails to make, and throws a NullReferenceException.
                    if (sourceTemplate == null)
                    {
                        sourceTemplate = targetTemplate;
                    }

                    TemplateChangeList templateChangeList = sourceTemplate.GetTemplateChangeList(targetTemplate);
                    TemplateManager.ChangeTemplate(targetItem, templateChangeList);
                }
            }

            targetItem.Editing.EndEdit();

            ClearCaches(targetItem.Database, targetItem.ID);
            targetItem.Reload();

            _logger.ChangedTemplate(targetItem, oldTemplate);

            return(true);
        }
        public override bool ChangeTemplate(ItemDefinition itemDefinition, TemplateChangeList changeList, CallContext context)
        {
            if (!base.ChangeTemplate(itemDefinition, changeList, context))
            {
                return(false);
            }

            foreach (var provider in _unicornDataProviders)
            {
                provider.ChangeTemplate(itemDefinition, changeList, context);
            }

            return(true);
        }
Beispiel #11
0
        protected void ChangeTemplateIfNeeded(SyncItem syncItem, Item targetItem)
        {
            if (targetItem.TemplateID.ToString() != syncItem.TemplateID)
            {
                var oldTemplate = targetItem.Template;
                var newTemplate = targetItem.Database.Templates[ID.Parse(syncItem.TemplateID)];

                Assert.IsNotNull(newTemplate, "Cannot change template of {0} because its new template {1} does not exist!", targetItem.ID, syncItem.TemplateID);

                using (new EditContext(targetItem))
                {
                    targetItem.RuntimeSettings.ReadOnlyStatistics = true;
                    try
                    {
                        targetItem.ChangeTemplate(newTemplate);
                    }
                    catch
                    {
                        // this generally means that we tried to sync an item and change its template AND we already deleted the item's old template in the same sync
                        // the Sitecore change template API chokes if the item's CURRENT template is unavailable, but we can get around that
                        // issure reported to Sitecore Support (406546)
                        lock (targetItem.SyncRoot)
                        {
                            Template sourceTemplate = TemplateManager.GetTemplate(targetItem);
                            Template targetTemplate = TemplateManager.GetTemplate(newTemplate.ID, targetItem.Database);

                            Error.AssertNotNull(targetTemplate, "Could not get target in ChangeTemplate");

                            // this is probably true if we got here. This is the check the Sitecore API fails to make, and throws a NullReferenceException.
                            if (sourceTemplate == null)
                            {
                                sourceTemplate = targetTemplate;
                            }

                            TemplateChangeList templateChangeList = sourceTemplate.GetTemplateChangeList(targetTemplate);
                            TemplateManager.ChangeTemplate(targetItem, templateChangeList);
                        }
                    }
                }

                ClearCaches(targetItem.Database, targetItem.ID);
                targetItem.Reload();

                _logger.ChangedTemplate(targetItem, oldTemplate);
            }
        }
Beispiel #12
0
        public void ChangeTemplateThrowsIfNoDbItemFound(
            [Greedy] FakeDataProvider sut,
            ItemDefinition def)
        {
            var doc      = new XmlDocument();
            var template = doc.AppendChild(doc.CreateElement("template"));

            XmlUtil.AddAttribute("name", "template", template);
            var changes = new TemplateChangeList(
                Template.Parse(template, new TemplateCollection()),
                Template.Parse(template, new TemplateCollection()));

            Action action = () => sut.ChangeTemplate(def, changes, null);

            action.ShouldThrow <InvalidOperationException>()
            .WithMessage("Unable to change item template. The item '{0}' is not found.".FormatWith(def.ID));
        }
Beispiel #13
0
 public override bool ChangeTemplate(ItemDefinition itemDefinition, TemplateChangeList changes, CallContext context)
 {
     return(base.ChangeTemplate(itemDefinition, changes, context));
 }
        public override bool ChangeTemplate(ItemDefinition itemDefinition, TemplateChangeList changeList, CallContext context)
        {
            if (!base.ChangeTemplate(itemDefinition, changeList, context)) return false;

            foreach (var provider in _unicornDataProviders)
                provider.ChangeTemplate(itemDefinition, changeList, context);

            return true;
        }
        public void ChangeTemplate(ItemDefinition itemDefinition, TemplateChangeList changeList, CallContext context)
        {
            if (DisableSerialization) return;

            Assert.ArgumentNotNull(itemDefinition, nameof(itemDefinition));
            Assert.ArgumentNotNull(changeList, nameof(changeList));

            var sourceItem = GetSourceFromId(itemDefinition.ID);

            var existingSerializedItem = _targetDataStore.GetByPathAndId(sourceItem.Path, sourceItem.Id, sourceItem.DatabaseName);

            if (existingSerializedItem == null) return;

            var newItem = new ProxyItem(sourceItem); // note: sourceItem gets dumped. Because it has field changes made to it.
            newItem.TemplateId = changeList.Target.ID.Guid;

            _targetDataStore.Save(newItem);

            _logger.SavedItem(_targetDataStore.FriendlyName, sourceItem, "TemplateChanged");
        }
        public void ChangeTemplateThrowsIfNoTargetTemplateFound([Greedy] FakeDataProvider sut, ItemDefinition def, TemplateChangeList changes, DbItem item)
        {
            sut.DataStorage.GetFakeItem(def.ID).Returns(item);

            Action action = () => sut.ChangeTemplate(def, changes, null);

            action.ShouldThrow <InvalidOperationException>()
            .WithMessage("Unable to change item template. The target template is not found.");
        }
        public void ChangeTemplateThrowsIfNoDbItemFound([Greedy] FakeDataProvider sut, ItemDefinition def, TemplateChangeList changes)
        {
            Action action = () => sut.ChangeTemplate(def, changes, null);

            action.ShouldThrow <InvalidOperationException>()
            .WithMessage("Unable to change item template. The item '{0}' is not found.".FormatWith(def.ID));
        }
 public override bool ChangeTemplate(ItemDefinition itemDefinition, TemplateChangeList changes, CallContext context)
 {
     return base.ChangeTemplate(itemDefinition, changes, context);
 }
 public sealed override bool ChangeTemplate(ItemDefinition itemDefinition, TemplateChangeList changes, CallContext context)
 {
     throw new NotSupportedException(MustNotBeCalled);
 }