Search in sources :

Example 51 with ConceptName

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

the class ConceptServiceImpl method purgeConcept.

/**
 * @see org.openmrs.api.ConceptService#purgeConcept(Concept)
 */
@Override
public void purgeConcept(Concept concept) throws APIException {
    checkIfLocked();
    if (concept.getConceptId() != null) {
        for (ConceptName conceptName : concept.getNames()) {
            if (hasAnyObservation(conceptName)) {
                throw new ConceptNameInUseException("Can't delete concept with id : " + concept.getConceptId() + " because it has a name '" + conceptName.getName() + "' which is being used by some observation(s)");
            }
        }
    }
    dao.purgeConcept(concept);
}
Also used : ConceptName(org.openmrs.ConceptName) ConceptNameInUseException(org.openmrs.api.ConceptNameInUseException)

Example 52 with ConceptName

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

the class ConceptServiceImpl method ensurePreferredNameForLocale.

private void ensurePreferredNameForLocale(Concept concept) {
    // Ensure if there's a name for a locale that at least one suitable name is marked preferred in that locale
    // Order of preference is:
    // 1) any name that concept.getPreferredName returns
    // 2) fully specified name
    // 3) any synonym
    // short name and index terms are never preferred.
    Set<Locale> checkedLocales = new HashSet<>();
    for (ConceptName n : concept.getNames()) {
        Locale locale = n.getLocale();
        if (checkedLocales.contains(locale)) {
            // we've already checked this locale
            continue;
        }
        // getPreferredName(locale) returns any name marked preferred,
        // or the fullySpecifiedName even if not marked preferred
        ConceptName possiblePreferredName = concept.getPreferredName(locale);
        if (possiblePreferredName != null) {
        // do nothing yet, but stick around to setLocalePreferred(true)
        } else if (concept.getFullySpecifiedName(locale) != null) {
            possiblePreferredName = concept.getFullySpecifiedName(locale);
        } else if (!CollectionUtils.isEmpty(concept.getSynonyms(locale))) {
            concept.getSynonyms(locale).iterator().next().setLocalePreferred(true);
        }
        if (possiblePreferredName != null) {
            // there may have been none
            possiblePreferredName.setLocalePreferred(true);
        }
        checkedLocales.add(locale);
    }
}
Also used : Locale(java.util.Locale) ConceptName(org.openmrs.ConceptName) HashSet(java.util.HashSet)

Example 53 with ConceptName

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

the class ConceptServiceImpl method mapConceptProposalToConcept.

/**
 * @see org.openmrs.api.ConceptService#mapConceptProposalToConcept(ConceptProposal, Concept, Locale)
 */
@Override
public Concept mapConceptProposalToConcept(ConceptProposal cp, Concept mappedConcept, Locale locale) throws APIException {
    if (cp.getState().equals(OpenmrsConstants.CONCEPT_PROPOSAL_REJECT)) {
        cp.rejectConceptProposal();
        Context.getConceptService().saveConceptProposal(cp);
        return null;
    }
    if (mappedConcept == null) {
        throw new APIException("Concept.mapped.illegal", (Object[]) null);
    }
    ConceptName conceptName = null;
    if (cp.getState().equals(OpenmrsConstants.CONCEPT_PROPOSAL_CONCEPT) || !StringUtils.hasText(cp.getFinalText())) {
        cp.setState(OpenmrsConstants.CONCEPT_PROPOSAL_CONCEPT);
        cp.setFinalText("");
    } else if (cp.getState().equals(OpenmrsConstants.CONCEPT_PROPOSAL_SYNONYM)) {
        checkIfLocked();
        String finalText = cp.getFinalText();
        conceptName = new ConceptName(finalText, null);
        conceptName.setConcept(mappedConcept);
        conceptName.setLocale(locale == null ? Context.getLocale() : locale);
        conceptName.setDateCreated(new Date());
        conceptName.setCreator(Context.getAuthenticatedUser());
        // If this is pre 1.9
        if (conceptName.getUuid() == null) {
            conceptName.setUuid(UUID.randomUUID().toString());
        }
        mappedConcept.addName(conceptName);
        mappedConcept.setChangedBy(Context.getAuthenticatedUser());
        mappedConcept.setDateChanged(new Date());
        ValidateUtil.validate(mappedConcept);
        Context.getConceptService().saveConcept(mappedConcept);
    }
    cp.setMappedConcept(mappedConcept);
    if (cp.getObsConcept() != null) {
        Obs ob = new Obs();
        ob.setEncounter(cp.getEncounter());
        ob.setConcept(cp.getObsConcept());
        ob.setValueCoded(cp.getMappedConcept());
        if (cp.getState().equals(OpenmrsConstants.CONCEPT_PROPOSAL_SYNONYM)) {
            ob.setValueCodedName(conceptName);
        }
        ob.setCreator(Context.getAuthenticatedUser());
        ob.setDateCreated(new Date());
        ob.setObsDatetime(cp.getEncounter().getEncounterDatetime());
        ob.setLocation(cp.getEncounter().getLocation());
        ob.setPerson(cp.getEncounter().getPatient());
        if (ob.getUuid() == null) {
            ob.setUuid(UUID.randomUUID().toString());
        }
        Context.getObsService().saveObs(ob, null);
        cp.setObs(ob);
    }
    return mappedConcept;
}
Also used : Obs(org.openmrs.Obs) APIException(org.openmrs.api.APIException) ConceptName(org.openmrs.ConceptName) Date(java.util.Date)

