private void addToObsGroup(Obs obsGroup, Obs member) {
   member.setPerson(obsGroup.getPerson());
   member.setObsDatetime(obsGroup.getObsDatetime());
   member.setLocation(obsGroup.getLocation());
   member.setEncounter(obsGroup.getEncounter());
   obsGroup.addGroupMember(member);
 }
 private Encounter buildEncounter(
     EncounterType encounterType,
     Patient patient,
     Location location,
     Form form,
     Date when,
     List<Obs> obsToCreate,
     List<Order> ordersToCreate) {
   Encounter encounter = new Encounter();
   encounter.setPatient(patient);
   encounter.setEncounterType(encounterType);
   encounter.setLocation(location);
   encounter.setForm(form);
   encounter.setEncounterDatetime(when);
   if (obsToCreate != null) {
     for (Obs obs : obsToCreate) {
       obs.setObsDatetime(new Date());
       encounter.addObs(obs);
     }
   }
   if (ordersToCreate != null) {
     for (Order order : ordersToCreate) {
       encounter.addOrder(order);
     }
   }
   return encounter;
 }
 public ObsBuilder addMember(Concept question, String valueText) {
   Obs child = new Obs();
   child.setPerson(obs.getPerson());
   child.setObsDatetime(obs.getObsDatetime());
   child.setConcept(question);
   child.setValueText(valueText);
   obs.addGroupMember(child);
   return this;
 }
  public QueueItem saveQueueItem(QueueItem queueItem) {
    log.debug("saveQueueItem(). Entering");
    boolean isNew = false;
    Date newDate = queueItem.getEncounter().getEncounterDatetime();
    log.debug("Encounter date: " + newDate);
    Date originalDate = null;

    if (queueItem.getQueueItemId() == null) {
      isNew = true;
      log.debug("Got a  new queue item");
    }

    // Not new we update some of the Encounter info
    if (!isNew) {
      log.info("Updating previously queued encounter!");
      Encounter encounter = queueItem.getEncounter();
      Patient p = encounter.getPatient();
      originalDate = encounter.getEncounterDatetime();
      if (OpenmrsUtil.compare(originalDate, newDate) != 0) {

        // if the obs datetime is the same as the
        // original encounter datetime, fix it
        if (OpenmrsUtil.compare(queueItem.getDateCreated(), originalDate) == 0) {
          encounter.setEncounterDatetime(newDate);
        }
      }

      // if the Person in the encounter doesn't match the Patient in the ,
      // fix it
      if (!encounter.getPatient().getPersonId().equals(p.getPatientId())) {
        encounter.setPatient(p);
      }

      for (Obs obs : encounter.getAllObs(true)) {
        // if the date was changed
        if (OpenmrsUtil.compare(originalDate, newDate) != 0) {

          // if the obs datetime is the same as the
          // original encounter datetime, fix it
          if (OpenmrsUtil.compare(obs.getObsDatetime(), originalDate) == 0) {
            obs.setObsDatetime(newDate);
          }
        }

        // if the Person in the obs doesn't match the Patient in the
        // encounter, fix it
        if (!obs.getPerson().getPersonId().equals(p.getPatientId())) {
          obs.setPerson(p);
        }
      }
    }
    log.debug("Saving queu item.");
    dao.saveQueueItem(queueItem);
    return queueItem;
  }
예제 #5
0
  /** @see {@link ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)} */
  @Test
  @Verifies(
      value = "should pass validation if all values present",
      method = "validate(java.lang.Object, org.springframework.validation.Errors)")
  public void validate_shouldPassValidationIfAllValuesPresent() throws Exception {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(5089));
    obs.setObsDatetime(new Date());
    obs.setValueNumeric(1.0);

    Errors errors = new BindException(obs, "obs");
    new ObsValidator().validate(obs, errors);

    Assert.assertFalse(errors.hasErrors());
  }
