public static Set<Set<Obs>> generateObsGroupFromSearchResults() {
    Set<Set<Obs>> obsGroups = new HashSet<Set<Obs>>();

    SearchAPI searchAPI = SearchAPI.getInstance();
    List<ChartListItem> searchResultsList = searchAPI.getResults();
    for (ChartListItem item :
        searchResultsList) { // for each item in results we classify it by its obsGroup, and add all
                             // of the group.
      if (item != null && item instanceof ObsItem && ((ObsItem) item).getObsId() != null) {
        int itemObsId = ((ObsItem) item).getObsId();
        Obs obsGrp = Context.getObsService().getObs(itemObsId).getObsGroup();
        if (obsGrp != null) {
          int groupId = obsGrp.getId();
          Set<Obs> obsGroup = obsGrp.getGroupMembers();
          boolean found = false; // if found == true then we don't need to add the group.
          for (Set<Obs> grp : obsGroups) {
            Obs ob = new Obs(-1);
            if (grp.iterator().hasNext()) {
              ob = grp.iterator().next();
            }
            if (ob.getObsGroup() != null && ob.getObsGroup().getId() != null) {
              if (ob.getObsGroup().getId() == groupId) {
                found = true;
              }
            }
          }
          if (!found) {
            obsGroups.add(obsGroup);
          }
        }
      }
    }
    return obsGroups;
  }
  private Obs doNotGoToServiceToFormatMembers(Obs obsGroup) {
    Set<Obs> replacements = new HashSet<Obs>();
    for (Obs member : obsGroup.getGroupMembers()) {
      replacements.add(new DoNotGoToServiceWhenFormatting(member));
    }

    obsGroup.setGroupMembers(replacements);
    return obsGroup;
  }
Ejemplo n.º 3
0
 private Obs findOrCreateObs(Obs diagnosisObs, Concept concept) {
   for (Obs o : diagnosisObs.getGroupMembers()) {
     if (concept.equals(o.getConcept())) {
       return o;
     }
   }
   Obs obs = new Obs();
   obs.setConcept(concept);
   return obs;
 }
Ejemplo n.º 4
0
  /**
   * This is an equivalent to a copy constructor. Creates a new copy of the given <code>obsToCopy
   * </code> with a null obs id
   *
   * @param obsToCopy The Obs that is going to be copied
   * @return a new Obs object with all the same attributes as the given obs
   */
  public static Obs newInstance(Obs obsToCopy) {
    Obs newObs =
        new Obs(
            obsToCopy.getPerson(),
            obsToCopy.getConcept(),
            obsToCopy.getObsDatetime(),
            obsToCopy.getLocation());

    newObs.setObsGroup(obsToCopy.getObsGroup());
    newObs.setAccessionNumber(obsToCopy.getAccessionNumber());
    newObs.setValueCoded(obsToCopy.getValueCoded());
    newObs.setValueDrug(obsToCopy.getValueDrug());
    newObs.setValueGroupId(obsToCopy.getValueGroupId());
    newObs.setValueDatetime(obsToCopy.getValueDatetime());
    newObs.setValueNumeric(obsToCopy.getValueNumeric());
    newObs.setValueModifier(obsToCopy.getValueModifier());
    newObs.setValueText(obsToCopy.getValueText());
    newObs.setComment(obsToCopy.getComment());
    newObs.setOrder(obsToCopy.getOrder());
    newObs.setEncounter(obsToCopy.getEncounter());
    newObs.setDateStarted(obsToCopy.getDateStarted());
    newObs.setDateStopped(obsToCopy.getDateStopped());
    newObs.setCreator(obsToCopy.getCreator());
    newObs.setDateCreated(obsToCopy.getDateCreated());
    newObs.setVoided(obsToCopy.getVoided());
    newObs.setVoidedBy(obsToCopy.getVoidedBy());
    newObs.setDateVoided(obsToCopy.getDateVoided());
    newObs.setVoidReason(obsToCopy.getVoidReason());

    newObs.setValueComplex(obsToCopy.getValueComplex());
    newObs.setComplexData(obsToCopy.getComplexData());

    if (obsToCopy.getGroupMembers() != null)
      for (Obs member : obsToCopy.getGroupMembers()) {
        // if the obs hasn't been saved yet, no need to duplicate it
        if (member.getObsId() == null) newObs.addGroupMember(member);
        else newObs.addGroupMember(Obs.newInstance(member));
      }

    return newObs;
  }
