From 1959fdc111f9703aa7881f40c41dbc96f23bf34e Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Fri, 26 Jan 2024 15:14:45 +0100 Subject: [PATCH] Sort non-n6 `spatial` entries by ID, not by label (RPB-47) Except for n6 itself, which is the first entry on the top level --- app/controllers/nwbib/Classification.java | 29 +++++++++++++++-------- test/tests/ApplicationTest.java | 4 ++-- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/app/controllers/nwbib/Classification.java b/app/controllers/nwbib/Classification.java index 401bc1cc..7c808282 100644 --- a/app/controllers/nwbib/Classification.java +++ b/app/controllers/nwbib/Classification.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; +import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -117,7 +118,7 @@ public Pair, Map>> buildHierarchy() { || Play.isTest())) { /* SpatialToSkos uses Play test server */ addFromCsv(subClasses); } - Collections.sort(topClasses, comparator); + Collections.sort(topClasses, comparator(Classification::idText)); return Pair.of(topClasses, subClasses); } @@ -154,7 +155,7 @@ private static void addFromCsv( */ public JsonNode buildRegister() { final List result = ids(classificationData()).stream() - .sorted(comparator).collect(Collectors.toList()); + .sorted(comparator(Classification::labelText)).collect(Collectors.toList()); return Json.toJson(result); } @@ -188,14 +189,20 @@ private enum Label { private static Node node; /** Compare German strings */ - public static Comparator comparator = - (JsonNode o1, JsonNode o2) -> Collator.getInstance(Locale.GERMAN) - .compare(labelText(o1), labelText(o2)); + public static Comparator comparator(Function fun) { + return (JsonNode o1, JsonNode o2) -> Collator.getInstance(Locale.GERMAN) + .compare(fun.apply(o1), fun.apply(o2)); + } private Classification() { /* Use via static functions, no instantiation. */ } + private static String idText(JsonNode node) { + String[] segments = node.get("value").asText().split("#"); + return segments[segments.length-1].replaceAll("^n6$", "n0"); + } + /** * @param turtleUrl The URL of the RDF in TURTLE format * @return The input, converted to JSON-LD, or null @@ -293,7 +300,7 @@ static List ids(SearchResponse response) { return result; } - private static String labelText(JsonNode json) { + public static String labelText(JsonNode json) { String label = json.get("label").asText(); if (label.contains("Stadtbezirk")) { List> roman = Arrays.asList(Pair.of("I", "a"), @@ -346,7 +353,7 @@ public static Pair, Map>> buildHierarchyCs subClasses.put(broaderId, new ArrayList()); List sub = subClasses.get(broaderId); sub.add(Json.toJson(ImmutableMap.of("value", thisId, "label", vg + ", Verbandsgemeinde", "notation", thisNotation, "hits", hits))); - Collections.sort(sub, comparator); + Collections.sort(sub, comparator(Classification::labelText)); broader = thisNotation; } } @@ -359,13 +366,13 @@ public static Pair, Map>> buildHierarchyCs subClasses.put(broaderId, new ArrayList()); List sub = subClasses.get(broaderId); sub.add(Json.toJson(ImmutableMap.of("value", id, "label", label, "notation", notation, "hits", hits))); - Collections.sort(sub, comparator); + Collections.sort(sub, comparator(Classification::labelText)); } } } catch (IOException e) { e.printStackTrace(); } - Collections.sort(topClasses, comparator); + Collections.sort(topClasses, comparator(Classification::labelText)); return Pair.of(topClasses, removeDuplicates(subClasses)); } @@ -416,7 +423,9 @@ private static void addAsSubClass(Map> subClasses, subClasses.put(broader, new ArrayList()); List list = subClasses.get(broader); list.addAll(valueAndLabelWithNotation(hit, json)); - Collections.sort(list, comparator); + // non-n6 entries have 2-digit IDs or a second `n` -> sort by ID (RPB-47) + boolean sortById = idText(list.get(0)).matches("^n\\d{2}$|^n\\d+n\\d+$"); + Collections.sort(list, comparator(sortById ? Classification::idText : Classification::labelText)); } private static String focus(JsonNode json) { diff --git a/test/tests/ApplicationTest.java b/test/tests/ApplicationTest.java index f856044e..e961970a 100644 --- a/test/tests/ApplicationTest.java +++ b/test/tests/ApplicationTest.java @@ -115,7 +115,7 @@ public void sortArabicNumerals() { Json.newObject().put("label", "Stadtbezirk 8"), Json.newObject().put("label", "Stadtbezirk 9"), Json.newObject().put("label", "Stadtbezirk 10") }; - Arrays.sort(in, Classification.comparator); + Arrays.sort(in, Classification.comparator(Classification::labelText)); Assert.assertArrayEquals(correct, in); } @@ -143,7 +143,7 @@ public void sortRomanNumerals() { Json.newObject().put("label", "Stadtbezirk VIII"), Json.newObject().put("label", "Stadtbezirk IX"), Json.newObject().put("label", "Stadtbezirk X") }; - Arrays.sort(in, Classification.comparator); + Arrays.sort(in, Classification.comparator(Classification::labelText)); Assert.assertArrayEquals(correct, in); }