Skip to content

Commit

Permalink
Extending project code to avoid collisions with legacy QBiC project c…
Browse files Browse the repository at this point in the history
…odes (#643)

The legacy project codes are of length 5, so the data manager should not create codes that might be confused with legacy codes.

The most simple solution also discussed with project management is to extend the prefix from "Q" to "Q2".


---------

Co-authored-by: wow-such-code <[email protected]>
  • Loading branch information
sven1103 and wow-such-code authored Jun 24, 2024
1 parent 484b79a commit 502753b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SampleCodeJpaRepositorySpec extends Specification{
ProjectId projectId = ProjectId.create()

and:
SampleStatisticEntry sampleStatisticEntry = SampleStatisticEntry.create(projectId, ProjectCode.parse("QTEST"))
SampleStatisticEntry sampleStatisticEntry = SampleStatisticEntry.create(projectId, ProjectCode.parse("Q2TEST"))

and:
sampleStatistic.findByProjectId(projectId) >> [sampleStatisticEntry]
Expand All @@ -32,7 +32,7 @@ class SampleCodeJpaRepositorySpec extends Specification{

then:
result.isValue()
result.getValue().code().equals("QTEST001AL")
result.getValue().code().equals("Q2TEST001A5")
}

def "Given 998 sample statistic entry, generate the next available sample code with no letter jump and counter starting with 999"() {
Expand All @@ -43,7 +43,7 @@ class SampleCodeJpaRepositorySpec extends Specification{
ProjectId projectId = ProjectId.create()

and:
SampleStatisticEntry sampleStatisticEntry = SampleStatisticEntry.create(projectId, ProjectCode.parse("QTEST"))
SampleStatisticEntry sampleStatisticEntry = SampleStatisticEntry.create(projectId, ProjectCode.parse("Q2TEST"))
// Prime to 999 sample numbers that have been drawn already
for (int i = 1; i <= 998; i++) {
sampleStatisticEntry.drawNextSampleNumber()
Expand All @@ -60,7 +60,7 @@ class SampleCodeJpaRepositorySpec extends Specification{

then:
result.isValue()
result.getValue().code().equals("QTEST999AW")
result.getValue().code().equals("Q2TEST999A8")
}


Expand All @@ -72,7 +72,7 @@ class SampleCodeJpaRepositorySpec extends Specification{
ProjectId projectId = ProjectId.create()

and:
SampleStatisticEntry sampleStatisticEntry = SampleStatisticEntry.create(projectId, ProjectCode.parse("QTEST"))
SampleStatisticEntry sampleStatisticEntry = SampleStatisticEntry.create(projectId, ProjectCode.parse("Q2TEST"))
// Prime to 999 sample numbers that have been drawn already
for (int i = 1; i <= 999; i++) {
sampleStatisticEntry.drawNextSampleNumber()
Expand All @@ -89,7 +89,7 @@ class SampleCodeJpaRepositorySpec extends Specification{

then:
result.isValue()
result.getValue().code().equals("QTEST001BU")
result.getValue().code().equals("Q2TEST001BF")
}

def "Given a 1998 sample statistic entry, generate the next available sample code with letter jump and counter starting with 001"() {
Expand All @@ -100,7 +100,7 @@ class SampleCodeJpaRepositorySpec extends Specification{
ProjectId projectId = ProjectId.create()

and:
SampleStatisticEntry sampleStatisticEntry = SampleStatisticEntry.create(projectId, ProjectCode.parse("QTEST"))
SampleStatisticEntry sampleStatisticEntry = SampleStatisticEntry.create(projectId, ProjectCode.parse("Q2TEST"))
for (int i = 1; i <= 1998; i++) {
sampleStatisticEntry.drawNextSampleNumber()
}
Expand All @@ -116,7 +116,7 @@ class SampleCodeJpaRepositorySpec extends Specification{

then:
result.isValue()
result.getValue().code().equals("QTEST001C5")
result.getValue().code().equals("Q2TEST001CP")
}

def "Given a 1997 sample statistic entry, generate the next available sample code with letter jump and counter starting with 001"() {
Expand All @@ -127,7 +127,7 @@ class SampleCodeJpaRepositorySpec extends Specification{
ProjectId projectId = ProjectId.create()

and:
SampleStatisticEntry sampleStatisticEntry = SampleStatisticEntry.create(projectId, ProjectCode.parse("QTEST"))
SampleStatisticEntry sampleStatisticEntry = SampleStatisticEntry.create(projectId, ProjectCode.parse("Q2TEST"))
for (int i = 1; i <= 1997; i++) {
sampleStatisticEntry.drawNextSampleNumber()
}
Expand All @@ -143,7 +143,7 @@ class SampleCodeJpaRepositorySpec extends Specification{

then:
result.isValue()
result.getValue().code().equals("QTEST999B7")
result.getValue().code().equals("Q2TEST999BI")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public class ProjectCode {
@Column(name = "projectCode")
private String value;

private static final int LENGTH = 5;
private static final int LENGTH_RANDOM_PART = 4;

private static final String PREFIX = "Q";
private static final String PREFIX = "Q2";

public static final char[] ALLOWED_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWX".toCharArray();

Expand All @@ -46,9 +46,9 @@ protected ProjectCode() {
*/
public static ProjectCode random() {
var randomCodeGenerator = new RandomCodeGenerator(ALLOWED_LETTERS, ALLOWED_NUMBERS);
String code = randomCodeGenerator.next(LENGTH - 1);
String code = randomCodeGenerator.next(LENGTH_RANDOM_PART);
while (isBlackListed(code)) {
code = randomCodeGenerator.next(LENGTH - 1);
code = randomCodeGenerator.next(LENGTH_RANDOM_PART);
}
return new ProjectCode(PREFIX + code);
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public static ProjectCode parse(String str) throws IllegalArgumentException {
}

private static boolean isGeneralFormatValid(String code) {
return code.startsWith(PREFIX) && (code.length() == LENGTH);
return code.startsWith(PREFIX) && (code.length() == LENGTH_RANDOM_PART + PREFIX.length());
}

private static boolean containsInvalidCharacters(String code) {
Expand Down Expand Up @@ -181,7 +181,7 @@ public String toString() {
}

public static int getLENGTH() {
return LENGTH;
return LENGTH_RANDOM_PART + PREFIX.length();
}

public static String getPREFIX() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ProjectCreationServiceSpec extends Specification {


Result<Project, ApplicationException> resultWithExperimentalDesign = projectCreationServiceWithStubs.createProject(
"source offer", "QABCD",
"source offer", "Q2ABCD",
null,
"objective",
personReference,
Expand All @@ -50,7 +50,7 @@ class ProjectCreationServiceSpec extends Specification {
def contact = new Contact("Mustermann", "[email protected]ü")

when: "create is called without a project manager"
Result<Project, ApplicationException> result = projectCreationServiceWithStubs.createProject("source offer", "QABCD",
Result<Project, ApplicationException> result = projectCreationServiceWithStubs.createProject("source offer", "Q2ABCD",
"my title",
"objective",
contact,
Expand Down Expand Up @@ -88,7 +88,7 @@ class ProjectCreationServiceSpec extends Specification {
def contact = new Contact("Mustermann", "[email protected]ü")

when: "create is called without a project manager"
Result<Project, ApplicationException> result = projectCreationServiceWithStubs.createProject("source offer", "QABCD",
Result<Project, ApplicationException> result = projectCreationServiceWithStubs.createProject("source offer", "Q2ABCD",
"my title",
"objective",
contact,
Expand All @@ -108,7 +108,7 @@ class ProjectCreationServiceSpec extends Specification {
def contact = new Contact("Mustermann", "[email protected]ü")

when: "a project is created with a non-empty title"
Result<Project, ApplicationException> result = projectCreationServiceWithStubs.createProject("source offer", "QABCD",
Result<Project, ApplicationException> result = projectCreationServiceWithStubs.createProject("source offer", "Q2ABCD",
"my title",
"objective",
contact,
Expand All @@ -126,7 +126,7 @@ class ProjectCreationServiceSpec extends Specification {
def contact = new Contact("Mustermann", "[email protected]ü")

when:
Result<Project, ApplicationException> result = projectCreationServiceWithStubs.createProject("source offer", "QABCD",
Result<Project, ApplicationException> result = projectCreationServiceWithStubs.createProject("source offer", "Q2ABCD",
"my title",
"objective",
contact,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ class ProjectCodeSpec extends Specification {
ProjectCode code2 = ProjectCode.random();

then:
code.value().length() == 5
code.value().startsWith("Q")
code2.value().length() == 5
code2.value().startsWith("Q")
code.value().length() == 6
code.value().startsWith("Q2")
code2.value().length() == 6
code2.value().startsWith("Q2")
code != code2
}

def "Parsing a code with a blacklisted expression throws an IllegalArgumentException"() {
when:
ProjectCode.parse("Q" + blacklistedExpression as String)
ProjectCode.parse("Q2" + blacklistedExpression as String)

then:
thrown(IllegalArgumentException)
Expand All @@ -31,15 +31,15 @@ class ProjectCodeSpec extends Specification {

def "Parsing a project code with a valid expression returns its object oriented form"() {
when:
ProjectCode code = ProjectCode.parse("Q" + "ABCD")
ProjectCode code = ProjectCode.parse("Q2" + "ABCD")

then:
code.value().contains("ABCD")
}

def "Parsing a project code with a invalid length throws an IllegalArgumentException"() {
when:
ProjectCode.parse("Q" + wrongLength)
ProjectCode.parse("Q2" + wrongLength)

then:
thrown(IllegalArgumentException)
Expand All @@ -50,7 +50,7 @@ class ProjectCodeSpec extends Specification {

def "Parsing a project code with a invalid character throws an IllegalArgumentException"() {
when:
ProjectCode.parse("Q" + invalidChar)
ProjectCode.parse("Q2" + invalidChar)

then:
thrown(IllegalArgumentException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void initLayout() {
Span projectDesignTitle = new Span(TITLE);
projectDesignTitle.addClassName("title");

codeField.setHelperText("Q and 4 letters/numbers");
codeField.setHelperText("Q2 and 4 letters/numbers");
codeField.setValue(ProjectCode.random().value());
codeField.addClassName("code-field");
initCodeGenerationButton();
Expand Down

0 comments on commit 502753b

Please sign in to comment.