/** * Unvoids an Obs * <p> * If the Obs argument is an obsGroup, all group members with the same dateVoided will also be * unvoided. * * @see org.openmrs.api.ObsService#unvoidObs(org.openmrs.Obs) * @param obs the Obs to unvoid * @return the unvoided Obs * @throws APIException */ @Override public Obs unvoidObs(Obs obs) throws APIException { return Context.getObsService().saveObs(obs,"unvoid obs"); }
private void saveObsGroup(Obs obs, String changeMessage){ if (obs.isObsGrouping()) { for (Obs o : obs.getGroupMembers(true)) { Context.getObsService().saveObs(o, changeMessage); } } }
private Obs saveObsNotDirty(Obs obs, String changeMessage) { if(!obs.isObsGrouping()){ return obs; } ObsService os = Context.getObsService(); boolean refreshNeeded = false; for (Obs o : obs.getGroupMembers(true)) { if (o.getId() == null) { os.saveObs(o, null); } else { Obs newObs = os.saveObs(o, changeMessage); refreshNeeded = !newObs.equals(o) || refreshNeeded; } } if(refreshNeeded) { Context.refreshEntity(obs); } return obs; }
private void mergeObservationsNotContainedInEncounters(Patient preferred, Patient notPreferred, PersonMergeLogData mergedData) { // move all obs that weren't contained in encounters // TODO: this should be a copy, not a move ObsService obsService = Context.getObsService(); for (Obs obs : obsService.getObservationsByPerson(notPreferred)) { if (obs.getEncounter() == null && !obs.getVoided()) { obs.setPerson(preferred); Obs persisted = obsService.saveObs(obs, "Merged from patient #" + notPreferred.getPatientId()); mergedData.addMovedIndependentObservation(persisted.getUuid()); } } }
@Test public void saveObs_shouldNotChangeStatusOfPreliminaryWhenModifyingAnObs() throws Exception { Obs existing = obsService.getObs(9); existing.setValueNumeric(175.0); Obs newObs = obsService.saveObs(existing, "testing"); assertThat(newObs.getValueNumeric(), is(175.0)); assertThat(newObs.getStatus(), is(Obs.Status.PRELIMINARY)); }
@Test public void saveObs_shouldSetStatusToAmendedWhenModifyingAnObsWithFinalStatus() throws Exception { Obs existing = obsService.getObs(7); existing.setValueNumeric(60.0); Obs amended = obsService.saveObs(existing, "testing"); assertThat(amended.getValueNumeric(), is(60.0)); assertThat(amended.getStatus(), is(Obs.Status.AMENDED)); assertThat(existing.getStatus(), is(Obs.Status.FINAL)); }
@Test public void saveObs_shouldLetYouChangeStatusFromPreliminaryToFinalWhenModifyingAnObs() throws Exception { Obs existing = obsService.getObs(9); existing.setValueNumeric(175.0); existing.setStatus(Obs.Status.FINAL); Obs newObs = obsService.saveObs(existing, "testing"); assertThat(newObs.getValueNumeric(), is(175.0)); assertThat(newObs.getStatus(), is(Obs.Status.FINAL)); }
/** * @see ObsService#saveObs(Obs,String) */ @Test public void saveObs_shouldReturnADifferentObjectWhenUpdatingAnObs() { ObsService obsService = Context.getObsService(); Obs obs = obsService.getObs(7); // change something on the obs and save it again obs.setComment("A new comment"); Obs obsSaved = obsService.saveObs(obs, "Testing that a new obs is returned"); assertFalse(obsSaved.getObsId().equals(obs.getObsId())); }
@Test public void shouldReturnAPIExceptionWhenObsIsNull(){ expectedException.expect(APIException.class); expectedException.expectMessage(Context.getMessageSourceService().getMessage("Obs.error.cannot.be.null")); ObsService os = Context.getObsService(); os.saveObs(null,"Null Obs"); }
/** * Tests that we support a manual workaround in case you need to modify a FINAL obs and leave its status as FINAL */ @Test public void shouldNotAutomaticallySetStatusWhenManuallyCopyingAnObs() throws Exception { Obs existing = obsService.getObs(7); Obs newObs = Obs.newInstance(existing); newObs.setValueNumeric(60.0); newObs.setPreviousVersion(existing); newObs = obsService.saveObs(newObs, null); obsService.voidObs(existing, "testing"); assertThat(existing.getStatus(), is(Obs.Status.FINAL)); assertThat(existing.getVoided(), is(true)); assertThat(newObs.getStatus(), is(Obs.Status.FINAL)); } }
@Test public void saveConcept_shouldUpdateConceptIfConceptIsAttachedToAnObservationAndItIsANonDatatypeChange() { executeDataSet(INITIAL_CONCEPTS_XML); Concept concept = conceptService.getConcept(1); assertNotNull(concept); ObsService obsService = Context.getObsService(); Obs obs = new Obs(new Person(1), concept, new Date(), new Location(1)); obs.setValueCoded(Context.getConceptService().getConcept(7)); obsService.saveObs(obs, "Creating a new observation with a concept"); conceptService.saveConcept(concept); }
@Test public void saveObs_shouldOverwriteObsPersonValueWithEncounterPatient() { String changeMessage = "Testing TRUNK-3283"; executeDataSet(ENCOUNTER_OBS_XML); ObsService obsService = Context.getObsService(); Obs obs = obsService.getObs(13); //overwrite ObsPerson with EncounterPatient Obs obsSaved = obsService.saveObs(obs, changeMessage); assertEquals(obs.getPerson(), obsSaved.getEncounter().getPatient()); }
/** * @see ObsService#saveObs(Obs,String) */ @Test public void saveObs_shouldSetVoidReasonMessageToChangeMessage() { // Set changeMessage arg to saveObs() method - should equal void reason on new Obs String changeMessage = "Testing TRUNK-3701"; int obsId = 7; ObsService obsService = Context.getObsService(); Obs obs = obsService.getObs(obsId); // change something on the obs and save it again obs.setComment("Comment to make sure obs changes"); obsService.saveObs(obs, changeMessage); obs = obsService.getObs(obsId); //refetch original (now voided) obs // check assertEquals(changeMessage, obs.getVoidReason()); }
/** * @see ObsService#saveObs(Obs,String) */ @Test public void saveObs_shouldVoidTheGivenObsInTheDatabase() { Obs obs = Context.getObsService().getObs(7); obs.setValueNumeric(1.0); Context.getObsService().saveObs(obs, "just testing"); // fetch the obs from the database again obs = Context.getObsService().getObs(7); Assert.assertTrue(obs.getVoided()); }
@Test(expected = ConceptInUseException.class) public void saveConcept_shouldNotUpdateConceptDataTypeIfConceptIsAttachedToAnObservation() { executeDataSet(INITIAL_CONCEPTS_XML); Concept concept = conceptService.getConcept(2); assertNotNull(concept); ObsService obsService = Context.getObsService(); Obs obs = new Obs(Context.getPersonService().getPerson(1), concept, new Date(), Context.getLocationService() .getLocation(1)); obs.setValueCoded(Context.getConceptService().getConcept(7)); obsService.saveObs(obs, "Creating a new observation with a concept"); ConceptDatatype newDatatype = conceptService.getConceptDatatypeByName("Text"); concept.setDatatype(newDatatype); conceptService.saveConcept(concept); }
/** * @see ObsService#saveObs(Obs,String) */ @Test public void saveObs_shouldCreateVeryBasicObsAndAddNewObsId() { Obs o = new Obs(); o.setConcept(Context.getConceptService().getConcept(3)); o.setPerson(new Patient(2)); o.setEncounter(new Encounter(3)); o.setObsDatetime(new Date()); o.setLocation(new Location(1)); o.setValueNumeric(50d); Obs oSaved = Context.getObsService().saveObs(o, null); // make sure the returned Obs and the passed in obs // now both have primary key obsIds assertTrue(oSaved.getObsId().equals(o.getObsId())); }
/** * @see ObsService#saveObs(Obs,String) */ @Test public void saveObs_shouldLinkOriginalAndUpdatedObs() { // build int obsId = 7; ObsService obsService = Context.getObsService(); Obs obs = obsService.getObs(obsId); // operate // change something on the obs and save it again obs.setComment("A new comment"); Obs obsSaved = obsService.saveObs(obs, "Testing linkage"); obs = obsService.getObs(obsId); // check assertNotNull(obsSaved); assertNotNull(obs); assertEquals(obs, obsSaved.getPreviousVersion()); }
/** * @see ObsService#saveObs(Obs,String) */ @Test public void saveObs_shouldSetCreatorAndDateCreatedOnNewObs() { Obs o = new Obs(); o.setConcept(Context.getConceptService().getConcept(3)); o.setPerson(new Patient(2)); o.setEncounter(new Encounter(3)); o.setObsDatetime(new Date()); o.setLocation(new Location(1)); o.setValueNumeric(50d); Context.getObsService().saveObs(o, null); assertNotNull(o.getDateCreated()); assertNotNull(o.getCreator()); }
/** * @see ObsService#saveObs(Obs,String) */ @Test public void saveObs_shouldCopyTheFormNamespaceAndPathFieldInEditedObs() { executeDataSet(INITIAL_OBS_XML); Obs obs = Context.getObsService().getObs(7); obs.setValueNumeric(5.0); Obs o2 = Context.getObsService().saveObs(obs, "just testing"); Assert.assertNotNull(obs.getFormFieldNamespace()); // fetch the obs from the database again obs = Context.getObsService().getObs(o2.getObsId()); Assert.assertNotNull(obs.getFormFieldNamespace()); Assert.assertNotNull(obs.getFormFieldPath()); }
/** * @see ConceptService#purgeConcept(Concept) */ @Test(expected = ConceptNameInUseException.class) public void purgeConcept_shouldFailIfAnyOfTheConceptNamesOfTheConceptIsBeingUsedByAnObs() { Obs o = new Obs(); o.setConcept(Context.getConceptService().getConcept(3)); o.setPerson(new Patient(2)); o.setEncounter(new Encounter(3)); o.setObsDatetime(new Date()); o.setLocation(new Location(1)); ConceptName conceptName = new ConceptName(1847); o.setValueCodedName(conceptName); Context.getObsService().saveObs(o, null); //ensure that the association between the conceptName and the obs has been established Assert.assertEquals(true, conceptService.hasAnyObservation(conceptName)); Concept concept = conceptService.getConceptByName("cd4 count"); //make sure the name concept name exists Assert.assertNotNull(concept); conceptService.purgeConcept(concept); }