Esempio n. 1
0
        /// <summary>
        /// Save changes that were made to an item to the database.
        /// </summary>
        /// <param name="itemDefinition">Used to identify the particular item</param>
        /// <param name="changes">A holder object that keeps track of the changes</param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override bool SaveItem(ItemDefinition itemDefinition, Sitecore.Data.Items.ItemChanges changes, CallContext context)
        {
            Item current = Items.FindOneById(itemDefinition.ID.ToGuid());

            if (current == null)
            {
                return(false);
            }

            if (changes.HasPropertiesChanged)
            {
                current.Name = StringUtil.GetString(changes.GetPropertyValue("name"), itemDefinition.Name);

                ID templateId = MainUtil.GetObject(changes.GetPropertyValue("templateid"), itemDefinition.TemplateID) as ID;
                current.TemplateID = templateId != ID.Null ? templateId.ToGuid() : Guid.Empty;

                ID branchId = MainUtil.GetObject(changes.GetPropertyValue("branchid"), itemDefinition.BranchId) as ID;
                current.BranchID = branchId != ID.Null ? branchId.ToGuid() : Guid.Empty;
            }
            if (changes.HasFieldsChanged)
            {
                foreach (Sitecore.Data.Items.FieldChange change in changes.FieldChanges)
                {
                    VersionUri fieldVersionUri = new VersionUri(
                        change.Definition == null || change.Definition.IsShared ? null : change.Language,
                        change.Definition == null || change.Definition.IsUnversioned ? null : change.Version);
                    var matchingFields = current.FieldValues.Where(fv => fv.Key.Matches(fieldVersionUri) && fv.Key.FieldId.Equals(change.FieldID.ToGuid()));

                    if (change.RemoveField)
                    {
                        if (matchingFields.Count() > 0)
                        {
                            current.FieldValues.Remove(matchingFields.First().Key);
                        }
                    }
                    else
                    {
                        if (matchingFields.Count() > 0)
                        {
                            current.FieldValues[matchingFields.First().Key] = change.Value;
                        }
                        else
                        {
                            current.FieldValues.Add(new FieldValueId()
                            {
                                FieldId  = change.FieldID.ToGuid(),
                                Language = fieldVersionUri.Language != null ? fieldVersionUri.Language.Name : null,
                                Version  = fieldVersionUri.Version != null ? fieldVersionUri.Version.Number : null as int?
                            }, change.Value);
                        }
                    }
                }

                Items.Save(current, SafeMode);
            }
            return(true);
        }
        private void UpdateItemDefinition([NotNull] ItemDefinition item, [NotNull] ItemChanges changes)
        {
            var itemName   = StringUtil.GetString(changes.GetPropertyValue("name"), item.Name);
            var templateID = MainUtil.GetObject(changes.GetPropertyValue("templateid"), item.TemplateID) as ID;
            var branchId   = MainUtil.GetObject(changes.GetPropertyValue("branchid"), item.BranchId) as ID;

            ItemsRow r;

            if (!Items.TryGetValue(item.ID, out r))
            {
                return;
            }

            r.Name       = itemName;
            r.TemplateID = templateID;
            r.BranchID   = branchId;
        }
        /// <summary>
        ///     Save changes that were made to an item to the database.
        /// </summary>
        /// <param name="itemDefinition">Used to identify the particular item</param>
        /// <param name="changes">A holder object that keeps track of the changes</param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override bool SaveItem(ItemDefinition itemDefinition, ItemChanges changes, CallContext context)
        {
            if (!ItemsById.ContainsKey(itemDefinition.ID))
            {
                return(false);
            }

            SyncItem current = ItemsById[itemDefinition.ID];

            if (changes.HasPropertiesChanged)
            {
                current.Name = StringUtil.GetString(changes.GetPropertyValue("name"), itemDefinition.Name);

                var templateId =
                    MainUtil.GetObject(changes.GetPropertyValue("templateid"), itemDefinition.TemplateID) as ID;
                current.TemplateID = templateId != ID.Null ? GetIdAsString(templateId) : null;

                var branchId = MainUtil.GetObject(changes.GetPropertyValue("branchid"), itemDefinition.BranchId) as ID;
                current.BranchId = branchId != ID.Null ? GetIdAsString(branchId) : null;
            }
            if (changes.HasFieldsChanged)
            {
                foreach (FieldChange change in changes.FieldChanges)
                {
                    string changeFieldId = GetIdAsString(change.FieldID);
                    IEnumerable <SyncField> matchingSharedFields =
                        current.SharedFields.Where(fv => changeFieldId.Equals(fv.FieldID));
                    IEnumerable <SyncVersion> matchingVersions = current.Versions
                                                                 .Where(
                        vr =>
                        vr.Version.Equals(change.Version.Number.ToString()) &&
                        vr.Language.Equals(change.Language.Name));
                    var matchingNonSharedFields = matchingVersions
                                                  .SelectMany(vr => vr.Fields.Select(fl => new { Ver = vr, Field = fl }))
                                                  .Where(fv => changeFieldId.Equals(fv.Field.FieldID));
                    if (change.RemoveField)
                    {
                        if (matchingSharedFields.Any())
                        {
                            current.SharedFields.Remove(matchingSharedFields.First());
                        }
                        if (matchingNonSharedFields.Any())
                        {
                            matchingNonSharedFields.First()
                            .Ver.RemoveField(matchingNonSharedFields.First().Field.FieldName);
                        }
                    }
                    else
                    {
                        bool changeMade = false;
                        if (matchingSharedFields.Any())
                        {
                            matchingSharedFields.First().FieldValue = change.Value;
                            changeMade = true;
                        }
                        if (matchingNonSharedFields.Any())
                        {
                            matchingNonSharedFields.First().Field.FieldValue = change.Value;
                            changeMade = true;
                        }
                        if (!changeMade && change.Definition != null)
                        {
                            if (change.Definition.IsShared || change.Definition.IsUnversioned)
                            {
                                current.AddSharedField(changeFieldId, change.Definition.Name, change.Definition.Key,
                                                       change.Value, true);
                            }
                            else if (matchingVersions.Any())
                            {
                                matchingVersions.First()
                                .AddField(changeFieldId, change.Definition.Name, change.Definition.Key, change.Value,
                                          true);
                            }
                        }
                    }
                }
            }
            return(true);
        }
