/** * @see PersonService#saveRelationship(Relationship) */ @Test public void saveRelationship_shouldCreateNewObjectWhenRelationshipIdIsNull() throws Exception { PersonService personService = Context.getPersonService(); Relationship relationship = new Relationship(); relationship.setPersonA(personService.getPerson(1)); relationship.setPersonB(personService.getPerson(2)); relationship.setRelationshipType(personService.getRelationshipType(1)); Assert.assertNull(relationship.getRelationshipId()); Relationship savedRelationship = personService.saveRelationship(relationship); Assert.assertNotNull(savedRelationship.getRelationshipId()); }
/** * The purpose of this method is to allow subclasses of Relationship to delegate a portion of * their copy() method back to the superclass, in case the base class implementation changes. * * @param target a Relationship that will have the state of <code>this</code> copied into it * @return the Relationship that was passed in, with state copied into it */ protected Relationship copyHelper(Relationship target) { target.personA = getPersonA(); target.relationshipType = getRelationshipType(); target.personB = getPersonB(); target.setCreator(getCreator()); target.setDateCreated(getDateCreated()); target.setVoided(getVoided()); target.setVoidedBy(getVoidedBy()); target.setDateVoided(getDateVoided()); target.setVoidReason(getVoidReason()); return target; }
/** * Does a shallow copy of this Relationship. Does NOT copy relationshipId * * @return a copy of this relationship */ public Relationship copy() { return copyHelper(new Relationship()); }
/** * generate a relationship hash for use in mergePatients; follows the convention: * [relationshipType][A|B][relativeId] * * @param rel relationship under consideration * @param primary the focus of the hash * @return hash depicting relevant information to avoid duplicates */ private String relationshipHash(Relationship rel, Person primary) { boolean isA = rel.getPersonA().equals(primary); return rel.getRelationshipType().getRelationshipTypeId().toString() + (isA ? "A" : "B") + (isA ? rel.getPersonB().getPersonId().toString() : rel.getPersonA().getPersonId().toString()); }
if (er.getRelationshipType().getId().equals(r.getId())) { if (er.getPersonB().equals(relatedPerson)) { create = false; else if (er.getPersonA().equals(session.getSubmissionActions().getCurrentPerson()) && replace) { session.getSubmissionActions().getRelationshipsToVoid().add(er); if (er.getPersonA().equals(relatedPerson)) { create = false; else if (er.getPersonB().equals(session.getSubmissionActions().getCurrentPerson()) && replace) { session.getSubmissionActions().getRelationshipsToVoid().add(er); Relationship rel = new Relationship(); if (side.equals("A")) { rel.setPersonA(session.getSubmissionActions().getCurrentPerson()); rel.setPersonB(relatedPerson); rel.setPersonB(session.getSubmissionActions().getCurrentPerson()); rel.setPersonA(relatedPerson); rel.setRelationshipType(r);
if (!rel.getVoided()) { boolean personAisPreferred = rel.getPersonA().equals(preferred); boolean personAisNotPreferred = rel.getPersonA().equals(notPreferred); boolean personBisPreferred = rel.getPersonB().equals(preferred); boolean personBisNotPreferred = rel.getPersonB().equals(notPreferred); String relHash = relationshipHash(rel, notPreferred); } else { Relationship tmpRel = rel.copy(); if (personAisNotPreferred) { tmpRel.setPersonA(preferred); tmpRel.setPersonB(preferred); log.debug("Copying relationship " + rel.getRelationshipId() + " to " + preferred.getPatientId()); Relationship persisted = personService.saveRelationship(tmpRel); mergedData.addCreatedRelationship(persisted.getUuid()); + " was merged, relationship copied to #" + tmpRel.getRelationshipId()); mergedData.addVoidedRelationship(rel.getUuid());
@Test public void voidRelationship_shouldVoidRelationshipIfGivenRelationshipIsNotVoided() { Relationship relationship = personService.getRelationship(1); assertFalse("We need an unvoided relationship to test the method", relationship.getVoided()); String voidReason = "Something"; // TODO - voiding is done by the BaseVoidHandler called via AOP before voidRelationship // is executed. Coverage of voidRelationship is low because relationship.getVoided() is true // when entering voidRelationship // Documented at TRUNK-5151 personService.voidRelationship(relationship, voidReason); Relationship voidedRelationship = personService.getRelationship(1); assertTrue(voidedRelationship.getVoided()); assertThat(voidedRelationship.getVoidReason(), is(voidReason)); assertNotNull(voidedRelationship.getDateVoided()); assertEquals(voidedRelationship.getVoidedBy(), Context.getAuthenticatedUser()); }
Relationship r = new Relationship(); // 0 r.setPersonA(personA); r.setPersonB(personB); r.setRelationshipType(rt); r.setStartDate(df.parse("1980-01-01")); r.setEndDate(df.parse("2010-01-01")); personService.saveRelationship(r); rels.add(r); r = new Relationship(); // 1 r.setPersonA(personA); r.setPersonB(personB); r.setRelationshipType(rt); r.setStartDate(df.parse("1990-01-01")); r.setEndDate(df.parse("2010-01-01")); personService.saveRelationship(r); rels.add(r); r = new Relationship(); // 2 r.setPersonA(personA); r.setPersonB(personB); r.setRelationshipType(rt); r.setStartDate(df.parse("1980-01-01")); r.setEndDate(df.parse("1990-01-01")); personService.saveRelationship(r); rels.add(r); r = new Relationship(); // 3 r.setPersonA(personA);
if(r.getRelationshipType().getId().equals(rt.getId())) if(r.getPersonA().equals(context.getExistingPatient())) String s = r.getPersonB().getGivenName()+" "+r.getPersonB().getFamilyName(); sb.append(s); val.append(s); sb.append(Context.getMessageSourceService().getMessage("htmlformentry.existingRelationshipsAdded")); sb.append(" - "); sb.append(new SimpleDateFormat("yyyy-MM-dd").format(r.getDateCreated())); if(r.getPersonB().equals(context.getExistingPatient())) String s =r.getPersonA().getGivenName()+" "+r.getPersonA().getFamilyName(); sb.append(s); val.append(s); sb.append(Context.getMessageSourceService().getMessage("htmlformentry.existingRelationshipsAdded")); sb.append(" - "); sb.append(new SimpleDateFormat("yyyy-MM-dd").format(r.getDateCreated()));
@Override public Relationship newDelegate() { return new Relationship(); }
/** * @see PersonService#saveRelationship(Relationship) */ @Test(expected = APIException.class) public void saveRelationship_shouldThrowAPIException() { Relationship relationship = new Relationship(); Person person = new Person(); relationship.setPersonA(person); relationship.setPersonB(person); personService.saveRelationship(relationship); }
/** * @see RelationshipValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfStartDateIsInFuture() { Relationship relationship = new Relationship(1); Map<String, String> map = new HashMap<>(); MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName()); Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, 1); Date nextYear = cal.getTime(); relationship.setStartDate(nextYear); new RelationshipValidator().validate(relationship, errors); Assert.assertTrue(errors.hasErrors()); }
/** * @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()); }
/** * @see org.openmrs.api.PersonService#saveRelationship(org.openmrs.Relationship) */ @Override public Relationship saveRelationship(Relationship relationship) throws APIException { if (relationship.getPersonA().equals(relationship.getPersonB())) { throw new APIException("Person.cannot.same", (Object[]) null); } return dao.saveRelationship(relationship); }
/** * @throws ParseException * @see RelationshipValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfEndDateIsBeforeStartDate() throws ParseException { Relationship relationship = new Relationship(1); relationship.setStartDate(Context.getDateFormat().parse("18/02/2012")); relationship.setEndDate(Context.getDateFormat().parse("18/02/2001")); Map<String, String> map = new HashMap<>(); MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName()); new RelationshipValidator().validate(relationship, errors); Assert.assertEquals(true, errors.hasErrors()); }
/** * @see PersonService#saveRelationship(Relationship) */ @Test public void saveRelationship_shouldUpdateExistingObjectWhenRelationshipIdIsNotNull() throws Exception { PersonService personService = Context.getPersonService(); Relationship savedRelationship = personService.getRelationship(1); Assert.assertNotNull(savedRelationship.getRelationshipId()); savedRelationship.setRelationshipType(personService.getRelationshipType(2)); Relationship updatedRelationship = personService.saveRelationship(savedRelationship); Assert.assertEquals(personService.getRelationshipType(2), updatedRelationship.getRelationshipType()); }
/** * @see PatientService#mergePatients(Patient,Patient) */ @Test public void mergePatients_shouldAuditCreatedRelationships() throws Exception { //create relationships and retrieve preferred and notPreferredPatient patient executeDataSet(PATIENT_RELATIONSHIPS_XML); Patient preferred = patientService.getPatient(7); Patient notPreferred = patientService.getPatient(2); voidOrders(Collections.singleton(notPreferred)); //merge the two patients and retrieve the audit object PersonMergeLog audit = mergeAndRetrieveAudit(preferred, notPreferred); //find the UUID of the created relationship as a result of the merge //note: since patient 2 is related to patient 1. patient 7 should now be related to patient 1 String createdRelationshipUuid = null; List<Relationship> relationships = personService.getRelationshipsByPerson(preferred); for (Relationship r : relationships) { if (r.getPersonB().getId().equals(1) || r.getPersonA().getId().equals(1)) { createdRelationshipUuid = r.getUuid(); } } Assert.assertNotNull("expected relationship was not found for the preferred patient after the merge", createdRelationshipUuid); Assert.assertTrue("relationship creation not audited", isValueInList(createdRelationshipUuid, audit.getPersonMergeLogData().getCreatedRelationships())); }
@Override public String toString() { String relType = getRelationshipType() == null ? "NULL" : getRelationshipType().getaIsToB(); return personA + " is the " + relType + " of " + personB; }
for (Relationship r : submissionActions.getRelationshipsToCreate()) { if (log.isDebugEnabled()) { log.debug("creating relationships" + r.getRelationshipType().getDescription()); for (Relationship r : submissionActions.getRelationshipsToVoid()) { if (log.isDebugEnabled()) { log.debug("voiding relationships" + r.getId()); for (Relationship r : submissionActions.getRelationshipsToCreate()) { if (log.isDebugEnabled()) { log.debug("editing relationships" + r.getId());
@Test public void shouldVoidARelationship() throws Exception { Relationship relationship = service.getRelationshipByUuid(RestTestConstants1_8.RELATIONSHIP_UUID); Assert.assertFalse(relationship.isVoided()); handle(newDeleteRequest(getURI() + "/" + getUuid(), new Parameter("reason", "test reason"))); relationship = service.getRelationshipByUuid(RestTestConstants1_8.RELATIONSHIP_UUID); Assert.assertTrue(relationship.isVoided()); Assert.assertEquals("test reason", relationship.getVoidReason()); }