--- ORU_R01Handler-NK1-processing.patch 2010-05-03 10:50:54.741880055 -0400 +++ ORU_R01Handler-NK1-processing-v2.patch 2010-05-11 12:46:11.588871236 -0400 @@ -1,6 +1,6 @@ Index: src/api/org/openmrs/hl7/HL7Service.java =================================================================== ---- src/api/org/openmrs/hl7/HL7Service.java (revision 13207) +--- src/api/org/openmrs/hl7/HL7Service.java (revision 13288) +++ src/api/org/openmrs/hl7/HL7Service.java (working copy) @@ -17,6 +17,7 @@ import java.util.List; @@ -52,7 +52,7 @@ * Clean up the current memory consumption */ public void garbageCollect(); -@@ -422,4 +440,31 @@ +@@ -422,4 +440,32 @@ */ public Message processHL7Message(Message hl7Message) throws HL7Exception; @@ -70,23 +70,24 @@ + public String getUuidFromIdentifiers(CX[] identifiers) throws HL7Exception; + + /** -+ * creates a Person from information held in an NK1 segment ++ * creates a Person from information held in an NK1 segment; if valid PatientIdentifiers ++ * exist, a Patient will be created and returned + * + * @param nk1 the NK1 segment with person information + * @return the newly formed (but not saved) person + * @throws HL7Exception -+ * @should return an unsaved new person ++ * @should return a saved new person + * @should return a Patient if valid patient identifiers exist + * @should fail if a person with the same UUID exists + * @should fail on an invalid gender + * @should fail if no gender specified + * @should fail if no birthdate specified + */ -+ public Person getPersonFromNK1(NK1 nk1) throws HL7Exception; ++ public Person createPersonFromNK1(NK1 nk1) throws HL7Exception; } Index: src/api/org/openmrs/hl7/handler/ORUR01Handler.java =================================================================== ---- src/api/org/openmrs/hl7/handler/ORUR01Handler.java (revision 13207) +--- src/api/org/openmrs/hl7/handler/ORUR01Handler.java (revision 13288) +++ src/api/org/openmrs/hl7/handler/ORUR01Handler.java (working copy) @@ -16,7 +16,10 @@ import java.util.ArrayList; @@ -132,24 +133,18 @@ PV1 pv1 = getPV1(oru); ORC orc = getORC(oru); // we're using the ORC assoc with first OBR to // hold data enterer and date entered for now -@@ -180,6 +188,16 @@ +@@ -180,6 +188,10 @@ log.error("Error while processing Discharge To Location (" + messageControlId + ")", e); } + // process NK1 (relationship) segments -+ for (NK1 nk1 : nk1List) { -+ try { -+ processNK1(patient, nk1); -+ } -+ catch (Exception e) { -+ log.error("Error while processing NK1 segment (" + messageControlId + ")", e); -+ } -+ } ++ for (NK1 nk1 : nk1List) ++ processNK1(patient, nk1); + // list of concepts proposed in the obs of this encounter. // these proposals need to be created after the encounter // has been created -@@ -334,9 +352,92 @@ +@@ -334,9 +346,87 @@ } @@ -187,7 +182,7 @@ + try { + relTypeId = Integer.parseInt(relIdentifier.substring(0, relIdentifier.length() - 1)); + } -+ catch (Exception e) { ++ catch (NumberFormatException e) { + throw new HL7Exception("Relationship type '" + relIdentifier + "' improperly formed in NK1 segment."); + } + @@ -201,8 +196,8 @@ + + // determine if the patient is person A or B; the relIdentifier indicates + // the relative's side of the relationship, so the patient is the inverse -+ Boolean patientIsPersonA = relIdentifier.endsWith("B"); -+ Boolean patientCanBeEitherPerson = relType.getbIsToA().equals(relType.getaIsToB()); ++ boolean patientIsPersonA = relIdentifier.endsWith("B"); ++ boolean patientCanBeEitherPerson = relType.getbIsToA().equals(relType.getaIsToB()); + + // look at existing relationships to determine if a new one is needed + Set rels = new HashSet(); @@ -219,14 +214,9 @@ + // check the relative's existence + if (relative == null) { + // create one based on NK1 information -+ relative = Context.getHL7Service().getPersonFromNK1(nk1); ++ relative = Context.getHL7Service().createPersonFromNK1(nk1); + if (relative == null) + throw new HL7Exception("could not create a new relative from NK1 segment"); -+ // save the new person or patient -+ if (relative.getClass().equals(Patient.class)) -+ Context.getPatientService().savePatient((Patient) relative); -+ else -+ Context.getPersonService().savePerson(relative); + } + + // create the relationship @@ -245,12 +235,13 @@ /** * Not used -@@ -356,6 +457,21 @@ +@@ -356,6 +446,24 @@ return oru.getPATIENT_RESULT().getPATIENT().getPID(); } + /** -+ * finds NK1 segments in a URU_R01 message ++ * finds NK1 segments in an ORU_R01 message. all messages have at least one NK1 segment but if ++ * the message truly does not contain an NK1, the setID will be null on the generated NK1 + * + * @param oru ORU_R01 message to be parsed for NK1 segments + * @return list of not-null NK1 segments @@ -258,7 +249,9 @@ + */ + public List getNK1List(ORU_R01 oru) throws HL7Exception { + List res = new ArrayList(); ++ // there will always be at least one NK1, even if the message does not contain one + for (int i = 0; i < oru.getPATIENT_RESULT().getPATIENT().getNK1Reps(); i++) ++ // if the setIDNK1 value is null, this NK1 is blank + if (oru.getPATIENT_RESULT().getPATIENT().getNK1(i).getSetIDNK1().getValue() != null) + res.add(oru.getPATIENT_RESULT().getPATIENT().getNK1(i)); + return res; @@ -267,7 +260,7 @@ private PV1 getPV1(ORU_R01 oru) { return oru.getPATIENT_RESULT().getPATIENT().getVISIT().getPV1(); } -@@ -790,6 +906,21 @@ +@@ -790,6 +898,21 @@ return patient; } @@ -291,7 +284,7 @@ Integer locationId = Context.getHL7Service().resolveLocationId(hl7Location); Index: src/api/org/openmrs/hl7/impl/HL7ServiceImpl.java =================================================================== ---- src/api/org/openmrs/hl7/impl/HL7ServiceImpl.java (revision 13207) +--- src/api/org/openmrs/hl7/impl/HL7ServiceImpl.java (revision 13288) +++ src/api/org/openmrs/hl7/impl/HL7ServiceImpl.java (working copy) @@ -15,6 +15,7 @@ @@ -316,19 +309,20 @@ import org.openmrs.api.context.Context; import org.openmrs.api.impl.BaseOpenmrsService; import org.openmrs.hl7.HL7Constants; -@@ -38,9 +42,11 @@ +@@ -38,9 +42,12 @@ import org.openmrs.hl7.HL7InQueue; import org.openmrs.hl7.HL7Service; import org.openmrs.hl7.HL7Source; +import org.openmrs.hl7.HL7Util; import org.openmrs.hl7.db.HL7DAO; ++import org.openmrs.util.FormConstants; import org.openmrs.util.OpenmrsConstants; import org.openmrs.util.OpenmrsUtil; +import org.openmrs.validator.PatientIdentifierValidator; import ca.uhn.hl7v2.HL7Exception; import ca.uhn.hl7v2.app.Application; -@@ -48,8 +54,12 @@ +@@ -48,8 +55,12 @@ import ca.uhn.hl7v2.app.MessageTypeRouter; import ca.uhn.hl7v2.model.Message; import ca.uhn.hl7v2.model.v25.datatype.CX; @@ -341,7 +335,7 @@ import ca.uhn.hl7v2.model.v25.segment.PID; import ca.uhn.hl7v2.parser.EncodingNotSupportedException; import ca.uhn.hl7v2.parser.GenericParser; -@@ -64,7 +74,7 @@ +@@ -64,7 +75,7 @@ private Log log = LogFactory.getLog(this.getClass()); @@ -350,7 +344,7 @@ protected HL7DAO dao; -@@ -74,6 +84,7 @@ +@@ -74,6 +85,7 @@ /** * Private constructor to only support on singleton instance. @@ -358,7 +352,7 @@ * @see #getInstance() */ private HL7ServiceImpl() { -@@ -90,7 +101,7 @@ +@@ -90,7 +102,7 @@ } return instance; } @@ -367,7 +361,7 @@ /** * @see org.openmrs.hl7.HL7Service#setHL7DAO(org.openmrs.hl7.db.HL7DAO) */ -@@ -456,7 +467,7 @@ +@@ -456,7 +468,7 @@ } } } @@ -376,7 +370,7 @@ /** * @see org.openmrs.hl7.HL7Service#resolvePersonId(ca.uhn.hl7v2.model.v25.datatype.XCN) */ -@@ -487,7 +498,7 @@ +@@ -487,7 +499,7 @@ } } } @@ -385,7 +379,7 @@ /** * @param pl HL7 component of data type PL (person location) (see Ch 2.A.53) * @return internal identifier of the specified location, or null if it is not found or -@@ -535,17 +546,27 @@ +@@ -535,17 +547,25 @@ * @throws HL7Exception */ public Integer resolvePatientId(PID pid) throws HL7Exception { @@ -410,8 +404,7 @@ - // patient_id numbers - Integer patientId = null; -+ Integer personId = null; - +- - CX[] patientIdentifierList = pid.getPatientIdentifierList(); - if (patientIdentifierList.length < 1) + // give up if no identifiers exist @@ -419,45 +412,53 @@ throw new HL7Exception("Missing patient identifier in PID segment"); // TODO other potential identifying characteristics in PID we could use -@@ -555,8 +576,8 @@ +@@ -555,10 +575,9 @@ // TS dateOfBirth = pid.getDateTimeOfBirth(); // Take the first uniquely matching identifier - for (CX identifier : patientIdentifierList) { - String hl7PatientId = identifier.getIDNumber().getValue(); +- // TODO if 1st component is blank, check 2nd and 3rd of assigning +- // authority + for (CX identifier : identifiers) { + String hl7PersonId = identifier.getIDNumber().getValue(); - // TODO if 1st component is blank, check 2nd and 3rd of assigning - // authority ++ // TODO if 1st component is blank, check 2nd and 3rd of assigning authority String assigningAuthority = identifier.getAssigningAuthority().getNamespaceID().getValue(); -@@ -567,41 +588,64 @@ + + if (assigningAuthority != null && assigningAuthority.length() > 0) { +@@ -567,41 +586,68 @@ PatientIdentifierType pit = Context.getPatientService().getPatientIdentifierTypeByName( assigningAuthority); if (pit == null) { + // there is no matching PatientIdentifierType -+ if ("UUID".equals(assigningAuthority)) { ++ if (assigningAuthority.equals(FormConstants.HL7_AUTHORITY_UUID)) { + // the identifier is a UUID + Person p = Context.getPersonService().getPersonByUuid(hl7PersonId); + if (p != null) + return p; + log.warn("Can't find person for UUID '" + hl7PersonId + "'"); + continue; // skip identifiers with unknown type -+ } else if ("L".equals(assigningAuthority)) { -+ // L designates LOCAL, meaning the ID is internal ++ } else if (assigningAuthority.equals(FormConstants.HL7_AUTHORITY_LOCAL)) { ++ // the ID is internal (local) + String idType = identifier.getIdentifierTypeCode().getValue(); -+ // PN = person number; PI = patient id -+ if (idType.equals("PN") || idType.equals("PI")) { -+ try { ++ try { ++ if (idType.equals(FormConstants.HL7_ID_PERSON)) { + Integer pid = Integer.parseInt(hl7PersonId); + // patient_id == person_id, so just look for the person + Person p = Context.getPersonService().getPerson(pid); + if (p != null) + return p; ++ } else if (idType.equals(FormConstants.HL7_ID_PATIENT)) { ++ Integer pid = Integer.parseInt(hl7PersonId); ++ // patient_id == person_id, so just look for the person ++ Patient p = Context.getPatientService().getPatient(pid); ++ if (p != null) ++ return p; + } -+ catch (NumberFormatException e) {} -+ log.warn("Can't find Local identifier of '" + hl7PersonId + "'"); -+ continue; // skip identifiers with unknown type + } ++ catch (NumberFormatException e) {} ++ log.warn("Can't find Local identifier of '" + hl7PersonId + "'"); ++ continue; // skip identifiers with unknown type + } log.warn("Can't find PatientIdentifierType named '" + assigningAuthority + "'"); continue; // skip identifiers with unknown type @@ -491,23 +492,21 @@ } else { try { - log.debug("PID contains patient ID '" + hl7PatientId -- + "' without assigning authority -- assuming patient.patient_id"); ++ log.debug("CX contains ID '" + hl7PersonId + + "' without assigning authority -- assuming patient.patient_id"); - patientId = Integer.parseInt(hl7PatientId); - return patientId; -+ log.debug("CX contains ID '" + hl7PersonId -+ + "' without assigning authority -- assuming person.person_id"); -+ personId = Integer.parseInt(hl7PersonId); -+ return Context.getPersonService().getPerson(personId); ++ return Context.getPatientService().getPatient(Integer.parseInt(hl7PersonId)); } catch (NumberFormatException e) { - // throw new HL7Exception("Invalid patient ID '" + - // hl7PatientId + "'"); - log.warn("Invalid patient ID '" + hl7PatientId + "'"); -+ log.warn("Invalid person ID '" + hl7PersonId + "'"); ++ log.warn("Invalid patient ID '" + hl7PersonId + "'"); } } } -@@ -701,11 +745,11 @@ +@@ -701,11 +747,11 @@ hl7InError.setErrorDetails(""); else { StringWriter sw = new StringWriter(); @@ -524,14 +523,14 @@ } Context.getHL7Service().saveHL7InError(hl7InError); Context.getHL7Service().purgeHL7InQueue(hl7InQueue); -@@ -779,4 +823,133 @@ +@@ -779,4 +825,139 @@ } } + /** -+ * @see org.openmrs.hl7.HL7Service#getPersonFromNK1(ca.uhn.hl7v2.model.v25.segment.NK1) ++ * @see org.openmrs.hl7.HL7Service#createPersonFromNK1(ca.uhn.hl7v2.model.v25.segment.NK1) + */ -+ public Person getPersonFromNK1(NK1 nk1) throws HL7Exception { ++ public Person createPersonFromNK1(NK1 nk1) throws HL7Exception { + // NOTE: following block (with minor modifications) stolen from ADTA28Handler + // TODO: generalize this for use with both PID and NK1 segments + @@ -634,6 +633,12 @@ + person.setBirthdateEstimated(true); + } + ++ // save the new person or patient ++ if (person instanceof Patient) ++ Context.getPatientService().savePatient((Patient) person); ++ else ++ Context.getPersonService().savePerson(person); ++ + return person; + } + @@ -660,14 +665,24 @@ } Index: src/api/org/openmrs/util/FormConstants.java =================================================================== ---- src/api/org/openmrs/util/FormConstants.java (revision 13207) +--- src/api/org/openmrs/util/FormConstants.java (revision 13288) +++ src/api/org/openmrs/util/FormConstants.java (working copy) -@@ -54,15 +54,17 @@ +@@ -52,16 +52,26 @@ + + public static final String HL7_BOOLEAN = "BIT"; - public static final Integer CLASS_DRUG = 3; +- public static final Integer CLASS_DRUG = 3; ++ public static final String HL7_AUTHORITY_UUID = "UUID"; - public static final String HL7_LOCAL_CONCEPT = "99DCT"; -- ++ public static final String HL7_AUTHORITY_LOCAL = "L"; + ++ public static final Object HL7_ID_PERSON = "PN"; ++ ++ public static final Object HL7_ID_PATIENT = "PI"; ++ ++ public static final Integer CLASS_DRUG = 3; ++ /** * Used in hl7 sextuplets: 123^Primary name^99DCT^345^Chosen name^99NAM */ @@ -676,15 +691,14 @@ public static final String HL7_LOCAL_CONCEPT_NAME = "99NAM"; public static final String HL7_LOCAL_DRUG = "99RX"; - ++ + public static final String HL7_LOCAL_RELATIONSHIP = "99REL"; -+ + // List of datatypes that do not require complex definitions public static final Hashtable simpleDatatypes = new Hashtable(); - static { Index: test/api/org/openmrs/hl7/HL7ServiceTest.java =================================================================== ---- test/api/org/openmrs/hl7/HL7ServiceTest.java (revision 13207) +--- test/api/org/openmrs/hl7/HL7ServiceTest.java (revision 13288) +++ test/api/org/openmrs/hl7/HL7ServiceTest.java (working copy) @@ -27,7 +27,9 @@ import org.openmrs.Concept; @@ -859,7 +873,7 @@ + } + + /** -+ * @see {@link HL7Service#getPersonFromNK1(NK1)} ++ * @see {@link HL7Service#createPersonFromNK1(NK1)} + */ + @Test(expected = HL7Exception.class) + @Verifies(value = "should fail if a person with the same UUID exists", method = "getPersonFromNK1(NK1)") @@ -877,12 +891,12 @@ + + "OBX|2|DT|5096^RETURN VISIT DATE^99DCT||20080229|||||||||20080212"); + ORU_R01 oru = (ORU_R01) message; + List nk1List = new ORUR01Handler().getNK1List(oru); -+ hl7service.getPersonFromNK1(nk1List.get(0)); ++ hl7service.createPersonFromNK1(nk1List.get(0)); + Assert.fail("should have thrown an exception"); + } + + /** -+ * @see {@link HL7Service#getPersonFromNK1(NK1)} ++ * @see {@link HL7Service#createPersonFromNK1(NK1)} + */ + @Test(expected = HL7Exception.class) + @Verifies(value = "should fail if no birthdate specified", method = "getPersonFromNK1(NK1)") @@ -899,12 +913,12 @@ + + "OBX|2|DT|5096^RETURN VISIT DATE^99DCT||20080229|||||||||20080212"); + ORU_R01 oru = (ORU_R01) message; + List nk1List = new ORUR01Handler().getNK1List(oru); -+ hl7service.getPersonFromNK1(nk1List.get(0)); ++ hl7service.createPersonFromNK1(nk1List.get(0)); + Assert.fail("should have thrown an exception"); + } + + /** -+ * @see {@link HL7Service#getPersonFromNK1(NK1)} ++ * @see {@link HL7Service#createPersonFromNK1(NK1)} + */ + @Test(expected = HL7Exception.class) + @Verifies(value = "should fail if no gender specified", method = "getPersonFromNK1(NK1)") @@ -921,12 +935,12 @@ + + "OBX|2|DT|5096^RETURN VISIT DATE^99DCT||20080229|||||||||20080212"); + ORU_R01 oru = (ORU_R01) message; + List nk1List = new ORUR01Handler().getNK1List(oru); -+ hl7service.getPersonFromNK1(nk1List.get(0)); ++ hl7service.createPersonFromNK1(nk1List.get(0)); + Assert.fail("should have thrown an exception"); + } + + /** -+ * @see {@link HL7Service#getPersonFromNK1(NK1)} ++ * @see {@link HL7Service#createPersonFromNK1(NK1)} + */ + @Test(expected = HL7Exception.class) + @Verifies(value = "should fail on an invalid gender", method = "getPersonFromNK1(NK1)") @@ -943,16 +957,16 @@ + + "OBX|2|DT|5096^RETURN VISIT DATE^99DCT||20080229|||||||||20080212"); + ORU_R01 oru = (ORU_R01) message; + List nk1List = new ORUR01Handler().getNK1List(oru); -+ hl7service.getPersonFromNK1(nk1List.get(0)); ++ hl7service.createPersonFromNK1(nk1List.get(0)); + Assert.fail("should have thrown an exception"); + } + + /** -+ * @see {@link HL7Service#getPersonFromNK1(NK1)} ++ * @see {@link HL7Service#createPersonFromNK1(NK1)} + */ + @Test -+ @Verifies(value = "should return an unsaved new person", method = "getPersonFromNK1(NK1)") -+ public void getPersonFromNK1_shouldReturnAnUnsavedNewPerson() throws Exception { ++ @Verifies(value = "should return a saved new person", method = "getPersonFromNK1(NK1)") ++ public void getPersonFromNK1_shouldReturnASavedNewPerson() throws Exception { + HL7Service hl7service = Context.getHL7Service(); + Message message = hl7service + .parseHL7String("MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20080226102656||ORU^R01|JqnfhKKtouEz8kzTk6Zo|P|2.5|1||||||||16^AMRS.ELD.FORMID\r" @@ -965,13 +979,13 @@ + + "OBX|2|DT|5096^RETURN VISIT DATE^99DCT||20080229|||||||||20080212"); + ORU_R01 oru = (ORU_R01) message; + List nk1List = new ORUR01Handler().getNK1List(oru); -+ Person result = hl7service.getPersonFromNK1(nk1List.get(0)); ++ Person result = hl7service.createPersonFromNK1(nk1List.get(0)); + Assert.assertNotNull("should have returned a person", result); -+ Assert.assertNull("the person should not exist yet", Context.getPersonService().getPersonByUuid(result.getUuid())); ++ Assert.assertNotNull("the person should exist", Context.getPersonService().getPersonByUuid(result.getUuid())); + } + + /** -+ * @see {@link HL7Service#getPersonFromNK1(NK1)} ++ * @see {@link HL7Service#createPersonFromNK1(NK1)} + */ + @Test + @Verifies(value = "should return a Patient if valid patient identifiers exist", method = "getPersonFromNK1(NK1)") @@ -989,9 +1003,9 @@ + + "OBX|2|DT|5096^RETURN VISIT DATE^99DCT||20080229|||||||||20080212"); + ORU_R01 oru = (ORU_R01) message; + List nk1List = new ORUR01Handler().getNK1List(oru); -+ Person result = hl7service.getPersonFromNK1(nk1List.get(0)); ++ Person result = hl7service.createPersonFromNK1(nk1List.get(0)); + Assert.assertNotNull("should have returned something", result); -+ Assert.assertEquals("should have returned a Patient", Patient.class, result.getClass()); ++ Assert.assertTrue("should have returned a Patient", result instanceof Patient); + } + + /** @@ -1124,14 +1138,15 @@ } Index: test/api/org/openmrs/hl7/handler/ORUR01HandlerTest.java =================================================================== ---- test/api/org/openmrs/hl7/handler/ORUR01HandlerTest.java (revision 13207) +--- test/api/org/openmrs/hl7/handler/ORUR01HandlerTest.java (revision 13288) +++ test/api/org/openmrs/hl7/handler/ORUR01HandlerTest.java (working copy) -@@ -32,15 +32,21 @@ +@@ -32,15 +32,22 @@ import org.openmrs.Encounter; import org.openmrs.Obs; import org.openmrs.Patient; +import org.openmrs.Person; +import org.openmrs.Relationship; ++import org.openmrs.RelationshipType; import org.openmrs.api.ConceptService; import org.openmrs.api.EncounterService; import org.openmrs.api.ObsService; @@ -1148,7 +1163,7 @@ import ca.uhn.hl7v2.parser.GenericParser; /** -@@ -78,12 +84,23 @@ +@@ -78,12 +85,23 @@ public void processMessage_shouldCreateEncounterAndObsFromHl7Message() throws Exception { ObsService obsService = Context.getObsService(); @@ -1173,7 +1188,7 @@ // check for any obs List obsForPatient3 = obsService.getObservationsByPerson(patient); assertNotNull(obsForPatient3); -@@ -115,7 +132,16 @@ +@@ -115,7 +133,16 @@ public void processMessage_shouldCreateObsGroupForOBRs() throws Exception { ObsService obsService = Context.getObsService(); @@ -1191,7 +1206,7 @@ Message hl7message = parser.parse(hl7string); router.processMessage(hl7message); -@@ -194,7 +220,12 @@ +@@ -194,7 +221,12 @@ // to append to assertNotNull(Context.getEncounterService().getEncounter(3)); @@ -1205,7 +1220,7 @@ Message hl7message = parser.parse(hl7string); router.processMessage(hl7message); -@@ -222,7 +253,14 @@ +@@ -222,7 +254,14 @@ List proposals = Context.getConceptService().getAllConceptProposals(false); Assert.assertEquals(0, proposals.size()); @@ -1221,7 +1236,7 @@ Message hl7message = parser.parse(hl7string); router.processMessage(hl7message); -@@ -244,7 +282,16 @@ +@@ -244,7 +283,16 @@ List proposals = Context.getConceptService().getAllConceptProposals(false); Assert.assertEquals(0, proposals.size()); @@ -1239,7 +1254,7 @@ Message hl7message = parser.parse(hl7string); router.processMessage(hl7message); -@@ -266,7 +313,13 @@ +@@ -266,7 +314,13 @@ ConceptService conceptService = Context.getConceptService(); EncounterService encService = Context.getEncounterService(); @@ -1254,7 +1269,7 @@ Message hl7message = parser.parse(hl7String); router.processMessage(hl7message); -@@ -297,7 +350,14 @@ +@@ -297,7 +351,14 @@ // sanity check to make sure this obs doesn't exist already Assert.assertEquals(0, obsService.getObservationsByPersonAndConcept(patient, concept).size()); @@ -1270,7 +1285,7 @@ Message hl7message = parser.parse(hl7String); router.processMessage(hl7message); -@@ -341,16 +401,308 @@ +@@ -341,16 +402,256 @@ */ @Test @Verifies(value = "should send message to error queue for empty concept proposals", method = "processMessage(Message)") @@ -1304,12 +1319,6 @@ + Patient relative = new Patient(2); // the patient that is related to patientA - } -+ // remove all existing relationships for both sides -+ for (Relationship rel : personService.getRelationshipsByPerson(patient)) -+ personService.purgeRelationship(rel); -+ for (Relationship rel : personService.getRelationshipsByPerson(relative)) -+ personService.purgeRelationship(rel); -+ + // process a message with a single NK1 segment + // defines relative as patient's Parent + String hl7String = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20090728170332||ORU^R01|gu99yBh4loLX2mh9cHaV|P|2.5|1||||||||4^AMRS.ELD.FORMID\r" @@ -1329,17 +1338,9 @@ + for (NK1 nk1 : nk1List) + oruHandler.processNK1(patient, nk1); + -+ // verify relationships were created -+ Assert.assertEquals(1, personService.getRelationshipsByPerson(patient).size()); -+ Assert.assertEquals(1, personService.getRelationshipsByPerson(relative).size()); -+ -+ // verify the type of relationship created -+ Relationship rel = personService.getRelationshipsByPerson(patient).get(0); -+ Assert.assertEquals(3, rel.getRelationshipType().getRelationshipTypeId().intValue()); -+ -+ // verify the sides of the relationship -+ // 3A^Parent^99REL ==> relative = A, patient = B -+ Assert.assertEquals(relative, rel.getPersonA()); ++ // verify relationship was created ++ List rels = personService.getRelationships(relative, patient, new RelationshipType(3)); ++ Assert.assertTrue("new relationship was not created", !rels.isEmpty() && rels.size() == 1); + } + /** @@ -1427,22 +1428,15 @@ + Patient patient = new Patient(3); // the patient that is the focus of this hl7 message + Patient relative = new Patient(2); // the patient that is related to patientA + -+ // remove all existing relationships for both sides -+ for (Relationship rel : personService.getRelationshipsByPerson(patient)) -+ personService.purgeRelationship(rel); -+ for (Relationship rel : personService.getRelationshipsByPerson(relative)) -+ personService.purgeRelationship(rel); -+ + // create a relationship in the database + Relationship rel = new Relationship(); -+ rel.setRelationshipType(personService.getRelationshipType(3)); ++ rel.setRelationshipType(new RelationshipType(3)); + rel.setPersonA(relative); + rel.setPersonB(patient); + personService.saveRelationship(rel); + + // verify relationship exists -+ Assert.assertEquals(1, personService.getRelationshipsByPerson(patient).size()); -+ Assert.assertEquals(1, personService.getRelationshipsByPerson(relative).size()); ++ Assert.assertEquals(1, personService.getRelationships(relative, patient, new RelationshipType(3)).size()); + + // process a message with a single NK1 segment + // defines relative as patient's Parent @@ -1463,17 +1457,9 @@ + for (NK1 nk1 : nk1List) + oruHandler.processNK1(patient, nk1); + -+ // verify no new relationships showed up -+ Assert.assertEquals(1, personService.getRelationshipsByPerson(patient).size()); -+ Assert.assertEquals(1, personService.getRelationshipsByPerson(relative).size()); -+ -+ // verify the type of relationship created -+ rel = personService.getRelationshipsByPerson(patient).get(0); -+ Assert.assertEquals(3, rel.getRelationshipType().getRelationshipTypeId().intValue()); -+ -+ // verify the sides of the relationship -+ // 3A^Parent^99REL ==> relative = A, patient = B -+ Assert.assertEquals(relative, rel.getPersonA()); ++ // verify existing relationship ++ List rels = personService.getRelationships(relative, patient, new RelationshipType(3)); ++ Assert.assertTrue("existing relationship was not retained", !rels.isEmpty() && rels.size() == 1); + } + + /** @@ -1486,22 +1472,15 @@ + Patient patient = new Patient(3); // the patient that is the focus of this hl7 message + Patient relative = new Patient(2); // the patient that is related to patientA + -+ // remove all existing relationships for both sides -+ for (Relationship rel : personService.getRelationshipsByPerson(patient)) -+ personService.purgeRelationship(rel); -+ for (Relationship rel : personService.getRelationshipsByPerson(relative)) -+ personService.purgeRelationship(rel); -+ + // create a relationship in the database + Relationship newRel = new Relationship(); -+ newRel.setRelationshipType(personService.getRelationshipType(3)); ++ newRel.setRelationshipType(new RelationshipType(3)); + newRel.setPersonA(relative); + newRel.setPersonB(patient); + personService.saveRelationship(newRel); + + // verify relationship exists -+ Assert.assertEquals(1, personService.getRelationshipsByPerson(patient).size()); -+ Assert.assertEquals(1, personService.getRelationshipsByPerson(relative).size()); ++ Assert.assertEquals(1, personService.getRelationships(relative, patient, new RelationshipType(3)).size()); + + // process a new message with multiple NK1 segments + // this one defines patientB as patientA's Sibling and Patient @@ -1519,29 +1498,17 @@ + Message hl7message = parser.parse(hl7String); + router.processMessage(hl7message); + -+ // verify two additional relationships were created -+ Assert.assertEquals(3, personService.getRelationshipsByPerson(patient).size()); -+ Assert.assertEquals(3, personService.getRelationshipsByPerson(relative).size()); -+ // verify relationships -+ for (Relationship rel : personService.getRelationshipsByPerson(patient)) -+ switch (rel.getRelationshipType().getId()) { -+ case 1: -+ // verify that the Doctor->Patient (A->B) relationship is retained (patientA is the doctor) -+ Assert.assertEquals(relative, rel.getPersonB()); -+ break; -+ case 2: -+ // verify that the Sibling->Sibling (A->B) relationship is retained (patientA should be personA) -+ Assert.assertEquals(relative, rel.getPersonB()); -+ break; -+ case 3: -+ // verify that the Parent->Child (B->A) relationship is retained (patientA is the child) -+ Assert.assertEquals(relative, rel.getPersonA()); -+ break; -+ default: -+ Assert.fail("relationship type ID: " + rel.getRelationshipType().getId() -+ + " was found; it was not provided in the NK1 segments"); -+ break; -+ } ++ // verify existing relationship ++ List rels = personService.getRelationships(relative, patient, new RelationshipType(3)); ++ Assert.assertTrue("existing relationship was not retained", !rels.isEmpty() && rels.size() == 1); ++ ++ // verify first new relationship ++ rels = personService.getRelationships(patient, relative, new RelationshipType(2)); ++ Assert.assertTrue("first new relationship was not created", !rels.isEmpty() && rels.size() == 1); ++ ++ // verify second new relationship ++ rels = personService.getRelationships(patient, relative, new RelationshipType(1)); ++ Assert.assertTrue("second new relationship was not created", !rels.isEmpty() && rels.size() == 1); + } + + /** @@ -1554,10 +1521,6 @@ + PersonService personService = Context.getPersonService(); + Patient patient = new Patient(3); // the patient that is the focus of this hl7 message + -+ // clear out existing relationships -+ for (Relationship rel : personService.getRelationshipsByPerson(patient)) -+ personService.purgeRelationship(rel); -+ + 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" + + "NK1|1|Jones^Jane^Lee^^RN|3A^Parent^99REL||||||||||||F|19751016|||||||||||||||||2178037d-f86b-4f12-8d8b-be3ebc220029^^^UUID^v4\r" @@ -1579,13 +1542,13 @@ + Assert.assertNotNull("a new person was not created", relative); + + // see if the relative made it into the relationship properly -+ List rels = personService.getRelationshipsByPerson(patient); -+ Assert.assertEquals("relative was not saved in the relationship", relative, rels.get(0).getPersonA()); ++ List rels = personService.getRelationships(relative, patient, new RelationshipType(3)); ++ Assert.assertTrue("new relationship was not created", !rels.isEmpty() && rels.size() == 1); + } } Index: test/api/org/openmrs/hl7/include/ORUTest-initialData.xml =================================================================== ---- test/api/org/openmrs/hl7/include/ORUTest-initialData.xml (revision 13207) +--- test/api/org/openmrs/hl7/include/ORUTest-initialData.xml (revision 13288) +++ test/api/org/openmrs/hl7/include/ORUTest-initialData.xml (working copy) @@ -81,6 +81,8 @@