Search in sources :

Example 11 with ConceptName

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

the class ConceptServiceImpl method saveConcept.

/**
 * @see org.openmrs.api.ConceptService#saveConcept(org.openmrs.Concept)
 * @should return the concept with new conceptID if creating new concept
 * @should return the concept with same conceptID if updating existing concept
 * @should leave preferred name preferred if set
 * @should set default preferred name to fully specified first
 * @should not set default preferred name to short or index terms
 * @should force set flag if set members exist
 */
@Override
public Concept saveConcept(Concept concept) throws APIException {
    ensureConceptMapTypeIsSet(concept);
    CustomDatatypeUtil.saveAttributesIfNecessary(concept);
    // make sure the administrator hasn't turned off concept editing
    checkIfLocked();
    checkIfDatatypeCanBeChanged(concept);
    List<ConceptName> changedConceptNames = null;
    Map<String, ConceptName> uuidClonedConceptNameMap = null;
    if (concept.getConceptId() != null) {
        uuidClonedConceptNameMap = new HashMap<>();
        for (ConceptName conceptName : concept.getNames()) {
            // ignore newly added names
            if (conceptName.getConceptNameId() != null) {
                ConceptName clone = cloneConceptName(conceptName);
                clone.setConceptNameId(null);
                uuidClonedConceptNameMap.put(conceptName.getUuid(), clone);
                if (hasNameChanged(conceptName)) {
                    if (changedConceptNames == null) {
                        changedConceptNames = new ArrayList<>();
                    }
                    changedConceptNames.add(conceptName);
                } else {
                    // put back the concept name id
                    clone.setConceptNameId(conceptName.getConceptNameId());
                    // Use the cloned version
                    try {
                        BeanUtils.copyProperties(conceptName, clone);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        log.error(errorMessage, e);
                    }
                }
            }
        }
    }
    if (CollectionUtils.isNotEmpty(changedConceptNames)) {
        for (ConceptName changedName : changedConceptNames) {
            // void old concept name
            changedName.setVoided(true);
            changedName.setDateVoided(new Date());
            changedName.setVoidedBy(Context.getAuthenticatedUser());
            changedName.setVoidReason(Context.getMessageSourceService().getMessage("Concept.name.voidReason.nameChanged"));
            makeVoidedNameSynonym(changedName);
            makeLocaleNotPreferred(changedName);
            // create a new concept name from the matching cloned
            // conceptName
            ConceptName clone = uuidClonedConceptNameMap.get(changedName.getUuid());
            clone.setUuid(UUID.randomUUID().toString());
            clone.setDateCreated(null);
            clone.setCreator(null);
            concept.addName(clone);
        }
    }
    ensurePreferredNameForLocale(concept);
    logConceptChangedData(concept);
    // force isSet when concept has members
    if (!concept.getSet() && (!concept.getSetMembers().isEmpty())) {
        concept.setSet(true);
    }
    return dao.saveConcept(concept);
}
Also used : ConceptName(org.openmrs.ConceptName) InvocationTargetException(java.lang.reflect.InvocationTargetException) Date(java.util.Date)

Example 12 with ConceptName

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

the class ConceptValidatorTest method validate_shouldNotFailIfAnyDescriptionIsNotEnteredWhileCreatingANewConcept.

@Test
public void validate_shouldNotFailIfAnyDescriptionIsNotEnteredWhileCreatingANewConcept() {
    concept.addName(new ConceptName("some name", Context.getLocale()));
    validator.validate(concept, errors);
    assertThat(errors, not(hasFieldErrors("description")));
}
Also used : ConceptName(org.openmrs.ConceptName) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 13 with ConceptName

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

the class ConceptValidatorTest method validate_shouldPassIfTheConceptHasASynonymThatIsAlsoAShortName.

@Test
public void validate_shouldPassIfTheConceptHasASynonymThatIsAlsoAShortName() {
    concept.addName(new ConceptName("CD4", Context.getLocale()));
    concept.addDescription(new ConceptDescription("some description", null));
    concept.setConceptClass(new ConceptClass());
    concept.setDatatype(new ConceptDatatype());
    // Add the short name. Because the short name is not counted as a Synonym.
    // ConceptValidator will not record any errors.
    ConceptName name = new ConceptName("CD4", Context.getLocale());
    name.setConceptNameType(ConceptNameType.SHORT);
    concept.addName(name);
    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)

Example 14 with ConceptName

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

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

the class ConceptValidatorTest method validate_shouldNotFailIfBlankConceptDescriptionIsPassed.

@Test
public void validate_shouldNotFailIfBlankConceptDescriptionIsPassed() {
    concept.addName(new ConceptName("some name", Context.getLocale()));
    concept.addDescription(new ConceptDescription("   ", null));
    validator.validate(concept, errors);
    assertThat(errors, not(hasFieldErrors("description")));
}
Also used : ConceptName(org.openmrs.ConceptName) ConceptDescription(org.openmrs.ConceptDescription) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

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