Search in sources :

Example 11 with ObsService

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

the class EncounterServiceImpl method saveEncounter.

/**
 * @see org.openmrs.api.EncounterService#saveEncounter(org.openmrs.Encounter)
 */
@Override
public Encounter saveEncounter(Encounter encounter) throws APIException {
    // if authenticated user is not supposed to edit encounter of certain type
    failIfDeniedToEdit(encounter);
    // If new encounter, try to assign a visit using the registered visit assignment handler.
    createVisitForNewEncounter(encounter);
    // check permissions
    boolean isNewEncounter = requirePrivilege(encounter);
    // This must be done after setting dateCreated etc on the obs because
    // of the way the ORM tools flush things and check for nullity
    // This also must be done before the save encounter so we can use the
    // orig date
    // after the save
    Patient p = encounter.getPatient();
    Date originalDate;
    Location originalLocation = null;
    if (!isNewEncounter) {
        // fetch the datetime from the database prior to saving for this
        // encounter
        // to see if it has changed and change all obs after saving if so
        originalDate = dao.getSavedEncounterDatetime(encounter);
        if (encounter.getLocation() != null) {
            originalLocation = dao.getSavedEncounterLocation(encounter);
        }
        // Our data model duplicates the patient column to allow for
        // observations to
        // not have to look up the parent Encounter to find the patient
        // Therefore, encounter.patient must always equal
        // encounter.observations[0-n].patient
        // If we are changing encounter.encounterDatetime, then we need to
        // also apply that
        // to Obs that inherited their obsDatetime from the encounter in the
        // first place
        Date newDate = encounter.getEncounterDatetime();
        Location newLocation = encounter.getLocation();
        for (Obs obs : encounter.getAllObs(true)) {
            // if the date was changed
            if (OpenmrsUtil.compare(originalDate, newDate) != 0 && OpenmrsUtil.compare(obs.getObsDatetime(), originalDate) == 0) {
                // if the obs datetime is the same as the
                // original encounter datetime, fix it
                obs.setObsDatetime(newDate);
            }
            if (!OpenmrsUtil.nullSafeEquals(newLocation, originalLocation) && obs.getLocation().equals(originalLocation)) {
                obs.setLocation(newLocation);
            }
            // encounter, fix it
            if (!obs.getPerson().getPersonId().equals(p.getPatientId())) {
                obs.setPerson(p);
            }
        }
    }
    // same goes for Orders
    for (Order o : encounter.getOrders()) {
        if (!p.equals(o.getPatient())) {
            o.setPatient(p);
        }
    }
    // do the actual saving to the database
    dao.saveEncounter(encounter);
    // save the new orderGroups
    for (OrderGroup orderGroup : encounter.getOrderGroups()) {
        Context.getOrderService().saveOrderGroup(orderGroup);
    }
    // save the new orders which do not have order groups
    for (Order o : encounter.getOrdersWithoutOrderGroups()) {
        if (o.getOrderId() == null) {
            Context.getOrderService().saveOrder(o, null);
        }
    }
    // save the Obs
    String changeMessage = Context.getMessageSourceService().getMessage("Obs.void.reason.default");
    ObsService os = Context.getObsService();
    List<Obs> obsToRemove = new ArrayList<>();
    List<Obs> obsToAdd = new ArrayList<>();
    for (Obs o : encounter.getObsAtTopLevel(true)) {
        if (o.getId() == null) {
            os.saveObs(o, null);
        } else {
            Obs newObs = os.saveObs(o, changeMessage);
            // The logic in saveObs evicts the old obs instance, so we need to update the collection
            // with the newly loaded and voided instance, apparently reloading the encounter
            // didn't do the tick
            obsToRemove.add(o);
            obsToAdd.add(os.getObs(o.getId()));
            obsToAdd.add(newObs);
        }
    }
    removeGivenObsAndTheirGroupMembersFromEncounter(obsToRemove, encounter);
    addGivenObsAndTheirGroupMembersToEncounter(obsToAdd, encounter);
    return encounter;
}
Also used : Order(org.openmrs.Order) Obs(org.openmrs.Obs) OrderGroup(org.openmrs.OrderGroup) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) ObsService(org.openmrs.api.ObsService) Date(java.util.Date) Location(org.openmrs.Location)

Example 12 with ObsService

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

the class ORUR01HandlerTest method parseObs_shouldAddCommentsToAnObservationFromNTESegments.

