Search in sources :

Example 11 with ConceptNumeric

use of org.openmrs.ConceptNumeric in project openmrs-module-pihcore by PIH.

the class ConceptDictionaryExportTest method exportData.

/**
 * Intentional issues currently with this, in order to isolate away known issues.  These should be removed as appropriate:
 * - Normalizes concept source name, due to known issue needed in OCL to facilitate source matching
 * - Excludes voided concept names (which are really retired names that may have references), as these are not provided by ocl
 * - Converts answer sort weight from null -> 1.0 if only one answer exists for the concept
 */
@Test
public void exportData() throws Exception {
    // Only run this test if it is being run alone.  This ensures this test will not run in normal build.
    if (props == null) {
        return;
    }
    if (!Context.isSessionOpen()) {
        Context.openSession();
    }
    authenticate();
    List<Map<String, Object>> concepts = new ArrayList<>();
    System.out.println("Pulling concepts");
    int i = 0;
    List<Concept> conceptList = Context.getConceptService().getAllConcepts();
    conceptList.sort(Comparator.comparing(t -> t.getUuid().toUpperCase()));
    List<Concept> conceptsToIgnore = getConceptsToIgnore();
    conceptList.removeAll(conceptsToIgnore);
    for (Concept concept : conceptList) {
        System.out.println(i++ + ": Pulling concept: " + concept.getUuid());
        Map<String, Object> c = new LinkedHashMap<>();
        concepts.add(c);
        c.put("uuid", concept.getUuid());
        c.put("concept_class", concept.getConceptClass().getName());
        c.put("concept_datatype", concept.getDatatype().getName());
        c.put("is_set", BooleanUtils.isTrue(concept.getSet()) ? "true" : "false");
        c.put("version", concept.getVersion() == null ? "" : concept.getVersion());
        c.put("retired", BooleanUtils.isTrue(concept.getRetired()) ? "true" : "false");
        c.put("retireReason", concept.getRetireReason() == null ? "" : concept.getRetireReason());
        if (concept instanceof ConceptNumeric) {
            ConceptNumeric cn = (ConceptNumeric) concept;
            c.put("hi_absolute", cn.getHiAbsolute() == null ? "" : cn.getHiAbsolute().toString());
            c.put("hi_critical", cn.getLowAbsolute() == null ? "" : cn.getLowAbsolute().toString());
            c.put("hi_normal", cn.getHiNormal() == null ? "" : cn.getHiNormal().toString());
            c.put("low_absolute", cn.getLowAbsolute() == null ? "" : cn.getLowAbsolute().toString());
            c.put("low_critical", cn.getLowCritical() == null ? "" : cn.getLowCritical().toString());
            c.put("low_normal", cn.getLowNormal() == null ? "" : cn.getLowNormal().toString());
            c.put("units", cn.getUnits() == null ? "" : cn.getUnits());
            c.put("allow_decimal", BooleanUtils.isTrue(cn.getAllowDecimal()) ? "true" : "false");
            c.put("display_precision", cn.getHiAbsolute() == null ? "" : cn.getHiAbsolute().toString());
        }
        if (concept instanceof ConceptComplex) {
            ConceptComplex cc = (ConceptComplex) concept;
            c.put("handler", cc.getHandler());
        }
        List<ConceptName> names = new ArrayList<>(concept.getNames(true));
        names.sort(Comparator.comparing(ConceptName::getName).thenComparing(cn -> cn.getConceptNameType() == null ? "" : cn.getConceptNameType().name()).thenComparing(cn -> cn.getLocale().toString()).thenComparing(ConceptName::getLocalePreferred));
        List<Map<String, Object>> nameList = new ArrayList<>();
        for (ConceptName name : names) {
            if (BooleanUtils.isNotTrue(name.getVoided())) {
                Map<String, Object> n = new LinkedHashMap<>();
                n.put("name", name.getName());
                n.put("locale", name.getLocale().toString());
                n.put("locale_preferred", BooleanUtils.isTrue(name.getLocalePreferred()) ? "true" : "false");
                n.put("type", name.getConceptNameType() == null ? "" : name.getConceptNameType().name());
                n.put("voided", BooleanUtils.isTrue(name.getVoided()) ? "true" : "false");
                nameList.add(n);
            }
        }
        c.put("names", nameList);
        List<ConceptDescription> descriptions = new ArrayList<>(concept.getDescriptions());
        descriptions.sort(Comparator.comparing(ConceptDescription::getDescription));
        List<Map<String, Object>> descriptionList = new ArrayList<>();
        for (ConceptDescription description : descriptions) {
            Map<String, Object> n = new LinkedHashMap<>();
            n.put("description", description.getDescription());
            n.put("locale", description.getLocale().toString());
            descriptionList.add(n);
        }
        c.put("descriptions", descriptionList);
        List<ConceptAnswer> answers = new ArrayList<>(concept.getAnswers(true));
        answers.sort(Comparator.comparingDouble(ConceptAnswer::getSortWeight).thenComparing(ca -> ca.getAnswerConcept().getUuid()));
        List<Map<String, Object>> answerList = new ArrayList<>();
        for (ConceptAnswer ca : answers) {
            if (!conceptsToIgnore.contains(ca.getAnswerConcept())) {
                Map<String, Object> n = new LinkedHashMap<>();
                n.put("answerConcept", ca.getAnswerConcept().getUuid());
                n.put("sortWeight", ca.getSortWeight() == null ? (answers.size() == 1 ? "1.0" : "") : ca.getSortWeight().toString());
                answerList.add(n);
            }
        }
        c.put("answers", answerList);
        List<ConceptSet> setMembers = new ArrayList<>(concept.getConceptSets());
        setMembers.sort(Comparator.comparingDouble(ConceptSet::getSortWeight).thenComparing(cs -> cs.getConcept().getUuid()));
        List<Map<String, Object>> setMemberList = new ArrayList<>();
        for (ConceptSet cs : setMembers) {
            if (!conceptsToIgnore.contains(cs.getConcept())) {
                Map<String, Object> n = new LinkedHashMap<>();
                n.put("concept", cs.getConcept().getUuid());
                n.put("sortWeight", cs.getSortWeight() == null ? "" : cs.getSortWeight().toString());
                setMemberList.add(n);
            }
        }
        c.put("setMembers", setMemberList);
        List<ConceptMap> mappings = new ArrayList<>(concept.getConceptMappings());
        mappings.sort(Comparator.comparing((ConceptMap m) -> m.getConceptReferenceTerm().getConceptSource().getName()).thenComparing(m -> m.getConceptMapType().getName()).thenComparing(m -> m.getConceptReferenceTerm().getCode()));
        List<Map<String, Object>> mappingList = new ArrayList<>();
        for (ConceptMap cm : mappings) {
            Map<String, Object> n = new LinkedHashMap<>();
            n.put("source", normalizeSourceName(cm.getConceptReferenceTerm().getConceptSource().getName()));
            n.put("type", cm.getConceptMapType().getName());
            n.put("code", cm.getConceptReferenceTerm().getCode());
            mappingList.add(n);
        }
        c.put("mappings", mappingList);
    }
    File exportFile = new File(EXPORT_DIR, EXPORT_FILENAME);
    System.out.println("Writing concepts to: " + exportFile);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.writerWithDefaultPrettyPrinter().writeValue(exportFile, concepts);
    System.out.println("Concepts exported successfully");
}
Also used : DbSessionFactory(org.openmrs.api.db.hibernate.DbSessionFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ConceptAnswer(org.openmrs.ConceptAnswer) ArrayList(java.util.ArrayList) BooleanUtils(org.apache.commons.lang.BooleanUtils) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConceptMap(org.openmrs.ConceptMap) SkipBaseSetup(org.openmrs.test.SkipBaseSetup) ConceptSet(org.openmrs.ConceptSet) Properties(java.util.Properties) BaseModuleContextSensitiveTest(org.openmrs.test.BaseModuleContextSensitiveTest) Test(org.junit.Test) ConceptName(org.openmrs.ConceptName) FileInputStream(java.io.FileInputStream) File(java.io.File) List(java.util.List) Concept(org.openmrs.Concept) Ignore(org.junit.Ignore) ConceptComplex(org.openmrs.ConceptComplex) ConceptDescription(org.openmrs.ConceptDescription) ConceptNumeric(org.openmrs.ConceptNumeric) Comparator(java.util.Comparator) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Context(org.openmrs.api.context.Context) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ConceptSet(org.openmrs.ConceptSet) ConceptName(org.openmrs.ConceptName) ConceptDescription(org.openmrs.ConceptDescription) ConceptMap(org.openmrs.ConceptMap) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Concept(org.openmrs.Concept) ConceptAnswer(org.openmrs.ConceptAnswer) ConceptNumeric(org.openmrs.ConceptNumeric) ConceptComplex(org.openmrs.ConceptComplex) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConceptMap(org.openmrs.ConceptMap) File(java.io.File) BaseModuleContextSensitiveTest(org.openmrs.test.BaseModuleContextSensitiveTest) Test(org.junit.Test)

Example 12 with ConceptNumeric

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

the class ConceptServiceTest method saveConcept_shouldSaveNonConceptNumericObjectAsConceptNumeric.

/**
 * @see ConceptService#saveConcept(Concept)
 */
@Test
public void saveConcept_shouldSaveNonConceptNumericObjectAsConceptNumeric() {
    executeDataSet(INITIAL_CONCEPTS_XML);
    // this tests saving a current concept as a newly changed conceptnumeric
    // assumes there is already a concept in the database
    // with a concept id of #1
    ConceptNumeric cn = new ConceptNumeric(1);
    cn.setDatatype(new ConceptDatatype(1));
    cn.setConceptClass(new ConceptClass(1));
    cn.addName(new ConceptName("a new conceptnumeric", Locale.US));
    cn.addDescription(new ConceptDescription("some description", null));
    cn.setHiAbsolute(20.0);
    conceptService.saveConcept(cn);
    Concept firstConcept = conceptService.getConceptNumeric(1);
    firstConcept.addDescription(new ConceptDescription("some description", null));
    assertEquals("a new conceptnumeric", firstConcept.getName(Locale.US).getName());
    assertTrue(firstConcept instanceof ConceptNumeric);
    ConceptNumeric firstConceptNumeric = (ConceptNumeric) firstConcept;
    assertEquals(20.0, firstConceptNumeric.getHiAbsolute(), 0);
}
Also used : OpenmrsMatchers.hasConcept(org.openmrs.test.OpenmrsMatchers.hasConcept) Concept(org.openmrs.Concept) ConceptClass(org.openmrs.ConceptClass) ConceptNumeric(org.openmrs.ConceptNumeric) ConceptName(org.openmrs.ConceptName) ConceptDescription(org.openmrs.ConceptDescription) ConceptDatatype(org.openmrs.ConceptDatatype) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 13 with ConceptNumeric

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

the class AuditableInterceptorTest method onFlushDirty_shouldNotFailWhenTheDaemonUserModifiesSomething.

/**
 * @see AuditableInterceptor#onFlushDirty(Object,Serializable,null,null,null,null)
 */
@Test
public void onFlushDirty_shouldNotFailWhenTheDaemonUserModifiesSomething() throws Throwable {
    new AsDaemonTask(new AbstractTask() {

        @Override
        public void execute() {
            ConceptNumeric weight = Context.getConceptService().getConceptNumeric(5089);
            Date dateChangedBefore = weight.getDateChanged();
            weight.setHiAbsolute(75d);
            Context.getConceptService().saveConcept(weight);
            Assert.assertNotSame(dateChangedBefore, weight.getDateChanged());
        }
    }).runTheTask();
}
Also used : AbstractTask(org.openmrs.scheduler.tasks.AbstractTask) ConceptNumeric(org.openmrs.ConceptNumeric) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 14 with ConceptNumeric

use of org.openmrs.ConceptNumeric 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 15 with ConceptNumeric

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

the class OpenmrsUtilUnitTest method isValidNumericValue_shouldReturnTrueIfValueIsEqualToHiRangeLimit.

@Test
public void isValidNumericValue_shouldReturnTrueIfValueIsEqualToHiRangeLimit() {
    ConceptNumeric concept = new ConceptNumeric();
    concept.setHiCritical(4.34d);
    concept.setLowCritical(3.67d);
    assertFalse(OpenmrsUtil.isInAbsoluteNumericRange(4.34f, concept));
}
Also used : ConceptNumeric(org.openmrs.ConceptNumeric) Test(org.junit.Test)

Aggregations

ConceptNumeric (org.openmrs.ConceptNumeric)39 Test (org.junit.Test)36 Concept (org.openmrs.Concept)7 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)7 OpenmrsMatchers.hasConcept (org.openmrs.test.OpenmrsMatchers.hasConcept)5 ConceptDatatype (org.openmrs.ConceptDatatype)4 ConceptDescription (org.openmrs.ConceptDescription)4 ConceptName (org.openmrs.ConceptName)4 ConceptClass (org.openmrs.ConceptClass)3 ConceptComplex (org.openmrs.ConceptComplex)3 Query (org.hibernate.Query)2 SQLQuery (org.hibernate.SQLQuery)2 LuceneQuery (org.openmrs.api.db.hibernate.search.LuceneQuery)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1