Skip to content

Commit 658368c

Browse files
Merge pull request #47 from OP-TED/bugfix/TEDEFO-4939-notice-subtype-sort-order
TEDEFO-4939 Fix notice subtype ordering by parsing IDs into prefix/number/suffix
2 parents e06d095 + 69d7c5d commit 658368c

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/main/java/eu/europa/ted/eforms/sdk/entity/SdkNoticeSubtype.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
11
package eu.europa.ted.eforms.sdk.entity;
22

33
import java.util.Objects;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
6+
47
import com.fasterxml.jackson.databind.JsonNode;
58

69
/**
710
* Represents a notice subtype from the SDK's notice-types.json file.
811
*/
912
public abstract class SdkNoticeSubtype implements Comparable<SdkNoticeSubtype> {
13+
private static final Pattern ID_PATTERN = Pattern.compile("^([A-Za-z_-]*)(\\d+)([A-Za-z_-][A-Za-z0-9_-]*)?$");
14+
1015
private final String subTypeId;
1116
private final String documentType;
1217
private final String type;
18+
private final String prefix;
19+
private final int number;
20+
private final String suffix;
1321

1422
protected SdkNoticeSubtype(String subTypeId, String documentType, String type) {
1523
this.subTypeId = subTypeId;
1624
this.documentType = documentType;
1725
this.type = type;
26+
27+
Matcher m = ID_PATTERN.matcher(subTypeId != null ? subTypeId : "");
28+
if (m.matches()) {
29+
this.prefix = m.group(1);
30+
this.number = Integer.parseInt(m.group(2));
31+
this.suffix = m.group(3) != null ? m.group(3) : "";
32+
} else {
33+
this.prefix = subTypeId != null ? subTypeId : "";
34+
this.number = 0;
35+
this.suffix = "";
36+
}
1837
}
1938

2039
protected SdkNoticeSubtype(JsonNode json) {
21-
this.subTypeId = json.get("subTypeId").asText(null);
22-
this.documentType = json.get("documentType").asText(null);
23-
this.type = json.get("type").asText(null);
40+
this(json.get("subTypeId").asText(null),
41+
json.get("documentType").asText(null),
42+
json.get("type").asText(null));
2443
}
2544

2645
/**
@@ -60,7 +79,15 @@ public boolean equals(Object obj) {
6079

6180
@Override
6281
public int compareTo(SdkNoticeSubtype o) {
63-
return this.subTypeId.compareTo(o.subTypeId);
82+
int cmp = this.prefix.compareTo(o.prefix);
83+
if (cmp != 0) {
84+
return cmp;
85+
}
86+
cmp = Integer.compare(this.number, o.number);
87+
if (cmp != 0) {
88+
return cmp;
89+
}
90+
return this.suffix.compareTo(o.suffix);
6491
}
6592

6693
@Override

0 commit comments

Comments
 (0)