Search in sources :

Example 16 with ConceptNameTag

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

the class ConceptNameTagValidator method validate.

/**
 * Checks the form object for any inconsistencies/errors
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if conceptNameTag is null
 * @should fail validation if tag is null or empty or whitespace
 * @should pass validation if tag does not exist and is not null, empty or whitespace
 * @should fail if the concept name tag is a duplicate
 * @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) {
    ConceptNameTag cnt = (ConceptNameTag) obj;
    if (cnt == null) {
        throw new IllegalArgumentException("The parameter obj should not be null");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "tag", "error.name");
        if (cnt.getTag() != null) {
            ConceptNameTag currentTag = Context.getConceptService().getConceptNameTagByName(cnt.getTag());
            if (currentTag != null && !OpenmrsUtil.nullSafeEqualsIgnoreCase(cnt.getUuid(), currentTag.getUuid()) && OpenmrsUtil.nullSafeEqualsIgnoreCase(currentTag.getTag(), cnt.getTag())) {
                errors.rejectValue("tag", "Concept.name.tag.duplicate");
            }
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "tag", "voidReason");
    }
}
Also used : ConceptNameTag(org.openmrs.ConceptNameTag)

Example 17 with ConceptNameTag

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

the class ConceptNameSaveHandler method handle.

/**
 * This method does a lookup on all tag name for all child {@link ConceptNameTag}s that have a
 * null {@link ConceptNameTag#getConceptNameTagId()}.
 *
 * @see org.openmrs.api.handler.RequiredDataHandler#handle(org.openmrs.OpenmrsObject,
 *      org.openmrs.User, java.util.Date, java.lang.String)
 * @should not fail if tags is null
 * @should replace tags without ids with database fetched tag
 * @should not replace tags without ids that are not in the database
 * @should not replace tags that have ids
 */
@Override
public void handle(ConceptName conceptName, User currentUser, Date currentDate, String reason) {
    // put Integer conceptNameTagIds onto ConceptNameTags that are missing them
    if (conceptName.getTags() != null) {
        Collection<ConceptNameTag> replacementTags = new ArrayList<>();
        Iterator<ConceptNameTag> tagsIt = conceptName.getTags().iterator();
        while (tagsIt.hasNext()) {
            ConceptNameTag tag = tagsIt.next();
            if (tag.getConceptNameTagId() == null) {
                ConceptNameTag replacementTag = Context.getConceptService().getConceptNameTagByName(tag.getTag());
                if (replacementTag != null) {
                    tagsIt.remove();
                    replacementTags.add(replacementTag);
                }
            }
        }
        if (!replacementTags.isEmpty()) {
            conceptName.getTags().addAll(replacementTags);
        }
    }
}
Also used : ConceptNameTag(org.openmrs.ConceptNameTag) ArrayList(java.util.ArrayList)

Example 18 with ConceptNameTag

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

the class ConceptServiceTest method getConceptNameTagByUuid_shouldFindObjectGivenValidUuid.

/**
 * @see ConceptService#getConceptNameTagByUuid(String)
 */
@Test
public void getConceptNameTagByUuid_shouldFindObjectGivenValidUuid() {
    String uuid = "9e9df183-2328-4117-acd8-fb9bf400911d";
    ConceptNameTag conceptNameTag = Context.getConceptService().getConceptNameTagByUuid(uuid);
    Assert.assertEquals(1, (int) conceptNameTag.getConceptNameTagId());
}
Also used : ConceptNameTag(org.openmrs.ConceptNameTag) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 19 with ConceptNameTag

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

the class ConceptServiceTest method saveConceptNameTag_shouldSaveATagIfItIsSupplied.

/**
 * @see ConceptService#saveConceptNameTag(Object,Errors)
 */
@Test
public void saveConceptNameTag_shouldSaveATagIfItIsSupplied() {
    ConceptNameTag cnt = new ConceptNameTag();
    cnt.setTag("abcd");
    cnt.setDescription("test");
    ConceptService cs = Context.getConceptService();
    Integer id = cs.saveConceptNameTag(cnt).getId();
    Context.flushSession();
    Context.clearSession();
    ConceptNameTag savedNameTag = cs.getConceptNameTag(id);
    assertEquals(savedNameTag.getTag(), "abcd");
    assertEquals(savedNameTag.getDescription(), "test");
}
Also used : ConceptNameTag(org.openmrs.ConceptNameTag) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 20 with ConceptNameTag

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

the class ConceptServiceTest method saveConceptNameTag_shouldSaveAnEditedNameTag.

/**
 * @see ConceptService#saveConceptNameTag(Object,Errors)
 */
@Test
public void saveConceptNameTag_shouldSaveAnEditedNameTag() {
    ConceptService cs = Context.getConceptService();
    ConceptNameTag cnt = cs.getConceptNameTag(1);
    cnt.setTag("dcba");
    Integer id = cs.saveConceptNameTag(cnt).getId();
    Context.flushSession();
    Context.clearSession();
    ConceptNameTag savedNameTag = cs.getConceptNameTag(id);
    assertEquals(savedNameTag.getTag(), "dcba");
}
Also used : ConceptNameTag(org.openmrs.ConceptNameTag) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Aggregations

ConceptNameTag (org.openmrs.ConceptNameTag)20 Test (org.junit.Test)18 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)18 BindException (org.springframework.validation.BindException)7 Errors (org.springframework.validation.Errors)7 ConceptName (org.openmrs.ConceptName)4 ArrayList (java.util.ArrayList)1 Concept (org.openmrs.Concept)1 ConceptClass (org.openmrs.ConceptClass)1 ConceptDatatype (org.openmrs.ConceptDatatype)1 ConceptDescription (org.openmrs.ConceptDescription)1 OpenmrsMatchers.hasConcept (org.openmrs.test.OpenmrsMatchers.hasConcept)1