|
| 1 | +package com.sdl.webapp.common.api.model.sorting; |
| 2 | + |
| 3 | +import com.google.common.collect.ComparisonChain; |
| 4 | +import com.sdl.dxa.api.datamodel.model.SitemapItemModelData; |
| 5 | +import com.sdl.webapp.common.api.model.entity.SitemapItem; |
| 6 | +import org.apache.commons.lang3.StringUtils; |
| 7 | +import org.springframework.util.comparator.NullSafeComparator; |
| 8 | + |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.Collections; |
| 11 | +import java.util.Comparator; |
| 12 | +import java.util.regex.Matcher; |
| 13 | +import java.util.regex.Pattern; |
| 14 | +import java.util.stream.Collectors; |
| 15 | + |
| 16 | +/** |
| 17 | + * A private class that contains the results of the regex so they only have to be done once for a whole sorting. |
| 18 | + */ |
| 19 | +public class SortableSiteMap { |
| 20 | + private static final Pattern TAXONOMY_ID_PATTERN = Pattern.compile("^(\\w?)(\\d+)-(\\w?)(\\d+)$"); |
| 21 | + |
| 22 | + public static final Comparator<SortableSiteMap> SORT_BY_TITLE_AND_ID = new NullSafeComparator<>(new Comparator<SortableSiteMap>() { |
| 23 | + @Override |
| 24 | + public int compare(SortableSiteMap o1, SortableSiteMap o2) { |
| 25 | + return ComparisonChain.start() |
| 26 | + .compareTrueFirst(o1 != null, o2 != null) |
| 27 | + .compare(o1.getOriginalTitle(), o2.getOriginalTitle()) |
| 28 | + .compare(o1.getId(), o2.getId()) |
| 29 | + .result(); |
| 30 | + } |
| 31 | + }, true); |
| 32 | + |
| 33 | + public static final Comparator<SortableSiteMap> SORT_BY_TAXONOMY_AND_KEYWORD = new NullSafeComparator<>(new Comparator<SortableSiteMap>() { |
| 34 | + @Override |
| 35 | + public int compare(SortableSiteMap o1, SortableSiteMap o2) { |
| 36 | + return ComparisonChain.start() |
| 37 | + .compareTrueFirst(o1 != null, o2 != null) |
| 38 | + .compare(o1.getFirstChar(), o2.getFirstChar()) |
| 39 | + .compare(o1.getFirstNumber(), o2.getFirstNumber()) |
| 40 | + .compare(o1.getSecondChar(), o2.getSecondChar()) |
| 41 | + .compare(o1.getSecondNumber(), o2.getSecondNumber()) |
| 42 | + .result(); |
| 43 | + } |
| 44 | + }, true); |
| 45 | + |
| 46 | + private Integer firstNumber = Integer.MIN_VALUE; |
| 47 | + private String firstChar = ""; |
| 48 | + private Integer secondNumber = Integer.MIN_VALUE; |
| 49 | + private String secondChar = ""; |
| 50 | + private String originalTitle = ""; |
| 51 | + private String id = ""; |
| 52 | + private SitemapItem sitemapItem; |
| 53 | + private SitemapItemModelData sitemapItemModelData; |
| 54 | + |
| 55 | + public SortableSiteMap(SitemapItemModelData sitemapItemModelData) { |
| 56 | + this.sitemapItemModelData = sitemapItemModelData; |
| 57 | + if (sitemapItemModelData == null) { |
| 58 | + return; |
| 59 | + } |
| 60 | + originalTitle = sitemapItemModelData.getOriginalTitle() == null ? "" : sitemapItemModelData.getOriginalTitle(); |
| 61 | + id = sitemapItemModelData.getId() == null ? "" : sitemapItemModelData.getId(); |
| 62 | + if (sitemapItemModelData.getId() == null) return; |
| 63 | + Matcher matcher = TAXONOMY_ID_PATTERN.matcher(sitemapItemModelData.getId()); |
| 64 | + if (!matcher.matches()) { |
| 65 | + return; |
| 66 | + } |
| 67 | + fillFromMatcher(matcher); |
| 68 | + } |
| 69 | + |
| 70 | + public SortableSiteMap(SitemapItem sitemapItem) { |
| 71 | + this.sitemapItem = sitemapItem; |
| 72 | + if (sitemapItem == null) { |
| 73 | + return; |
| 74 | + } |
| 75 | + originalTitle = sitemapItem.getOriginalTitle() == null ? "" : sitemapItem.getOriginalTitle(); |
| 76 | + id = sitemapItem.getId() == null ? "" : sitemapItem.getId(); |
| 77 | + if (sitemapItem.getId() == null) return; |
| 78 | + Matcher matcher = TAXONOMY_ID_PATTERN.matcher(sitemapItem.getId()); |
| 79 | + if (!matcher.matches()) { |
| 80 | + return; |
| 81 | + } |
| 82 | + fillFromMatcher(matcher); |
| 83 | + } |
| 84 | + |
| 85 | + private void fillFromMatcher(Matcher matcher) { |
| 86 | + String group2 = matcher.group(2); |
| 87 | + String group4 = matcher.group(4); |
| 88 | + if (StringUtils.isNotEmpty(group2)) { |
| 89 | + this.firstNumber = Integer.parseInt(group2); |
| 90 | + } |
| 91 | + if (StringUtils.isNotEmpty(group4)) { |
| 92 | + this.secondNumber = Integer.parseInt(group4); |
| 93 | + } |
| 94 | + this.firstChar = matcher.group(1); |
| 95 | + this.secondChar = matcher.group(3); |
| 96 | + } |
| 97 | + |
| 98 | + public Integer getFirstNumber() { |
| 99 | + return firstNumber; |
| 100 | + } |
| 101 | + |
| 102 | + public Integer getSecondNumber() { |
| 103 | + return secondNumber; |
| 104 | + } |
| 105 | + |
| 106 | + public String getFirstChar() { |
| 107 | + return firstChar; |
| 108 | + } |
| 109 | + |
| 110 | + public String getSecondChar() { |
| 111 | + return secondChar; |
| 112 | + } |
| 113 | + |
| 114 | + public SitemapItem getSitemapItem() { |
| 115 | + return sitemapItem; |
| 116 | + } |
| 117 | + |
| 118 | + public SitemapItemModelData getSitemapItemModelData() { |
| 119 | + return sitemapItemModelData; |
| 120 | + } |
| 121 | + |
| 122 | + public String getOriginalTitle() { |
| 123 | + return originalTitle; |
| 124 | + } |
| 125 | + |
| 126 | + public String getId() { |
| 127 | + return id; |
| 128 | + } |
| 129 | + |
| 130 | + public static Collection<SitemapItem> sortItem(Collection<SitemapItem> entries, Comparator<SortableSiteMap> comparator) { |
| 131 | + if (entries == null) return Collections.emptyList(); |
| 132 | + return entries |
| 133 | + .stream() |
| 134 | + .map(SortableSiteMap::new) |
| 135 | + .sorted(comparator) |
| 136 | + .map(SortableSiteMap::getSitemapItem).collect(Collectors.toList()); |
| 137 | + } |
| 138 | + |
| 139 | + public static Collection<SitemapItemModelData> sortModelData(Collection<SitemapItemModelData> entries, Comparator<SortableSiteMap> comparator) { |
| 140 | + if (entries == null) return Collections.emptyList(); |
| 141 | + return entries |
| 142 | + .stream() |
| 143 | + .map(SortableSiteMap::new) |
| 144 | + .sorted(comparator) |
| 145 | + .map(SortableSiteMap::getSitemapItemModelData).collect(Collectors.toList()); |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments