Esempio n. 1
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;
  }
  /** @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"));
  }