Skip to content

Commit 49321f3

Browse files
committed
make measurement prefix more robust
1 parent ef174f8 commit 49321f3

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Diff for: src/main/groovy/life/qbic/registration/MeasurementID.groovy

+11-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import life.qbic.registration.types.QDatasetType
55

66
import java.util.regex.Matcher
77
import java.util.regex.Pattern
8+
import java.util.stream.Collectors
89

910
/**
1011
* A measurement ID references an openBIS object code used for measurements
@@ -16,8 +17,10 @@ class MeasurementID {
1617

1718
/**
1819
* <p>The regular expression of a valid QBiC measurement code.</p>
20+
* Prefixes are checked later
21+
* Z and Y are not allowed, as barcode scanners can switch these depending on country settings.
1922
*/
20-
static final Pattern QBIC_MEASUREMENT_CODE_SCHEMA = ~/N*[GM]SQ[A-X0-9]{4}[0-9]{3}[A-X0-9]{2}-[0-9]{14}/
23+
static final Pattern QBIC_MEASUREMENT_CODE_SCHEMA = ~/[A-X]*Q[A-X0-9]{4}[0-9]{3}[A-X0-9]{2}-[0-9]{14}/
2124
/**
2225
* <p>Holds the project code, which is always part of the object code.</p>
2326
*/
@@ -81,13 +84,17 @@ class MeasurementID {
8184
String withoutPrefix = sampleId.replace(prefix, '')
8285
this.projectCode = withoutPrefix[0..4]
8386
this.runningNumber = Integer.parseInt(withoutPrefix[5..7])
84-
this.runningDigit = withoutPrefix[8]
85-
this.checksum = withoutPrefix[9]
87+
this.runningDigit = withoutPrefix[8] as char
88+
this.checksum = withoutPrefix[9] as char
8689
this.suffix = Long.parseLong(withoutPrefix[11..-1])
8790
}
8891

8992
private void findMeasurementType(String id) {
90-
for(String prefix : QDatasetType.BY_PREFIX.keySet()) {
93+
// we try to find the longest matching prefix, in case their names overlap
94+
List<String> prefixesByLength = QDatasetType.BY_PREFIX.keySet().stream()
95+
.sorted(Comparator.comparingInt(String::length).reversed())
96+
.collect(Collectors.toList());
97+
for(String prefix : prefixesByLength) {
9198
if(id.startsWith(prefix)) {
9299
this.prefix = prefix
93100
this.datasetType = QDatasetType.BY_PREFIX.get(prefix)

0 commit comments

Comments
 (0)