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);
}
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;
}
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());
}
}
Aggregations