Search in sources :

Example 6 with ConceptMapType

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

the class DrugValidator method validate.

/**
 * Validates an Drug object
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail if the drug object is null
 * @should fail if drug on drugReferenceMap is null
 * @should fail if conceptReferenceTerm on drugReferenceMap is null
 * @should invoke ConceptReferenceTermValidator if term on drugReferenceMap is new
 * @should invoke ConceptMapTypeValidator if conceptMapType on drugReferenceMap is new
 * @should pass if all fields are correct
 * @should reject drug multiple mappings 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) {
    if (obj == null || !(obj instanceof Drug)) {
        throw new IllegalArgumentException("The parameter obj should not be null and must be of type" + Drug.class);
    } else {
        Drug drug = (Drug) obj;
        Set<DrugReferenceMap> drugReferenceMaps = drug.getDrugReferenceMaps();
        Set<String> mappedTermUuids = new HashSet<>();
        int index = 0;
        for (DrugReferenceMap referenceMap : drugReferenceMaps) {
            Drug mappedDrug = referenceMap.getDrug();
            ConceptReferenceTerm referenceTerm = referenceMap.getConceptReferenceTerm();
            ConceptMapType mapType = referenceMap.getConceptMapType();
            if (mappedDrug == null) {
                errors.rejectValue("drugReferenceMaps[" + index + "].drug", "Drug.drugReferenceMap.mappedDrug");
            }
            if (referenceTerm == null) {
                errors.rejectValue("drugReferenceMaps[" + index + "].conceptReferenceTerm", "Drug.drugReferenceMap.conceptReferenceTerm");
            } else if (referenceTerm.getConceptReferenceTermId() == null) {
                try {
                    errors.pushNestedPath("drugReferenceMaps[" + index + "].conceptReferenceTerm");
                    ValidationUtils.invokeValidator(new ConceptReferenceTermValidator(), referenceTerm, errors);
                } finally {
                    errors.popNestedPath();
                }
            }
            if (mapType == null) {
                errors.rejectValue("drugReferenceMaps[" + index + "].conceptMapType", "Drug.drugReferenceMap.conceptMapType");
            } else if (mapType.getConceptMapTypeId() == null) {
                try {
                    errors.pushNestedPath("drugReferenceMaps[" + index + "].conceptMapType");
                    ValidationUtils.invokeValidator(new ConceptMapTypeValidator(), mapType, errors);
                } finally {
                    errors.popNestedPath();
                }
            }
            // don't proceed to the next map
            if (errors.hasErrors()) {
                return;
            }
            // if we already have a mapping to this term, reject it this map
            if (!mappedTermUuids.add(referenceMap.getConceptReferenceTerm().getUuid())) {
                errors.rejectValue("drugReferenceMaps[" + index + "].conceptReferenceTerm", "Drug.drugReferenceMap.termAlreadyMapped", "Cannot map a drug multiple times to the same reference term");
            }
            index++;
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "retireReason", "strength");
    }
}
Also used : Drug(org.openmrs.Drug) ConceptMapType(org.openmrs.ConceptMapType) DrugReferenceMap(org.openmrs.DrugReferenceMap) ConceptReferenceTerm(org.openmrs.ConceptReferenceTerm) HashSet(java.util.HashSet)

Example 7 with ConceptMapType

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

the class ConceptMapTypeValidatorTest method validate_shouldFailIfTheNameIsAnEmptyString.

/**
 * @see ConceptMapTypeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailIfTheNameIsAnEmptyString() {
    ConceptMapType mapType = new ConceptMapType();
    mapType.setName("");
    Errors errors = new BindException(mapType, "mapType");
    new ConceptMapTypeValidator().validate(mapType, errors);
    Assert.assertEquals(true, errors.hasFieldErrors("name"));
}
Also used : Errors(org.springframework.validation.Errors) ConceptMapType(org.openmrs.ConceptMapType) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 8 with ConceptMapType

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

the class ConceptMapTypeValidatorTest method validate_shouldFailValidationIfFieldLengthsAreNotCorrect.

/**
 * @see ConceptMapTypeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() {
    ConceptMapType mapType = new ConceptMapType();
    mapType.setName("unique-name");
    mapType.setDescription("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    mapType.setRetireReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    Errors errors = new BindException(mapType, "mapType");
    new ConceptMapTypeValidator().validate(mapType, errors);
    Assert.assertEquals(true, errors.hasFieldErrors("description"));
    Assert.assertEquals(true, errors.hasFieldErrors("retireReason"));
}
Also used : Errors(org.springframework.validation.Errors) ConceptMapType(org.openmrs.ConceptMapType) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 9 with ConceptMapType

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

the class ConceptMapTypeValidatorTest method validate_shouldPassValidationIfFieldLengthsAreCorrect.

/**
 * @see ConceptMapTypeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfFieldLengthsAreCorrect() {
    ConceptMapType mapType = new ConceptMapType();
    mapType.setName("unique-name");
    mapType.setDescription("Description");
    mapType.setRetireReason("RetireReason");
    Errors errors = new BindException(mapType, "mapType");
    new ConceptMapTypeValidator().validate(mapType, errors);
    Assert.assertEquals(false, errors.hasErrors());
}
Also used : Errors(org.springframework.validation.Errors) ConceptMapType(org.openmrs.ConceptMapType) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 10 with ConceptMapType

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

the class ConceptMapTypeValidatorTest method validate_shouldFailIfTheConceptMapTypeNameIsADuplicate.

/**
 * @see ConceptMapTypeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailIfTheConceptMapTypeNameIsADuplicate() {
    ConceptMapType mapType = new ConceptMapType();
    mapType.setName("is a");
    Errors errors = new BindException(mapType, "mapType");
    new ConceptMapTypeValidator().validate(mapType, errors);
    Assert.assertEquals(true, errors.hasFieldErrors("name"));
}
Also used : Errors(org.springframework.validation.Errors) ConceptMapType(org.openmrs.ConceptMapType) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

ConceptMapType (org.openmrs.ConceptMapType)34 Test (org.junit.Test)26 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)26 BindException (org.springframework.validation.BindException)10 Errors (org.springframework.validation.Errors)10 Drug (org.openmrs.Drug)8 ConceptSource (org.openmrs.ConceptSource)6 Criteria (org.hibernate.Criteria)3 ArrayList (java.util.ArrayList)2 Concept (org.openmrs.Concept)2 ConceptClass (org.openmrs.ConceptClass)2 ConceptDatatype (org.openmrs.ConceptDatatype)2 ConceptReferenceTerm (org.openmrs.ConceptReferenceTerm)2 DrugReferenceMap (org.openmrs.DrugReferenceMap)2 DAOException (org.openmrs.api.db.DAOException)2 ConceptBuilder (org.openmrs.module.metadatadeploy.builder.ConceptBuilder)2 ConceptMapBuilder (org.openmrs.module.metadatadeploy.builder.ConceptMapBuilder)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 FlushMode (org.hibernate.FlushMode)1