diff --git a/src/Umbraco.Core/Models/ContentSchedule.cs b/src/Umbraco.Core/Models/ContentSchedule.cs index 0466a1e23a..cac4a0fd1c 100644 --- a/src/Umbraco.Core/Models/ContentSchedule.cs +++ b/src/Umbraco.Core/Models/ContentSchedule.cs @@ -13,7 +13,18 @@ namespace Umbraco.Core.Models /// /// Initializes a new instance of the class. /// - public ContentSchedule(int id, string culture, DateTime date, ContentScheduleAction action) + public ContentSchedule(string culture, DateTime date, ContentScheduleAction action) + { + Id = Guid.Empty; // will be assigned by document repository + Culture = culture; + Date = date; + Action = action; + } + + /// + /// Initializes a new instance of the class. + /// + public ContentSchedule(Guid id, string culture, DateTime date, ContentScheduleAction action) { Id = id; Culture = culture; @@ -25,7 +36,7 @@ namespace Umbraco.Core.Models /// Gets the unique identifier of the document targeted by the scheduled action. /// [DataMember] - public int Id { get; } + public Guid Id { get; internal set; } /// /// Gets the culture of the scheduled action. diff --git a/src/Umbraco.Core/Models/ContentScheduleCollection.cs b/src/Umbraco.Core/Models/ContentScheduleCollection.cs index 2d7206ff06..46813bdb45 100644 --- a/src/Umbraco.Core/Models/ContentScheduleCollection.cs +++ b/src/Umbraco.Core/Models/ContentScheduleCollection.cs @@ -75,14 +75,14 @@ namespace Umbraco.Core.Models if (releaseDate.HasValue) { - var entry = new ContentSchedule(0, culture, releaseDate.Value, ContentScheduleAction.Release); + var entry = new ContentSchedule(culture, releaseDate.Value, ContentScheduleAction.Release); changes.Add(releaseDate.Value, entry); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, entry)); } if (expireDate.HasValue) { - var entry = new ContentSchedule(0, culture, expireDate.Value, ContentScheduleAction.Expire); + var entry = new ContentSchedule(culture, expireDate.Value, ContentScheduleAction.Expire); changes.Add(expireDate.Value, entry); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, entry)); } diff --git a/src/Umbraco.Core/Persistence/Dtos/ContentScheduleDto.cs b/src/Umbraco.Core/Persistence/Dtos/ContentScheduleDto.cs index 9f77ac8d02..492a3d7cbd 100644 --- a/src/Umbraco.Core/Persistence/Dtos/ContentScheduleDto.cs +++ b/src/Umbraco.Core/Persistence/Dtos/ContentScheduleDto.cs @@ -5,15 +5,15 @@ using Umbraco.Core.Persistence.DatabaseAnnotations; namespace Umbraco.Core.Persistence.Dtos { [TableName(TableName)] - [PrimaryKey("id", AutoIncrement = true)] + [PrimaryKey("id", AutoIncrement = false)] [ExplicitColumns] internal class ContentScheduleDto { public const string TableName = Constants.DatabaseSchema.Tables.ContentSchedule; [Column("id")] - [PrimaryKeyColumn(AutoIncrement = true)] - public int Id { get; set; } + [PrimaryKeyColumn(AutoIncrement = false)] + public Guid Id { get; set; } [Column("nodeId")] [ForeignKey(typeof(ContentDto))] diff --git a/src/Umbraco.Core/Persistence/Factories/ContentBaseFactory.cs b/src/Umbraco.Core/Persistence/Factories/ContentBaseFactory.cs index c44244bd1b..7a59c5020e 100644 --- a/src/Umbraco.Core/Persistence/Factories/ContentBaseFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/ContentBaseFactory.cs @@ -173,17 +173,17 @@ namespace Umbraco.Core.Persistence.Factories return dto; } - public static IEnumerable BuildScheduleDto(IContent entity, ILanguageRepository languageRepository) + public static IEnumerable<(ContentSchedule Model, ContentScheduleDto Dto)> BuildScheduleDto(IContent entity, ILanguageRepository languageRepository) { return entity.ContentSchedule.FullSchedule.Select(x => - new ContentScheduleDto + (x, new ContentScheduleDto { Action = x.Action.ToString(), Date = x.Date, NodeId = entity.Id, LanguageId = languageRepository.GetIdByIsoCode(x.Culture, false), Id = x.Id - }); + })); } /// diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs index 88adbc346a..35496aaba7 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs @@ -363,7 +363,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement // persist the document dto // at that point, when publishing, the entity still has its old Published value - // so we need to explicitely update the dto to persist the correct value + // so we need to explicitly update the dto to persist the correct value if (content.PublishedState == PublishedState.Publishing) dto.Published = true; dto.NodeId = nodeDto.NodeId; @@ -371,12 +371,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement Database.Insert(dto); //insert the schedule - var scheduleDtos = ContentBaseFactory.BuildScheduleDto(content, LanguageRepository); - foreach (var scheduleDto in scheduleDtos) - { - scheduleDto.NodeId = nodeDto.NodeId; - Database.Insert(scheduleDto); - } + PersistContentSchedule(content, false); // persist the variations if (content.ContentType.VariesByCulture()) @@ -588,7 +583,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement // update the document dto // at that point, when un/publishing, the entity still has its old Published value - // so we need to explicitely update the dto to persist the correct value + // so we need to explicitly update the dto to persist the correct value if (content.PublishedState == PublishedState.Publishing) dto.Published = true; else if (content.PublishedState == PublishedState.Unpublishing) @@ -596,30 +591,12 @@ namespace Umbraco.Core.Persistence.Repositories.Implement content.Edited = dto.Edited = !dto.Published || edited; // if not published, always edited Database.Update(dto); + //update the schedule if (content.IsPropertyDirty("ContentSchedule")) - { - //update the schedule, get the existing one so we know what to update - var scheduleDtos = ContentBaseFactory.BuildScheduleDto(content, LanguageRepository).ToList(); - - //remove any that no longer exist - var ids = scheduleDtos.Where(x => x.Id > 0).Select(x => x.Id).Distinct(); - Database.Execute(Sql() - .Delete() - .Where(x => x.NodeId == content.Id) - .WhereNotIn(x => x.Id, ids)); - - //add/update the rest - foreach (var scheduleDto in scheduleDtos) - { - if (scheduleDto.Id == 0) - Database.Insert(scheduleDto); - else - Database.Update(scheduleDto); - } - } + PersistContentSchedule(content, true); // if entity is publishing, update tags, else leave tags there - // means that implicitely unpublished, or trashed, entities *still* have tags in db + // means that implicitly unpublished, or trashed, entities *still* have tags in db if (content.PublishedState == PublishedState.Publishing) SetEntityTags(entity, _tagRepository); @@ -649,8 +626,8 @@ namespace Umbraco.Core.Persistence.Repositories.Implement ClearEntityTags(entity, _tagRepository); } - // note re. tags: explicitely unpublished entities have cleared tags, - // but masked or trashed entitites *still* have tags in the db fixme so what? + // note re. tags: explicitly unpublished entities have cleared tags, + // but masked or trashed entities *still* have tags in the db fixme so what? entity.ResetDirtyProperties(); @@ -667,6 +644,35 @@ namespace Umbraco.Core.Persistence.Repositories.Implement //} } + private void PersistContentSchedule(IContent content, bool update) + { + var schedules = ContentBaseFactory.BuildScheduleDto(content, LanguageRepository).ToList(); + + //remove any that no longer exist + if (update) + { + var ids = schedules.Where(x => x.Model.Id != Guid.Empty).Select(x => x.Model.Id).Distinct(); + Database.Execute(Sql() + .Delete() + .Where(x => x.NodeId == content.Id) + .WhereNotIn(x => x.Id, ids)); + } + + //add/update the rest + foreach (var schedule in schedules) + { + if (schedule.Model.Id == Guid.Empty) + { + schedule.Model.Id = schedule.Dto.Id = Guid.NewGuid(); + Database.Insert(schedule.Dto); + } + else + { + Database.Update(schedule.Dto); + } + } + } + protected override void PersistDeletedItem(IContent entity) { // raise event first else potential FK issues