Fixing PublishLater/CreatedUtc behaviors

This commit is contained in:
Sebastien Ros
2014-09-16 17:03:04 -07:00
parent d89e36d78d
commit 21b5a1b2ac
10 changed files with 99 additions and 8 deletions

View File

@@ -2,6 +2,7 @@
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Core.Common.Models;
using Orchard.Core.Common.Utilities;
using Orchard.Core.Common.ViewModels;
using Orchard.Localization;
using Orchard.Localization.Services;
@@ -51,7 +52,11 @@ namespace Orchard.Core.Common.DateEditor {
var itemHasNeverBeenPublished = part.PublishedUtc == null;
var thisIsTheInitialVersionRecord = part.ContentItem.Version < 2;
var theDatesHaveNotBeenModified = part.CreatedUtc == part.VersionCreatedUtc;
// Dates are assumed the same if the millisecond part is the only difference.
// This is because SqlCe doesn't support high precision times (Datetime2) and the infoset does
// implying some discrepancies between values read from different storage mechanism.
var theDatesHaveNotBeenModified = DateUtils.DatesAreEquivalent(part.CreatedUtc, part.VersionCreatedUtc);
var theEditorShouldBeBlank =
itemHasNeverBeenPublished &&
@@ -71,7 +76,6 @@ namespace Orchard.Core.Common.DateEditor {
try {
var utcDateTime = _dateServices.ConvertFromLocalString(model.Editor.Date, model.Editor.Time);
part.CreatedUtc = utcDateTime;
part.VersionCreatedUtc = utcDateTime;
}
catch (FormatException) {
updater.AddModelError(Prefix, T("'{0} {1}' could not be parsed as a valid date and time.", model.Editor.Date, model.Editor.Time));
@@ -87,5 +91,6 @@ namespace Orchard.Core.Common.DateEditor {
return model;
});
}
}
}

View File

