Search in sources :

Example 1 with ConceptReferenceTerm

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

the class HibernateConceptDAO method getConceptReferenceTermByCode.

/**
 * @see org.openmrs.api.db.ConceptDAO#getConceptReferenceTermByCode(java.lang.String,
 *      org.openmrs.ConceptSource)
 */
@SuppressWarnings("rawtypes")
@Override
public ConceptReferenceTerm getConceptReferenceTermByCode(String code, ConceptSource conceptSource) throws DAOException {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ConceptReferenceTerm.class);
    criteria.add(Restrictions.eq("code", code));
    criteria.add(Restrictions.eq("conceptSource", conceptSource));
    List terms = criteria.list();
    if (terms.isEmpty()) {
        return null;
    } else if (terms.size() > 1) {
        throw new APIException("ConceptReferenceTerm.foundMultipleTermsWithCodeInSource", new Object[] { code, conceptSource.getName() });
    }
    return (ConceptReferenceTerm) terms.get(0);
}
Also used : APIException(org.openmrs.api.APIException) List(java.util.List) ArrayList(java.util.ArrayList) OpenmrsObject(org.openmrs.OpenmrsObject) Criteria(org.hibernate.Criteria) ConceptReferenceTerm(org.openmrs.ConceptReferenceTerm)

Example 2 with ConceptReferenceTerm

use of org.openmrs.ConceptReferenceTerm 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 3 with ConceptReferenceTerm

use of org.openmrs.ConceptReferenceTerm 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 4 with ConceptReferenceTerm

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

the class ConceptValidatorTest method validate_shouldNotFailIfATermHasTwoNewMappingsOnIt.

@Test
public void validate_shouldNotFailIfATermHasTwoNewMappingsOnIt() {
    concept.addName(new ConceptName("my name", Context.getLocale()));
    ConceptReferenceTerm newTerm = new ConceptReferenceTerm(conceptService.getConceptSource(1), "1234", "term one two three four");
    ConceptMap map1 = new ConceptMap(newTerm, conceptService.getConceptMapType(1));
    concept.addConceptMapping(map1);
    ConceptReferenceTerm newTermTwo = new ConceptReferenceTerm(conceptService.getConceptSource(1), "12345", "term one two three four five");
    ConceptMap map2 = new ConceptMap(newTermTwo, conceptService.getConceptMapType(1));
    concept.addConceptMapping(map2);
    validator.validate(concept, errors);
    assertThat(errors, not(hasFieldErrors("conceptMappings[1]")));
}
Also used : ConceptName(org.openmrs.ConceptName) ConceptMap(org.openmrs.ConceptMap) ConceptReferenceTerm(org.openmrs.ConceptReferenceTerm) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 5 with ConceptReferenceTerm

use of org.openmrs.ConceptReferenceTerm 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)

Aggregations

ConceptReferenceTerm (org.openmrs.ConceptReferenceTerm)36 Test (org.junit.Test)32 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)31 BindException (org.springframework.validation.BindException)19 Errors (org.springframework.validation.Errors)19 ConceptSource (org.openmrs.ConceptSource)6 ConceptReferenceTermMap (org.openmrs.ConceptReferenceTermMap)5 Ignore (org.junit.Ignore)3 APIException (org.openmrs.api.APIException)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Criteria (org.hibernate.Criteria)2 ConceptMapType (org.openmrs.ConceptMapType)2 Drug (org.openmrs.Drug)2 DrugReferenceMap (org.openmrs.DrugReferenceMap)2 OpenmrsObject (org.openmrs.OpenmrsObject)2 LinkedHashSet (java.util.LinkedHashSet)1 ConceptMap (org.openmrs.ConceptMap)1 ConceptName (org.openmrs.ConceptName)1