Search in sources :

Example 6 with ConceptProposal

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

the class ORUR01Handler method createConceptProposal.

/**
 * Creates a ConceptProposal object that will need to be saved to the database at a later point.
 *
 * @param encounter
 * @param concept
 * @param originalText
 * @return
 */
private ConceptProposal createConceptProposal(Encounter encounter, Concept concept, String originalText) {
    // value is a proposed concept, create a ConceptProposal
    // instead of an Obs for this observation
    // TODO: at this point if componentSeparator (^) is in text,
    // we'll only use the text before that delimiter!
    ConceptProposal conceptProposal = new ConceptProposal();
    conceptProposal.setOriginalText(originalText);
    conceptProposal.setState(OpenmrsConstants.CONCEPT_PROPOSAL_UNMAPPED);
    conceptProposal.setEncounter(encounter);
    conceptProposal.setObsConcept(concept);
    return conceptProposal;
}
Also used : ConceptProposal(org.openmrs.ConceptProposal)

Example 7 with ConceptProposal

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

the class ORUR01Handler method processORU_R01.

/**
 * Bulk of the processing done here. Called by the main processMessage method
 *
 * @param oru the message to process
 * @return the processed message
 * @throws HL7Exception
 * @should process multiple NK1 segments
 */
private Message processORU_R01(ORU_R01 oru) throws HL7Exception {
    // TODO: ideally, we would branch or alter our behavior based on the
    // sending application.
    // validate message
    validate(oru);
    // extract segments for convenient use below
    MSH msh = getMSH(oru);
    PID pid = getPID(oru);
    List<NK1> nk1List = getNK1List(oru);
    PV1 pv1 = getPV1(oru);
    // we're using the ORC assoc with first OBR to
    ORC orc = getORC(oru);
    // hold data enterer and date entered for now
    // Obtain message control id (unique ID for message from sending
    // application)
    String messageControlId = msh.getMessageControlID().getValue();
    if (log.isDebugEnabled()) {
        log.debug("Found HL7 message in inbound queue with control id = " + messageControlId);
    }
    // create the encounter
    Patient patient = getPatient(pid);
    if (log.isDebugEnabled()) {
        log.debug("Processing HL7 message for patient " + patient.getPatientId());
    }
    Encounter encounter = createEncounter(msh, patient, pv1, orc);
    // do the discharge to location logic
    try {
        updateHealthCenter(patient, pv1);
    } catch (Exception e) {
        log.error("Error while processing Discharge To Location (" + messageControlId + ")", e);
    }
    // process NK1 (relationship) segments
    for (NK1 nk1 : nk1List) {
        processNK1(patient, nk1);
    }
    // list of concepts proposed in the obs of this encounter.
    // these proposals need to be created after the encounter
    // has been created
    List<ConceptProposal> conceptProposals = new ArrayList<>();
    // create observations
    if (log.isDebugEnabled()) {
        log.debug("Creating observations for message " + messageControlId + "...");
    }
    // we ignore all MEDICAL_RECORD_OBSERVATIONS that are OBRs.  We do not
    // create obs_groups for them
    List<Integer> ignoredConceptIds = new ArrayList<>();
    String obrConceptId = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_MEDICAL_RECORD_OBSERVATIONS, "1238");
    if (StringUtils.hasLength(obrConceptId)) {
        ignoredConceptIds.add(Integer.valueOf(obrConceptId));
    }
    // we also ignore all PROBLEM_LIST that are OBRs
    String obrProblemListConceptId = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PROBLEM_LIST, "1284");
    if (StringUtils.hasLength(obrProblemListConceptId)) {
        ignoredConceptIds.add(Integer.valueOf(obrProblemListConceptId));
    }
    ORU_R01_PATIENT_RESULT patientResult = oru.getPATIENT_RESULT();
    int numObr = patientResult.getORDER_OBSERVATIONReps();
    for (int i = 0; i < numObr; i++) {
        if (log.isDebugEnabled()) {
            log.debug("Processing OBR (" + i + " of " + numObr + ")");
        }
        ORU_R01_ORDER_OBSERVATION orderObs = patientResult.getORDER_OBSERVATION(i);
        // the parent obr
        OBR obr = orderObs.getOBR();
        if (!StringUtils.hasText(obr.getUniversalServiceIdentifier().getIdentifier().getValue())) {
            throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.errorInvalidOBR ", new Object[] { messageControlId }, null));
        }
        // if we're not ignoring this obs group, create an
        // Obs grouper object that the underlying obs objects will use
        Obs obsGrouper = null;
        Concept obrConcept = getConcept(obr.getUniversalServiceIdentifier(), messageControlId);
        if (obrConcept != null && !ignoredConceptIds.contains(obrConcept.getId())) {
            // maybe check for a parent obs group from OBR-29 Parent ?
            // create an obs for this obs group too
            obsGrouper = new Obs();
            obsGrouper.setConcept(obrConcept);
            obsGrouper.setPerson(encounter.getPatient());
            obsGrouper.setEncounter(encounter);
            Date datetime = getDatetime(obr);
            if (datetime == null) {
                datetime = encounter.getEncounterDatetime();
            }
            obsGrouper.setObsDatetime(datetime);
            obsGrouper.setLocation(encounter.getLocation());
            obsGrouper.setCreator(encounter.getCreator());
            // set comments if there are any
            StringBuilder comments = new StringBuilder();
            ORU_R01_ORDER_OBSERVATION parent = (ORU_R01_ORDER_OBSERVATION) obr.getParent();
            int totalNTEs = parent.getNTEReps();
            for (int iNTE = 0; iNTE < totalNTEs; iNTE++) {
                for (FT obxComment : parent.getNTE(iNTE).getComment()) {
                    if (comments.length() > 0) {
                        comments.append(" ");
                    }
                    comments.append(obxComment.getValue());
                }
            }
            // only set comments if there are any
            if (StringUtils.hasText(comments.toString())) {
                obsGrouper.setComment(comments.toString());
            }
            // add this obs as another row in the obs table
            encounter.addObs(obsGrouper);
        }
        // loop over the obs and create each object, adding it to the encounter
        int numObs = orderObs.getOBSERVATIONReps();
        HL7Exception errorInHL7Queue = null;
        for (int j = 0; j < numObs; j++) {
            if (log.isDebugEnabled()) {
                log.debug("Processing OBS (" + j + " of " + numObs + ")");
            }
            OBX obx = orderObs.getOBSERVATION(j).getOBX();
            try {
                log.debug("Parsing observation");
                Obs obs = parseObs(encounter, obx, obr, messageControlId);
                if (obs != null) {
                    // the creator/dateCreated from the encounter
                    if (encounter.getEncounterId() != null) {
                        obs.setCreator(getEnterer(orc));
                        obs.setDateCreated(new Date());
                    }
                    // set the obsGroup on this obs
                    if (obsGrouper != null) {
                        // set the obs to the group.  This assumes the group is already
                        // on the encounter and that when the encounter is saved it will
                        // propagate to the children obs
                        obsGrouper.addGroupMember(obs);
                    } else {
                        // set this obs on the encounter object that we
                        // will be saving later
                        log.debug("Obs is not null. Adding to encounter object");
                        encounter.addObs(obs);
                        log.debug("Done with this obs");
                    }
                }
            } catch (ProposingConceptException proposingException) {
                Concept questionConcept = proposingException.getConcept();
                String value = proposingException.getValueName();
                // if the sender never specified any text for the proposed concept
                if (!StringUtils.isEmpty(value)) {
                    conceptProposals.add(createConceptProposal(encounter, questionConcept, value));
                } else {
                    errorInHL7Queue = new HL7Exception(Context.getMessageSourceService().getMessage("Hl7.proposed.concept.name.empty"), proposingException);
                    // stop any further processing of current message
                    break;
                }
            } catch (HL7Exception e) {
                errorInHL7Queue = e;
            } finally {
                // Handle obs-level exceptions
                if (errorInHL7Queue != null) {
                    throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.improperlyFormattedOBX", new Object[] { PipeParser.encode(obx, new EncodingCharacters('|', "^~\\&")) }, null), HL7Exception.DATA_TYPE_ERROR, errorInHL7Queue);
                }
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Finished creating observations");
        log.debug("Current thread: " + Thread.currentThread());
        log.debug("Creating the encounter object");
    }
    Context.getEncounterService().saveEncounter(encounter);
    // now that the encounter is saved
    for (ConceptProposal proposal : conceptProposals) {
        Context.getConceptService().saveConceptProposal(proposal);
    }
    return oru;
}
Also used : ORC(ca.uhn.hl7v2.model.v25.segment.ORC) ArrayList(java.util.ArrayList) FT(ca.uhn.hl7v2.model.v25.datatype.FT) NK1(ca.uhn.hl7v2.model.v25.segment.NK1) Encounter(org.openmrs.Encounter) OBR(ca.uhn.hl7v2.model.v25.segment.OBR) EncodingCharacters(ca.uhn.hl7v2.parser.EncodingCharacters) Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) MSH(ca.uhn.hl7v2.model.v25.segment.MSH) ORU_R01_PATIENT_RESULT(ca.uhn.hl7v2.model.v25.group.ORU_R01_PATIENT_RESULT) OBX(ca.uhn.hl7v2.model.v25.segment.OBX) Patient(org.openmrs.Patient) PID(ca.uhn.hl7v2.model.v25.segment.PID) PV1(ca.uhn.hl7v2.model.v25.segment.PV1) DataTypeException(ca.uhn.hl7v2.model.DataTypeException) HL7Exception(ca.uhn.hl7v2.HL7Exception) ApplicationException(ca.uhn.hl7v2.app.ApplicationException) Date(java.util.Date) ORU_R01_ORDER_OBSERVATION(ca.uhn.hl7v2.model.v25.group.ORU_R01_ORDER_OBSERVATION) ConceptProposal(org.openmrs.ConceptProposal) HL7Exception(ca.uhn.hl7v2.HL7Exception)