Example 54 with ConceptName

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

the class ConceptServiceTest method saveConcept_shouldNotFailWhenADuplicateNameIsEditedToAUniqueValue.

/**
 * @see ConceptService#saveConcept(Concept)
 */
@Test
public void saveConcept_shouldNotFailWhenADuplicateNameIsEditedToAUniqueValue() {
    // Insert a row to simulate an existing duplicate fully specified/preferred name that needs to be edited
    executeDataSet("org/openmrs/api/include/ConceptServiceTest-conceptWithDuplicateName.xml");
    Concept conceptToEdit = conceptService.getConcept(10000);
    Locale locale = new Locale("en", "GB");
    conceptToEdit.addDescription(new ConceptDescription("some description", locale));
    ConceptName duplicateNameToEdit = conceptToEdit.getFullySpecifiedName(locale);
    // Ensure the name is a duplicate in it's locale
    Concept otherConcept = conceptService.getConcept(5497);
    Assert.assertTrue(duplicateNameToEdit.getName().equalsIgnoreCase(otherConcept.getFullySpecifiedName(locale).getName()));
    duplicateNameToEdit.setName("new unique name");
    conceptService.saveConcept(conceptToEdit);
}
Also used : OpenmrsMatchers.hasConcept(org.openmrs.test.OpenmrsMatchers.hasConcept) Concept(org.openmrs.Concept) Locale(java.util.Locale) ConceptName(org.openmrs.ConceptName) ConceptDescription(org.openmrs.ConceptDescription) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 55 with ConceptName

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

the class ConceptServiceTest method saveConcept_shouldSaveAConceptNumericAsAConcept.

/**
 * @see ConceptService#saveConcept(Concept)
 */
@Test
public void saveConcept_shouldSaveAConceptNumericAsAConcept() {
    executeDataSet(INITIAL_CONCEPTS_XML);
    // This will automatically add the given locale to the list of allowed locales
    Context.setLocale(Locale.US);
    // this tests saving a previously conceptnumeric as just a concept
    Concept c2 = new Concept(2);
    ConceptName cn = new ConceptName("not a numeric anymore", Locale.US);
    c2.addName(cn);
    c2.addDescription(new ConceptDescription("some description", null));
    c2.setDatatype(new ConceptDatatype(3));
    c2.setConceptClass(new ConceptClass(1));
    conceptService.saveConcept(c2);
    Concept secondConcept = conceptService.getConcept(2);
    // this will probably still be a ConceptNumeric object.  what to do about that?
    // revisit this problem when discriminators are in place
    // assertFalse(secondConcept instanceof ConceptNumeric);
    // this shouldn't think its a conceptnumeric object though
    assertFalse(secondConcept.isNumeric());
    assertEquals("not a numeric anymore", secondConcept.getName(Locale.US).getName());
}
Also used : OpenmrsMatchers.hasConcept(org.openmrs.test.OpenmrsMatchers.hasConcept) Concept(org.openmrs.Concept) ConceptClass(org.openmrs.ConceptClass) ConceptName(org.openmrs.ConceptName) ConceptDescription(org.openmrs.ConceptDescription) ConceptDatatype(org.openmrs.ConceptDatatype) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Aggregations

ConceptName (org.openmrs.ConceptName)100 Test (org.junit.Test)78 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)71 Concept (org.openmrs.Concept)62 ConceptDescription (org.openmrs.ConceptDescription)42 ConceptDatatype (org.openmrs.ConceptDatatype)34 ConceptClass (org.openmrs.ConceptClass)33 Locale (java.util.Locale)32 OpenmrsMatchers.hasConcept (org.openmrs.test.OpenmrsMatchers.hasConcept)22 ArrayList (java.util.ArrayList)11 Date (java.util.Date)9 Obs (org.openmrs.Obs)9 BindException (org.springframework.validation.BindException)8 ConceptMap (org.openmrs.ConceptMap)7 Errors (org.springframework.validation.Errors)7 LinkedList (java.util.LinkedList)6 List (java.util.List)6 Patient (org.openmrs.Patient)6 Encounter (org.openmrs.Encounter)5 OrderFrequency (org.openmrs.OrderFrequency)5