Search in sources :

Example 1 with ConceptReferenceTermMap

use of org.openmrs.ConceptReferenceTermMap in project openmrs-core by openmrs.

the class ConceptReferenceTermValidator method validate.

/**
 * Checks that a given concept reference term object is valid.
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail if the concept reference term object is null
 * @should fail if the name is a white space character
 * @should fail if the code is null
 * @should fail if the code is an empty string
 * @should fail if the code is a white space character
 * @should fail if the concept reference term code is a duplicate in its concept source
 * @should fail if the concept source is null
 * @should pass if all the required fields are set and valid
 * @should pass if the duplicate name is for a term from another concept source
 * @should pass if the duplicate code is for a term from another concept source
 * @should fail if a concept reference term map has no concept map type
 * @should fail if termB of a concept reference term map is not set
 * @should fail if a term is mapped to itself
 * @should fail if a term is mapped multiple times to the same term
 * @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) throws APIException {
    if (obj == null || !(obj instanceof ConceptReferenceTerm)) {
        throw new IllegalArgumentException("The parameter obj should not be null and must be of type" + ConceptReferenceTerm.class);
    }
    ConceptReferenceTerm conceptReferenceTerm = (ConceptReferenceTerm) obj;
    String code = conceptReferenceTerm.getCode();
    boolean hasBlankFields = false;
    if (!StringUtils.hasText(code)) {
        errors.rejectValue("code", "ConceptReferenceTerm.error.codeRequired", "The code property is required for a concept reference term");
        hasBlankFields = true;
    }
    if (conceptReferenceTerm.getConceptSource() == null) {
        errors.rejectValue("conceptSource", "ConceptReferenceTerm.error.sourceRequired", "The conceptSource property is required for a concept reference term");
        hasBlankFields = true;
    }
    if (hasBlankFields) {
        return;
    }
    code = code.trim();
    // Ensure that there are no terms with the same code in the same source
    ConceptReferenceTerm termWithDuplicateCode = Context.getConceptService().getConceptReferenceTermByCode(code, conceptReferenceTerm.getConceptSource());
    if (termWithDuplicateCode != null && !OpenmrsUtil.nullSafeEquals(termWithDuplicateCode.getUuid(), conceptReferenceTerm.getUuid())) {
        errors.rejectValue("code", "ConceptReferenceTerm.duplicate.code", "Duplicate concept reference term code in its concept source: " + code);
    }
    // validate the concept reference term maps
    if (CollectionUtils.isNotEmpty(conceptReferenceTerm.getConceptReferenceTermMaps())) {
        int index = 0;
        Set<String> mappedTermUuids = null;
        for (ConceptReferenceTermMap map : conceptReferenceTerm.getConceptReferenceTermMaps()) {
            if (map == null) {
                throw new APIException("ConceptReferenceTerm.add.null", (Object[]) null);
            }
            if (map.getConceptMapType() == null) {
                errors.rejectValue("conceptReferenceTermMaps[" + index + "].conceptMapType", "ConceptReferenceTerm.error.mapTypeRequired", "Concept Map Type is required");
            } else if (map.getTermB() == null) {
                errors.rejectValue("conceptReferenceTermMaps[" + index + "].termB", "ConceptReferenceTerm.error.termBRequired", "Mapped Term is required");
            } else if (map.getTermB().equals(conceptReferenceTerm)) {
                errors.rejectValue("conceptReferenceTermMaps[" + index + "].termB", "ConceptReferenceTerm.map.sameTerm", "Cannot map a concept reference term to itself");
            }
            // don't proceed to the next map
            if (errors.hasErrors()) {
                return;
            }
            if (mappedTermUuids == null) {
                mappedTermUuids = new HashSet<>();
            }
            // if we already have a mapping to this term, reject it this map
            if (!mappedTermUuids.add(map.getTermB().getUuid())) {
                errors.rejectValue("conceptReferenceTermMaps[" + index + "].termB", "ConceptReferenceTerm.termToTerm.alreadyMapped", "Cannot map a reference term multiple times to the same concept reference term");
            }
            index++;
        }
    }
    ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "code", "version", "description", "retireReason");
}
Also used : APIException(org.openmrs.api.APIException) ConceptReferenceTermMap(org.openmrs.ConceptReferenceTermMap) ConceptReferenceTerm(org.openmrs.ConceptReferenceTerm)

Example 2 with ConceptReferenceTermMap

use of org.openmrs.ConceptReferenceTermMap in project openmrs-core by openmrs.

the class ConceptReferenceTermValidatorTest method validate_shouldFailIfAConceptReferenceTermMapHasNoConceptMapType.

/**
 * @see ConceptReferenceTermValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailIfAConceptReferenceTermMapHasNoConceptMapType() {
    ConceptReferenceTerm term = new ConceptReferenceTerm();
    term.setName("name");
    term.setCode("code");
    term.setConceptSource(Context.getConceptService().getConceptSource(1));
    term.addConceptReferenceTermMap(new ConceptReferenceTermMap(new ConceptReferenceTerm(1), null));
    Errors errors = new BindException(term, "term");
    new ConceptReferenceTermValidator().validate(term, errors);
    Assert.assertEquals(true, errors.hasFieldErrors("conceptReferenceTermMaps[0].conceptMapType"));
}
Also used : Errors(org.springframework.validation.Errors) ConceptReferenceTermMap(org.openmrs.ConceptReferenceTermMap) BindException(org.springframework.validation.BindException) ConceptReferenceTerm(org.openmrs.ConceptReferenceTerm) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 3 with ConceptReferenceTermMap

use of org.openmrs.ConceptReferenceTermMap in project openmrs-core by openmrs.

the class ConceptReferenceTermValidatorTest method validate_shouldFailIfATermIsMappedMultipleTimesToTheSameTerm.

/**
 * @see ConceptReferenceTermValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailIfATermIsMappedMultipleTimesToTheSameTerm() {
    ConceptReferenceTerm term = new ConceptReferenceTerm();
    ConceptService cs = Context.getConceptService();
    term.setCode("unique code");
    term.setConceptSource(Context.getConceptService().getConceptSource(1));
    ConceptReferenceTermMap map1 = new ConceptReferenceTermMap(cs.getConceptReferenceTerm(1), cs.getConceptMapType(1));
    term.addConceptReferenceTermMap(map1);
    // test should fail if we change the term below
    ConceptReferenceTermMap map2 = new ConceptReferenceTermMap(cs.getConceptReferenceTerm(1), cs.getConceptMapType(1));
    term.addConceptReferenceTermMap(map2);
    Errors errors = new BindException(term, "term");
    new ConceptReferenceTermValidator().validate(term, errors);
    System.err.println(errors.getAllErrors());
    // the term for second mapping should be rejected
    Assert.assertEquals(true, errors.hasFieldErrors("conceptReferenceTermMaps[1].termB"));
}
Also used : Errors(org.springframework.validation.Errors) ConceptReferenceTermMap(org.openmrs.ConceptReferenceTermMap) BindException(org.springframework.validation.BindException) ConceptService(org.openmrs.api.ConceptService) ConceptReferenceTerm(org.openmrs.ConceptReferenceTerm) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 4 with ConceptReferenceTermMap

use of org.openmrs.ConceptReferenceTermMap in project openmrs-core by openmrs.

the class ConceptReferenceTermValidatorTest method validate_shouldFailIfATermIsMappedToItself.

/**
 * @see ConceptReferenceTermValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailIfATermIsMappedToItself() {
    ConceptReferenceTerm term = Context.getConceptService().getConceptReferenceTerm(1);
    Set<ConceptReferenceTermMap> maps = term.getConceptReferenceTermMaps();
    ConceptReferenceTermMap invalidMap = maps.iterator().next();
    invalidMap.setTermB(term);
    term.setConceptReferenceTermMaps(maps);
    Errors errors = new BindException(term, "term");
    new ConceptReferenceTermValidator().validate(term, errors);
    Assert.assertEquals(true, errors.hasFieldErrors("conceptReferenceTermMaps[0].termB"));
}
Also used : Errors(org.springframework.validation.Errors) ConceptReferenceTermMap(org.openmrs.ConceptReferenceTermMap) BindException(org.springframework.validation.BindException) ConceptReferenceTerm(org.openmrs.ConceptReferenceTerm) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 5 with ConceptReferenceTermMap

use of org.openmrs.ConceptReferenceTermMap in project openmrs-core by openmrs.

the class ConceptReferenceTermValidatorTest method validate_shouldFailIfTermBOfAConceptReferenceTermMapIsNotSet.

/**
 * @see ConceptReferenceTermValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailIfTermBOfAConceptReferenceTermMapIsNotSet() {
    ConceptReferenceTerm term = new ConceptReferenceTerm();
    term.setName("name");
    term.setCode("code");
    term.setConceptSource(Context.getConceptService().getConceptSource(1));
    Set<ConceptReferenceTermMap> maps = new LinkedHashSet<>();
    maps.add(new ConceptReferenceTermMap(null, new ConceptMapType(1)));
    term.setConceptReferenceTermMaps(maps);
    Errors errors = new BindException(term, "term");
    new ConceptReferenceTermValidator().validate(term, errors);
    Assert.assertEquals(true, errors.hasFieldErrors("conceptReferenceTermMaps[0].termB"));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Errors(org.springframework.validation.Errors) ConceptMapType(org.openmrs.ConceptMapType) ConceptReferenceTermMap(org.openmrs.ConceptReferenceTermMap) BindException(org.springframework.validation.BindException) ConceptReferenceTerm(org.openmrs.ConceptReferenceTerm) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

ConceptReferenceTerm (org.openmrs.ConceptReferenceTerm)5 ConceptReferenceTermMap (org.openmrs.ConceptReferenceTermMap)5 Test (org.junit.Test)4 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)4 BindException (org.springframework.validation.BindException)4 Errors (org.springframework.validation.Errors)4 LinkedHashSet (java.util.LinkedHashSet)1 ConceptMapType (org.openmrs.ConceptMapType)1 APIException (org.openmrs.api.APIException)1 ConceptService (org.openmrs.api.ConceptService)1