Example 8 with ConceptProposal

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

the class ORUR01HandlerTest method processMessage_shouldNotCreateProblemListObservationWithConceptProposals.

/**
 * Tests that a ConceptProposal row can be written by the processor
 *
 * @see ORUR01Handler#processMessage(Message)
 */
@Test
public void processMessage_shouldNotCreateProblemListObservationWithConceptProposals() throws Exception {
    ObsService obsService = Context.getObsService();
    ConceptService conceptService = Context.getConceptService();
    EncounterService encService = Context.getEncounterService();
    String hl7String = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20080630094800||ORU^R01|kgWdFt0SVwwClOfJm3pe|P|2.5|1||||||||15^AMRS.ELD.FORMID\r" + "PID|||3^^^^~d3811480^^^^||John3^Doe^||\r" + "PV1||O|1^Unknown||||1^Super User (admin)|||||||||||||||||||||||||||||||||||||20080208|||||||V\r" + "ORC|RE||||||||20080208000000|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT\r" + "OBR|1|||1284^PROBLEM LIST^99DCT\r" + "OBX|1|CWE|6042^PROBLEM ADDED^99DCT||PROPOSED^SEVERO DOLOR DE CABEZA^99DCT|||||||||20080208";
    Message hl7message = parser.parse(hl7String);
    router.processMessage(hl7message);
    Patient patient = new Patient(3);
    // check for any obs
    assertEquals("There should not be any obs created for #3", 0, obsService.getObservationsByPerson(patient).size());
    // check for a new encounter
    assertEquals("There should be 1 new encounter created for #3", 1, encService.getEncountersByPatient(patient).size());
    // check for the proposed concept
    List<ConceptProposal> proposedConcepts = conceptService.getConceptProposals("SEVERO DOLOR DE CABEZA");
    assertEquals("There should be a proposed concept by this name", 1, proposedConcepts.size());
    assertEquals(encService.getEncountersByPatient(patient).get(0), proposedConcepts.get(0).getEncounter());
}
Also used : Message(ca.uhn.hl7v2.model.Message) ConceptProposal(org.openmrs.ConceptProposal) Patient(org.openmrs.Patient) ObsService(org.openmrs.api.ObsService) ConceptService(org.openmrs.api.ConceptService) EncounterService(org.openmrs.api.EncounterService) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 9 with ConceptProposal

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