/**
 * @see ORUR01Handler#parseObs(Encounter,OBX,OBR,String)
 */
@Test
public void parseObs_shouldAddCommentsToAnObservationFromNTESegments() throws Exception {
    ObsService os = Context.getObsService();
    String hl7string = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20080226102656||ORU^R01|JqnfhKKtouEz8kzTk6Zo|P|2.5|1||||||||16^AMRS.ELD.FORMID\r" + "PID|||7^^^^||Collet^Test^Chebaskwony||\r" + "PV1||O|1^Unknown Location||||1^Super User (1-8)|||||||||||||||||||||||||||||||||||||20080212|||||||V\r" + "ORC|RE||||||||20080226102537|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT\r" + "OBX|1|NM|5497^CD4, BY FACS^99DCT||1|||||||||20080206\r" + "NTE|1|L|This is a comment";
    // the expected question for the obs in the hl7 message has to be
    // numeric
    Assert.assertEquals("Numeric", Context.getConceptService().getConcept(5497).getDatatype().getName());
    List<Obs> oldList = os.getObservationsByPersonAndConcept(new Person(7), new Concept(5497));
    Message hl7message = parser.parse(hl7string);
    router.processMessage(hl7message);
    List<Obs> newList = os.getObservationsByPersonAndConcept(new Person(7), new Concept(5497));
    Obs newObservation = null;
    for (Obs newObs : newList) {
        if (!oldList.contains(newObs) && !newObs.isObsGrouping()) {
            newObservation = newObs;
        }
    }
    Assert.assertEquals("This is a comment", newObservation.getComment());
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) Message(ca.uhn.hl7v2.model.Message) ObsService(org.openmrs.api.ObsService) Person(org.openmrs.Person) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 13 with ObsService

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

the class ORUR01HandlerTest method processMessage_shouldSetValue_NumericForObsIfQuestionDatatypeIsNumericAndTheAnswerIsEither0Or1.

/**
 * @see ORUR01Handler#processMessage(Message)
 */
@Test
public void processMessage_shouldSetValue_NumericForObsIfQuestionDatatypeIsNumericAndTheAnswerIsEither0Or1() throws Exception {
    ObsService os = Context.getObsService();
    String hl7string = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20080226102656||ORU^R01|JqnfhKKtouEz8kzTk6Zo|P|2.5|1||||||||16^AMRS.ELD.FORMID\r" + "PID|||7^^^^||Collet^Test^Chebaskwony||\r" + "PV1||O|1^Unknown Location||||1^Super User (1-8)|||||||||||||||||||||||||||||||||||||20080212|||||||V\r" + "ORC|RE||||||||20080226102537|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT\r" + "OBX|1|NM|5497^CD4, BY FACS^99DCT||1|||||||||20080206";
    // the expected question for the obs in the hl7 message has to be
    // numeric
    Assert.assertEquals("Numeric", Context.getConceptService().getConcept(5497).getDatatype().getName());
    List<Obs> oldList = os.getObservationsByPersonAndConcept(new Person(7), new Concept(5497));
    Message hl7message = parser.parse(hl7string);
    router.processMessage(hl7message);
    List<Obs> newList = os.getObservationsByPersonAndConcept(new Person(7), new Concept(5497));
    Obs newObservation = null;
    for (Obs newObs : newList) {
        if (!oldList.contains(newObs) && !newObs.isObsGrouping()) {
            newObservation = newObs;
        }
    }
    Assert.assertEquals(1, newObservation.getValueNumeric().intValue());
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) Message(ca.uhn.hl7v2.model.Message) ObsService(org.openmrs.api.ObsService) Person(org.openmrs.Person) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 14 with ObsService

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

the class ORUR01HandlerTest method parseObs_shouldAddCommentsToAnObservationGroup.

/**
 * @see ORUR01Handler#parseObs(Encounter,OBX,OBR,String)
 */
