diff --git a/src/Umbraco.Core/Models/Content.cs b/src/Umbraco.Core/Models/Content.cs index f4af3b842e..a805e01b93 100644 --- a/src/Umbraco.Core/Models/Content.cs +++ b/src/Umbraco.Core/Models/Content.cs @@ -337,74 +337,7 @@ namespace Umbraco.Core.Models ChangePublishedState(PublishedState.Unpublished); } } - - /// - /// Indicates whether a specific property on the current entity is dirty. - /// - /// Name of the property to check - /// True if Property is dirty, otherwise False - public override bool IsPropertyDirty(string propertyName) - { - bool existsInEntity = base.IsPropertyDirty(propertyName); - if (existsInEntity) - return true; - - return Properties.Any(x => x.IsPropertyDirty(propertyName)); - } - - /// - /// Indicates whether the current entity is dirty. - /// - /// True if entity is dirty, otherwise False - public override bool IsDirty() - { - return IsEntityDirty() || IsAnyUserPropertyDirty(); - } - - /// - /// Returns true if only the entity properties are direty - /// - /// - public bool IsEntityDirty() - { - return base.IsDirty(); - } - - /// - /// Returns true if any of the properties are dirty - /// - /// - public bool IsAnyUserPropertyDirty() - { - return Properties.Any(x => x.IsDirty()); - } - - /// - /// Returns a list of all dirty user defined properties - /// - /// - public IEnumerable GetDirtyUserProperties() - { - return Properties.Where(x => x.IsDirty()).Select(x => x.Alias); - } - - /// - /// Resets dirty properties by clearing the dictionary used to track changes. - /// - /// - /// Please note that resetting the dirty properties could potentially - /// obstruct the saving of a new or updated entity. - /// - public override void ResetDirtyProperties() - { - base.ResetDirtyProperties(); - - foreach (var property in Properties) - { - property.ResetDirtyProperties(); - } - } - + /// /// Method to call when Entity is being saved /// diff --git a/src/Umbraco.Core/Models/ContentBase.cs b/src/Umbraco.Core/Models/ContentBase.cs index da9061fdc6..6beead06e6 100644 --- a/src/Umbraco.Core/Models/ContentBase.cs +++ b/src/Umbraco.Core/Models/ContentBase.cs @@ -466,6 +466,8 @@ namespace Umbraco.Core.Models public abstract void ChangeTrashedState(bool isTrashed, int parentId = -20); + #region Dirty property handling + /// /// We will override this method to ensure that when we reset the dirty properties that we /// also reset the dirty changes made to the content's Properties (user defined) @@ -480,6 +482,89 @@ namespace Umbraco.Core.Models prop.ResetDirtyProperties(rememberPreviouslyChangedProperties); } } - + + /// + /// Indicates whether the current entity is dirty. + /// + /// True if entity is dirty, otherwise False + public override bool IsDirty() + { + return IsEntityDirty() || this.IsAnyUserPropertyDirty(); + } + + /// + /// Indicates whether the current entity was dirty. + /// + /// True if entity was dirty, otherwise False + public override bool WasDirty() + { + return WasEntityDirty() || this.WasAnyUserPropertyDirty(); + } + + /// + /// Returns true if only the entity properties are dirty + /// + /// + public bool IsEntityDirty() + { + return base.IsDirty(); + } + + /// + /// Returns true if only the entity properties were dirty + /// + /// + public bool WasEntityDirty() + { + return base.WasDirty(); + } + + /// + /// Indicates whether a specific property on the current entity is dirty. + /// + /// Name of the property to check + /// + /// True if any of the class properties are dirty or + /// True if any of the user defined PropertyType properties are dirty based on their alias, + /// otherwise False + /// + public override bool IsPropertyDirty(string propertyName) + { + bool existsInEntity = base.IsPropertyDirty(propertyName); + if (existsInEntity) + return true; + + if (Properties.Contains(propertyName)) + { + return Properties[propertyName].IsDirty(); + } + + return false; + } + + /// + /// Indicates whether a specific property on the current entity was changed and the changes were committed + /// + /// Name of the property to check + /// + /// True if any of the class properties are dirty or + /// True if any of the user defined PropertyType properties are dirty based on their alias, + /// otherwise False + /// + public override bool WasPropertyDirty(string propertyName) + { + bool existsInEntity = base.WasPropertyDirty(propertyName); + if (existsInEntity) + return true; + + if (Properties.Contains(propertyName)) + { + return Properties[propertyName].WasDirty(); + } + + return false; + } + + #endregion } } \ No newline at end of file diff --git a/src/Umbraco.Core/Models/ContentExtensions.cs b/src/Umbraco.Core/Models/ContentExtensions.cs index 3c6a892900..62c32546b3 100644 --- a/src/Umbraco.Core/Models/ContentExtensions.cs +++ b/src/Umbraco.Core/Models/ContentExtensions.cs @@ -77,10 +77,9 @@ namespace Umbraco.Core.Models /// internal static bool RequiresSaving(this IContent entity, PublishedState publishedState) { - var dirtyEntity = (ICanBeDirty)entity; - var publishedChanged = dirtyEntity.IsPropertyDirty("Published") && publishedState != PublishedState.Unpublished; + var publishedChanged = entity.IsPropertyDirty("Published") && publishedState != PublishedState.Unpublished; //check if any user prop has changed - var propertyValueChanged = ((Content)entity).IsAnyUserPropertyDirty(); + var propertyValueChanged = entity.IsAnyUserPropertyDirty(); //We need to know if any other property apart from Published was changed here //don't create a new version if the published state has changed to 'Save' but no data has actually been changed @@ -114,6 +113,25 @@ namespace Umbraco.Core.Models return ShouldCreateNewVersion(entity, publishedState); } + /// + /// Returns a list of all dirty user defined properties + /// + /// + public static IEnumerable GetDirtyUserProperties(this IContentBase entity) + { + return entity.Properties.Where(x => x.IsDirty()).Select(x => x.Alias); + } + + public static bool IsAnyUserPropertyDirty(this IContentBase entity) + { + return entity.Properties.Any(x => x.IsDirty()); + } + + public static bool WasAnyUserPropertyDirty(this IContentBase entity) + { + return entity.Properties.Any(x => x.WasDirty()); + } + /// /// Determines if a new version should be created /// @@ -128,15 +146,13 @@ namespace Umbraco.Core.Models /// internal static bool ShouldCreateNewVersion(this IContent entity, PublishedState publishedState) { - var dirtyEntity = (ICanBeDirty)entity; - //check if the published state has changed or the language - var publishedChanged = dirtyEntity.IsPropertyDirty("Published") && publishedState != PublishedState.Unpublished; - var langChanged = dirtyEntity.IsPropertyDirty("Language"); + var publishedChanged = entity.IsPropertyDirty("Published") && publishedState != PublishedState.Unpublished; + var langChanged = entity.IsPropertyDirty("Language"); var contentChanged = publishedChanged || langChanged; //check if any user prop has changed - var propertyValueChanged = ((Content)entity).IsAnyUserPropertyDirty(); + var propertyValueChanged = entity.IsAnyUserPropertyDirty(); //return true if published or language has changed if (contentChanged) diff --git a/src/Umbraco.Core/Models/EntityBase/TracksChangesEntityBase.cs b/src/Umbraco.Core/Models/EntityBase/TracksChangesEntityBase.cs index cd5b762f84..0d5378d253 100644 --- a/src/Umbraco.Core/Models/EntityBase/TracksChangesEntityBase.cs +++ b/src/Umbraco.Core/Models/EntityBase/TracksChangesEntityBase.cs @@ -78,7 +78,7 @@ namespace Umbraco.Core.Models.EntityBase /// Indicates that the entity had been changed and the changes were committed /// /// - public bool WasDirty() + public virtual bool WasDirty() { return _lastPropertyChangedInfo != null && _lastPropertyChangedInfo.Any(); } diff --git a/src/Umbraco.Tests/Models/ContentTests.cs b/src/Umbraco.Tests/Models/ContentTests.cs index de4f95863e..72d1e31fec 100644 --- a/src/Umbraco.Tests/Models/ContentTests.cs +++ b/src/Umbraco.Tests/Models/ContentTests.cs @@ -712,9 +712,21 @@ namespace Umbraco.Tests.Models // Act content.SetPropertyValue("title", "new title"); + Assert.That(content.IsEntityDirty(), Is.False); + Assert.That(content.IsDirty(), Is.True); + Assert.That(content.IsPropertyDirty("title"), Is.True); + Assert.That(content.IsAnyUserPropertyDirty(), Is.True); + Assert.That(content.GetDirtyUserProperties().Count(), Is.EqualTo(1)); + Assert.That(content.Properties[0].IsDirty(), Is.True); + Assert.That(content.Properties["title"].IsDirty(), Is.True); + content.ResetDirtyProperties(); //this would be like committing the entity // Assert + Assert.That(content.WasDirty(), Is.True); + Assert.That(content.Properties[0].WasDirty(), Is.True); + + Assert.That(content.WasPropertyDirty("title"), Is.True); Assert.That(content.Properties["title"].IsDirty(), Is.False); Assert.That(content.Properties["title"].WasDirty(), Is.True);