Search in sources :

Example 1 with ConceptService

use of org.openmrs.api.ConceptService in project openmrs-core by openmrs.

the class ConceptSetsEditor method setAsText.

/**
 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {
    log.debug("setting conceptSets with text: " + text);
    if (StringUtils.hasText(text)) {
        ConceptService cs = Context.getConceptService();
        String[] conceptIds = text.split(" ");
        List<Integer> requestConceptIds = new ArrayList<>();
        // and removal of duplicates
        for (String id : conceptIds) {
            id = id.trim();
            if (!("".equals(id)) && !requestConceptIds.contains(Integer.valueOf(id))) {
                // remove whitespace, blank lines, and duplicate entries
                requestConceptIds.add(Integer.valueOf(id));
            }
        }
        // used when adding in concept sets
        List<Integer> originalConceptSetIds = new ArrayList<>(originalConceptSets.size());
        // remove all sets that aren't in the request (aka, that have been deleted by the user)
        Collection<ConceptSet> copyOfOriginalConceptSets = new ArrayList<>(originalConceptSets);
        for (ConceptSet origConceptSet : copyOfOriginalConceptSets) {
            if (!requestConceptIds.contains(origConceptSet.getConcept().getConceptId())) {
                originalConceptSets.remove(origConceptSet);
            }
            // add to quick list used when adding later
            originalConceptSetIds.add(origConceptSet.getConcept().getConceptId());
        }
        // Also normalize all weight attributes
        for (int x = 0; x < requestConceptIds.size(); x++) {
            Integer requestConceptId = requestConceptIds.get(x);
            if (!originalConceptSetIds.contains(requestConceptId)) {
                // the null weight will be reset in the next step of normalization
                originalConceptSets.add(new ConceptSet(cs.getConcept(requestConceptId), (double) x));
            } else {
                // find this conceptId in the original set and set its weight
                for (ConceptSet conceptSet : originalConceptSets) {
                    if (conceptSet.getConcept().getConceptId().equals(requestConceptId)) {
                        conceptSet.setSortWeight((double) x);
                    }
                }
            }
        }
    } else {
        originalConceptSets.clear();
    }
    setValue(originalConceptSets);
}
Also used : ConceptSet(org.openmrs.ConceptSet) ArrayList(java.util.ArrayList) ConceptService(org.openmrs.api.ConceptService)

Example 2 with ConceptService

use of org.openmrs.api.ConceptService in project openmrs-core by openmrs.

the class ConceptAnswersEditor method setAsText.

/**
 * loops over the textbox assigned to this property. The textbox is assumed to be a string of
 * conceptIds^drugIds separated by spaces.
 *
 * @param text list of conceptIds (not conceptAnswerIds)
 * @should set the sort weights with the least possible changes
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {
    if (StringUtils.hasText(text)) {
        ConceptService cs = Context.getConceptService();
        String[] conceptIds = text.split(" ");
        List<String> requestConceptIds = new ArrayList<>();
        // set up parameter answer Set for easier add/delete functions and removal of duplicates
        for (String id : conceptIds) {
            id = id.trim();
            if (!("".equals(id)) && !requestConceptIds.contains(id)) {
                // remove whitespace, blank lines, and duplicates
                requestConceptIds.add(id);
            }
        }
        Collection<ConceptAnswer> deletedConceptAnswers = new HashSet<>();
        // loop over original concept answers to find any deleted answers
        for (ConceptAnswer origConceptAnswer : originalConceptAnswers) {
            boolean answerDeleted = true;
            for (String conceptId : requestConceptIds) {
                Integer id = getConceptId(conceptId);
                Integer drugId = getDrugId(conceptId);
                Drug answerDrug = origConceptAnswer.getAnswerDrug();
                if (id.equals(origConceptAnswer.getAnswerConcept().getConceptId())) {
                    if (drugId == null && answerDrug == null) {
                        answerDeleted = false;
                    } else if ((drugId != null && answerDrug != null) && drugId.equals(origConceptAnswer.getAnswerDrug().getDrugId())) {
                        answerDeleted = false;
                    }
                }
            }
            if (answerDeleted) {
                deletedConceptAnswers.add(origConceptAnswer);
            }
        }
        // loop over those deleted answers to delete them
        for (ConceptAnswer conceptAnswer : deletedConceptAnswers) {
            originalConceptAnswers.remove(conceptAnswer);
        }
        // loop over concept ids in the request to add any that are new
        for (String conceptId : requestConceptIds) {
            Integer id = getConceptId(conceptId);
            Integer drugId = getDrugId(conceptId);
            boolean newAnswerConcept = true;
            for (ConceptAnswer origConceptAnswer : originalConceptAnswers) {
                Drug answerDrug = origConceptAnswer.getAnswerDrug();
                if (id.equals(origConceptAnswer.getAnswerConcept().getConceptId())) {
                    if (drugId == null && answerDrug == null) {
                        newAnswerConcept = false;
                    } else if ((drugId != null && answerDrug != null) && drugId.equals(answerDrug.getDrugId())) {
                        newAnswerConcept = false;
                    }
                }
            }
            // if the current request answer is new, add it to the originals
            if (newAnswerConcept) {
                Concept answer = cs.getConcept(id);
                Drug drug = null;
                if (drugId != null) {
                    drug = cs.getDrug(drugId);
                }
                ConceptAnswer ac = new ConceptAnswer(answer, drug);
                originalConceptAnswers.add(ac);
            }
        }
        // loop over to set the order
        // as the list comes into 'requestConceptIds' in the order the user wants
        // there are 2 conditions that will require the sort_weights to be reassigned
        // 1) any ConceptAnswer.sortWeight == NULL (meaning it is just added)
        // 2) the list is not in ASCENDING order (example sort order of the list is 1, 2, 10, 9)
        // -startIdx (start index) is where in this list we will start to reassign the sort_weights
        Double lastWeightSeen = null;
        // the idx to start at, if we have a NULL sort weight (new concept answer) or sort weights are not ascending
        int startIdx = -1;
        for (int i = 0; i < requestConceptIds.size() - 1; i++) {
            Integer id1 = getConceptId(requestConceptIds.get(i));
            ConceptAnswer ca1 = getConceptAnswerFromOriginal(id1);
            if (ca1.getSortWeight() == null) {
                if (lastWeightSeen == null) {
                    // start at 1, we're at the beginning
                    lastWeightSeen = 1d;
                } else {
                    // we start at +1
                    lastWeightSeen += 1;
                }
                startIdx = i;
                break;
            }
            Integer id2 = getConceptId(requestConceptIds.get(i + 1));
            ConceptAnswer ca2 = getConceptAnswerFromOriginal(id2);
            int c = ca1.compareTo(ca2);
            if (c > 0) {
                startIdx = i;
                lastWeightSeen = ca1.getSortWeight();
                break;
            }
            lastWeightSeen = ca1.getSortWeight();
        }
        if (startIdx != -1) {
            // then we need to re-weight
            for (int i = startIdx; i < requestConceptIds.size(); i++) {
                Integer id = getConceptId(requestConceptIds.get(i));
                ConceptAnswer ca = getConceptAnswerFromOriginal(id);
                ca.setSortWeight(lastWeightSeen++);
            }
        }
        log.debug("originalConceptAnswers.getConceptId(): ");
        for (ConceptAnswer a : originalConceptAnswers) {
            log.debug("id: " + a.getAnswerConcept().getConceptId());
        }
        log.debug("requestConceptIds: ");
        for (String i : requestConceptIds) {
            log.debug("id: " + i);
        }
    } else {
        originalConceptAnswers.clear();
    }
    setValue(originalConceptAnswers);
}
Also used : Drug(org.openmrs.Drug) Concept(org.openmrs.Concept) ConceptAnswer(org.openmrs.ConceptAnswer) ArrayList(java.util.ArrayList) ConceptService(org.openmrs.api.ConceptService) HashSet(java.util.HashSet)

Example 3 with ConceptService

use of org.openmrs.api.ConceptService in project openmrs-core by openmrs.

the class ConceptAnswersEditorTest method setAsText_shouldSetTheSortWeightsWithTheLeastPossibleChanges.

/**
 * @see ConceptAnswersEditor#setAsText(String)
 */
