Skip to content

Commit

Permalink
Add 'groups' configuration option for the UniqueCodeGenerator processor
Browse files Browse the repository at this point in the history
  • Loading branch information
epuzanov committed Nov 22, 2023
1 parent e96ad33 commit 1b7e0d8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public void testConstructor1() throws IOException, ExecutionException, Interrupt
(String) m.get("suffix"),
(boolean) m.get("padding"),
(Long) m.get("max"),
(String) m.get("codeSystem")
(String) m.get("codeSystem"),
null
);
assertEquals(2, codeGenerator.getLen());
}
Expand All @@ -115,7 +116,8 @@ public void testConstructor2() throws IOException, ExecutionException, Interrupt
(String) m.get("suffix"),
(boolean) m.get("padding"),
(Long) m.get("max"),
(String) m.get("codeSystem")
(String) m.get("codeSystem"),
null
);
} catch (Exception e) {
message = e.getMessage();
Expand All @@ -139,7 +141,8 @@ public void testCheckNextNumberWithinRange() throws IOException, ExecutionExcept
(String) m.get("suffix"),
(boolean) m.get("padding"),
(Long) m.get("max"),
(String) m.get("codeSystem")
(String) m.get("codeSystem"),
null
);
assertTrue(codeGenerator.checkNextNumberWithinRange(3L, (Long) m.get("max")));
assertTrue(codeGenerator.checkNextNumberWithinRange(4L, (Long) m.get("max")));
Expand Down Expand Up @@ -179,7 +182,8 @@ public void testDistinguishDifferentSuffix() throws IOException, ExecutionExcept
(String)m.get("suffix"),
(boolean)m.get("padding"),
(Long)m.get("max"),
(String)m.get("codeSystem")
(String)m.get("codeSystem"),
null
);

UUID uuid1 = UUID.randomUUID();
Expand Down Expand Up @@ -231,7 +235,8 @@ public void testAddTwoSubstances() throws IOException, ExecutionException, Inter
(String)m.get("suffix"),
(boolean)m.get("padding"),
(Long)m.get("max"),
(String)m.get("codeSystem")
(String)m.get("codeSystem"),
null
);
AutowireHelper.getInstance().autowire(codeGenerator);
UUID uuid1 = UUID.randomUUID();
Expand Down Expand Up @@ -287,7 +292,8 @@ public void testAddThreeSubstancesMaxTooSmall() throws IOException, ExecutionExc
(String) m.get("suffix"),
(boolean) m.get("padding"),
(Long) m.get("max"),
(String) m.get("codeSystem")
(String) m.get("codeSystem"),
null
);
AutowireHelper.getInstance().autowire(codeGenerator);

Expand Down Expand Up @@ -410,7 +416,8 @@ public void testDontUseLegacy() throws IOException, ExecutionException, Interrup
(String) m.get("suffix"),
(boolean) m.get("padding"),
(Long) m.get("max"),
(String) m.get("codeSystem")
(String) m.get("codeSystem"),
null
);
AutowireHelper.getInstance().autowire(codeGenerator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void testSeqGen() {
boolean padding = true;
String codeSystem = "Codes R Us";
Long max = Long.MAX_VALUE;
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem);
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem, null);
ProteinSubstance substance = getSubstanceFromFile("YYD6UT8T47");
AutowireHelper.getInstance().autowire(codeGenerator);
codeGenerator.addCode(substance);
Expand All @@ -117,7 +117,7 @@ public void testSeqGenNoSuffix() {
boolean padding = true;
String codeSystem = "Codes R Us";
Long max = Long.MAX_VALUE;
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem);
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem, null);
ProteinSubstance substance = getSubstanceFromFile("YYD6UT8T47");
AutowireHelper.getInstance().autowire(codeGenerator);
codeGenerator.addCode(substance);
Expand All @@ -132,7 +132,7 @@ public void testSeqGenNoCode() {
boolean padding = true;
String codeSystem = "";
Long max = Long.MAX_VALUE;
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem);
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem, null);
ProteinSubstance substance = getSubstanceFromFile("YYD6UT8T47");
AutowireHelper.getInstance().autowire(codeGenerator);
codeGenerator.addCode(substance);
Expand All @@ -149,7 +149,7 @@ public void testSeqGenTopBottomRange() {
// should null be allowed/handled?
String codeSystem = "Codes R Us1";
Long max = 1L;
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem);
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem, null);
ProteinSubstance substance = getSubstanceFromFile("YYD6UT8T47");
AutowireHelper.getInstance().autowire(codeGenerator);
codeGenerator.addCode(substance);
Expand All @@ -163,7 +163,7 @@ public void testSeqGenTopBottomRange() {
// should null be allowed/handled?
String codeSystem = "Codes R Us1";
Long max = 1L;
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem);
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem, null);
ChemicalSubstance substance = getAnotherSubstanceFromFile("660YQ98I10");
AutowireHelper.getInstance().autowire(codeGenerator);
codeGenerator.addCode(substance);
Expand All @@ -179,7 +179,7 @@ public void testSeqGenCheckDefaults() {
boolean padding = true;
Long max = null;
String codeSystem = "Codes R Us";
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem);
CodeSequentialGenerator codeGenerator = new CodeSequentialGenerator(seqGenName, length, suffix, padding, max, codeSystem, null);
ProteinSubstance substance = getSubstanceFromFile("YYD6UT8T47");
AutowireHelper.getInstance().autowire(codeGenerator);
assertEquals(codeGenerator.getMax(), codeGenerator.DEFAULT_MAX);
Expand Down Expand Up @@ -241,6 +241,23 @@ public void testAddCode() {
Assertions.assertTrue(substance.codes.stream().anyMatch(c -> c.codeSystem.equals(codeSystemName)));
}

