Search in sources :

Example 1 with LuhnIdentifierValidator

use of org.openmrs.patient.impl.LuhnIdentifierValidator in project openmrs-core by openmrs.

the class HibernateUserDAO method hasDuplicateUsername.

/**
 * @see org.openmrs.api.UserService#hasDuplicateUsername(org.openmrs.User)
 */
@Override
public boolean hasDuplicateUsername(String username, String systemId, Integer userId) {
    if (username == null || username.length() == 0) {
        username = "-";
    }
    if (systemId == null || systemId.length() == 0) {
        systemId = "-";
    }
    if (userId == null) {
        userId = -1;
    }
    String usernameWithCheckDigit = username;
    try {
        // Hardcoding in Luhn since past user IDs used this validator.
        usernameWithCheckDigit = new LuhnIdentifierValidator().getValidIdentifier(username);
    } catch (Exception e) {
    }
    Query query = sessionFactory.getCurrentSession().createQuery("select count(*) from User u where (u.username = :uname1 or u.systemId = :uname2 or u.username = :sysid1 or u.systemId = :sysid2 or u.systemId = :uname3) and u.userId <> :uid");
    query.setString("uname1", username);
    query.setString("uname2", username);
    query.setString("sysid1", systemId);
    query.setString("sysid2", systemId);
    query.setString("uname3", usernameWithCheckDigit);
    query.setInteger("uid", userId);
    Long count = (Long) query.uniqueResult();
    log.debug("# users found: " + count);
    return (count != null && count != 0);
}
Also used : LuhnIdentifierValidator(org.openmrs.patient.impl.LuhnIdentifierValidator) Query(org.hibernate.Query) DAOException(org.openmrs.api.db.DAOException)

Example 2 with LuhnIdentifierValidator

use of org.openmrs.patient.impl.LuhnIdentifierValidator in project openmrs-core by openmrs.

the class UserServiceImpl method generateSystemId.

/**
 * Generates system ids based on the following algorithm scheme: user_id-check digit
 *
 * @see org.openmrs.api.UserService#generateSystemId()
 */
@Override
@Transactional(readOnly = true)
public String generateSystemId() {
    // Hardcoding Luhn algorithm since all existing openmrs user ids have
    // had check digits generated this way.
    LuhnIdentifierValidator liv = new LuhnIdentifierValidator();
    String systemId;
    Integer offset = 0;
    do {
        // generate and increment the system id if necessary
        Integer generatedId = dao.generateSystemId() + offset++;
        systemId = generatedId.toString();
        try {
            systemId = liv.getValidIdentifier(systemId);
        } catch (Exception e) {
            log.error("error getting check digit", e);
            return systemId;
        }
    // loop until we find a system id that no one has
    } while (dao.hasDuplicateUsername(null, systemId, null));
    return systemId;
}
Also used : LuhnIdentifierValidator(org.openmrs.patient.impl.LuhnIdentifierValidator) APIException(org.openmrs.api.APIException) DAOException(org.openmrs.api.db.DAOException) APIAuthenticationException(org.openmrs.api.APIAuthenticationException) CannotDeleteRoleWithChildrenException(org.openmrs.api.CannotDeleteRoleWithChildrenException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with LuhnIdentifierValidator

use of org.openmrs.patient.impl.LuhnIdentifierValidator in project openmrs-core by openmrs.

the class PatientServiceTest method shouldCreatePatientWithValidatedIdentifier.

/**
 * Tests creating patients with identifiers that are or are not validated.
 *
 * @throws Exception
 */
@Test
public void shouldCreatePatientWithValidatedIdentifier() throws Exception {
    executeDataSet(CREATE_PATIENT_VALID_IDENT_XML);
    Patient patient = createBasicPatient();
    Patient patient2 = createBasicPatient();
    PatientIdentifierType pit = patientService.getPatientIdentifierType(1);
    PatientIdentifier ident1 = new PatientIdentifier("123-1", pit, locationService.getLocation(1));
    PatientIdentifier ident2 = new PatientIdentifier("123", pit, locationService.getLocation(1));
    PatientIdentifier ident3 = new PatientIdentifier("123-0", pit, locationService.getLocation(1));
    PatientIdentifier ident4 = new PatientIdentifier("123-A", pit, locationService.getLocation(1));
    try {
        ident1.setPreferred(true);
        patient.addIdentifier(ident1);
        patientService.savePatient(patient);
        fail("Patient creation should have failed with identifier " + ident1.getIdentifier());
    } catch (InvalidCheckDigitException ex) {
    } catch (APIException e) {
        if (!(e.getMessage() != null && e.getMessage().contains("failed to validate with reason: " + Context.getMessageSourceService().getMessage("PatientIdentifier.error.checkDigitWithParameter", new Object[] { ident1.getIdentifier() }, null)))) {
            fail("Patient creation should have failed with identifier " + ident1.getIdentifier());
        }
    }
    patient.removeIdentifier(ident1);
    try {
        ident2.setPreferred(true);
        patient.addIdentifier(ident2);
        patientService.savePatient(patient);
        fail("Patient creation should have failed with identifier " + ident2.getIdentifier());
    } catch (InvalidCheckDigitException ex) {
    } catch (APIException e) {
        if (!(e.getMessage() != null && e.getMessage().contains("failed to validate with reason: " + Context.getMessageSourceService().getMessage("PatientIdentifier.error.unallowedIdentifier", new Object[] { ident2.getIdentifier(), new LuhnIdentifierValidator().getName() }, null)))) {
            fail("Patient creation should have failed with identifier " + ident2.getIdentifier());
        }
    }
    patient.removeIdentifier(ident2);
    try {
        ident3.setPreferred(true);
        patient.addIdentifier(ident3);
        patientService.savePatient(patient);
        patientService.purgePatient(patient);
        patient.removeIdentifier(ident3);
        ident4.setPreferred(true);
        patient2.addIdentifier(ident4);
        patientService.savePatient(patient2);
    } catch (InvalidCheckDigitException ex) {
        fail("Patient creation should have worked with identifiers " + ident3.getIdentifier() + " and " + ident4.getIdentifier());
    }
}
Also used : LuhnIdentifierValidator(org.openmrs.patient.impl.LuhnIdentifierValidator) Patient(org.openmrs.Patient) PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifier(org.openmrs.PatientIdentifier) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Aggregations

LuhnIdentifierValidator (org.openmrs.patient.impl.LuhnIdentifierValidator)3 DAOException (org.openmrs.api.db.DAOException)2 Query (org.hibernate.Query)1 Test (org.junit.Test)1 Patient (org.openmrs.Patient)1 PatientIdentifier (org.openmrs.PatientIdentifier)1 PatientIdentifierType (org.openmrs.PatientIdentifierType)1 APIAuthenticationException (org.openmrs.api.APIAuthenticationException)1 APIException (org.openmrs.api.APIException)1 CannotDeleteRoleWithChildrenException (org.openmrs.api.CannotDeleteRoleWithChildrenException)1 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)1 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)1 Transactional (org.springframework.transaction.annotation.Transactional)1