@Test
public void setAsText_shouldSetTheSortWeightsWithTheLeastPossibleChanges() {
    ConceptService service = Context.getConceptService();
    Concept c = service.getConcept(21);
    ConceptAnswersEditor editor = new ConceptAnswersEditor(c.getAnswers(true));
    editor.setAsText("22 7 8");
    // conceptId=7
    ConceptAnswer ca1 = service.getConceptAnswer(1);
    // conceptId=8
    ConceptAnswer ca2 = service.getConceptAnswer(2);
    // conceptId=22
    ConceptAnswer ca3 = service.getConceptAnswer(3);
    Concept cafter = service.getConcept(21);
    Assert.assertEquals(3, cafter.getAnswers(true).size());
    Assert.assertTrue(ca3.getSortWeight() < ca1.getSortWeight());
    Assert.assertTrue(ca1.getSortWeight() < ca2.getSortWeight());
}
Also used : Concept(org.openmrs.Concept) ConceptAnswer(org.openmrs.ConceptAnswer) ConceptService(org.openmrs.api.ConceptService) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 4 with ConceptService

use of org.openmrs.api.ConceptService in project openmrs-core by openmrs.

the class OrderFrequencyValidatorTest method validate_shouldFailValidationIfFieldLengthsAreNotCorrect.

