Exemplo n.º 1
0
        protected virtual void PasteSharedFields(IItemData serializedItemData, Item targetItem, bool newItemWasCreated, List <TemplateMissingFieldException> softErrors)
        {
            bool commitEdit = 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) && _fieldFilter.Includes(field.ID.Guid))
                    {
                        _logger.ResetFieldThatDidNotExistInSerialized(field);

                        field.Reset();

                        commitEdit = true;
                    }
                }

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

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

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

                    targetItem.Reload();

                    ResetTemplateEngineIfItemIsTemplate(targetItem);
                }
            }
            finally
            {
                if (targetItem.Editing.IsEditing)
                {
                    targetItem.Editing.CancelEdit();
                }
            }
        }
Exemplo n.º 2
0
        protected virtual bool PasteSharedFields(IItemData serializedItemData, Item targetItem, bool newItemWasCreated, List <TemplateMissingFieldException> softErrors, IFieldValueManipulator fieldValueManipulator)
        {
            bool commitEdit = 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 && fieldValueManipulator?.GetFieldValueTransformer(field.Name) == null && !allTargetSharedFields.Contains(field.ID.Guid) && _fieldFilter.Includes(field.ID.Guid))
                    {
                        _logger.ResetFieldThatDidNotExistInSerialized(field);

                        field.Reset();

                        commitEdit = true;
                    }
                }

                var transformersArray = fieldValueManipulator?.GetFieldValueTransformers();
                List <IFieldValueTransformer> transformersList = new List <IFieldValueTransformer>();
                if (transformersArray != null)
                {
                    transformersList = new List <IFieldValueTransformer>(transformersArray);
                }

                foreach (var field in serializedItemData.SharedFields)
                {
                    try
                    {
                        if (PasteField(targetItem, field, newItemWasCreated, fieldValueManipulator))
                        {
                            commitEdit = true;
                        }

                        var t = transformersList.FirstOrDefault(x => x.FieldName.Equals(field.NameHint));
                        if (t != null)
                        {
                            transformersList.Remove(t);
                        }
                    }
                    catch (TemplateMissingFieldException tex)
                    {
                        softErrors.Add(tex);
                    }
                }

                // Whatever remains here are field transformers that are NOT represented in the serialized data shared fields
                foreach (var t in transformersList)
                {
                    var fieldMeta = targetItem.Template.GetField(t.FieldName);

                    // If the field doesn't exist on the target template, it's time to just skip
                    if (fieldMeta != null)
                    {
                        // We're only dealing with Shared Fields in this method
                        if (!fieldMeta.IsShared)
                        {
                            continue;
                        }

                        var existingField = targetItem.Fields[fieldMeta.ID];
                        if (t.ShouldDeployFieldValue(existingField.Value, null))
                        {
                            var fakeField = new ItemFieldValue(existingField, null);
                            if (PasteField(targetItem, fakeField, newItemWasCreated, fieldValueManipulator))
                            {
                                commitEdit = true;
                            }
                        }
                    }
                }

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

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

                    targetItem.Reload();

                    ResetTemplateEngineIfItemIsTemplate(targetItem);

                    return(true);
                }
            }
            finally
            {
                if (targetItem.Editing.IsEditing)
                {
                    targetItem.Editing.CancelEdit();
                }
            }

            return(false);
        }