Ejemplo n.º 5
0
  /** @see Encounter#addObs(Obs) */
  @Test
  @Verifies(
      value = "should add encounter attrs to obs if attributes are null",
      method = "addObs(Obs)")
  public void addObs_shouldAddEncounterAttrsToObsGroupMembersIfAttributesAreNull()
      throws Exception {
    /// an encounter that will hav the date/location/patient on it
    Encounter encounter = new Encounter();

    Date date = new Date();
    encounter.setEncounterDatetime(date);

    Location location = new Location(1);
    encounter.setLocation(location);

    Patient patient = new Patient(1);
    encounter.setPatient(patient);

    // add an obs that doesn't have date/location/patient set on it.
    Obs obs = new Obs(123);
    Obs childObs = new Obs(456);
    obs.addGroupMember(childObs);

    // check for infinite recursion
    // childObs-->childObs2   and childObs2-->childObs
    Obs childObs2 = new Obs(456);
    childObs.addGroupMember(childObs2);
    childObs2.addGroupMember(childObs);

    assertTrue(obs.getGroupMembers() != null && obs.getGroupMembers().size() == 1);

    encounter.addObs(obs);

    // check the values of the obs attrs to see if they were added
    assertTrue(childObs.getObsDatetime().equals(date));
    assertTrue(childObs.getLocation().equals(location));
    assertTrue(childObs.getPerson().equals(patient));
    assertTrue(childObs2.getObsDatetime().equals(date));
    assertTrue(childObs2.getLocation().equals(location));
    assertTrue(childObs2.getPerson().equals(patient));
  }
 public List<FHIRResource> mapToFhirObservation(
     Obs observation, Encounter fhirEncounter, SystemProperties systemProperties) {
   List<FHIRResource> result = new ArrayList<>();
   FHIRResource entry = mapObservation(observation, fhirEncounter, systemProperties);
   Observation fhirObservation = (Observation) entry.getResource();
   fhirObservation.setStatus(ObservationStatusEnum.PRELIMINARY);
   for (Obs member : observation.getGroupMembers()) {
     mapGroupMember(member, fhirEncounter, fhirObservation, result, systemProperties);
   }
   result.add(entry);
   return result;
 }