/**
 * @see OrderFrequencyValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() {
    ConceptService cs = Context.getConceptService();
    Concept concept = new Concept();
    ConceptName cn = new ConceptName("new name", Context.getLocale());
    concept.setDatatype(cs.getConceptDatatype(1));
    concept.setConceptClass(cs.getConceptClass(19));
    concept.addName(cn);
    concept.addDescription(new ConceptDescription("some description", null));
    cs.saveConcept(concept);
    OrderFrequency orderFrequency = new OrderFrequency();
    orderFrequency.setConcept(concept);
    orderFrequency.setRetireReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    Errors errors = new BindException(orderFrequency, "orderFrequency");
    new OrderFrequencyValidator().validate(orderFrequency, errors);
    Assert.assertTrue(errors.hasFieldErrors("retireReason"));
}
Also used : Concept(org.openmrs.Concept) Errors(org.springframework.validation.Errors) ConceptName(org.openmrs.ConceptName) BindException(org.springframework.validation.BindException) ConceptDescription(org.openmrs.ConceptDescription) ConceptService(org.openmrs.api.ConceptService) OrderFrequency(org.openmrs.OrderFrequency) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 5 with ConceptService

use of org.openmrs.api.ConceptService in project openmrs-core by openmrs.

the class OrderFrequencyValidatorTest method validate_shouldPassForAValidNewOrderFrequency.

/**
 * @see OrderFrequencyValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldPassForAValidNewOrderFrequency() {
    ConceptService cs = Context.getConceptService();
    Concept concept = new Concept();
    ConceptName cn = new ConceptName("new name", Context.getLocale());
    concept.setDatatype(cs.getConceptDatatype(1));
    concept.setConceptClass(cs.getConceptClass(19));
    concept.addName(cn);
    concept.addDescription(new ConceptDescription("some description", null));
    cs.saveConcept(concept);
    OrderFrequency orderFrequency = new OrderFrequency();
    orderFrequency.setConcept(concept);
    Errors errors = new BindException(orderFrequency, "orderFrequency");
    new OrderFrequencyValidator().validate(orderFrequency, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : Concept(org.openmrs.Concept) Errors(org.springframework.validation.Errors) ConceptName(org.openmrs.ConceptName) BindException(org.springframework.validation.BindException) ConceptDescription(org.openmrs.ConceptDescription) ConceptService(org.openmrs.api.ConceptService) OrderFrequency(org.openmrs.OrderFrequency) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

ConceptService (org.openmrs.api.ConceptService)21 Test (org.junit.Test)11 Concept (org.openmrs.Concept)11 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)11 BindException (org.springframework.validation.BindException)7 Errors (org.springframework.validation.Errors)7 ArrayList (java.util.ArrayList)4 ConceptName (org.openmrs.ConceptName)4 Encounter (org.openmrs.Encounter)4 Patient (org.openmrs.Patient)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 CareSetting (org.openmrs.CareSetting)3 ConceptDescription (org.openmrs.ConceptDescription)3 OrderFrequency (org.openmrs.OrderFrequency)3 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 Date (java.util.Date)2 StringTokenizer (java.util.StringTokenizer)2