the class ConceptServiceTest method mapConceptProposalToConcept_shouldSetValueCodedNameWhenAddSynonymIsSelected.

/**
 * @see ConceptService#mapConceptProposalToConcept(ConceptProposal,Concept,Locale)
 */
@Test
public void mapConceptProposalToConcept_shouldSetValueCodedNameWhenAddSynonymIsSelected() {
    ConceptProposal cp = conceptService.getConceptProposal(2);
    Assert.assertEquals(OpenmrsConstants.CONCEPT_PROPOSAL_UNMAPPED, cp.getState());
    final Concept civilStatusConcept = conceptService.getConcept(4);
    final int mappedConceptId = 6;
    final String finalText = "Weight synonym";
    Assert.assertTrue(Context.getObsService().getObservationsByPersonAndConcept(cp.getEncounter().getPatient(), civilStatusConcept).isEmpty());
    Concept mappedConcept = conceptService.getConcept(mappedConceptId);
    mappedConcept.addDescription(new ConceptDescription("some description", Context.getLocale()));
    cp.setFinalText(finalText);
    cp.setObsConcept(civilStatusConcept);
    cp.setState(OpenmrsConstants.CONCEPT_PROPOSAL_SYNONYM);
    conceptService.mapConceptProposalToConcept(cp, mappedConcept, null);
    mappedConcept = conceptService.getConcept(mappedConceptId);
    List<Obs> observations = Context.getObsService().getObservationsByPersonAndConcept(cp.getEncounter().getPatient(), civilStatusConcept);
    Assert.assertEquals(1, observations.size());
    Obs obs = observations.get(0);
    Assert.assertNotNull(obs.getValueCodedName());
    Assert.assertEquals(finalText, obs.getValueCodedName().getName());
}
Also used : OpenmrsMatchers.hasConcept(org.openmrs.test.OpenmrsMatchers.hasConcept) Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) ConceptProposal(org.openmrs.ConceptProposal) ConceptDescription(org.openmrs.ConceptDescription) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 10 with ConceptProposal

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

