Search in sources :

Example 1 with LogEntry

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;
}
Also used : ArrayList(java.util.ArrayList) SimpleDateFormat(java.text.SimpleDateFormat) LogEntry(org.openmrs.module.idgen.LogEntry)

Example 2 with LogEntry

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;
        }
    });
}
Also used : User(org.openmrs.User) APIException(org.openmrs.api.APIException) KghIdGeneratorProcessor(org.openmrs.module.pihcore.identifier.sierraLeone.KghIdGeneratorProcessor) BaseIdentifierSourceService(org.openmrs.module.idgen.service.BaseIdentifierSourceService) IdentifierSource(org.openmrs.module.idgen.IdentifierSource) SequentialIdentifierGenerator(org.openmrs.module.idgen.SequentialIdentifierGenerator) SimpleDateFormat(java.text.SimpleDateFormat) LogEntry(org.openmrs.module.idgen.LogEntry) Date(java.util.Date) Before(org.junit.Before)

Aggregations

SimpleDateFormat (java.text.SimpleDateFormat)2 LogEntry (org.openmrs.module.idgen.LogEntry)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Before (org.junit.Before)1 User (org.openmrs.User)1 APIException (org.openmrs.api.APIException)1 IdentifierSource (org.openmrs.module.idgen.IdentifierSource)1 SequentialIdentifierGenerator (org.openmrs.module.idgen.SequentialIdentifierGenerator)1 BaseIdentifierSourceService (org.openmrs.module.idgen.service.BaseIdentifierSourceService)1 KghIdGeneratorProcessor (org.openmrs.module.pihcore.identifier.sierraLeone.KghIdGeneratorProcessor)1