Search in sources :

Example 1 with DrugReferenceMap

use of org.openmrs.DrugReferenceMap 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 2 with DrugReferenceMap

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

the class DrugValidatorTest method validate_shouldInvokeConceptReferenceTermValidatorIfTermOnDrugReferenceMapIsNew.

/**
 * @see DrugValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldInvokeConceptReferenceTermValidatorIfTermOnDrugReferenceMapIsNew() {
    Drug drug = new Drug();
    drug.addDrugReferenceMap(new DrugReferenceMap(new ConceptReferenceTerm(), null));
    Errors errors = new BindException(drug, "drug");
    new DrugValidator().validate(drug, errors);
    // reference term validator should have been called which should reject a null code
    Assert.assertTrue(errors.hasFieldErrors("drugReferenceMaps[0].conceptReferenceTerm.code"));
}
Also used : Drug(org.openmrs.Drug) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) DrugReferenceMap(org.openmrs.DrugReferenceMap) ConceptReferenceTerm(org.openmrs.ConceptReferenceTerm) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 3 with DrugReferenceMap

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

the class DrugValidatorTest method validate_shouldFailIfConceptReferenceTermOnDrugReferenceMapIsNull.

/**
 * @see DrugValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailIfConceptReferenceTermOnDrugReferenceMapIsNull() {
    Drug drug = new Drug();
    drug.addDrugReferenceMap(new DrugReferenceMap());
    Errors errors = new BindException(drug, "drug");
    new DrugValidator().validate(drug, errors);
    Assert.assertTrue(errors.hasFieldErrors("drugReferenceMaps[0].conceptReferenceTerm"));
}
Also used : Drug(org.openmrs.Drug) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) DrugReferenceMap(org.openmrs.DrugReferenceMap) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 4 with DrugReferenceMap

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

the class DrugValidatorTest method validate_shouldPassIfAllFieldsAreCorrect.

/**
 * @see DrugValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldPassIfAllFieldsAreCorrect() {
    Drug drug = new Drug();
    drug.addDrugReferenceMap(new DrugReferenceMap(conceptService.getConceptReferenceTerm(1), conceptService.getConceptMapType(1)));
    Errors errors = new BindException(drug, "drug");
    new DrugValidator().validate(drug, errors);
    Assert.assertFalse(errors.hasFieldErrors());
}
Also used : Drug(org.openmrs.Drug) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) DrugReferenceMap(org.openmrs.DrugReferenceMap) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 5 with DrugReferenceMap

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

the class DrugValidatorTest method validate_shouldFailIfDrugOnDrugReferenceMapIsNull.

/**
 * @see DrugValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailIfDrugOnDrugReferenceMapIsNull() {
    Drug drug = new Drug();
    drug.setDrugReferenceMaps(Collections.singleton(new DrugReferenceMap()));
    Errors errors = new BindException(drug, "drug");
    new DrugValidator().validate(drug, errors);
    Assert.assertTrue(errors.hasFieldErrors("drugReferenceMaps[0].drug"));
}
Also used : Drug(org.openmrs.Drug) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) DrugReferenceMap(org.openmrs.DrugReferenceMap) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Drug (org.openmrs.Drug)9 DrugReferenceMap (org.openmrs.DrugReferenceMap)9 Test (org.junit.Test)8 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)8 BindException (org.springframework.validation.BindException)8 Errors (org.springframework.validation.Errors)8 ConceptMapType (org.openmrs.ConceptMapType)2 ConceptReferenceTerm (org.openmrs.ConceptReferenceTerm)2 HashSet (java.util.HashSet)1