use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class RelationshipValidatorTest method validate_shouldPassIfEndDateIsAfterStartDate.
/**
* @throws ParseException
* @see RelationshipValidator#validate(Object,Errors)
*/
@Test
public void validate_shouldPassIfEndDateIsAfterStartDate() throws ParseException {
Relationship relationship = new Relationship(1);
relationship.setStartDate(Context.getDateFormat().parse("18/02/2012"));
relationship.setEndDate(Context.getDateFormat().parse("18/03/2012"));
Map<String, String> map = new HashMap<>();
MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName());
new RelationshipValidator().validate(relationship, errors);
Assert.assertFalse(errors.hasErrors());
}
use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class RelationshipValidatorTest method validate_shouldPassValidationIfFieldLengthsAreCorrect.
/**
* @see RelationshipValidator#validate(Object,Errors)
*/
@Test
public void validate_shouldPassValidationIfFieldLengthsAreCorrect() {
Relationship relationship = new Relationship(1);
relationship.setVoidReason("voidReason");
Map<String, String> map = new HashMap<>();
MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName());
new RelationshipValidator().validate(relationship, errors);
Assert.assertFalse(errors.hasErrors());
}
use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class MigrationHelper method importRelationships.
/**
* Takes a list of Strings of the format RELATIONSHIP:<user last name>,<user first
* name>,<relationship type name>,<patient identifier type name>,<identifier> so if user hfraser
* if the cardiologist of the patient with patient_id 8039 in PIH's old emr, then:
* RELATIONSHIP:hfraser,Cardiologist,HIV-EMRV1,8039 (the "RELATIONSHIP:" is not actually
* necessary. Anything before and including the first : will be dropped If autoCreateUsers is
* true, and no user exists with the given username, one will be created. If autoAddRole is
* true, then whenever a user is auto-created, if a role exists with the same name as
* relationshipType.name, then the user will be added to that role
*/
public static int importRelationships(Collection<String> relationships, boolean autoCreateUsers, boolean autoAddRole) {
PatientService ps = Context.getPatientService();
UserService us = Context.getUserService();
PersonService personService = Context.getPersonService();
List<Relationship> relsToAdd = new ArrayList<>();
Random rand = new Random();
for (String s : relationships) {
if (s.contains(":")) {
s = s.substring(s.indexOf(":") + 1);
}
String[] ss = s.split(",");
if (ss.length < 5) {
throw new IllegalArgumentException("The line '" + s + "' is in the wrong format");
}
String userLastName = ss[0];
String userFirstName = ss[1];
String username = (userFirstName + userLastName).replaceAll(" ", "");
String relationshipType = ss[2];
String identifierType = ss[3];
String identifier = ss[4];
User user = null;
{
// first try looking for non-voided users
List<User> users = us.getUsersByName(userFirstName, userLastName, false);
if (users.size() == 1) {
user = users.get(0);
} else if (users.size() > 1) {
throw new IllegalArgumentException("Found " + users.size() + " users named '" + userLastName + ", " + userFirstName + "'");
}
}
if (user == null) {
// next try looking for voided users
List<User> users = us.getUsersByName(userFirstName, userLastName, false);
if (users.size() == 1) {
user = users.get(0);
} else if (users.size() > 1) {
throw new IllegalArgumentException("Found " + users.size() + " voided users named '" + userLastName + ", " + userFirstName + "'");
}
}
if (user == null && autoCreateUsers) {
user = new User();
user.setPerson(new Person());
PersonName pn = new PersonName(userFirstName, "", userLastName);
user.addName(pn);
user.setUsername(username);
// Generate a temporary password: 8-12 random characters
String pass;
{
int length = rand.nextInt(4) + 8;
char[] password = new char[length];
for (int x = 0; x < length; x++) {
int randDecimalAsciiVal = rand.nextInt(93) + 33;
password[x] = (char) randDecimalAsciiVal;
}
pass = new String(password);
}
if (autoAddRole) {
Role role = us.getRole(relationshipType);
if (role != null) {
user.addRole(role);
}
}
us.createUser(user, pass);
}
if (user == null) {
throw new IllegalArgumentException("Can't find user '" + userLastName + ", " + userFirstName + "'");
}
Person person = personService.getPerson(user.getUserId());
RelationshipType relationship = personService.getRelationshipTypeByName(relationshipType);
PatientIdentifierType pit = ps.getPatientIdentifierTypeByName(identifierType);
List<PatientIdentifier> found = ps.getPatientIdentifiers(identifier, Collections.singletonList(pit), null, null, null);
if (found.size() != 1) {
throw new IllegalArgumentException("Found " + found.size() + " patients with identifier '" + identifier + "' of type " + identifierType);
}
Person relative = personService.getPerson(found.get(0).getPatient().getPatientId());
Relationship rel = new Relationship();
rel.setPersonA(person);
rel.setRelationshipType(relationship);
rel.setPersonB(relative);
relsToAdd.add(rel);
}
int addedSoFar = 0;
for (Relationship rel : relsToAdd) {
personService.saveRelationship(rel);
++addedSoFar;
}
return addedSoFar;
}
use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class ORUR01HandlerTest method processORU_R01_shouldProcessMultipleNK1Segments.
/**
* @see ORUR01Handler#processORU_R01(ORU_R01)
*/
@Test
public void processORU_R01_shouldProcessMultipleNK1Segments() throws Exception {
PersonService personService = Context.getPersonService();
// the patient that is the focus of
Patient patient = new Patient(3);
// this hl7 message
// the patient that is related to
Patient relative = new Patient(2);
// patientA
// create a relationship in the database
Relationship newRel = new Relationship();
newRel.setRelationshipType(new RelationshipType(3));
newRel.setPersonA(relative);
newRel.setPersonB(patient);
personService.saveRelationship(newRel);
// verify relationship exists
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
String hl7String = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20090728170333||ORU^R01|gu99yBh4loLX2mh9cHaV|P|2.5|1||||||||4^AMRS.ELD.FORMID\r" + "PID|||3^^^^||Beren^John^Bondo||\r" + "NK1|1|Jones^Jane^Lee^^RN|2A^Sibling^99REL||||||||||||F|19751016|||||||||||||||||2^^^L^PI\r" + "NK1|2|Jones^Jane^Lee^^RN|1B^Patient^99REL||||||||||||F|19751016|||||||||||||||||2^^^L^PI\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);
// verify existing relationship
List<Relationship> 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);
}
use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class ORUR01HandlerTest method processNK1_shouldNotCreateARelationshipIfOneExists.
/**
* @see ORUR01Handler#processNK1(Patient,NK1)
*/
@Test
public void processNK1_shouldNotCreateARelationshipIfOneExists() throws Exception {
PersonService personService = Context.getPersonService();
// the patient that is the focus of
Patient patient = new Patient(3);
// this hl7 message
// the patient that is related to
Patient relative = new Patient(2);
// patientA
// create a relationship in the database
Relationship rel = new Relationship();
rel.setRelationshipType(new RelationshipType(3));
rel.setPersonA(relative);
rel.setPersonB(patient);
personService.saveRelationship(rel);
// verify relationship exists
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
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|||||||||||||||||2^^^L^PI\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";
ORUR01Handler oruHandler = new ORUR01Handler();
Message hl7message = parser.parse(hl7String);
ORU_R01 oru = (ORU_R01) hl7message;
List<NK1> nk1List = oruHandler.getNK1List(oru);
for (NK1 nk1 : nk1List) oruHandler.processNK1(patient, nk1);
// verify existing relationship
List<Relationship> rels = personService.getRelationships(relative, patient, new RelationshipType(3));
Assert.assertTrue("existing relationship was not retained", !rels.isEmpty() && rels.size() == 1);
}
Aggregations