/** * @see org.springframework.validation.Validator#validate(java.lang.Object, * org.springframework.validation.Errors) * @should fail validation if name is null * @should fail validation if format is empty * @should fail validation if name already in use * @should pass validation if description is null or empty or whitespace * @should pass validation if all fields are correct * @should pass validation if field lengths are correct * @should fail validation if field lengths are not correct */ @Override public void validate(Object obj, Errors errors) { PersonAttributeType patObj = (PersonAttributeType) obj; ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "PersonAttributeType.error.nameEmpty"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "format", "PersonAttributeType.error.formatEmpty"); PersonService ps = Context.getPersonService(); PersonAttributeType pat = ps.getPersonAttributeTypeByName(patObj.getName()); if (pat != null && !pat.getUuid().equals(patObj.getUuid())) { errors.rejectValue("name", "PersonAttributeType.error.nameAlreadyInUse"); } ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "format", "retireReason"); } }
/** * Sets the attribute type for a person attribute. * * @param instance * @param attributeType * @throws org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException */ @PropertySetter("attributeType") public void setAttributeType(PersonAttribute instance, PersonAttributeType attributeType) { instance.setAttributeType(Context.getPersonService().getPersonAttributeTypeByUuid(attributeType.getUuid())); }
/** * Sets attributes on the given person. * * @param instance * @param attrs */ @PropertySetter("attributes") public static void setAttributes(Person instance, List<PersonAttribute> attrs) { for (PersonAttribute attr : attrs) { PersonAttribute existingAttribute = instance.getAttribute(Context.getPersonService() .getPersonAttributeTypeByUuid(attr.getAttributeType().getUuid())); if (existingAttribute != null) { if (attr.getValue() == null) { instance.removeAttribute(existingAttribute); } else { existingAttribute.setValue(attr.getValue()); } } else { instance.addAttribute(attr); } } }