Ejemplo n.º 7
0
  /** @see org.openmrs.api.db.ObsDAO#saveObs(org.openmrs.Obs) */
  public Obs saveObs(Obs obs) throws DAOException {
    if (obs.hasGroupMembers() && obs.getObsId() != null) {
      // hibernate has a problem updating child collections
      // if the parent object was already saved so we do it
      // explicitly here
      for (Obs member : obs.getGroupMembers()) if (member.getObsId() == null) saveObs(member);
    }

    sessionFactory.getCurrentSession().saveOrUpdate(obs);

    return obs;
  }
  @Test
  public void shouldAddNewObservationGroup() throws Exception {
    executeDataSet("shouldAddNewObservation.xml");
    String encounterDateTime = "2005-01-02T00:00:00.000+0000";
    String observationTime = "2005-01-02T12:00:00.000+0000";
    String json =
        "{ \"patientUuid\" : \"a76e8d23-0c38-408c-b2a8-ea5540f01b51\", "
            + "\"visitTypeUuid\" : \"b45ca846-c79a-11e2-b0c0-8e397087571c\", "
            + "\"encounterTypeUuid\": \"2b377dba-62c3-4e53-91ef-b51c68899890\", "
            + "\"encounterDateTime\" : \""
            + encounterDateTime
            + "\", "
            + "\"observations\":["
            + "{\"concept\":{\"uuid\": \"e102c80f-1yz9-4da3-bb88-8122ce8868dd\"}, "
            + " \"groupMembers\" : [{\"concept\":{\"uuid\": \"d102c80f-1yz9-4da3-bb88-8122ce8868dd\"}, \"value\":20, \"comment\":\"overweight\", \"observationDateTime\": \""
            + observationTime
            + "\"}] }"
            + "]}";

    EncounterTransaction response =
        deserialize(
            handle(newPostRequest("/rest/emrapi/encounter", json)), EncounterTransaction.class);

    Visit visit = visitService.getVisitByUuid(response.getVisitUuid());
    Encounter encounter = (Encounter) visit.getEncounters().toArray()[0];

    assertEquals(1, encounter.getObs().size());
    Obs obs = (Obs) encounter.getAllObs().toArray()[0];
    assertEquals("e102c80f-1yz9-4da3-bb88-8122ce8868dd", obs.getConcept().getUuid());

    assertEquals(1, obs.getGroupMembers().size());
    Obs member = obs.getGroupMembers().iterator().next();
    assertEquals("d102c80f-1yz9-4da3-bb88-8122ce8868dd", member.getConcept().getUuid());
    assertEquals(new Double(20.0), member.getValueNumeric());
    assertEquals("a76e8d23-0c38-408c-b2a8-ea5540f01b51", member.getPerson().getUuid());
    assertEquals("f13d6fae-baa9-4553-955d-920098bec08f", member.getEncounter().getUuid());
    assertEquals("overweight", member.getComment());
    assertEquals(
        new SimpleDateFormat(dateTimeFormat).parse(observationTime), member.getObsDatetime());
  }
  @Test
  public void shouldChangeObservationsOfPreviousEncounters() throws Exception {
    Date visitStartDate = getDateFromString("2014-06-22 00:00:00");
    Date encounterDate = getDateFromString("2014-06-23 00:00:00");

    List<Document> documents = new ArrayList<>();
    documents.add(
        new Document(
            "/radiology/foo.jpg",
            null,
            "5f596de5-5caa-11e3-a4c0-0800271c1b75",
            "6d0ae386-707a-4629-9850-f15206e63kj0",
            encounterDate,
            false));

    VisitDocumentRequest visitDocumentRequest =
        new VisitDocumentRequest(
            patientUUID,
            firstVisitUuid,
            visitTypeUUID,
            visitStartDate,
            null,
            secondEncounterTypeUUID,
            encounterDate,
            documents,
            secondProviderUuid,
            secondLocationUuid,
            null);
    executeDataSet("visitDocumentData.xml");
    visitDocumentService.upload(visitDocumentRequest);

    Context.flushSession();
    Context.clearSession();

    Encounter encounter = encounterService.getEncounterByUuid(secondEncounterUuid);
    for (Obs obs : encounter.getAllObs(true)) {
      if (obs.getUuid().equals("6d0ae386-707a-4629-9850-f15606e63666")
          || obs.getUuid().equals("6d0ae386-707a-4629-9850-f15206e63kj0")) {
        assertThat(obs.getVoided(), is(true));
      }
    }

    Obs savedDoc = getSavedDocument(encounter.getAllObs(), "5f596de5-5caa-11e3-a4c0-0800271c1b75");

    assertNotNull(savedDoc);
    assertThat(savedDoc.getConcept().getId(), is(333));
    assertThat(
        savedDoc.getGroupMembers().iterator().next().getValueText(), is("/radiology/foo.jpg"));
    assertEquals(FIRST_LOCATION_UUID, encounter.getLocation().getUuid());
  }
  @Test
  public void shouldUpdateObservations() throws Exception {
    executeDataSet("shouldUpdateObservations.xml");

    String json =
        "{ \"patientUuid\" : \"a76e8d23-0c38-408c-b2a8-ea5540f01b51\", "
            + "\"visitTypeUuid\" : \"b45ca846-c79a-11e2-b0c0-8e397087571c\", "
            + "\"encounterTypeUuid\": \"2b377dba-62c3-4e53-91ef-b51c68899890\","
            + "\"encounterDateTime\" : \"2013-01-01T00:00:00.000+0000\", "
            + "\"observations\":["
            + "{\"uuid\":\"z9fb7f47-e80a-4056-9285-bd798be13c63\", "
            + " \"groupMembers\" : [{\"uuid\":\"ze48cdcb-6a76-47e3-9f2e-2635032f3a9a\", \"value\":20, \"comment\":\"new gc\" }] }, "
            + "{\"uuid\":\"zf616900-5e7c-4667-9a7f-dcb260abf1de\", \"comment\" : \"new c\", \"value\":100 }"
            + "]}";

    EncounterTransaction response =
        deserialize(
            handle(newPostRequest("/rest/emrapi/encounter", json)), EncounterTransaction.class);

    Visit visit = visitService.getVisitByUuid(response.getVisitUuid());
    Encounter encounter = (Encounter) visit.getEncounters().toArray()[0];

    assertEquals(2, encounter.getObsAtTopLevel(false).size());
    Iterator<Obs> iterator = encounter.getObsAtTopLevel(false).iterator();

    Obs obs1 = iterator.next();
    assertEquals("zf616900-5e7c-4667-9a7f-dcb260abf1de", obs1.getUuid());
    assertEquals(new Double(100), obs1.getValueNumeric());
    assertEquals("new c", obs1.getComment());

    Obs obs2 = iterator.next();
    assertEquals("z9fb7f47-e80a-4056-9285-bd798be13c63", obs2.getUuid());
    assertEquals(1, obs2.getGroupMembers().size());
    Obs member = obs2.getGroupMembers().iterator().next();
    assertEquals(new Double(20), member.getValueNumeric());
    assertEquals("new gc", member.getComment());
  }
 private void mapGroupMember(
     Obs obs,
     Encounter fhirEncounter,
     Observation parentObservation,
     List<FHIRResource> result,
     SystemProperties systemProperties) {
   if (hasIgnoredConcept(obs)) {
     return;
   }
   FHIRResource entry = mapObservation(obs, fhirEncounter, systemProperties);
   Observation observation = (Observation) entry.getResource();
   mapRelatedObservation(observation).mergeWith(parentObservation, systemProperties);
   for (Obs member : obs.getGroupMembers()) {
     mapGroupMember(member, fhirEncounter, observation, result, systemProperties);
   }
   result.add(entry);
 }
  @Test
  public void shouldAddDiagnosesAdObservation() throws Exception {
    executeDataSet("baseMetaData.xml");
    executeDataSet("diagnosisMetaData.xml");
    executeDataSet("shouldAddDiagnosisAsObservation.xml");
    String cancerDiagnosisUuid = "d102c80f-1yz9-4da3-bb88-8122ce8868dh";
    String encounterDateTime = "2005-01-02T00:00:00.000+0000";
    String diagnosisDateTime = "2005-01-02T01:00:00.000+0000";
    String postData =
        "{"
            + "\"patientUuid\" : \"a76e8d23-0c38-408c-b2a8-ea5540f01b51\", "
            + "\"visitTypeUuid\" : \"b45ca846-c79a-11e2-b0c0-8e397087571c\", "
            + "\"encounterTypeUuid\": \"2b377dba-62c3-4e53-91ef-b51c68899891\", "
            + "\"encounterDateTime\" : \""
            + encounterDateTime
            + "\", "
            + "\"diagnoses\":["
            + "{\"order\":\"PRIMARY\", \"certainty\": \"CONFIRMED\", \"codedAnswer\": { \"uuid\": \""
            + cancerDiagnosisUuid
            + "\"}, \"diagnosisDateTime\": \""
            + diagnosisDateTime
            + "\" }"
            + "]"
            + "}";

    EncounterTransaction response =
        deserialize(
            handle(newPostRequest("/rest/emrapi/encounter", postData)), EncounterTransaction.class);

    Visit visit = visitService.getVisitByUuid(response.getVisitUuid());
    Encounter encounter = visit.getEncounters().iterator().next();

    Set<Obs> obsAtTopLevel = encounter.getObsAtTopLevel(false);
    assertEquals(1, obsAtTopLevel.size());
    Obs parentObservation = obsAtTopLevel.iterator().next();
    assertTrue(parentObservation.isObsGrouping());
    assertEquals(
        DateUtils.parseDate(diagnosisDateTime, dateTimeFormat), parentObservation.getObsDatetime());

    Set<Obs> diagnosisObservationGroupMembers = parentObservation.getGroupMembers();
    assertEquals(3, diagnosisObservationGroupMembers.size());
    ArrayList<String> valueCodedNames = getValuCodedNames(diagnosisObservationGroupMembers);
    assertTrue(valueCodedNames.contains("Confirmed"));
    assertTrue(valueCodedNames.contains("Primary"));
    assertTrue(valueCodedNames.contains("Cancer"));
  }
  @Test
  public void shouldPreferVoidOverUpdateWhenEditingADocument() throws Exception {
    Date visitStartDate = getDateFromString("2014-06-22 00:00:00");
    Date encounterDate = getDateFromString("2014-06-23 00:00:00");

    List<Document> documents = new ArrayList<>();
    String obsUuid = "6d0ae386-707a-4629-9850-f15206e63kj0";
    documents.add(
        new Document(
            "/radiology/foo.jpg",
            null,
            "3f596de5-5caa-11e3-a4c0-0800271c1b75",
            obsUuid,
            encounterDate,
            true));

    VisitDocumentRequest visitDocumentRequest =
        new VisitDocumentRequest(
            patientUUID,
            firstVisitUuid,
            visitTypeUUID,
            visitStartDate,
            null,
            secondEncounterTypeUUID,
            encounterDate,
            documents,
            secondProviderUuid,
            secondLocationUuid,
            null);
    visitDocumentService.upload(visitDocumentRequest);

    Context.flushSession();
    Context.clearSession();

    Encounter encounter = encounterService.getEncounterByUuid(secondEncounterUuid);

    Obs savedObs = obsService.getObsByUuid(obsUuid);
    assertTrue("Observation is not voided", savedObs.getVoided());
    assertTrue(
        "Observation is not voided", savedObs.getGroupMembers(true).iterator().next().getVoided());

    Obs savedDoc = getSavedDocument(encounter.getAllObs(), "3f596de5-5caa-11e3-a4c0-0800271c1b75");

    assertNull(savedDoc);
  }
  /**
   * Auto generated method comment
   *
   * @param obs
   * @param conceptId
   * @return
   */
  public static String convsetObsValueByConcept(Obs obs, Integer conceptId) {
    String res = "-";
    try {
      if (obs != null) {
        for (Obs ob : obs.getGroupMembers()) {
          if (ob.getConcept().getConceptId() == conceptId.intValue()) {
            res = ob.getValueAsString(Context.getLocale());
            break;
          }
        }
      }
    } catch (Exception e) {
      log.error(">>>>>>>>>>>>>>>>>>>VCT>>Module>>Tag>>>> An error occured : " + e.getMessage());
      e.printStackTrace();
    }

    return res;
  }
  @Test
  public void shouldNotChangeObservationsIfSameDetailsProvidedOnceAgain() throws Exception {
    Date visitStartDate = getDateFromString("2014-06-22 00:00:00");
    Date encounterDate = getDateFromString("2014-06-23 00:00:00");

    List<Document> documents = new ArrayList<>();
    documents.add(
        new Document(
            "/radiology/foo.jpg",
            null,
            conceptUuid,
            "6d0ae386-707a-4629-9850-f15206e63kj0",
            encounterDate,
            false));

    VisitDocumentRequest visitDocumentRequest =
        new VisitDocumentRequest(
            patientUUID,
            firstVisitUuid,
            visitTypeUUID,
            visitStartDate,
            null,
            secondEncounterTypeUUID,
            encounterDate,
            documents,
            secondProviderUuid,
            secondLocationUuid,
            null);
    visitDocumentService.upload(visitDocumentRequest);

    Context.flushSession();
    Context.clearSession();

    Encounter encounter = encounterService.getEncounterByUuid(secondEncounterUuid);

    Obs savedDoc = getSavedDocument(encounter.getAllObs(), conceptUuid);

    assertNotNull(savedDoc);
    Set<Obs> groupMembers = savedDoc.getGroupMembers();
    assertThat(groupMembers.size(), is(equalTo(1)));
    assertThat(groupMembers.iterator().next().getValueText(), is("/radiology/foo.jpg"));
    assertThat(
        groupMembers.iterator().next().getUuid(), is("6d0ae386-707a-4629-9850-f15606e63666"));
  }
  /**
   * Column headers must be unique to avoid clobbering and to ensure it is clear which columns are
   * grouped together by obsgroup. As such, duplicate concepts at the parent level or duplicate
   * concepts in the same obs group are given a unique occurrence number. For instance, in an obs
   * group Functional Review Of Symptoms, there might be two child obs with the concept Symptom
   * Present. To distinguish them, one is given an occurrence number of 1 and the next an occurrence
   * number of 2.
   *
   * @param fieldMap
   * @param obs
   * @return Map<Obs, Integer> the filled in fieldMap
   */
  public static Map<Obs, Integer> addChildrenObsToFieldMap(Map<Obs, Integer> fieldMap, Obs obs) {

    if (obs != null && obs.isObsGrouping()) {

      Set<Obs> childSet = obs.getGroupMembers();
      List<Obs> children = new ArrayList<Obs>(childSet);

      Map<Concept, Integer> childMap = new HashMap<Concept, Integer>();
      for (Obs o : children) {
        // check for duplicate concepts among this batch of children
        Integer occurrence =
            (childMap.get(o.getConcept()) != null) ? childMap.get(o.getConcept()) : 0;
        occurrence++;
        childMap.put(o.getConcept(), occurrence);

        fieldMap.put(o, occurrence);
        addChildrenObsToFieldMap(fieldMap, o);
      }
    }

    return fieldMap;
  }
  @Test
  public void shouldCreateObservations() throws Exception {
    Date visitStartDate = getDateFromString("2014-06-22 00:00:00");
    Date encounterDate = getDateFromString("2014-06-23 00:00:00");
    Date obsDate = getDateFromString("2014-06-24 00:10:00");

    List<Document> documents = new ArrayList<>();
    documents.add(new Document("/radiology/fooo-bar.jpg", null, conceptUuid, null, obsDate, false));

    visitDocumentRequest =
        new VisitDocumentRequest(
            patientUUID,
            secondVisitUuid,
            visitTypeUUID,
            visitStartDate,
            null,
            firstEncounterTypeUUID,
            encounterDate,
            documents,
            firstProviderUuid,
            FIRST_LOCATION_UUID,
            null);

    visitDocumentService.upload(visitDocumentRequest);

    Context.flushSession();
    Context.clearSession();

    Encounter encounter = encounterService.getEncounterByUuid(firstEncounterUuid);

    Obs savedDoc = getSavedDocument(encounter.getAllObs(), conceptUuid);

    assertNotNull(savedDoc);
    assertThat(savedDoc.getConcept().getId(), is(222));
    assertThat(
        savedDoc.getGroupMembers().iterator().next().getValueText(), is("/radiology/fooo-bar.jpg"));
    assertEquals(FIRST_LOCATION_UUID, encounter.getLocation().getUuid());
  }
  @Test
  public void shouldDeleteObservationsOfPreviousEncounters() throws ParseException {
    Date visitStartDate = getDateFromString("2014-06-22 00:00:00");
    Date encounterDate = getDateFromString("2014-06-23 00:00:00");

    List<Document> documents = new ArrayList<>();
    String testUuid = "3f596de5-5caa-11e3-a4c0-0800271c1b75";
    String obsUuid = "6d0ae386-707a-4629-9850-f15206e63j8s";
    String providerUuid = "331c6bf8-7846-11e3-a96a-0800271c1b75";
    documents.add(new Document("/patient_file", null, testUuid, obsUuid, encounterDate, true));

    visitDocumentRequest =
        new VisitDocumentRequest(
            patientUUID,
            secondVisitUuid,
            visitTypeUUID,
            visitStartDate,
            null,
            firstEncounterTypeUUID,
            encounterDate,
            documents,
            providerUuid,
            null,
            null);
    visitDocumentService.upload(visitDocumentRequest);

    Context.flushSession();
    Context.clearSession();

    Encounter encounter = encounterService.getEncounterByUuid(firstEncounterUuid);
    for (Obs obs : encounter.getAllObs(true)) {
      if (obs.getUuid().equals(obsUuid)) {
        assertThat(obs.getVoided(), is(true));
        assertThat(obs.getGroupMembers(true).iterator().next().getVoided(), is(true));
      }
    }
  }
 private String getImageName(Obs obs) {
   Set<Obs> groupMembers = obs.getGroupMembers();
   return groupMembers.iterator().hasNext() ? groupMembers.iterator().next().getValueText() : null;
 }