@Test
public void testPublicCode() {
Map<String, Object> instantiationMap = new HashMap<>();
instantiationMap.put("name", "whatever");
instantiationMap.put("suffix", "OO");
instantiationMap.put("length", String.valueOf(Long.MAX_VALUE).length()+2);
instantiationMap.put("codesystem", codeSystemName);
instantiationMap.put("padding", true);
instantiationMap.put("max", Long.MAX_VALUE);
instantiationMap.put("groups", Collections.singletonMap(Integer.valueOf(0), "protected"));
UniqueCodeGenerator uniqueCodeGenerator = new UniqueCodeGenerator(instantiationMap);
AutowireHelper.getInstance().autowire(uniqueCodeGenerator);
ProteinSubstance substance = getSubstanceFromFile("YYD6UT8T47");
uniqueCodeGenerator.prePersist(substance);
Assertions.assertFalse(substance.codes.stream().filter(c -> c.codeSystem.equals(codeSystemName)).findFirst().get().isPublic());
}

@Test
public void testSkipAddingCode() {
Map<String, Object> instantiationMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public UniqueCodeGenerator(Map with) {
int length = (Integer) with.get("length");
boolean padding = (Boolean) with.get("padding");
Boolean useLegacy = (Boolean) with.get("useLegacy");
Map<Integer, String> groups = (Map<Integer, String>) with.get("groups");
Long maxValue;
try {
maxValue = Long.parseLong(String.valueOf(with.get("max")));
Expand All @@ -60,7 +61,7 @@ public UniqueCodeGenerator(Map with) {
});
} else {
seqGen = CachedSupplier.runOnce(()->{
CodeSequentialGenerator gen= new CodeSequentialGenerator(name, length, codeSystemSuffix, padding, max, codeSystem);
CodeSequentialGenerator gen= new CodeSequentialGenerator(name, length, codeSystemSuffix, padding, max, codeSystem, groups);
return AutowireHelper.getInstance().autowireAndProxy(gen);

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import ix.ginas.models.v1.Substance;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Objects;
/*
See UniqueCodeGenerator.
Expand All @@ -38,7 +39,8 @@
"suffix"="ZZ",
"length"=5,
"padding"=true,
"max"=999
"max"=999,
"groups"=["protected"]
}
}
*/
Expand All @@ -47,7 +49,6 @@
@Component
public class CodeSequentialGenerator extends SequentialNumericIDGenerator<Substance> {

private static final String GROUP_PROTECTED = "protected";
public static final Long DEFAULT_MAX = Long.MAX_VALUE;

@Autowired
Expand All @@ -59,6 +60,7 @@ public class CodeSequentialGenerator extends SequentialNumericIDGenerator<Substa
private String codeSystem;
private String name;
private Long max;
private String[] groups;

public CodeRepository getCodeRepository() {
return codeRepository;
Expand All @@ -82,13 +84,15 @@ public CodeSequentialGenerator( @JsonProperty("name") String name,
@JsonProperty("suffix") String suffix,
@JsonProperty("padding") boolean padding,
@JsonProperty("max") Long max,
@JsonProperty("codeSystem") String codeSystem) {
@JsonProperty("codeSystem") String codeSystem,
@JsonProperty("groups") Map<Integer, String> groups) {

super(len, suffix, padding);
if(max==null) { max=DEFAULT_MAX; }

this.max = max;
if(suffix==null) { this.suffix= "";}
this.groups = (groups != null) ? groups.values().stream().toArray(String[]::new) : null;
if(!this.getClass().equals(LegacyCodeSequentialGenerator.class)) {
// Legacy could be an extension of this class, and if so these checks aren't appropriate.
if (len < 1) {
Expand Down Expand Up @@ -159,7 +163,7 @@ public Code getCode() {

public Code addCode(Substance s) {
try {
return codeEntityService.createNewSystemCode(s, this.codeSystem, c -> this.generateID(), GROUP_PROTECTED);
return codeEntityService.createNewSystemCode(s, this.codeSystem, c -> this.generateID(), this.groups);
} catch (Throwable t) {
return Sneak.sneakyThrow(new Exception("Throwing exception in addCode in CodeSequentialGenerator. " + ((t.getCause()!=null)?t.getCause():"")));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ix.ginas.utils;

import java.util.Collections;
import java.util.Comparator;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;
Expand Down Expand Up @@ -74,7 +75,7 @@ public LegacyCodeSequentialGenerator(@JsonProperty("name") String name,
@JsonProperty("suffix") String suffix,
@JsonProperty("padding") boolean padding,
@JsonProperty("codeSystem") String codeSystem) {
super(name, len, suffix, padding, null, codeSystem);
super(name, len, suffix, padding, null, codeSystem, Collections.singletonMap(Integer.valueOf(0), GROUP_PROTECTED));
this.name = name;
this.codeSystem = codeSystem;
this.lastNum = CachedSupplier.runOnce(this::findHighestValueCode);
Expand Down

0 comments on commit 1b7e0d8

Please sign in to comment.