Update TermCountProcessor.cs

Don't process the same term multiple times if it happens to be the parent of multiple terms on the list.
This commit is contained in:
mvarblow
2015-09-18 10:59:32 -04:00
parent d080037af9
commit c7fde5bc72

View File

@@ -1,3 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using Orchard.Taxonomies.Models;
@@ -9,20 +13,31 @@ namespace Orchard.Taxonomies.Services {
_taxonomyService = taxonomyService;
}
public void Process(params int[] termPartRecordIds) {
public void Process(IEnumerable<int> termPartRecordIds)
{
var processedTermPartRecordIds = new List<int>();
foreach (var id in termPartRecordIds) {
var termPart = _taxonomyService.GetTerm(id);
while (termPart != null) {
termPart.Count = (int)_taxonomyService.GetContentItemsCount(termPart);
// compute count for the hierarchy too
if (termPart.Container != null) {
var parentTerm = termPart.Container.As<TermPart>();
termPart = parentTerm;
if (!processedTermPartRecordIds.Contains(id)) {
var termPart = _taxonomyService.GetTerm(id);
if (termPart != null) {
ProcessTerm(termPart, processedTermPartRecordIds);
}
}
}
}
private void ProcessTerm(TermPart termPart, ICollection<int> processedTermPartRecordIds)
{
termPart.Count = (int)_taxonomyService.GetContentItemsCount(termPart);
processedTermPartRecordIds.Add(termPart.Id);
// Look for a parent term that has not yet been processed
if (termPart.Container != null) {
var parentTerm = termPart.Container.As<TermPart>();
if (parentTerm != null && !processedTermPartRecordIds.Contains(parentTerm.Id)) {
ProcessTerm(parentTerm, processedTermPartRecordIds);
}
}
}
}
}