@Test
public void parseObs_shouldAddCommentsToAnObservationGroup() throws Exception {
    ObsService os = Context.getObsService();
    String hl7string = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20080226102656||ORU^R01|JqnfhKKtouEz8kzTk6Zo|P|2.5|1||||||||16^AMRS.ELD.FORMID\r" + "PID|||7^^^^||Collet^Test^Chebaskwony||\r" + "PV1||O|1^Unknown Location||||1^Super User (1-8)|||||||||||||||||||||||||||||||||||||20080212|||||||V\r" + "ORC|RE||||||||20080226102537|1^Super User\r" + "OBR|1|||23^FOOD CONSTRUCT^99DCT\r" + "NTE|1|L|This is a comment\r" + "OBX|1|NM|5497^CD4, BY FACS^99DCT||1|||||||||20080206\r" + "NTE|1|L|This should not be considered :-)";
    List<Obs> oldList = os.getObservationsByPersonAndConcept(new Person(7), new Concept(23));
    Message hl7message = parser.parse(hl7string);
    router.processMessage(hl7message);
    List<Obs> newList = os.getObservationsByPersonAndConcept(new Person(7), new Concept(23));
    Obs newObservation = null;
    for (Obs newObs : newList) {
        if (!oldList.contains(newObs) && newObs.isObsGrouping()) {
            newObservation = newObs;
        }
    }
    Assert.assertEquals("This is a comment", newObservation.getComment());
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) Message(ca.uhn.hl7v2.model.Message) ObsService(org.openmrs.api.ObsService) Person(org.openmrs.Person) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 15 with ObsService

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

the class ORUR01HandlerTest method processMessage_shouldCreateEncounterAndObsFromHl7Message.

/**
 * @see ORUR01Handler#processMessage(Message)
 */
@Test
public void processMessage_shouldCreateEncounterAndObsFromHl7Message() throws Exception {
    ObsService obsService = Context.getObsService();
    String hl7string = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20080226102656||ORU^R01|JqnfhKKtouEz8kzTk6Zo|P|2.5|1||||||||16^AMRS.ELD.FORMID\r" + "PID|||3^^^^||John3^Doe^||\r" + "PV1||O|1^Unknown Location||||1^Super User (1-8)|||||||||||||||||||||||||||||||||||||20080212|||||||V\r" + "ORC|RE||||||||20080226102537|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT\r" + "OBX|1|NM|5497^CD4, BY FACS^99DCT||450|||||||||20080206\r" + "OBX|2|DT|5096^RETURN VISIT DATE^99DCT||20080229|||||||||20080212";
    Message hl7message = parser.parse(hl7string);
    router.processMessage(hl7message);
    Patient patient = new Patient(3);
    // check for an encounter
    List<Encounter> encForPatient3 = Context.getEncounterService().getEncountersByPatient(patient);
    assertNotNull(encForPatient3);
    assertTrue("There should be an encounter created", encForPatient3.size() == 1);
    // check for any obs
    List<Obs> obsForPatient3 = obsService.getObservationsByPerson(patient);
    assertNotNull(obsForPatient3);
    assertTrue("There should be some obs created for #3", obsForPatient3.size() > 0);
    // check for the return visit date obs
    Concept returnVisitDateConcept = new Concept(5096);
    Calendar cal = Calendar.getInstance();
    cal.set(2008, Calendar.FEBRUARY, 29, 0, 0, 0);
    Date returnVisitDate = cal.getTime();
    List<Obs> returnVisitDateObsForPatient3 = obsService.getObservationsByPersonAndConcept(patient, returnVisitDateConcept);
    assertEquals("There should be a return visit date", 1, returnVisitDateObsForPatient3.size());
    Obs firstObs = (Obs) returnVisitDateObsForPatient3.toArray()[0];
    cal.setTime(firstObs.getValueDatetime());
    Date firstObsValueDatetime = cal.getTime();
    assertEquals("The date should be the 29th", returnVisitDate.toString(), firstObsValueDatetime.toString());
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) Message(ca.uhn.hl7v2.model.Message) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Patient(org.openmrs.Patient) Encounter(org.openmrs.Encounter) ObsService(org.openmrs.api.ObsService) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

ObsService (org.openmrs.api.ObsService)17 Obs (org.openmrs.Obs)16 Message (ca.uhn.hl7v2.model.Message)11 Test (org.junit.Test)11 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)11 Concept (org.openmrs.Concept)10 Person (org.openmrs.Person)7 Patient (org.openmrs.Patient)5 Date (java.util.Date)4 Order (org.openmrs.Order)4 ArrayList (java.util.ArrayList)3 APIException (org.openmrs.api.APIException)3 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2 Encounter (org.openmrs.Encounter)2 OrderService (org.openmrs.api.OrderService)2 ConceptName (org.openmrs.ConceptName)1 ConceptProposal (org.openmrs.ConceptProposal)1 GlobalProperty (org.openmrs.GlobalProperty)1 Location (org.openmrs.Location)1