Ejemplo n.º 1
0
        /// <summary>
        /// Inserts field value into item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="field">The field.</param>
        /// <param name="creatingNewItem">Whether the item under update is new or not (controls logging verbosity)</param>
        /// <exception cref="T:Sitecore.Data.Serialization.Exceptions.FieldIsMissingFromTemplateException"/>
        protected virtual bool PasteField(Item item, IItemFieldValue field, bool creatingNewItem)
        {
            if (!_fieldFilter.Includes(field.FieldId))
            {
                _logger.SkippedPastingIgnoredField(item, field);
                return(false);
            }

            Template template = AssertTemplate(item.Database, item.TemplateID, item.Paths.Path);

            if (template.GetField(new ID(field.FieldId)) == null)
            {
                item.Database.Engines.TemplateEngine.Reset();
                template = AssertTemplate(item.Database, item.TemplateID, item.Paths.Path);
            }

            if (template.GetField(new ID(field.FieldId)) == null)
            {
                throw new TemplateMissingFieldException(item.TemplateName, item.Database.Name + ":" + item.Paths.FullPath, field);
            }

            Field itemField = item.Fields[new ID(field.FieldId)];

            if (itemField.IsBlobField)
            {
                Guid existingBlobId;
                bool hasExistingId = Guid.TryParse(itemField.Value, out existingBlobId);

                // serialized blob has no value (media item with detached media)
                if (!field.BlobId.HasValue)
                {
                    if (!hasExistingId || existingBlobId == Guid.Empty)
                    {
                        return(false);                                                                    // no blob ID and none in DB either, so we're cool
                    }
                    // existing blob in DB but none in serialized
                    // so clear the blob and field value
                    ItemManager.RemoveBlobStream(existingBlobId, item.Database);
                    itemField.SetValue(string.Empty, true);
                    _logger.WroteBlobStream(item, field);

                    return(true);
                }

                // check if existing blob is here with the same ID; abort if so
                if (hasExistingId && existingBlobId == field.BlobId.Value)
                {
                    return(false);
                }

                byte[] buffer = Convert.FromBase64String(field.Value);

                // if an existing blob of a different ID exists, drop it from the database
                if (existingBlobId != default(Guid))
                {
                    ItemManager.RemoveBlobStream(existingBlobId, item.Database);
                }

                // write the new blob to the database
                ItemManager.SetBlobStream(new MemoryStream(buffer, false), field.BlobId.Value, item.Database);

                // set the value of the blob field to the correct blob ID
                itemField.SetValue(MainUtil.GuidToString(field.BlobId.Value), true);

                if (!creatingNewItem)
                {
                    _logger.WroteBlobStream(item, field);
                }

                return(true);
            }

            if (field.Value != null && !field.Value.Equals(itemField.GetValue(false, false)))
            {
                var oldValue = itemField.Value;
                itemField.SetValue(field.Value, true);

                if (!creatingNewItem)
                {
                    _logger.UpdatedChangedFieldValue(item, field, oldValue);
                }

                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Inserts field value into item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="field">The field.</param>
        /// <param name="creatingNewItem">Whether the item under update is new or not (controls logging verbosity)</param>
        /// <exception cref="T:Sitecore.Data.Serialization.Exceptions.FieldIsMissingFromTemplateException"/>
        protected virtual bool PasteField(Item item, IItemFieldValue field, bool creatingNewItem)
        {
            if (!_fieldFilter.Includes(field.FieldId))
            {
                _logger.SkippedPastingIgnoredField(item, field);
                return(false);
            }

            Template template = AssertTemplate(item.Database, item.TemplateID, item.Paths.Path);

            if (template.GetField(new ID(field.FieldId)) == null)
            {
                item.Database.Engines.TemplateEngine.Reset();
                template = AssertTemplate(item.Database, item.TemplateID, item.Paths.Path);
            }

            if (template.GetField(new ID(field.FieldId)) == null)
            {
                throw new TemplateMissingFieldException(item.TemplateName, item.Database.Name + ":" + item.Paths.FullPath, field);
            }

            Field itemField = item.Fields[new ID(field.FieldId)];

            if (itemField.IsBlobField)
            {
                if (!field.BlobId.HasValue)
                {
                    throw new InvalidOperationException("Field " + field.FieldId + " is a blob field, but it had no blob ID.");
                }

                // check if existing blob is here with the same ID; abort if so
                Guid existingBlobId;
                if (Guid.TryParse(itemField.Value, out existingBlobId) && existingBlobId == field.BlobId.Value)
                {
                    return(false);
                }

                byte[] buffer = Convert.FromBase64String(field.Value);

                // if an existing blob of a different ID exists, drop it from the database
                if (existingBlobId != default(Guid))
                {
                    ItemManager.RemoveBlobStream(existingBlobId, item.Database);
                }

                // write the new blob to the database
                ItemManager.SetBlobStream(new MemoryStream(buffer, false), field.BlobId.Value, item.Database);

                // set the value of the blob field to the correct blob ID
                itemField.SetValue(MainUtil.GuidToString(field.BlobId.Value), true);

                if (!creatingNewItem)
                {
                    _logger.WroteBlobStream(item, field);
                }

                return(true);
            }

            if (field.Value != null && !field.Value.Equals(itemField.GetValue(false, false)))
            {
                var oldValue = itemField.Value;
                itemField.SetValue(field.Value, true);

                if (!creatingNewItem)
                {
                    _logger.UpdatedChangedFieldValue(item, field, oldValue);
                }

                return(true);
            }

            return(false);
        }