Search in sources :

Example 1 with UnallowedIdentifierException

use of org.openmrs.patient.UnallowedIdentifierException in project openmrs-core by openmrs.

the class PatientIdentifierValidator method checkIdentifierAgainstValidator.

/**
 * Validates that a given identifier string is valid for a given IdentifierValidator
 *
 * @param identifier the identifier to check against the passed {@link PatientIdentifierType}
 * @param validator the IdentifierValidator to use to check the identifier
 * @throws PatientIdentifierException if the identifier is does not match the format
 * @should fail validation if identifier is blank
 * @should fail validation if identifier is invalid
 * @should pass validation if identifier is valid
 * @should pass validation if validator is null
 */
public static void checkIdentifierAgainstValidator(String identifier, IdentifierValidator validator) throws PatientIdentifierException {
    log.debug("Checking identifier: " + identifier + " against validator: " + validator);
    if (StringUtils.isBlank(identifier)) {
        throw new BlankIdentifierException("PatientIdentifier.error.nullOrBlank");
    }
    if (validator == null) {
        log.debug("Validator is null, identifier passes.");
        return;
    }
    // Check identifier against IdentifierValidator
    try {
        if (!validator.isValid(identifier)) {
            throw new InvalidCheckDigitException(getMessage("PatientIdentifier.error.checkDigitWithParameter", identifier));
        }
    } catch (UnallowedIdentifierException e) {
        throw new InvalidCheckDigitException(getMessage("PatientIdentifier.error.unallowedIdentifier", identifier, validator.getName()));
    }
    log.debug("The identifier passed validation.");
}
Also used : InvalidCheckDigitException(org.openmrs.api.InvalidCheckDigitException) UnallowedIdentifierException(org.openmrs.patient.UnallowedIdentifierException) BlankIdentifierException(org.openmrs.api.BlankIdentifierException)

Example 2 with UnallowedIdentifierException

use of org.openmrs.patient.UnallowedIdentifierException in project openmrs-core by openmrs.

the class BaseHyphenatedIdentifierValidator method isValid.

/**
 * @see org.openmrs.patient.IdentifierValidator#isValid(java.lang.String)
 */
@Override
public boolean isValid(String identifier) throws UnallowedIdentifierException {
    if (identifier.indexOf("-") < 1) {
        throw new UnallowedIdentifierException("Identifier \"" + identifier + "\" must contain a hyphen followed by a check digit character (A-J).");
    }
    if (identifier.endsWith("-")) {
        throw new UnallowedIdentifierException("Identifier \"" + identifier + "\" must not end with a hyphen - a check digit character (A-J) must follow.");
    }
    String idWithoutCheckDigit = identifier.substring(0, identifier.indexOf("-"));
    checkAllowedIdentifier(idWithoutCheckDigit);
    int computedCheckDigit = getCheckDigit(idWithoutCheckDigit);
    String checkDigit = identifier.substring(identifier.indexOf("-") + 1, identifier.length());
    if (checkDigit.length() != 1) {
        throw new UnallowedIdentifierException("Identifier must have a check digit of length 1.");
    }
    if ("A".equalsIgnoreCase(checkDigit)) {
        checkDigit = "0";
    }
    if ("B".equalsIgnoreCase(checkDigit)) {
        checkDigit = "1";
    }
    if ("C".equalsIgnoreCase(checkDigit)) {
        checkDigit = "2";
    }
    if ("D".equalsIgnoreCase(checkDigit)) {
        checkDigit = "3";
    }
    if ("E".equalsIgnoreCase(checkDigit)) {
        checkDigit = "4";
    }
    if ("F".equalsIgnoreCase(checkDigit)) {
        checkDigit = "5";
    }
    if ("G".equalsIgnoreCase(checkDigit)) {
        checkDigit = "6";
    }
    if ("H".equalsIgnoreCase(checkDigit)) {
        checkDigit = "7";
    }
    if ("I".equalsIgnoreCase(checkDigit)) {
        checkDigit = "8";
    }
    if ("J".equalsIgnoreCase(checkDigit)) {
        checkDigit = "9";
    }
    int givenCheckDigit;
    try {
        givenCheckDigit = Integer.valueOf(checkDigit);
    } catch (NumberFormatException e) {
        throw new UnallowedIdentifierException("Check digit must either be a character from A to J or a single digit integer.");
    }
    return (computedCheckDigit == givenCheckDigit);
}
Also used : UnallowedIdentifierException(org.openmrs.patient.UnallowedIdentifierException)

Aggregations

UnallowedIdentifierException (org.openmrs.patient.UnallowedIdentifierException)2 BlankIdentifierException (org.openmrs.api.BlankIdentifierException)1 InvalidCheckDigitException (org.openmrs.api.InvalidCheckDigitException)1