protected virtual void PasteSharedFields(IItemData serializedItemData, Item targetItem, bool newItemWasCreated, List<TemplateMissingFieldException> softErrors)
		{
			bool commitEditContext = false;

			try
			{
				targetItem.Editing.BeginEdit();

				targetItem.RuntimeSettings.ReadOnlyStatistics = true;
				targetItem.RuntimeSettings.SaveAll = true;

				var allTargetSharedFields = serializedItemData.SharedFields.ToLookup(field => field.FieldId);

				foreach (Field field in targetItem.Fields)
				{
					if (field.Shared && !allTargetSharedFields.Contains(field.ID.Guid))
					{
						_logger.ResetFieldThatDidNotExistInSerialized(field);

						field.Reset();

						commitEditContext = true;
					}
				}

				foreach (var field in serializedItemData.SharedFields)
				{
					try
					{
						if (PasteField(targetItem, field, newItemWasCreated))
							commitEditContext = true;
					}
					catch (TemplateMissingFieldException tex)
					{
						softErrors.Add(tex);
					}
				}

				// we commit the edit context - and write to the DB - only if we changed something
				if (commitEditContext)
				{
					targetItem.Editing.EndEdit();

					ClearCaches(targetItem.Database, new ID(serializedItemData.Id));

					targetItem.Reload();

					ResetTemplateEngineIfItemIsTemplate(targetItem);
				}
			}
			finally
			{
				if (targetItem.Editing.IsEditing)
					targetItem.Editing.CancelEdit();
			}
		}
		protected void RenameIfNeeded(IItemData serializedItemData, Item targetItem)
		{
			if (targetItem.Name == serializedItemData.Name && targetItem.BranchId.Guid.Equals(serializedItemData.BranchId)) return;

			string oldName = targetItem.Name;
			Guid oldBranchId = targetItem.BranchId.Guid;

			using (new EditContext(targetItem))
			{
				targetItem.RuntimeSettings.ReadOnlyStatistics = true;
				targetItem.Name = serializedItemData.Name;
				targetItem.BranchId = ID.Parse(serializedItemData.BranchId);
			}

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

			if (oldName != serializedItemData.Name)
				_logger.RenamedItem(targetItem, oldName);

			if (oldBranchId != serializedItemData.BranchId)
				_logger.ChangedBranchTemplate(targetItem, new ID(oldBranchId).ToString());
		}
		protected void ChangeTemplateIfNeeded(IItemData serializedItemData, Item targetItem)
		{
			if (targetItem.TemplateID.Guid == serializedItemData.TemplateId) return;

			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);

			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);
		}
        protected void RenameIfNeeded(SyncItem syncItem, Item targetItem)
        {
            if (targetItem.Name != syncItem.Name || targetItem.BranchId.ToString() != syncItem.BranchId)
            {
                string oldName = targetItem.Name;
                string oldBranchId = targetItem.BranchId.ToString();

                using (new EditContext(targetItem))
                {
                    targetItem.RuntimeSettings.ReadOnlyStatistics = true;
                    targetItem.Name = syncItem.Name;
                    targetItem.BranchId = ID.Parse(syncItem.BranchId);
                }

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

                if (oldName != syncItem.Name)
                    _logger.RenamedItem(targetItem, oldName);

                if (oldBranchId != syncItem.BranchId)
                    _logger.ChangedBranchTemplate(targetItem, oldBranchId);
            }
        }