예제 #6
0
  /** @see {@link ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)} */
  @Test
  @Verifies(
      value = "should fail validation if concept datatype is text and valueText is null",
      method = "validate(java.lang.Object, org.springframework.validation.Errors)")
  public void validate_shouldFailValidationIfConceptDatatypeIsTextAndValueTextIsNull()
      throws Exception {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(19));
    obs.setObsDatetime(new Date());

    Errors errors = new BindException(obs, "obs");
    new ObsValidator().validate(obs, errors);

    Assert.assertTrue(errors.hasFieldErrors("valueText"));
  }
예제 #7
0
  /** @see {@link ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)} */
  @Test
  @Verifies(
      value = "should fail if obs has no values and not parent",
      method = "validate(java.lang.Object, org.springframework.validation.Errors)")
  public void validate_shouldFailIfObsHasNoValuesAndNotParent() throws Exception {

    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(18));
    obs.setObsDatetime(new Date());

    Errors errors = new BindException(obs, "obs");
    new ObsValidator().validate(obs, errors);

    Assert.assertTrue(errors.getGlobalErrorCount() > 0);
  }
예제 #8
0
  /** @see {@link ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)} */
  @Test
  @Verifies(
      value = "should fail validation if concept is null",
      method = "validate(java.lang.Object, org.springframework.validation.Errors)")
  public void validate_shouldFailValidationIfConceptIsNull() throws Exception {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setObsDatetime(new Date());
    obs.setValueNumeric(1.0);

    Errors errors = new BindException(obs, "obs");
    new ObsValidator().validate(obs, errors);

    Assert.assertFalse(errors.hasFieldErrors("person"));
    Assert.assertTrue(errors.hasFieldErrors("concept"));
    Assert.assertFalse(errors.hasFieldErrors("obsDatetime"));
    Assert.assertFalse(errors.hasFieldErrors("valueNumeric"));
  }
예제 #9
0
  /** @see {@link ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)} */
  @Test
  @Verifies(
      value = "should fail validation if concept datatype is boolean and valueBoolean is null",
      method = "validate(java.lang.Object, org.springframework.validation.Errors)")
  public void validate_shouldFailValidationIfConceptDatatypeIsBooleanAndValueBooleanIsNull()
      throws Exception {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(18));
    obs.setObsDatetime(new Date());

    Errors errors = new BindException(obs, "obs");
    new ObsValidator().validate(obs, errors);

    Assert.assertFalse(errors.hasFieldErrors("person"));
    Assert.assertFalse(errors.hasFieldErrors("concept"));
    Assert.assertFalse(errors.hasFieldErrors("obsDatetime"));
    Assert.assertTrue(
        errors.hasFieldErrors("valueNumeric")); // Booleans translates to numeric at the moment
  }
예제 #10
0
  /** @see {@link ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)} */
  @Test
  @Verifies(
      value =
          "should fail validation if concept value for text datatype is greater than the maximum length",
      method = "validate(java.lang.Object, org.springframework.validation.Errors)")
  public void validate_shouldFailValidationIfValueTextIsGreaterThanTheMaximumLength()
      throws Exception {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(19));
    obs.setObsDatetime(new Date());
    obs.setValueText(
        "the limit of valueText is 50. So we are trying to test it with more than 50 characters and this is the test text	1");

    Errors errors = new BindException(obs, "obs");
    new ObsValidator().validate(obs, errors);

    Assert.assertFalse(errors.hasFieldErrors("person"));
    Assert.assertFalse(errors.hasFieldErrors("concept"));
    Assert.assertFalse(errors.hasFieldErrors("obsDatetime"));
    Assert.assertTrue(errors.hasFieldErrors("valueText"));
  }
예제 #11
0
  /** @see {@link ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)} */
  @Test
  @Verifies(
      value = "should fail validation if obs ancestors contains obs",
      method = "validate(java.lang.Object, org.springframework.validation.Errors)")
  public void validate_shouldFailValidationIfObsAncestorsContainsObs() throws Exception {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(3)); // datatype = N/A
    obs.setObsDatetime(new Date());

    Set<Obs> group = new HashSet<Obs>();
    group.add(obs);
    obs.setGroupMembers(group);

    Errors errors = new BindException(obs, "obs");
    new ObsValidator().validate(obs, errors);

    Assert.assertFalse(errors.hasFieldErrors("person"));
    Assert.assertFalse(errors.hasFieldErrors("concept"));
    Assert.assertFalse(errors.hasFieldErrors("obsDatetime"));
    Assert.assertTrue(errors.hasFieldErrors("groupMembers"));
  }
