use of org.openmrs.api.BlankIdentifierException 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.");
}
use of org.openmrs.api.BlankIdentifierException in project openmrs-core by openmrs.
the class PatientIdentifierValidator method validateIdentifier.
/**
* Validates that a given identifier string is valid for a given {@link PatientIdentifierType}
* Checks for things like blank identifiers, invalid check digits, and invalid format.
*
* @param pit - the {@link PatientIdentifierType} to validate against
* @param identifier - the identifier to check against the passed {@link PatientIdentifierType}
* @throws PatientIdentifierException if the identifier is invalid
* @should fail validation if PatientIdentifierType is null
* @should fail validation if identifier is blank
* @see #checkIdentifierAgainstFormat(String, String, String)
* @see #checkIdentifierAgainstValidator(String, IdentifierValidator)
*/
public static void validateIdentifier(String identifier, PatientIdentifierType pit) throws PatientIdentifierException {
log.debug("Checking identifier: " + identifier + " for type: " + pit);
// Validate input parameters
if (pit == null) {
throw new BlankIdentifierException("PatientIdentifierType.null");
}
if (StringUtils.isBlank(identifier)) {
throw new BlankIdentifierException("PatientIdentifier.error.nullOrBlank");
}
checkIdentifierAgainstFormat(identifier, pit.getFormat(), pit.getFormatDescription());
// Check identifier against IdentifierValidator
if (pit.hasValidator()) {
IdentifierValidator validator = Context.getPatientService().getIdentifierValidator(pit.getValidator());
checkIdentifierAgainstValidator(identifier, validator);
}
log.debug("The identifier check was successful");
}
use of org.openmrs.api.BlankIdentifierException in project openmrs-core by openmrs.
the class PatientIdentifierValidator method validateIdentifier.
/**
* Checks that the given {@link PatientIdentifier} is valid
*
* @param pi - the {@link PatientIdentifier} to validate
* @throws PatientIdentifierException if the {@link PatientIdentifier} is invalid
* @should fail validation if PatientIdentifier is null
* @should pass validation if PatientIdentifier is voided
* @should fail validation if another patient has a matching identifier of the same type
* @should pass if in use and id type uniqueness is set to non unique
* @see #validateIdentifier(String, PatientIdentifierType)
*/
public static void validateIdentifier(PatientIdentifier pi) throws PatientIdentifierException {
// Validate that the identifier is non-null
if (pi == null) {
throw new BlankIdentifierException("PatientIdentifier.error.null");
}
// Only validate if the PatientIdentifier is not voided
if (!pi.getVoided()) {
// Check that this is a valid identifier
validateIdentifier(pi.getIdentifier(), pi.getIdentifierType());
// Check that location is included if it is required (default behavior is to require it)
LocationBehavior lb = pi.getIdentifierType().getLocationBehavior();
if (pi.getLocation() == null && (lb == null || lb == LocationBehavior.REQUIRED)) {
String identifierString = (pi.getIdentifier() != null) ? pi.getIdentifier() : "";
throw new PatientIdentifierException(Context.getMessageSourceService().getMessage("PatientIdentifier.location.null", new Object[] { identifierString }, Context.getLocale()));
}
if (pi.getIdentifierType().getUniquenessBehavior() != UniquenessBehavior.NON_UNIQUE && Context.getPatientService().isIdentifierInUseByAnotherPatient(pi)) {
// Check is already in use by another patient
throw new IdentifierNotUniqueException(Context.getMessageSourceService().getMessage("PatientIdentifier.error.notUniqueWithParameter", new Object[] { pi.getIdentifier() }, Context.getLocale()), pi);
}
}
}
Aggregations