/** * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors) * @should fail validation if aIsToB(or A is To B) is null or empty or whitespace * @should fail validation if bIsToA(or B is To A) is null or empty or whitespace * @should fail validation if description is null or empty or whitespace * @should pass validation if all required fields are set * @should fail validation if relationshipTypeName already exist * @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) { RelationshipType relationshipType = (RelationshipType) obj; if (relationshipType == null) { errors.rejectValue("relationshipType", "error.general"); } else { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "aIsToB", "RelationshipType.aIsToB.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "bIsToA", "RelationshipType.bIsToA.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "RelationshipType.description.required"); RelationshipType exist = Context.getPersonService().getRelationshipTypeByName( relationshipType.getaIsToB() + "/" + relationshipType.getbIsToA()); if (exist != null && !exist.getRetired() && !OpenmrsUtil.nullSafeEquals(relationshipType.getUuid(), exist.getUuid())) { errors.reject("duplicate.relationshipType"); } ValidateUtil.validateFieldLengths(errors, obj.getClass(), "aIsToB", "bIsToA", "description", "retireReason"); } } }
String output = "<update tableName=\"relationship_type\"><column name=\"uuid\" value=\"" + type.getUuid() + "\"/><where> relationship_type_id = '" + type.getRelationshipTypeId() + "' and a_is_to_b = '" + type.getaIsToB().replace("'", "\\'") + "' and b_is_to_a = '" + type.getbIsToA().replace("'", "\\'")
@PropertyGetter("displayAIsToB") public String getDisplayAIsToBe(RelationshipType delegate) { String localization = getLocalization(delegate.getUuid(), "aIsToB"); if (localization != null) { return localization; } else { return StringUtils.isEmpty(delegate.getaIsToB()) ? "" : delegate.getaIsToB(); } }
@PropertyGetter("displayBIsToA") public String getDisplayBIsToAe(RelationshipType delegate) { String localization = getLocalization(delegate.getUuid(), "bIsToA"); if (localization != null) { return localization; } else { return StringUtils.isEmpty(delegate.getbIsToA()) ? "" : delegate.getbIsToA(); } }
@Test public void shouldEditARelationship() throws Exception { final String newRelationshipTypeUuid = "d47f056e-f147-49a3-88e1-0c91d199510d"; Relationship relationship = service.getRelationshipByUuid(RestTestConstants1_8.RELATIONSHIP_UUID); Assert.assertNotNull(relationship); //sanity checks Assert.assertFalse(newRelationshipTypeUuid.equalsIgnoreCase(relationship.getRelationshipType().getUuid())); String json = "{\"relationshipType\":\"" + newRelationshipTypeUuid + "\"" + "}"; handle(newPostRequest(getURI() + "/" + getUuid(), json)); Relationship updated = service.getRelationshipByUuid(RestTestConstants1_8.RELATIONSHIP_UUID); Assert.assertNotNull(updated); Assert.assertEquals(newRelationshipTypeUuid, updated.getRelationshipType().getUuid()); }