Search in sources :

Example 31 with ConceptDatatype

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

the class ObsValidator method validateHelper.

/**
 * Checks whether obs has all required values, and also checks to make sure that no obs group
 * contains any of its ancestors
 *
 * @param obs
 * @param errors
 * @param ancestors
 * @param atRootNode whether or not this is the obs that validate() was originally called on. If
 *            not then we shouldn't reject fields by name.
 */
private void validateHelper(Obs obs, Errors errors, List<Obs> ancestors, boolean atRootNode) {
    if (obs.getPersonId() == null) {
        errors.rejectValue("person", "error.null");
    }
    if (obs.getObsDatetime() == null) {
        errors.rejectValue("obsDatetime", "error.null");
    }
    // if this is an obs group (i.e., parent) make sure that it has no values (other than valueGroupId) set
    if (obs.hasGroupMembers()) {
        if (obs.getValueCoded() != null) {
            errors.rejectValue("valueCoded", "error.not.null");
        }
        if (obs.getValueDrug() != null) {
            errors.rejectValue("valueDrug", "error.not.null");
        }
        if (obs.getValueDatetime() != null) {
            errors.rejectValue("valueDatetime", "error.not.null");
        }
        if (obs.getValueNumeric() != null) {
            errors.rejectValue("valueNumeric", "error.not.null");
        }
        if (obs.getValueModifier() != null) {
            errors.rejectValue("valueModifier", "error.not.null");
        }
        if (obs.getValueText() != null) {
            errors.rejectValue("valueText", "error.not.null");
        }
        if (obs.getValueBoolean() != null) {
            errors.rejectValue("valueBoolean", "error.not.null");
        }
        if (obs.getValueComplex() != null) {
            errors.rejectValue("valueComplex", "error.not.null");
        }
    } else // if this is NOT an obs group, make sure that it has at least one value set (not counting obsGroupId)
    if (obs.getValueBoolean() == null && obs.getValueCoded() == null && obs.getValueCodedName() == null && obs.getValueComplex() == null && obs.getValueDatetime() == null && obs.getValueDrug() == null && obs.getValueModifier() == null && obs.getValueNumeric() == null && obs.getValueText() == null && obs.getComplexData() == null) {
        errors.reject("error.noValue");
    }
    // make sure there is a concept associated with the obs
    Concept c = obs.getConcept();
    if (c == null) {
        errors.rejectValue("concept", "error.null");
    } else // if there is a concept, and this isn't a group, perform validation tests specific to the concept datatype
    if (!obs.hasGroupMembers()) {
        ConceptDatatype dt = c.getDatatype();
        if (dt != null) {
            if (dt.isBoolean() && obs.getValueBoolean() == null) {
                if (atRootNode) {
                    errors.rejectValue("valueBoolean", "error.null");
                } else {
                    errors.rejectValue("groupMembers", "Obs.error.inGroupMember");
                }
            } else if (dt.isCoded() && obs.getValueCoded() == null) {
                if (atRootNode) {
                    errors.rejectValue("valueCoded", "error.null");
                } else {
                    errors.rejectValue("groupMembers", "Obs.error.inGroupMember");
                }
            } else if ((dt.isDateTime() || dt.isDate() || dt.isTime()) && obs.getValueDatetime() == null) {
                if (atRootNode) {
                    errors.rejectValue("valueDatetime", "error.null");
                } else {
                    errors.rejectValue("groupMembers", "Obs.error.inGroupMember");
                }
            } else if (dt.isNumeric() && obs.getValueNumeric() == null) {
                if (atRootNode) {
                    errors.rejectValue("valueNumeric", "error.null");
                } else {
                    errors.rejectValue("groupMembers", "Obs.error.inGroupMember");
                }
            } else if (dt.isNumeric()) {
                ConceptNumeric cn = Context.getConceptService().getConceptNumeric(c.getConceptId());
                // If the concept numeric is not precise, the value cannot be a float, so raise an error
                if (!cn.getAllowDecimal() && Math.ceil(obs.getValueNumeric()) != obs.getValueNumeric()) {
                    if (atRootNode) {
                        errors.rejectValue("valueNumeric", "Obs.error.precision");
                    } else {
                        errors.rejectValue("groupMembers", "Obs.error.inGroupMember");
                    }
                }
                // If the number is higher than the absolute range, raise an error
                if (cn.getHiAbsolute() != null && cn.getHiAbsolute() < obs.getValueNumeric()) {
                    if (atRootNode) {
                        errors.rejectValue("valueNumeric", "error.outOfRange.high");
                    } else {
                        errors.rejectValue("groupMembers", "Obs.error.inGroupMember");
                    }
                }
                // If the number is lower than the absolute range, raise an error as well
                if (cn.getLowAbsolute() != null && cn.getLowAbsolute() > obs.getValueNumeric()) {
                    if (atRootNode) {
                        errors.rejectValue("valueNumeric", "error.outOfRange.low");
                    } else {
                        errors.rejectValue("groupMembers", "Obs.error.inGroupMember");
                    }
                }
            } else if (dt.isText() && obs.getValueText() == null) {
                if (atRootNode) {
                    errors.rejectValue("valueText", "error.null");
                } else {
                    errors.rejectValue("groupMembers", "Obs.error.inGroupMember");
                }
            }
            // If valueText is longer than the maxlength, raise an error as well.
            if (dt.isText() && obs.getValueText() != null && obs.getValueText().length() > VALUE_TEXT_MAX_LENGTH) {
                if (atRootNode) {
                    errors.rejectValue("valueText", "error.exceededMaxLengthOfField");
                } else {
                    errors.rejectValue("groupMembers", "Obs.error.inGroupMember");
                }
            }
        } else {
            // dt is null
            errors.rejectValue("concept", "must have a datatype");
        }
    }
    // If an obs fails validation, don't bother checking its children
    if (errors.hasErrors()) {
        return;
    }
    if (ancestors.contains(obs)) {
        errors.rejectValue("groupMembers", "Obs.error.groupContainsItself");
    }
    if (obs.isObsGrouping()) {
        ancestors.add(obs);
        for (Obs child : obs.getGroupMembers()) {
            validateHelper(child, errors, ancestors, false);
        }
        ancestors.remove(ancestors.size() - 1);
    }
    if (obs.getValueCoded() != null && obs.getValueDrug() != null && obs.getValueDrug().getConcept() != null) {
        Concept trueConcept = Context.getConceptService().getTrueConcept();
        Concept falseConcept = Context.getConceptService().getFalseConcept();
        // Ignore if this is not a true or false response since they are stored as coded too
        if (!obs.getValueCoded().equals(trueConcept) && !obs.getValueCoded().equals(falseConcept) && !obs.getValueDrug().getConcept().equals(obs.getValueCoded())) {
            errors.rejectValue("valueDrug", "Obs.error.invalidDrug");
        }
    }
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) ConceptNumeric(org.openmrs.ConceptNumeric) ConceptDatatype(org.openmrs.ConceptDatatype)

