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);
 }
 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;
 }
  /**
   * Get new obs corresponding to given radiologyOrder
   *
   * @param radiologyOrder radiology order for which the obs should be returned
   * @return model and view populated with a new obs
   * @should populate model and view with new obs given a valid radiology order
   */
  @RequestMapping(value = "/module/radiology/radiologyObs.form", method = RequestMethod.GET)
  protected ModelAndView getNewObs(
      @RequestParam(value = "orderId", required = true) RadiologyOrder radiologyOrder) {

    Obs obs = new Obs();
    obs.setOrder(radiologyOrder);
    obs.setPerson(radiologyOrder.getPatient());
    obs.setEncounter(radiologyOrder.getEncounter());
    return populateModelAndView(radiologyOrder, obs);
  }
  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;
  }
  /** @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());
  }
  /** @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"));
  }
  /** @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);
  }
  /** @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"));
  }
  /** @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
  }
  /** @see {@link ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)} */
  @Test
  @Verifies(
      value = "should fail if the parent obs has values",
      method = "validate(java.lang.Object, org.springframework.validation.Errors)")
  public void validate_shouldFailIfParentObshasValues() throws Exception {

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

    obs.setValueBoolean(false);
    obs.setValueCoded(Context.getConceptService().getConcept(18));
    obs.setValueComplex("test");
    obs.setValueDatetime(new Date());
    obs.setValueDrug(Context.getConceptService().getDrug(3));
    obs.setValueGroupId(getLoadCount());
    obs.setValueModifier("test");
    obs.setValueNumeric(1212.0);
    obs.setValueText("test");

    Set<Obs> group = new HashSet<Obs>();
    group.add(Context.getObsService().getObs(7));
    group.add(Context.getObsService().getObs(9));
    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.assertTrue(errors.hasFieldErrors("valueCoded"));
    Assert.assertTrue(errors.hasFieldErrors("valueDrug"));
    Assert.assertTrue(errors.hasFieldErrors("valueDatetime"));
    Assert.assertTrue(errors.hasFieldErrors("valueNumeric"));
    Assert.assertTrue(errors.hasFieldErrors("valueModifier"));
    Assert.assertTrue(errors.hasFieldErrors("valueText"));
    Assert.assertTrue(errors.hasFieldErrors("valueBoolean"));
    Assert.assertTrue(errors.hasFieldErrors("valueComplex"));
  }
  /** @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"));
  }
  /** @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"));
  }
 public ObsBuilder setPerson(Person person) {
   obs.setPerson(person);
   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;
  }
Exemple #15
0
 /**
  * To associate a patient with an obs, use <code>setPerson(org.openmrs.Person)</code>
  *
  * @deprecated use setPerson(org.openmrs.Person)
  * @param patient
  */
 public void setPatient(Patient patient) {
   setPerson(patient);
 }