use of org.openmrs.api.ObsService in project openmrs-core by openmrs.
the class ObsServiceImpl method saveObsNotDirty.
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;
}
use of org.openmrs.api.ObsService in project openmrs-core by openmrs.
the class EncounterServiceImpl method voidEncounter.
/**
* @see org.openmrs.api.EncounterService#voidEncounter(org.openmrs.Encounter, java.lang.String)
*/
@Override
public Encounter voidEncounter(Encounter encounter, String reason) {
// if authenticated user is not supposed to edit encounter of certain type
if (!canEditEncounter(encounter, null)) {
throw new APIException("Encounter.error.privilege.required.void", new Object[] { encounter.getEncounterType().getEditPrivilege() });
}
if (reason == null) {
throw new IllegalArgumentException("The argument 'reason' is required and so cannot be null");
}
ObsService os = Context.getObsService();
for (Obs o : encounter.getObsAtTopLevel(false)) {
if (!o.getVoided()) {
os.voidObs(o, reason);
}
}
OrderService orderService = Context.getOrderService();
for (Order o : encounter.getOrders()) {
if (!o.getVoided()) {
orderService.voidOrder(o, reason);
}
}
encounter.setVoided(true);
encounter.setVoidedBy(Context.getAuthenticatedUser());
// unvoid handler to work
if (encounter.getDateVoided() == null) {
encounter.setDateVoided(new Date());
}
encounter.setVoidReason(reason);
Context.getEncounterService().saveEncounter(encounter);
return encounter;
}
use of org.openmrs.api.ObsService in project openmrs-core by openmrs.
the class ORUR01HandlerTest method processMessage_shouldSetValueAsBooleanForObsIfTheAnswerIs0Or1AndQuestionDatatypeIsBoolean.
/**
* @see ORUR01Handler#processMessage(Message)
*/
@Test
public void processMessage_shouldSetValueAsBooleanForObsIfTheAnswerIs0Or1AndQuestionDatatypeIsBoolean() throws Exception {
GlobalProperty trueConceptGlobalProperty = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_TRUE_CONCEPT, "7", "Concept id of the concept defining the TRUE boolean concept");
GlobalProperty falseConceptGlobalProperty = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_FALSE_CONCEPT, "8", "Concept id of the concept defining the TRUE boolean concept");
Context.getAdministrationService().saveGlobalProperty(trueConceptGlobalProperty);
Context.getAdministrationService().saveGlobalProperty(falseConceptGlobalProperty);
ObsService os = Context.getObsService();
Assert.assertNull(os.getObs(17));
String hl7string = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20080226102656||ORU^R01|JqnfhKKtouEz8kzTk6Zo|P|2.5|1||||||||16^AMRS.ELD.FORMID\r" + "PID|||7^^^^||Collet^Test^Chebaskwony||\r" + "PV1||O|1^Unknown Location||||1^Super User (1-8)|||||||||||||||||||||||||||||||||||||20080212|||||||V\r" + "ORC|RE||||||||20080226102537|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT\r" + "OBX|1|NM|18^FOOD ASSISTANCE^99DCT||0|||||||||20080206";
// the expected question for the obs in the hl7 message has to be
// Boolean
Assert.assertEquals("Boolean", Context.getConceptService().getConcept(18).getDatatype().getName());
List<Obs> oldList = os.getObservationsByPersonAndConcept(new Person(7), new Concept(18));
Message hl7message = parser.parse(hl7string);
router.processMessage(hl7message);
List<Obs> newList = os.getObservationsByPersonAndConcept(new Person(7), new Concept(18));
Obs newObservation = null;
for (Obs newObs : newList) {
if (!oldList.contains(newObs) && !newObs.isObsGrouping()) {
newObservation = newObs;
}
}
Assert.assertEquals(false, newObservation.getValueBoolean());
}
use of org.openmrs.api.ObsService in project openmrs-core by openmrs.
the class ORUR01HandlerTest method processMessage_shouldCreateObsGroupForOBRs.
/**
* This method checks that obs grouping is happening correctly when processing an ORUR01
*
* @see ORUR01Handler#processMessage(Message)
*/
@Test
public void processMessage_shouldCreateObsGroupForOBRs() throws Exception {
ObsService obsService = Context.getObsService();
String hl7string = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20080226103553||ORU^R01|OD9PWqcD9g0NKn81rvSD|P|2.5|1||||||||66^AMRS.ELD.FORMID\r" + "PID|||3^^^^||John^Doe^||\r" + "PV1||O|1^Unknown Location||||1^Super User (1-8)|||||||||||||||||||||||||||||||||||||20080205|||||||V\r" + "ORC|RE||||||||20080226103428|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT\r" + "OBX|1|DT|1592^MISSED RETURNED VISIT DATE^99DCT||20080201|||||||||20080205\r" + "OBR|2|||1726^FOLLOW-UP ACTION^99DCT\r" + "OBX|1|CWE|1558^PATIENT CONTACT METHOD^99DCT|1|1555^PHONE^99DCT|||||||||20080205\r" + "OBX|2|NM|1553^NUMBER OF ATTEMPTS^99DCT|1|1|||||||||20080205\r" + "OBX|3|NM|1554^SUCCESSFUL^99DCT|1|1|||||||||20080205";
Message hl7message = parser.parse(hl7string);
router.processMessage(hl7message);
Patient patient = new Patient(3);
Context.clearSession();
// check for any obs
List<Obs> obsForPatient2 = obsService.getObservationsByPerson(patient);
assertNotNull(obsForPatient2);
assertTrue("There should be some obs created for #3", obsForPatient2.size() > 0);
// check for the missed return visit date obs
Concept returnVisitDateConcept = new Concept(1592);
Calendar cal = new GregorianCalendar();
cal.set(2008, Calendar.FEBRUARY, 1, 0, 0, 0);
Date returnVisitDate = cal.getTime();
List<Obs> returnVisitDateObsForPatient2 = obsService.getObservationsByPersonAndConcept(patient, returnVisitDateConcept);
assertEquals("There should be a return visit date", 1, returnVisitDateObsForPatient2.size());
Obs firstObs = (Obs) returnVisitDateObsForPatient2.toArray()[0];
cal.setTime(firstObs.getValueDatetime());
cal.clear(Calendar.HOUR);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
assertEquals("The date should be the 1st", returnVisitDate.toString(), cal.getTime().toString());
// check for the grouped obs
Concept contactMethod = new Concept(1558);
Concept phoneContact = Context.getConceptService().getConcept(1555);
List<Obs> contactMethodObsForPatient2 = obsService.getObservationsByPersonAndConcept(patient, contactMethod);
assertEquals("There should be a contact method", 1, contactMethodObsForPatient2.size());
Obs firstContactMethodObs = (Obs) contactMethodObsForPatient2.toArray()[0];
assertEquals("The contact method should be phone", phoneContact, firstContactMethodObs.getValueCoded());
// check that there is a group id
Obs obsGroup = firstContactMethodObs.getObsGroup();
assertNotNull("Their should be a grouping obs", obsGroup);
assertNotNull("Their should be an associated encounter", firstContactMethodObs.getEncounter());
// check that the obs that are grouped have the same group id
List<Integer> groupedConceptIds = new ArrayList<>();
groupedConceptIds.add(1558);
groupedConceptIds.add(1553);
groupedConceptIds.add(1554);
// total obs should be 5
assertEquals(5, obsForPatient2.size());
int groupedObsCount = 0;
for (Obs obs : obsForPatient2) {
if (groupedConceptIds.contains(obs.getConcept().getConceptId())) {
groupedObsCount += 1;
assertEquals("All of the parent groups should match", obsGroup, obs.getObsGroup());
}
}
// the number of obs that were grouped
assertEquals(3, groupedObsCount);
}
use of org.openmrs.api.ObsService in project openmrs-core by openmrs.
the class ORUR01HandlerTest method processMessage_shouldCreateObsValueCodedName.
/**
* @see ORUR01Handler#processMessage(Message)
*/
@Test
public void processMessage_shouldCreateObsValueCodedName() throws Exception {
ObsService obsService = Context.getObsService();
// the patient that is the focus of
Patient patient = new Patient(3);
// this hl7 message
// the question concept for
Concept concept = new Concept(21);
// "Food assistance for entire family?"
// sanity check to make sure this obs doesn't exist already
Assert.assertEquals(0, obsService.getObservationsByPersonAndConcept(patient, concept).size());
String hl7String = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20090728170332||ORU^R01|gu99yBh4loLX2mh9cHaV|P|2.5|1||||||||4^AMRS.ELD.FORMID\r" + "PID|||3^^^^||Beren^John^Bondo||\r" + "PV1||O|1^Unknown||||1^Super User (admin)|||||||||||||||||||||||||||||||||||||20090714|||||||V\r" + "ORC|RE||||||||20090728165937|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT\r" + "OBX|2|NM|5497^CD4 COUNT^99DCT||123|||||||||20090714\r" + "OBR|3|||23^FOOD CONSTRUCT^99DCT\r" + "OBX|1|CWE|21^FOOD ASSISTANCE FOR ENTIRE FAMILY^99DCT||22^UNKNOWN^99DCT^2471^UNKNOWN^99NAM|||||||||20090714";
Message hl7message = parser.parse(hl7String);
router.processMessage(hl7message);
List<Obs> obss = obsService.getObservationsByPersonAndConcept(patient, concept);
ConceptName name = obss.get(0).getValueCodedName();
Assert.assertNotNull(name);
Assert.assertEquals("The valueCodedName should be 2471", 2471, name.getId().intValue());
}
Aggregations