Example 32 with ConceptDatatype

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

the class ConceptDatatypeValidator method validate.

/**
 * Checks the form object for any inconsistencies/errors
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should pass validation if description is null or empty or whitespace	 *
 * @should fail validation if name is null or empty or whitespace
 * @should pass validation if all required fields have proper values
 * @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) {
    ConceptDatatype cd = (ConceptDatatype) obj;
    if (cd == null) {
        errors.rejectValue("conceptDatatype", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "hl7Abbreviation", "description", "retireReason");
    }
}
Also used : ConceptDatatype(org.openmrs.ConceptDatatype)

Example 33 with ConceptDatatype

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

the class ConceptValidatorTest method validate_shouldFailIfAnyNameIsANullValue.

@Test
public void validate_shouldFailIfAnyNameIsANullValue() {
    concept.addDescription(new ConceptDescription("some description", null));
    concept.setConceptClass(new ConceptClass(1));
    concept.setDatatype(new ConceptDatatype(1));
    concept.addName(new ConceptName("name", Context.getLocale()));
    concept.addName(new ConceptName(null, Context.getLocale()));
    validator.validate(concept, errors);
    assertThat(errors, hasGlobalErrors("Concept.name.empty"));
}
Also used : ConceptClass(org.openmrs.ConceptClass) ConceptName(org.openmrs.ConceptName) ConceptDescription(org.openmrs.ConceptDescription) ConceptDatatype(org.openmrs.ConceptDatatype) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 34 with ConceptDatatype

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

the class ConceptValidatorTest method validate_shouldFailIfAnyNameIsAnEmptyString.

@Test
public void validate_shouldFailIfAnyNameIsAnEmptyString() {
    concept.addDescription(new ConceptDescription("some description", null));
    concept.setConceptClass(new ConceptClass(1));
    concept.setDatatype(new ConceptDatatype(1));
    concept.addName(new ConceptName("name", Context.getLocale()));
    concept.addName(new ConceptName("", Context.getLocale()));
    validator.validate(concept, errors);
    assertThat(errors, hasGlobalErrors("Concept.name.empty"));
}
Also used : ConceptClass(org.openmrs.ConceptClass) ConceptName(org.openmrs.ConceptName) ConceptDescription(org.openmrs.ConceptDescription) ConceptDatatype(org.openmrs.ConceptDatatype) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 35 with ConceptDatatype

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

the class ConceptValidatorTest method validate_shouldPassValidationIfFieldLengthsAreCorrect.

@Test
public void validate_shouldPassValidationIfFieldLengthsAreCorrect() {
    concept.addName(new ConceptName("CD4", Context.getLocale()));
    concept.addDescription(new ConceptDescription("some description", null));
    concept.setVersion("version");
    concept.setRetireReason("retireReason");
    concept.setConceptClass(new ConceptClass());
    concept.setDatatype(new ConceptDatatype());
    validator.validate(concept, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : ConceptClass(org.openmrs.ConceptClass) ConceptName(org.openmrs.ConceptName) ConceptDescription(org.openmrs.ConceptDescription) ConceptDatatype(org.openmrs.ConceptDatatype) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

ConceptDatatype (org.openmrs.ConceptDatatype)54 Test (org.junit.Test)49 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)45 ConceptClass (org.openmrs.ConceptClass)38 ConceptName (org.openmrs.ConceptName)34 Concept (org.openmrs.Concept)32 ConceptDescription (org.openmrs.ConceptDescription)32 OpenmrsMatchers.hasConcept (org.openmrs.test.OpenmrsMatchers.hasConcept)13 Locale (java.util.Locale)12 BindException (org.springframework.validation.BindException)7 Errors (org.springframework.validation.Errors)7 Date (java.util.Date)4 ConceptNumeric (org.openmrs.ConceptNumeric)4 Obs (org.openmrs.Obs)4 ConceptMap (org.openmrs.ConceptMap)3 ConceptBuilder (org.openmrs.module.emrapi.test.builder.ConceptBuilder)3 ConceptComplex (org.openmrs.ConceptComplex)2 ConceptMapType (org.openmrs.ConceptMapType)2 ConceptSearchResult (org.openmrs.ConceptSearchResult)2 ConceptSource (org.openmrs.ConceptSource)2