Exemplo 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);
        }
Exemplo n.º 2
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)
        {
            if (!ItemsById.ContainsKey(itemDefinition.ID))
            {
                return(false);
            }

            SyncItem current = ItemsById[itemDefinition.ID];

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

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

                ID branchId = Sitecore.MainUtil.GetObject(changes.GetPropertyValue("branchid"), itemDefinition.BranchId) as ID;
                current.BranchId = branchId != ID.Null ? GetIdAsString(branchId) : null;
            }
            if (changes.HasFieldsChanged)
            {
                foreach (Sitecore.Data.Items.FieldChange change in changes.FieldChanges)
                {
                    string changeFieldId        = GetIdAsString(change.FieldID);
                    var    matchingSharedFields = current.SharedFields.Where(fv => changeFieldId.Equals(fv.FieldID));
                    var    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);
        }