protected virtual bool ShouldUpdateExisting(ISerializedItem serializedItem, ISourceItem existingItem, DeferredLogWriter <ISerializedAsMasterEvaluatorLogger> deferredUpdateLog) { Assert.ArgumentNotNull(serializedItem, "serializedItem"); Assert.ArgumentNotNull(existingItem, "existingItem"); if (existingItem.Id == RootId) { return(false); // we never want to update the Sitecore root item } // check if templates are different if (IsTemplateMatch(existingItem, serializedItem, deferredUpdateLog)) { return(true); } // check if names are different if (IsNameMatch(existingItem, serializedItem, deferredUpdateLog)) { return(true); } // check if source has version(s) that serialized does not var orphanVersions = existingItem.Versions.Where(sourceItemVersion => serializedItem.GetVersion(sourceItemVersion.Language, sourceItemVersion.VersionNumber) == null).ToArray(); if (orphanVersions.Length > 0) { deferredUpdateLog.AddEntry(x => x.OrphanSourceVersion(existingItem, serializedItem, orphanVersions)); return(true); // source contained versions not present in the serialized version, which is a difference } // check if shared fields have any mismatching values if (AnyFieldMatch(serializedItem.SharedFields, existingItem.SharedFields, existingItem, serializedItem, deferredUpdateLog)) { return(true); } // see if the serialized versions have any mismatching values in the source data return(serializedItem.Versions.Any(serializedItemVersion => { var sourceItemVersion = existingItem.GetVersion(serializedItemVersion.Language, serializedItemVersion.VersionNumber); // version exists in serialized item but does not in source version if (sourceItemVersion == null) { deferredUpdateLog.AddEntry(x => x.NewSerializedVersionMatch(serializedItemVersion, serializedItem, existingItem)); return true; } // field values mismatch var fieldMatch = AnyFieldMatch(serializedItemVersion.Fields, sourceItemVersion.Fields, existingItem, serializedItem, deferredUpdateLog, serializedItemVersion); if (fieldMatch) { return true; } // if we get here everything matches to the best of our knowledge, so we return false (e.g. "do not update this item") return false; })); }
protected virtual void DeserializeRole(IRoleData role) { bool addedRole = false; // Add role if needed var name = role.RoleName; if (!System.Web.Security.Roles.RoleExists(name)) { _loaderLogger.AddedNewRole(role); addedRole = true; System.Web.Security.Roles.CreateRole(name); } Role targetRole = Role.FromName(name); var currentSourceParents = new SitecoreRoleData(targetRole).MemberOfRoles; var currentTargetParents = role.MemberOfRoles; var addedRoleMembership = new List <string>(); var removedRoleMembership = new List <string>(); var deferredUpdateLog = new DeferredLogWriter <IRoleLoaderLogger>(); // Loop over the serialized parent roles and set db roles if needed foreach (var serializedMemberRoleName in currentTargetParents) { var memberRole = Role.FromName(serializedMemberRoleName); // add nonexistant parent role if needed. NOTE: parent role need not be one we have serialized or included. if (!Role.Exists(serializedMemberRoleName)) { deferredUpdateLog.AddEntry(log => log.AddedNewRoleMembership(new SitecoreRoleData(memberRole))); System.Web.Security.Roles.CreateRole(serializedMemberRoleName); } // Add membership if not already in the parent role if (!RolesInRolesManager.IsRoleInRole(targetRole, memberRole, false)) { addedRoleMembership.Add(memberRole.Name); RolesInRolesManager.AddRoleToRole(targetRole, memberRole); } } // Loop over parent roles that exist in the database but not in serialized and remove them var membershipToRemove = currentSourceParents.Where(parent => !currentTargetParents.Contains(parent, StringComparer.OrdinalIgnoreCase)); foreach (var roleToRemove in membershipToRemove) { removedRoleMembership.Add(roleToRemove); RolesInRolesManager.RemoveRoleFromRole(targetRole, Role.FromName(roleToRemove)); } if (!addedRole && (addedRoleMembership.Count > 0 || removedRoleMembership.Count > 0)) { _loaderLogger.RoleMembershipChanged(role, addedRoleMembership.ToArray(), removedRoleMembership.ToArray()); } deferredUpdateLog.ExecuteDeferredActions(_loaderLogger); }
protected virtual bool IsNameMatch(ISourceItem existingItem, ISerializedItem serializedItem, DeferredLogWriter <ISerializedAsMasterEvaluatorLogger> deferredUpdateLog) { if (!serializedItem.Name.Equals(existingItem.Name)) { deferredUpdateLog.AddEntry(x => x.IsNameMatch(serializedItem, existingItem)); return(true); } return(false); }
protected virtual bool IsTemplateMatch(ISourceItem existingItem, ISerializedItem serializedItem, DeferredLogWriter <ISerializedAsMasterEvaluatorLogger> deferredUpdateLog) { if (existingItem.TemplateId == (ID)null && serializedItem.TemplateId == (ID)null) { return(false); } bool match = !serializedItem.TemplateId.Equals(existingItem.TemplateId); if (match) { deferredUpdateLog.AddEntry(x => x.IsTemplateMatch(serializedItem, existingItem)); } return(match); }
protected virtual bool ShouldUpdateExisting(IItemData sourceItem, IItemData targetItem, DeferredLogWriter <ISerializedAsMasterEvaluatorLogger> deferredUpdateLog) { Assert.ArgumentNotNull(targetItem, "targetItem"); Assert.ArgumentNotNull(sourceItem, "sourceItem"); if (sourceItem.Id == RootId) { return(false); // we never want to update the Sitecore root item } // filter out ignored fields before we do the comparison var filteredTargetItem = new FilteredItem(targetItem, _fieldFilter); var filteredSourceItem = new FilteredItem(sourceItem, _fieldFilter); var comparison = _itemComparer.FastCompare(filteredSourceItem, filteredTargetItem); if (comparison.IsRenamed || comparison.IsMoved) { deferredUpdateLog.AddEntry(log => log.Renamed(sourceItem, targetItem)); } if (comparison.IsTemplateChanged) { deferredUpdateLog.AddEntry(log => log.TemplateChanged(sourceItem, targetItem)); } foreach (var sharedChange in comparison.ChangedSharedFields) { deferredUpdateLog.AddEntry(log => log.SharedFieldIsChanged(targetItem, (sharedChange.TargetField ?? sharedChange.SourceField).FieldId, ((sharedChange.TargetField != null) ? sharedChange.TargetField.Value : null), ((sharedChange.SourceField != null) ? sharedChange.SourceField.Value : null))); } foreach (var versionChange in comparison.ChangedVersions) { if (versionChange.SourceVersion == null) { deferredUpdateLog.AddEntry(log => log.NewTargetVersion(versionChange.TargetVersion, targetItem, sourceItem)); } else if (versionChange.TargetVersion == null) { deferredUpdateLog.AddEntry(log => log.OrphanSourceVersion(sourceItem, targetItem, new[] { versionChange.SourceVersion })); } else { foreach (var field in versionChange.ChangedFields) { var sourceFieldValue = field.SourceField == null ? null : field.SourceField.Value; var targetFieldValue = field.TargetField == null ? null : field.TargetField.Value; var fieldId = (field.SourceField ?? field.TargetField).FieldId; deferredUpdateLog.AddEntry(log => log.VersionedFieldIsChanged(targetItem, versionChange.SourceVersion ?? versionChange.TargetVersion, fieldId, targetFieldValue, sourceFieldValue)); } } } return(!comparison.AreEqual); }
protected virtual bool AnyFieldMatch(FieldDictionary sourceFields, FieldDictionary targetFields, ISourceItem existingItem, ISerializedItem serializedItem, DeferredLogWriter <ISerializedAsMasterEvaluatorLogger> deferredUpdateLog, ItemVersion version = null) { if (sourceFields == null) { return(false); } return(sourceFields.Any(x => { if (!_fieldPredicate.Includes(x.Key).IsIncluded) { return false; } if (!existingItem.IsFieldComparable(x.Key)) { return false; } bool isMatch = IsFieldMatch(x.Value, targetFields, x.Key); if (isMatch) { deferredUpdateLog.AddEntry(logger => { string sourceFieldValue; targetFields.TryGetValue(x.Key, out sourceFieldValue); if (version == null) { logger.IsSharedFieldMatch(serializedItem, x.Key, x.Value, sourceFieldValue); } else { logger.IsVersionedFieldMatch(serializedItem, version, x.Key, x.Value, sourceFieldValue); } }); } return isMatch; })); }
protected virtual bool ShouldUpdateExisting(IItemData sourceItem, IItemData targetItem, DeferredLogWriter <ISerializedAsMasterEvaluatorLogger> deferredUpdateLog, IFieldValueManipulator fieldValueManipulator) { Assert.ArgumentNotNull(targetItem, "targetItem"); Assert.ArgumentNotNull(sourceItem, "sourceItem"); if (sourceItem.Id == RootId) { return(false); // we never want to update the Sitecore root item } // Taking a shortcut for now. If there is a dynamic field value manipiulator, it's true result can only really be obtained when doing the _actual_ write, not when trying to second guess if a write is needed if (fieldValueManipulator != null) { return(true); } // filter out ignored fields before we do the comparison var filteredTargetItem = new FilteredItem(targetItem, _fieldFilter); var filteredSourceItem = new FilteredItem(sourceItem, _fieldFilter); var comparison = _itemComparer.FastCompare(filteredSourceItem, filteredTargetItem); if (comparison.IsRenamed || comparison.IsMoved) { deferredUpdateLog.AddEntry(log => log.Renamed(sourceItem, targetItem)); } if (comparison.IsTemplateChanged) { deferredUpdateLog.AddEntry(log => log.TemplateChanged(sourceItem, targetItem)); } foreach (var sharedChange in comparison.ChangedSharedFields) { deferredUpdateLog.AddEntry(log => log.SharedFieldIsChanged( targetItem, (sharedChange.TargetField ?? sharedChange.SourceField).FieldId, sharedChange.TargetField?.Value, sharedChange.SourceField?.Value)); } foreach (var unversionedChange in comparison.ChangedUnversionedFields) { foreach (var uvField in unversionedChange.ChangedFields) { deferredUpdateLog.AddEntry(log => log.UnversionedFieldIsChanged( targetItem, unversionedChange.Language.Language, (uvField.TargetField ?? uvField.SourceField).FieldId, uvField.TargetField?.Value, uvField.SourceField?.Value)); } } foreach (var versionChange in comparison.ChangedVersions) { if (versionChange.SourceVersion == null) { deferredUpdateLog.AddEntry(log => log.NewTargetVersion(versionChange.TargetVersion, targetItem, sourceItem)); } else if (versionChange.TargetVersion == null) { deferredUpdateLog.AddEntry(log => log.OrphanSourceVersion(sourceItem, targetItem, new[] { versionChange.SourceVersion })); } else { foreach (var field in versionChange.ChangedFields) { var sourceFieldValue = field.SourceField?.Value; var targetFieldValue = field.TargetField?.Value; var fieldId = (field.SourceField ?? field.TargetField).FieldId; deferredUpdateLog.AddEntry(log => log.VersionedFieldIsChanged(targetItem, versionChange.SourceVersion ?? versionChange.TargetVersion, fieldId, targetFieldValue, sourceFieldValue)); } } } return(!comparison.AreEqual); }
protected virtual bool ShouldUpdateExisting(IItemData sourceItem, IItemData targetItem, DeferredLogWriter<ISerializedAsMasterEvaluatorLogger> deferredUpdateLog) { Assert.ArgumentNotNull(targetItem, "targetItem"); Assert.ArgumentNotNull(sourceItem, "sourceItem"); if (sourceItem.Id == RootId) return false; // we never want to update the Sitecore root item // filter out ignored fields before we do the comparison var filteredTargetItem = new FilteredItem(targetItem, _fieldFilter); var filteredSourceItem = new FilteredItem(sourceItem, _fieldFilter); var comparison = _itemComparer.FastCompare(filteredSourceItem, filteredTargetItem); if (comparison.IsRenamed || comparison.IsMoved) { deferredUpdateLog.AddEntry(log => log.Renamed(sourceItem, targetItem)); } if (comparison.IsTemplateChanged) { deferredUpdateLog.AddEntry(log => log.TemplateChanged(sourceItem, targetItem)); } foreach (var sharedChange in comparison.ChangedSharedFields) { deferredUpdateLog.AddEntry(log => log.SharedFieldIsChanged(targetItem, (sharedChange.TargetField ?? sharedChange.SourceField).FieldId, ((sharedChange.TargetField != null) ? sharedChange.TargetField.Value : null), ((sharedChange.SourceField != null) ? sharedChange.SourceField.Value : null))); } foreach (var versionChange in comparison.ChangedVersions) { if (versionChange.SourceVersion == null) deferredUpdateLog.AddEntry(log => log.NewTargetVersion(versionChange.TargetVersion, targetItem, sourceItem)); else if (versionChange.TargetVersion == null) deferredUpdateLog.AddEntry(log => log.OrphanSourceVersion(sourceItem, targetItem, new[] { versionChange.SourceVersion })); else { foreach (var field in versionChange.ChangedFields) { var sourceFieldValue = field.SourceField == null ? null : field.SourceField.Value; var targetFieldValue = field.TargetField == null ? null : field.TargetField.Value; var fieldId = (field.SourceField ?? field.TargetField).FieldId; deferredUpdateLog.AddEntry(log => log.VersionedFieldIsChanged(targetItem, versionChange.SourceVersion ?? versionChange.TargetVersion, fieldId, targetFieldValue, sourceFieldValue)); } } } return !comparison.AreEqual; }
protected virtual void DeserializeRole(IRoleData role) { bool addedRole = false; // Add role if needed var name = role.RoleName; if (!System.Web.Security.Roles.RoleExists(name)) { _loaderLogger.AddedNewRole(role); addedRole = true; System.Web.Security.Roles.CreateRole(name); } Role targetRole = Role.FromName(name); var currentSourceParents = new SitecoreRoleData(targetRole).MemberOfRoles; var currentTargetParents = role.MemberOfRoles; var addedRoleMembership = new List<string>(); var removedRoleMembership = new List<string>(); var deferredUpdateLog = new DeferredLogWriter<IRoleLoaderLogger>(); // Loop over the serialized parent roles and set db roles if needed foreach (var serializedMemberRoleName in currentTargetParents) { var memberRole = Role.FromName(serializedMemberRoleName); // add nonexistant parent role if needed. NOTE: parent role need not be one we have serialized or included. if (!Role.Exists(serializedMemberRoleName)) { deferredUpdateLog.AddEntry(log => log.AddedNewRoleMembership(new SitecoreRoleData(memberRole))); System.Web.Security.Roles.CreateRole(serializedMemberRoleName); } // Add membership if not already in the parent role if (!RolesInRolesManager.IsRoleInRole(targetRole, memberRole, false)) { addedRoleMembership.Add(memberRole.Name); RolesInRolesManager.AddRoleToRole(targetRole, memberRole); } } // Loop over parent roles that exist in the database but not in serialized and remove them var membershipToRemove = currentSourceParents.Where(parent => !currentTargetParents.Contains(parent, StringComparer.OrdinalIgnoreCase)); foreach (var roleToRemove in membershipToRemove) { removedRoleMembership.Add(roleToRemove); RolesInRolesManager.RemoveRoleFromRole(targetRole, Role.FromName(roleToRemove)); } if (!addedRole && (addedRoleMembership.Count > 0 || removedRoleMembership.Count > 0)) { _loaderLogger.RoleMembershipChanged(role, addedRoleMembership.ToArray(), removedRoleMembership.ToArray()); } deferredUpdateLog.ExecuteDeferredActions(_loaderLogger); }