use of org.openmrs.module.idgen.LogEntry in project openmrs-module-pihcore by PIH.
the class KghIdGeneratorProcessor method generateBatch.
@Override
protected synchronized List<String> generateBatch(SequentialIdentifierGenerator seq, Long sequenceValue, int batchSize) {
// If this is not the KGH ID, then defer to standard functionality in the superclass
if (!seq.getUuid().equals(ConfigureSierraLeoneIdGenerators.KGH_ID_IDENTIFIER_SOURCE_UUID)) {
return super.generateBatch(seq, sequenceValue, batchSize);
}
// The KGH ID does not use a checksum or have any reserved identifiers, so we can bypass those features here
// The prefix on this source should be configured with the date format we wish to use
String dateFormat = seq.getPrefix() == null ? "'KGH'yyMM" : seq.getPrefix();
String prefix = new SimpleDateFormat(dateFormat).format(getDate());
// Get the last generated identifier. If it does not start with the expected prefix, reset the sequence
LogEntry mostRecentLogEntry = identifierSourceService.getMostRecentLogEntry(seq);
if (mostRecentLogEntry != null) {
if (!mostRecentLogEntry.getIdentifier().startsWith(prefix)) {
sequenceValue = resetToFirstSequenceValue(seq);
}
}
// Generate the batch starting from this sequence.
// Override the default generator in order to more easily add our custom prefix without additional code
List<String> identifiers = new ArrayList<>();
for (int i = 0; i < batchSize; i++) {
String firstIdentifierBase = seq.getFirstIdentifierBase() == null ? "0001" : seq.getFirstIdentifierBase();
char[] charSet = seq.getBaseCharacterSet().toCharArray();
int seqLength = firstIdentifierBase.length();
String baseIdentifier = IdgenUtil.convertToBase(sequenceValue, charSet, seqLength);
String identifier = prefix + baseIdentifier;
identifiers.add(identifier);
sequenceValue++;
}
identifierSourceService.saveSequenceValue(seq, sequenceValue);
return identifiers;
}
use of org.openmrs.module.idgen.LogEntry in project openmrs-module-pihcore by PIH.
the class KghIdGeneratorProcessorTest method setup.
@Before
public void setup() throws Exception {
mockStatic(Context.class);
testDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-31");
generator = new SequentialIdentifierGenerator();
generator.setName("KGH ID Identifier Generator");
generator.setUuid(ConfigureSierraLeoneIdGenerators.KGH_ID_IDENTIFIER_SOURCE_UUID);
generator.setPrefix("'KGH'yyMM");
generator.setMinLength(11);
generator.setMaxLength(12);
generator.setBaseCharacterSet("1234567890");
generator.setFirstIdentifierBase("0001");
lastLogEntry = new LogEntry(generator, "KGH2019120012", testDate, new User(), "Test");
processor = new KghIdGeneratorProcessor() {
public Date getDate() {
return testDate;
}
};
processor.setIdentifierSourceService(new BaseIdentifierSourceService() {
long nextSequenceNum = 1L;
public void saveSequenceValue(SequentialIdentifierGenerator sequentialIdentifierGenerator, long l) {
nextSequenceNum = l;
lastLogEntry.setIdentifier("KGH" + new SimpleDateFormat("yyMM").format(testDate));
}
public Long getSequenceValue(SequentialIdentifierGenerator sequentialIdentifierGenerator) {
return nextSequenceNum;
}
public LogEntry getMostRecentLogEntry(IdentifierSource source) throws APIException {
return lastLogEntry;
}
});
}
Aggregations