Skip to content

Commit

Permalink
UI+Persistence for history cleanup per doc type
Browse files Browse the repository at this point in the history
  • Loading branch information
bergmania committed Oct 28, 2021
1 parent ae20c49 commit b96ec5a
Show file tree
Hide file tree
Showing 17 changed files with 367 additions and 313 deletions.
16 changes: 16 additions & 0 deletions src/Umbraco.Core/Models/ContentEditing/HistoryCleanup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Runtime.Serialization;

namespace Umbraco.Core.Models.ContentEditing;

[DataContract(Name = "historyCleanup", Namespace = "")]
public class HistoryCleanup
{
[DataMember(Name = "preventCleanup")]
public bool PreventCleanup { get; set; }

[DataMember(Name = "keepAllVersionsNewerThanDays")]
public int? KeepAllVersionsNewerThanDays { get;set; }

[DataMember(Name = "keepLatestVersionPerDayForDays")]
public int? KeepLatestVersionPerDayForDays { get;set; }
}
5 changes: 5 additions & 0 deletions src/Umbraco.Core/Models/ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using Umbraco.Core.Models.ContentEditing;

namespace Umbraco.Core.Models
{
Expand Down Expand Up @@ -47,6 +48,8 @@ public ContentType(IContentType parent, string alias)
/// <inheritdoc />
public override bool SupportsPublishing => SupportsPublishingConst;



//Custom comparer for enumerable
private static readonly DelegateEqualityComparer<IEnumerable<ITemplate>> TemplateComparer = new DelegateEqualityComparer<IEnumerable<ITemplate>>(
(templates, enumerable) => templates.UnsortedSequenceEqual(enumerable),
Expand Down Expand Up @@ -93,6 +96,8 @@ public IEnumerable<ITemplate> AllowedTemplates
}
}

public HistoryCleanup HistoryCleanup { get; set; }

/// <summary>
/// Determines if AllowedTemplates contains templateId
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Umbraco.Core/Models/IContentType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Umbraco.Core.Models.ContentEditing;

namespace Umbraco.Core.Models
{
Expand All @@ -17,6 +18,10 @@ public interface IContentType : IContentTypeComposition
/// </summary>
IEnumerable<ITemplate> AllowedTemplates { get; set; }

/// <summary>
/// Gets or Sets the history cleanup configuration
/// </summary>
HistoryCleanup HistoryCleanup { get; set; }
/// <summary>
/// Determines if AllowedTemplates contains templateId
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Umbraco.Core.Cache;
using Umbraco.Core.Exceptions;
using Umbraco.Core.Models;
using Umbraco.Core.Models.ContentEditing;
using Umbraco.Core.Persistence.Dtos;
using Umbraco.Core.Persistence.Factories;
using Umbraco.Core.Scoping;
Expand Down Expand Up @@ -109,6 +110,7 @@ private IEnumerable<IContentTypeComposition> GetAllTypesInternal()
MapTemplates(contentTypes);
MapComposition(contentTypes);
MapGroupsAndProperties(contentTypes);
MapHistoryCleanup(contentTypes);

// finalize
foreach (var contentType in contentTypes.Values)
Expand All @@ -119,6 +121,35 @@ private IEnumerable<IContentTypeComposition> GetAllTypesInternal()
return contentTypes.Values;
}

private void MapHistoryCleanup(Dictionary<int, IContentTypeComposition> contentTypes)
{
// get templates
var sql1 = Sql()
.Select<ContentVersionCleanupPolicyDto>()
.From<ContentVersionCleanupPolicyDto>()
.OrderBy<ContentVersionCleanupPolicyDto>(x => x.ContentTypeId);

var contentVersionCleanupPolicyDtos = Database.Fetch<ContentVersionCleanupPolicyDto>(sql1);

var contentVersionCleanupPolicyDictionary =
contentVersionCleanupPolicyDtos.ToDictionary(x => x.ContentTypeId);
foreach (var c in contentTypes.Values)
{
if (!(c is ContentType contentType)) continue;

var historyCleanup = new HistoryCleanup();

if (contentVersionCleanupPolicyDictionary.TryGetValue(contentType.Id, out var versionCleanup))
{
historyCleanup.PreventCleanup = versionCleanup.PreventCleanup;
historyCleanup.KeepAllVersionsNewerThanDays = versionCleanup.KeepAllVersionsNewerThanDays;
historyCleanup.KeepLatestVersionPerDayForDays = versionCleanup.KeepLatestVersionPerDayForDays;
}

contentType.HistoryCleanup = historyCleanup;
}
}

private void MapTemplates(Dictionary<int, IContentTypeComposition> contentTypes)
{
// get templates
Expand All @@ -145,7 +176,7 @@ private void MapTemplates(Dictionary<int, IContentTypeComposition> contentTypes)
templateDtoIx++;
if (!templates.TryGetValue(allowedDto.TemplateNodeId, out var template)) continue;
allowedTemplates.Add(template);

if (allowedDto.IsDefault)
defaultTemplateId = template.Id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,22 @@ protected override void PersistUpdatedItem(IContentType entity)

PersistUpdatedBaseContentType(entity);
PersistTemplates(entity, true);
PersistHistoryCleanup(entity);

entity.ResetDirtyProperties();
}

private void PersistHistoryCleanup(IContentType entity)
{
ContentVersionCleanupPolicyDto dto = new ContentVersionCleanupPolicyDto()
{
ContentTypeId = entity.Id,
Updated = DateTime.Now,
PreventCleanup = entity.HistoryCleanup.PreventCleanup,
KeepAllVersionsNewerThanDays = entity.HistoryCleanup.KeepAllVersionsNewerThanDays,
KeepLatestVersionPerDayForDays = entity.HistoryCleanup.KeepLatestVersionPerDayForDays,
};
Database.InsertOrUpdate(dto);
}
}
}
1 change: 1 addition & 0 deletions src/Umbraco.Core/Umbraco.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
<Compile Include="Models\ContentDataIntegrityReport.cs" />
<Compile Include="Models\ContentDataIntegrityReportEntry.cs" />
<Compile Include="Models\ContentDataIntegrityReportOptions.cs" />
<Compile Include="Models\ContentEditing\HistoryCleanup.cs" />
<Compile Include="Models\ContentVersionCleanupPolicySettings.cs" />
<Compile Include="Models\Identity\ExternalLogin.cs" />
<Compile Include="Models\Identity\IExternalLogin.cs" />
Expand Down
Loading

0 comments on commit b96ec5a

Please sign in to comment.