@@ -1,6 +1,7 @@
using JetBrains.Annotations;
using Orchard.Core.Common.Models;
using Orchard.ContentManagement.Handlers;
using Orchard.Core.Common.Utilities;
namespace Orchard.Core.Common.DateEditor {
[UsedImplicitly]
@@ -13,7 +14,7 @@ namespace Orchard.Core.Common.DateEditor {
}
var thisIsTheInitialVersionRecord = part.ContentItem.Version < 2;
var theDatesHaveNotBeenModified = part.CreatedUtc == part.VersionCreatedUtc;
var theDatesHaveNotBeenModified = DateUtils.DatesAreEquivalent(part.CreatedUtc, part.VersionCreatedUtc);
var theContentDateShouldBeUpdated = thisIsTheInitialVersionRecord && theDatesHaveNotBeenModified;
if (theContentDateShouldBeUpdated) {

View File

@@ -49,11 +49,13 @@ namespace Orchard.Core.Common.Handlers {
OnPublishing<CommonPart>(AssignPublishingDates);
OnIndexing<CommonPart>((context, commonPart) => {
var utcNow = _clock.UtcNow;
context.DocumentIndex
.Add("type", commonPart.ContentItem.ContentType).Store()
.Add("created", commonPart.CreatedUtc ?? _clock.UtcNow).Store()
.Add("published", commonPart.PublishedUtc ?? _clock.UtcNow).Store()
.Add("modified", commonPart.ModifiedUtc ?? _clock.UtcNow).Store();
.Add("created", commonPart.CreatedUtc ?? utcNow).Store()
.Add("published", commonPart.PublishedUtc ?? utcNow).Store()
.Add("modified", commonPart.ModifiedUtc ?? utcNow).Store();
if (commonPart.Container != null) {
context.DocumentIndex.Add("container-id", commonPart.Container.Id).Store();
@@ -93,6 +95,7 @@ namespace Orchard.Core.Common.Handlers {
protected void AssignCreatingDates(InitializingContentContext context, CommonPart part) {
// assign default create/modified dates
var utcNow = _clock.UtcNow;
part.CreatedUtc = utcNow;
part.ModifiedUtc = utcNow;
part.VersionCreatedUtc = utcNow;
@@ -101,6 +104,7 @@ namespace Orchard.Core.Common.Handlers {
private void AssignUpdateDates(UpdateEditorContext context, CommonPart part) {
var utcNow = _clock.UtcNow;
part.ModifiedUtc = utcNow;
part.VersionModifiedUtc = utcNow;
}
@@ -116,16 +120,17 @@ namespace Orchard.Core.Common.Handlers {
building.VersionPublishedUtc = null;
// assign the created
building.CreatedUtc = existing.CreatedUtc ?? _clock.UtcNow;
building.CreatedUtc = existing.CreatedUtc ?? utcNow;
// persist any published dates
building.PublishedUtc = existing.PublishedUtc;
// assign modified date for the new version
building.ModifiedUtc = _clock.UtcNow;
building.ModifiedUtc = utcNow;
}
protected void AssignPublishingDates(PublishContentContext context, CommonPart part) {
var utcNow = _clock.UtcNow;
part.PublishedUtc = utcNow;
part.VersionPublishedUtc = utcNow;
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Orchard.Core.Common.Utilities {
public class DateUtils {
/// <summary>
/// Compares two <see cref="DateTime" /> instance without their milliseconds portion.
/// </summary>
/// <param name="a">The first <see cref="DateTime" /> to compare.</param>
/// <param name="b">The second <see cref="DateTime" /> to compare.</param>
/// <returns><c>True</c> if the two instances are in the same second, <c>False</c> otherwise.</returns>
public static bool DatesAreEquivalent(DateTime a, DateTime b) {
a = a.ToUniversalTime();
b = b.ToUniversalTime();
return
new DateTime(a.Year, a.Month, a.Day, a.Hour, a.Minute, a.Second) ==
new DateTime(b.Year, b.Month, b.Day, b.Hour, b.Minute, b.Second);
}
/// <summary>
/// Compares two <see cref="DateTime?" /> instance without their milliseconds portion.
/// </summary>
/// <param name="a">The first <see cref="DateTime?" /> to compare.</param>
/// <param name="b">The second <see cref="DateTime?" /> to compare.</param>
/// <returns><c>True</c> if the two instances are in the same second, <c>False</c> otherwise.</returns>
public static bool DatesAreEquivalent(DateTime? a, DateTime? b) {
if (!a.HasValue && !b.HasValue) {
return true;
}
if (a.HasValue != b.HasValue) {
return false;
}
return DatesAreEquivalent(a.Value, b.Value);
}
}
}

View File

@@ -92,6 +92,7 @@
<Compile Include="Common\DateEditor\DateEditorViewModel.cs" />
<Compile Include="Common\Settings\TextFieldSettingsEvents.cs" />
<Compile Include="Common\Settings\TextFieldSettings.cs" />
<Compile Include="Common\Utilities\DateUtils.cs" />
<Compile Include="Common\ViewModels\DateTimeEditor.cs" />
<Compile Include="Common\ViewModels\TextFieldDriverViewModel.cs" />
<Compile Include="Common\ViewModels\TextFieldSettingsEventsViewModel.cs" />

View File

@@ -27,6 +27,8 @@
<fieldset class="publish-later-datetime">
<legend>@T("Publish")</legend>
@Html.HiddenFor(m => m.Editor.ShowDate)
@Html.HiddenFor(m => m.Editor.ShowTime)
@Html.EditorFor(m => m.Editor)
<button type="submit" name="submit.Save" value="submit.PublishLater">@T("Publish Later")</button>
</fieldset>

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.AcceptanceCriteria;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions.Instances;
using NHibernate.Type;
namespace Orchard.Data.Conventions {
public class UtcDateTimeConvention : IPropertyConvention, IPropertyConventionAcceptance {
public void Apply(IPropertyInstance instance) {
instance.CustomType<UtcDateTimeType>();
}
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria) {
criteria.Expect(x =>
x.Property.Name.EndsWith("Utc", StringComparison.OrdinalIgnoreCase) && (
x.Property.PropertyType.Equals(typeof(DateTime)) ||
x.Property.PropertyType.Equals(typeof(DateTime?))
)
);
}
}
}

View File

@@ -91,6 +91,7 @@ namespace Orchard.Data.Providers {
.Conventions.Setup(x => x.Add(AutoImport.Never()))
.Conventions.Add(new RecordTableNameConvention(recordDescriptors))
.Conventions.Add(new CacheConventions(recordDescriptors))
.Conventions.Add(new UtcDateTimeConvention())
.Alterations(alt => {
foreach (var recordAssembly in recordDescriptors.Select(x => x.Type.Assembly).Distinct()) {
alt.Add(new AutoMappingOverrideAlteration(recordAssembly));

View File

@@ -142,6 +142,10 @@ namespace Orchard.Data {
var pathName = GetPathName(_shellSettings.Name);
hash.AddString(_appDataFolder.MapPath(pathName).ToLowerInvariant());
// Orchard version, to rebuild the mappings for each new version
var orchardVersion = new System.Reflection.AssemblyName(typeof(Orchard.ContentManagement.ContentItem).Assembly.FullName).Version.ToString();
hash.AddString(orchardVersion);
// Shell settings data
hash.AddString(_shellSettings.DataProvider);
hash.AddString(_shellSettings.DataTablePrefix);

View File

@@ -175,6 +175,7 @@
<Compile Include="ContentManagement\Utilities\ComputedField.cs" />
<Compile Include="Data\AbstractSessionInterceptor.cs" />
<Compile Include="Data\Bags\SArray.cs" />
<Compile Include="Data\Conventions\UtcDateTimeConvention.cs" />
<Compile Include="Data\FetchRequest.cs" />
<Compile Include="Data\Migration\Interpreters\MySqlCommandInterpreter.cs" />
<Compile Include="Data\Providers\MySqlStatementProvider.cs" />