mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Fixed #4905: Taxonomy term counter does not decrease.
I think the problem is that the TermsPartHandler triggers the TermCountProcessor as a Task async and the TermCountProcessor used the ContentTypeId as a parameter. If a ContentItem gets deleted, the TermCountProcessor cannot access it anymore and the Terms won't getting reprocessed which results in a wrong count. I altered the ITermCountProcessor interface to accept TermPartRecordId's instead of the ContentTypeId and use this id's directly. I also had to add a call for the ITermCountProcessor in the TermService UpdateTerm method. This is because when you re-publish a ContentItem only the new Terms are available in the PublishedEvent - but also terms from the previous version needed to be processed.
This commit is contained in:
@@ -91,7 +91,8 @@ namespace Orchard.Taxonomies.Handlers {
|
|||||||
|
|
||||||
// Fires off a processing engine task to run the count processing after the request so it's non-blocking.
|
// Fires off a processing engine task to run the count processing after the request so it's non-blocking.
|
||||||
private void RecalculateCount(IProcessingEngine processingEngine, ShellSettings shellSettings, IShellDescriptorManager shellDescriptorManager, TermsPart part) {
|
private void RecalculateCount(IProcessingEngine processingEngine, ShellSettings shellSettings, IShellDescriptorManager shellDescriptorManager, TermsPart part) {
|
||||||
processingEngine.AddTask(shellSettings, shellDescriptorManager.GetShellDescriptor(), "ITermCountProcessor.Process", new Dictionary<string, object> { { "termsPartId", part.ContentItem.Id } });
|
var termPartRecordIds = part.Terms.Select(t => t.TermRecord.Id).ToArray();
|
||||||
|
processingEngine.AddTask(shellSettings, shellDescriptorManager.GetShellDescriptor(), "ITermCountProcessor.Process", new Dictionary<string, object> { { "termPartRecordIds", termPartRecordIds } });
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -7,6 +7,6 @@ using Orchard.Events;
|
|||||||
|
|
||||||
namespace Orchard.Taxonomies.Services {
|
namespace Orchard.Taxonomies.Services {
|
||||||
public interface ITermCountProcessor : IEventHandler {
|
public interface ITermCountProcessor : IEventHandler {
|
||||||
void Process(int termsPartId);
|
void Process(params int[] termPartRecordIds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -9,6 +9,9 @@ using Orchard.ContentManagement.MetaData;
|
|||||||
using Orchard.Core.Common.Models;
|
using Orchard.Core.Common.Models;
|
||||||
using Orchard.Core.Title.Models;
|
using Orchard.Core.Title.Models;
|
||||||
using Orchard.Data;
|
using Orchard.Data;
|
||||||
|
using Orchard.Environment.Configuration;
|
||||||
|
using Orchard.Environment.Descriptor;
|
||||||
|
using Orchard.Environment.State;
|
||||||
using Orchard.Localization;
|
using Orchard.Localization;
|
||||||
using Orchard.Logging;
|
using Orchard.Logging;
|
||||||
using Orchard.Security;
|
using Orchard.Security;
|
||||||
@@ -23,6 +26,10 @@ namespace Orchard.Taxonomies.Services {
|
|||||||
private readonly IAuthorizationService _authorizationService;
|
private readonly IAuthorizationService _authorizationService;
|
||||||
private readonly IContentDefinitionManager _contentDefinitionManager;
|
private readonly IContentDefinitionManager _contentDefinitionManager;
|
||||||
private readonly IOrchardServices _services;
|
private readonly IOrchardServices _services;
|
||||||
|
private readonly IProcessingEngine _processingEngine;
|
||||||
|
private readonly ShellSettings _shellSettings;
|
||||||
|
private readonly IShellDescriptorManager _shellDescriptorManager;
|
||||||
|
|
||||||
|
|
||||||
public TaxonomyService(
|
public TaxonomyService(
|
||||||
IRepository<TermContentItem> termContentItemRepository,
|
IRepository<TermContentItem> termContentItemRepository,
|
||||||
@@ -30,13 +37,20 @@ namespace Orchard.Taxonomies.Services {
|
|||||||
INotifier notifier,
|
INotifier notifier,
|
||||||
IContentDefinitionManager contentDefinitionManager,
|
IContentDefinitionManager contentDefinitionManager,
|
||||||
IAuthorizationService authorizationService,
|
IAuthorizationService authorizationService,
|
||||||
IOrchardServices services) {
|
IOrchardServices services,
|
||||||
|
IProcessingEngine processingEngine,
|
||||||
|
ShellSettings shellSettings,
|
||||||
|
IShellDescriptorManager shellDescriptorManager)
|
||||||
|
{
|
||||||
_termContentItemRepository = termContentItemRepository;
|
_termContentItemRepository = termContentItemRepository;
|
||||||
_contentManager = contentManager;
|
_contentManager = contentManager;
|
||||||
_notifier = notifier;
|
_notifier = notifier;
|
||||||
_authorizationService = authorizationService;
|
_authorizationService = authorizationService;
|
||||||
_contentDefinitionManager = contentDefinitionManager;
|
_contentDefinitionManager = contentDefinitionManager;
|
||||||
_services = services;
|
_services = services;
|
||||||
|
_processingEngine = processingEngine;
|
||||||
|
_shellSettings = shellSettings;
|
||||||
|
_shellDescriptorManager = shellDescriptorManager;
|
||||||
|
|
||||||
Logger = NullLogger.Instance;
|
Logger = NullLogger.Instance;
|
||||||
T = NullLocalizer.Instance;
|
T = NullLocalizer.Instance;
|
||||||
@@ -208,24 +222,31 @@ namespace Orchard.Taxonomies.Services {
|
|||||||
var termsPart = contentItem.As<TermsPart>();
|
var termsPart = contentItem.As<TermsPart>();
|
||||||
|
|
||||||
// removing current terms for specific field
|
// removing current terms for specific field
|
||||||
var fieldIndexes = termsPart.Terms.Select((t, i) => new {Term = t, Index = i})
|
var termList = termsPart.Terms.Select((t, i) => new {Term = t, Index = i})
|
||||||
.Where(x => x.Term.Field == field)
|
.Where(x => x.Term.Field == field)
|
||||||
.Select(x => x.Index)
|
.Select(x => x)
|
||||||
.OrderByDescending(i => i)
|
.OrderByDescending(i => i.Index)
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
foreach(var x in fieldIndexes) {
|
foreach (var x in termList) {
|
||||||
termsPart.Terms.RemoveAt(x);
|
termsPart.Terms.RemoveAt(x.Index);
|
||||||
}
|
}
|
||||||
|
|
||||||
// adding new terms list
|
// adding new terms list
|
||||||
foreach(var term in terms) {
|
foreach(var term in terms) {
|
||||||
|
// Remove the newly added terms because they will get processed by the Published-Event
|
||||||
|
termList.RemoveAll(t => t.Term.Id == term.Id);
|
||||||
termsPart.Terms.Add(
|
termsPart.Terms.Add(
|
||||||
new TermContentItem {
|
new TermContentItem {
|
||||||
TermsPartRecord = termsPart.Record,
|
TermsPartRecord = termsPart.Record,
|
||||||
TermRecord = term.Record, Field = field
|
TermRecord = term.Record, Field = field
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var termPartRecordIds = termList.Select(t => t.Term.TermRecord.Id).ToArray();
|
||||||
|
_processingEngine.AddTask(_shellSettings, _shellDescriptorManager.GetShellDescriptor(), "ITermCountProcessor.Process", new Dictionary<string, object> { { "termPartRecordIds", termPartRecordIds } });
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IContentQuery<TermsPart, TermsPartRecord> GetContentItemsQuery(TermPart term, string fieldName = null) {
|
public IContentQuery<TermsPart, TermsPartRecord> GetContentItemsQuery(TermPart term, string fieldName = null) {
|
||||||
|
@@ -15,16 +15,10 @@ namespace Orchard.Taxonomies.Services {
|
|||||||
_taxonomyService = taxonomyService;
|
_taxonomyService = taxonomyService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Process(int termsPartId) {
|
public void Process(params int[] termPartRecordIds) {
|
||||||
var termsPart = _contentManager.Get<TermsPart>(termsPartId);
|
|
||||||
|
|
||||||
if (termsPart == null) {
|
foreach (var id in termPartRecordIds) {
|
||||||
return;
|
var termPart = _taxonomyService.GetTerm(id);
|
||||||
}
|
|
||||||
|
|
||||||
// Retrieve the number of associated content items, for the whole hierarchy
|
|
||||||
foreach (var term in termsPart.Terms) {
|
|
||||||
var termPart = _taxonomyService.GetTerm(term.TermRecord.Id);
|
|
||||||
while (termPart != null) {
|
while (termPart != null) {
|
||||||
termPart.Count = (int)_taxonomyService.GetContentItemsCount(termPart);
|
termPart.Count = (int)_taxonomyService.GetContentItemsCount(termPart);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user