VisitWrapper extends AbstractCustomizableWrapper<Visit, VisitAttribute> { public boolean overlaps() { Patient patient = target.getPatient(); for (Visit existingVisit : Context.getVisitService().getVisitsByPatient(patient)) { if (existingVisit.getVisitId().equals(target.getVisitId())) { continue; } if (OpenmrsUtil.compareWithNullAsLatest(target.getStartDatetime(), existingVisit.getStopDatetime()) <= 0 && OpenmrsUtil.compareWithNullAsLatest(target.getStopDatetime(), existingVisit.getStartDatetime()) >= 0) { return true; } } return false; } VisitWrapper(Visit target); boolean overlaps(); Form getSourceForm();  }
@Test public void overlaps_shouldReturnTrueIfVisitOverlaps() { Patient patient8 = Context.getPatientService().getPatient(8); VisitType outpatient = MetadataUtils.existing(VisitType.class, CommonMetadata._VisitType.OUTPATIENT); Visit visit1 = TestUtils.saveVisit(patient8, outpatient, TestUtils.date(2011, 1, 1), TestUtils.date(2011, 1, 3)); Visit visit2 = TestUtils.saveVisit(patient8, outpatient, TestUtils.date(2011, 1, 7), TestUtils.date(2011, 1, 10)); Visit visit3 = TestUtils.saveVisit(patient8, outpatient, TestUtils.date(2011, 1, 13), null); Context.flushSession(); Visit visit = new Visit(); visit.setPatient(patient8); visit.setVisitType(outpatient); visit.setStartDatetime(TestUtils.date(2011, 1, 4)); visit.setStopDatetime(TestUtils.date(2011, 1, 5)); VisitWrapper wrapper = new VisitWrapper(visit); Assert.assertFalse(wrapper.overlaps()); visit.setStartDatetime(TestUtils.date(2011, 1, 2)); visit.setStopDatetime(TestUtils.date(2011, 1, 4)); Assert.assertTrue(wrapper.overlaps()); visit.setStartDatetime(TestUtils.date(2011, 1, 3)); visit.setStopDatetime(TestUtils.date(2011, 1, 4)); Assert.assertTrue(wrapper.overlaps()); visit.setStartDatetime(TestUtils.date(2011, 2, 2)); visit.setStopDatetime(TestUtils.date(2011, 2, 4)); Assert.assertTrue(wrapper.overlaps()); Assert.assertFalse(new VisitWrapper(visit2).overlaps()); }