Changes Sort to order by FullWeight and move old code to an obsolete method. Adds documentation to the term Weight and FullWeight. (#7926)

This commit is contained in:
Julián Alazorza
2018-01-04 21:23:43 +01:00
committed by Sébastien Ros
parent b396d40281
commit 481eee3561
2 changed files with 17 additions and 2 deletions

View File

@@ -46,11 +46,21 @@ namespace Orchard.Taxonomies.Models {
set { Store(x => x.Selectable, value); } set { Store(x => x.Selectable, value); }
} }
/// <summary>
/// Property used to sort terms that have the same level or path
/// </summary>
public int Weight { public int Weight {
get { return Retrieve(x => x.Weight); } get { return Retrieve(x => x.Weight); }
set { Store(x => x.Weight, value); } set { Store(x => x.Weight, value); }
} }
/// <summary>
/// This property is used to represent the lexicographic order of the term inside the taxonomy.
/// The term FullWeight is composed by his parent FullWeight and the lexicographic representation of the own term separated with a slash '/'.
/// The term is represented at the end of his FullWeight using the term Weight padded with zeros and the term Id separated with a point '.'.
/// e.g: Term1=> Id:144, Path:"/", Weight:-1, FullWeight: "-0000001.144/"
/// e.g: Term2=> Id:154, Path:"/144/", Weight:-1, FullWeight: "-0000001.144/-0000001.154/"
/// </summary>
public string FullWeight { public string FullWeight {
get { return Record.FullWeight; } get { return Record.FullWeight; }
set { Record.FullWeight = value; } set { Record.FullWeight = value; }
@@ -59,6 +69,11 @@ namespace Orchard.Taxonomies.Models {
public string FullPath { get { return String.Concat(Path, Id); } } public string FullPath { get { return String.Concat(Path, Id); } }
public static IEnumerable<TermPart> Sort(IEnumerable<TermPart> terms) { public static IEnumerable<TermPart> Sort(IEnumerable<TermPart> terms) {
return terms.OrderBy(term => term.FullWeight);
}
[Obsolete]
public static IEnumerable<TermPart> SortObsolete(IEnumerable<TermPart> terms) {
var list = terms.ToList(); var list = terms.ToList();
var index = list.ToDictionary(x => x.FullPath); var index = list.ToDictionary(x => x.FullPath);
return list.OrderBy(x => x, new TermsComparer(index)); return list.OrderBy(x => x, new TermsComparer(index));
@@ -74,7 +89,7 @@ namespace Orchard.Taxonomies.Models {
public int Compare(TermPart x, TermPart y) { public int Compare(TermPart x, TermPart y) {
// if two nodes have the same parent, then compare by weight, then by path // if two nodes have the same parent, then compare by weight, then by path
// /1/2/3 vs /1/2/4 => 3 vs 4 // /1/2/3 vs /1/2/4 => 3 vs 4
if (x.Path == y.Path) { if (x.Path == y.Path) {
var weight = y.Weight.CompareTo(x.Weight); var weight = y.Weight.CompareTo(x.Weight);

View File

@@ -83,7 +83,7 @@ namespace Upgrade.Controllers {
public JsonResult MigrateTerms(int id) { public JsonResult MigrateTerms(int id) {
var lastContentItemId = id; var lastContentItemId = id;
foreach (var taxonomy in _taxonomyService.GetTaxonomies()) { foreach (var taxonomy in _taxonomyService.GetTaxonomies()) {
foreach (var term in TermPart.Sort(_taxonomyService.GetTerms(taxonomy.Id))) { foreach (var term in TermPart.SortObsolete(_taxonomyService.GetTerms(taxonomy.Id))) {
term.FullWeight = ""; term.FullWeight = "";
var container = term.Container.As<TermPart>(); var container = term.Container.As<TermPart>();
for (int i = 0; i < term.Path.Count(x => x == '/')-1; i++) { for (int i = 0; i < term.Path.Count(x => x == '/')-1; i++) {