the class ConceptServiceTest method mapConceptProposalToConcept_shouldNotRequireMappedConceptOnRejectAction.

/**
 * @see ConceptService#mapConceptProposalToConcept(ConceptProposal,Concept)
 */
@Test
public void mapConceptProposalToConcept_shouldNotRequireMappedConceptOnRejectAction() {
    String uuid = "af4ae460-0e2b-11e0-a94b-469c3c5a0c2f";
    ConceptProposal proposal = Context.getConceptService().getConceptProposalByUuid(uuid);
    Assert.assertNotNull("could not find proposal " + uuid, proposal);
    proposal.setState(OpenmrsConstants.CONCEPT_PROPOSAL_REJECT);
    try {
        Context.getConceptService().mapConceptProposalToConcept(proposal, null);
    } catch (APIException ex) {
        Assert.fail("cought APIException when rejecting a proposal with null mapped concept");
    }
}
Also used : ConceptProposal(org.openmrs.ConceptProposal) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Aggregations

ConceptProposal (org.openmrs.ConceptProposal)11 Test (org.junit.Test)9 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)9 Concept (org.openmrs.Concept)5 Obs (org.openmrs.Obs)4 OpenmrsMatchers.hasConcept (org.openmrs.test.OpenmrsMatchers.hasConcept)4 Locale (java.util.Locale)2 ConceptDescription (org.openmrs.ConceptDescription)2 Patient (org.openmrs.Patient)2 HL7Exception (ca.uhn.hl7v2.HL7Exception)1 ApplicationException (ca.uhn.hl7v2.app.ApplicationException)1 DataTypeException (ca.uhn.hl7v2.model.DataTypeException)1 Message (ca.uhn.hl7v2.model.Message)1 FT (ca.uhn.hl7v2.model.v25.datatype.FT)1 ORU_R01_ORDER_OBSERVATION (ca.uhn.hl7v2.model.v25.group.ORU_R01_ORDER_OBSERVATION)1 ORU_R01_PATIENT_RESULT (ca.uhn.hl7v2.model.v25.group.ORU_R01_PATIENT_RESULT)1 MSH (ca.uhn.hl7v2.model.v25.segment.MSH)1 NK1 (ca.uhn.hl7v2.model.v25.segment.NK1)1 OBR (ca.uhn.hl7v2.model.v25.segment.OBR)1 OBX (ca.uhn.hl7v2.model.v25.segment.OBX)1