예제 #12
0
 /**
  * Creates an OpenMRS Obs instance
  *
  * @param concept concept associated with the Obs
  * @param value value associated with the Obs
  * @param datetime date/time associated with the Obs (may be null)
  * @param accessionNumber accession number associatd with the Obs (may be null)
  * @return the created Obs instance
  */
 public static Obs createObs(
     Concept concept, Object value, Date datetime, String accessionNumber) {
   Obs obs = new Obs();
   obs.setConcept(concept);
   ConceptDatatype dt = obs.getConcept().getDatatype();
   if (dt.isNumeric()) {
     obs.setValueNumeric(Double.parseDouble(value.toString()));
   } else if (dt.isText()) {
     if (value instanceof Location) {
       obs.setValueText(((Location) value).getLocationId().toString());
     } else {
       obs.setValueText(value.toString());
     }
   } else if (dt.isCoded()) {
     if (value instanceof Concept) obs.setValueCoded((Concept) value);
     else obs.setValueCoded((Concept) convertToType(value.toString(), Concept.class));
   } else if (dt.isBoolean()) {
     boolean booleanValue =
         value != null && !Boolean.FALSE.equals(value) && !"false".equals(value);
     obs.setValueNumeric(booleanValue ? 1.0 : 0.0);
   } else if (dt.isDate()) {
     Date date = (Date) value;
     obs.setValueDatetime(date);
   } else if ("ZZ".equals(dt.getHl7Abbreviation())) {
     // don't set a value
   } else {
     throw new IllegalArgumentException(
         "concept datatype not yet implemented: "
             + dt.getName()
             + " with Hl7 Abbreviation: "
             + dt.getHl7Abbreviation());
   }
   if (datetime != null) obs.setObsDatetime(datetime);
   if (accessionNumber != null) obs.setAccessionNumber(accessionNumber);
   return obs;
 }
 public ObsBuilder setObsDatetime(Date obsDatetime) {
   obs.setObsDatetime(obsDatetime);
   return this;
 }
  /** @see org.openmrs.api.EncounterService#saveEncounter(org.openmrs.Encounter) */
  public Encounter saveEncounter(Encounter encounter) throws APIException {

    // if authenticated user is not supposed to edit encounter of certain type
    if (!canEditEncounter(encounter, null)) {
      throw new APIException(
          "Encounter.error.privilege.required.edit",
          new Object[] {encounter.getEncounterType().getEditPrivilege()});
    }

    // If new encounter, try to assign a visit using the registered visit assignment handler.
    if (encounter.getEncounterId() == null) {

      // Am using Context.getEncounterService().getActiveEncounterVisitHandler() instead of just
      // getActiveEncounterVisitHandler() for modules which may want to AOP around this call.
      EncounterVisitHandler encounterVisitHandler =
          Context.getEncounterService().getActiveEncounterVisitHandler();
      if (encounterVisitHandler != null) {
        encounterVisitHandler.beforeCreateEncounter(encounter);

        // If we have been assigned a new visit, persist it.
        if (encounter.getVisit() != null && encounter.getVisit().getVisitId() == null) {
          Context.getVisitService().saveVisit(encounter.getVisit());
        }
      }
    }

    boolean isNewEncounter = false;
    Date newDate = encounter.getEncounterDatetime();
    Date originalDate = null;
    Location newLocation = encounter.getLocation();
    Location originalLocation = null;
    // check permissions
    if (encounter.getEncounterId() == null) {
      isNewEncounter = true;
      Context.requirePrivilege(PrivilegeConstants.ADD_ENCOUNTERS);
    } else {
      Context.requirePrivilege(PrivilegeConstants.EDIT_ENCOUNTERS);
    }

    // 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();

    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

      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);
        }

        // if the Person in the obs doesn't match the Patient in the
        // 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 orders
    for (Order o : encounter.getOrders()) {
      if (o.getOrderId() == null) {
        Context.getOrderService().saveOrder(o, null);
      }
    }
    return encounter;
  }