PersonNameValidator.java

/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.validator;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.PersonName;
import org.openmrs.annotation.Handler;
import org.openmrs.api.context.Context;
import org.openmrs.util.OpenmrsConstants;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * This class validates a PersonName object.
 * 
 * @since 1.7
 */
@Handler(supports = { PersonName.class }, order = 50)
public class PersonNameValidator implements Validator {
	
	private static Log log = LogFactory.getLog(PersonNameValidator.class);
	
	/**
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	public boolean supports(Class c) {
		return PersonName.class.isAssignableFrom(c);
	}
	
	/**
	 * Checks whether person name has all required values, and whether values are proper length
	 * 
	 * @param personName
	 * @param errors
	 * @should fail validation if PersonName object is null
	 * @should pass validation if name is invalid but voided
	 */
	public void validate(Object object, Errors errors) {
		if (log.isDebugEnabled())
			log.debug(this.getClass().getName() + ".validate...");
		PersonName personName = (PersonName) object;
		try {
			// Validate that the person name object is not null
			if (personName == null) {
				errors.reject("error.name");
			} else if (!personName.isVoided()) {
				validatePersonName(personName, errors, true, false);
			}
		}
		catch (Exception e) {
			errors.reject(e.getMessage());
		}
	}
	
	/**
	 * Checks that the given {@link PersonName} is valid
	 * 
	 * @param personName the {@link PersonName} to validate
	 * @param errors
	 * @param arrayInd indicates whether or not a names[0] array needs to be prepended to field
	 * @should fail validation if PersonName object is null
	 * @should fail validation if PersonName.givenName is null
	 * @should fail validation if PersonName.givenName is empty
	 * @should fail validation if PersonName.givenName is just spaces
	 * @should fail validation if PersonName.givenName is spaces surrounded by quotation marks
	 * @should pass validation if PersonName.givenName is not blank
	 * @should fail validation if PersonName.familyName is null
	 * @should fail validation if PersonName.familyName is empty
	 * @should fail validation if PersonName.familyName is just spaces
	 * @should fail validation if PersonName.familyName is spaces surrounded by quotation marks
	 * @should pass validation if PersonName.familyName is not blank
	 * @should fail validation if PersonName.prefix is too long
	 * @should pass validation if PersonName.prefix is exactly max length
	 * @should pass validation if PersonName.prefix is less than maximum field length
	 * @should fail validation if PersonName.givenName is too long
	 * @should pass validation if PersonName.givenName is exactly max length
	 * @should pass validation if PersonName.givenName is less than maximum field length
	 * @should fail validation if PersonName.middleName is too long
	 * @should pass validation if PersonName.middleName is exactly max length
	 * @should pass validation if PersonName.middleName is less than maximum field length
	 * @should fail validation if PersonName.familyNamePrefix is too long
	 * @should pass validation if PersonName.familyNamePrefix is exactly max length
	 * @should pass validation if PersonName.familyNamePrefix is less than maximum field length
	 * @should fail validation if PersonName.familyName is too long
	 * @should pass validation if PersonName.familyName is exactly max length
	 * @should pass validation if PersonName.familyName is less than maximum field length
	 * @should fail validation if PersonName.familyName2 is too long
	 * @should pass validation if PersonName.familyName2 is exactly max length
	 * @should pass validation if PersonName.familyName2 is less than maximum field length
	 * @should fail validation if PersonName.familyNameSuffix is too long
	 * @should pass validation if PersonName.familyNameSuffix is exactly max length
	 * @should pass validation if PersonName.familyNameSuffix is less than maximum field length
	 * @should fail validation if PersonName.degree is too long
	 * @should pass validation if PersonName.degree is exactly max length
	 * @should pass validation if PersonName.degree is less than maximum field length
	 * @should fail validation if PersonName.givenName is invalid
	 * @should pass validation if PersonName.givenName is valid
	 * @should fail validation if PersonName.middleName is invalid
	 * @should pass validation if PersonName.middleName is valid
	 * @should fail validation if PersonName.familyName is invalid
	 * @should pass validation if PersonName.familyName is valid
	 * @should fail validation if PersonName.familyName2 is invalid
	 * @should pass validation if PersonName.familyName2 is valid
	 * @should pass validation if regex string is null
	 * @should pass validation if regex string is empty
	 * @should not validate against regex for blank names
	 */
	public void validatePersonName(PersonName personName, Errors errors, boolean arrayInd, boolean testInd) {
		
		if (personName == null)
			errors.reject("error.name");
		// Make sure they assign a name
		if (StringUtils.isBlank(personName.getGivenName())
		        || StringUtils.isBlank(personName.getGivenName().replaceAll("\"", "")))
			errors.rejectValue(getFieldKey("givenName", arrayInd, testInd), "Patient.names.required.given.family");
		if (StringUtils.isBlank(personName.getFamilyName())
		        || StringUtils.isBlank(personName.getFamilyName().replaceAll("\"", "")))
			errors.rejectValue(getFieldKey("familyName", arrayInd, testInd), "Patient.names.required.given.family");
		// Make sure the entered name value is sensible 
		String namePattern = Context.getAdministrationService().getGlobalProperty(
		    OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_NAME_REGEX);
		if (namePattern != null && namePattern != "") {
			if (StringUtils.isNotBlank(personName.getGivenName()) && !personName.getGivenName().matches(namePattern))
				errors.rejectValue(getFieldKey("givenName", arrayInd, testInd), "GivenName.invalid");
			if (StringUtils.isNotBlank(personName.getMiddleName()) && !personName.getMiddleName().matches(namePattern))
				errors.rejectValue(getFieldKey("middleName", arrayInd, testInd), "MiddleName.invalid");
			if (StringUtils.isNotBlank(personName.getFamilyName()) && !personName.getFamilyName().matches(namePattern))
				errors.rejectValue(getFieldKey("familyName", arrayInd, testInd), "FamilyName.invalid");
			if (StringUtils.isNotBlank(personName.getFamilyName2()) && !personName.getFamilyName2().matches(namePattern))
				errors.rejectValue(getFieldKey("familyName2", arrayInd, testInd), "FamilyName2.invalid");
		}
		// Make sure the length does not exceed database column size
		if (StringUtils.length(personName.getPrefix()) > 50)
			rejectPersonNameOnLength(errors, "prefix", arrayInd, testInd);
		if (StringUtils.length(personName.getGivenName()) > 50)
			rejectPersonNameOnLength(errors, "givenName", arrayInd, testInd);
		if (StringUtils.length(personName.getMiddleName()) > 50)
			rejectPersonNameOnLength(errors, "middleName", arrayInd, testInd);
		if (StringUtils.length(personName.getFamilyNamePrefix()) > 50)
			rejectPersonNameOnLength(errors, "familyNamePrefix", arrayInd, testInd);
		if (StringUtils.length(personName.getFamilyName()) > 50)
			rejectPersonNameOnLength(errors, "familyName", arrayInd, testInd);
		if (StringUtils.length(personName.getFamilyName2()) > 50)
			rejectPersonNameOnLength(errors, "familyName2", arrayInd, testInd);
		if (StringUtils.length(personName.getFamilyNameSuffix()) > 50)
			rejectPersonNameOnLength(errors, "familyNameSuffix", arrayInd, testInd);
		if (StringUtils.length(personName.getDegree()) > 50)
			rejectPersonNameOnLength(errors, "degree", arrayInd, testInd);
	}
	
	private void rejectPersonNameOnLength(Errors errors, String fieldKey, boolean arrayInd, boolean testInd) {
		errors.rejectValue(getFieldKey(fieldKey, arrayInd, testInd), "error.name.max.length", new Object[] {
		        getInternationizedFieldName("PersonName." + fieldKey), 50 }, "error.name");
	}
	
	/***********************************************************************************************************
	 * @param field the field name
	 * @param arrayInd indicates whether or not a names[0] array needs to be prepended to field
	 * @return formated
	 */
	private String getFieldKey(String field, boolean arrayInd, boolean testInd) {
		return testInd ? field : arrayInd ? "names[0]." + field : "name." + field;
	}
	
	private static String getInternationizedFieldName(String messageKey) {
		return Context.getMessageSourceService().getMessage(messageKey, null, Context.getLocale());
	}
	
}