Esempio n. 4
0
        public override bool SaveItem(ItemDefinition itemDefinition, ItemChanges changes, CallContext context)
        {
            var current = Provider.GetItem(itemDefinition.ID.ToGuid());

            if (current == null)
            {
                return(false);
            }

            if (changes.HasPropertiesChanged)
            {
                current.Name = StringUtil.GetString(changes.GetPropertyValue("name"), itemDefinition.Name);

                var templateId = MainUtil.GetObject(changes.GetPropertyValue("templateid"), itemDefinition.TemplateID) as ID;
                current.TemplateId = templateId != ID.Null ? templateId.ToGuid() : Guid.Empty;

                var branchId = MainUtil.GetObject(changes.GetPropertyValue("branchid"), itemDefinition.BranchId) as ID;
                current.BranchId = branchId != ID.Null ? branchId.ToGuid() : Guid.Empty;
            }

            if (changes.HasFieldsChanged)
            {
                foreach (FieldChange change in changes.FieldChanges)
                {
                    var fieldVersionUri = new VersionUri(
                        change.Definition == null || change.Definition.IsShared ? null : change.Language,
                        change.Definition == null || change.Definition.IsUnversioned ? null : change.Version);

                    var matchingFields = current.FieldValues.Where(fv => fv.Matches(fieldVersionUri) && fv.Id.Equals(change.FieldID.ToGuid())).ToList();

                    if (change.RemoveField)
                    {
                        if (matchingFields.Any())
                        {
                            current.FieldValues.Remove(matchingFields.First());
                        }
                    }
                    else
                    {
                        if (matchingFields.Any())
                        {
                            current.FieldValues.Find(fv => fv.Matches(fieldVersionUri) && fv.Id.Equals(change.FieldID.ToGuid())).Value = change.Value;
                        }
                        else
                        {
                            current.FieldValues.Add(new FieldDto
                            {
                                Id       = change.FieldID.ToGuid(),
                                Language = fieldVersionUri.Language != null ? fieldVersionUri.Language.Name : null,
                                Version  = fieldVersionUri.Version != null ? fieldVersionUri.Version.Number : null as int?,
                                Value    = change.Value
                            });
                        }


                        if (change.FieldID == FieldIDs.WorkflowState)
                        {
                            Guid workflowStateId = Guid.Empty;
                            Guid.TryParse(change.Value, out workflowStateId);

                            current.WorkflowStateId = workflowStateId;
                        }
                    }
                }



                Provider.WritableProvider.Store(current);
            }

            return(true);
        }