Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.joda.time.DateTime;
import org.openmrs.Location;
import org.openmrs.Patient;
import org.openmrs.api.AdministrationService;
import org.openmrs.module.appui.AppUiConstants;
import org.openmrs.module.emrapi.adt.AdtService;
import org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException;
Expand All @@ -22,9 +23,13 @@

public class RetrospectiveVisitFragmentController {

static final String ALLOW_OVERLAPPING_VISIT_AT_ANOTHER_LOCATION_GP =
"coreapps.addPastVisitsAllowOverlappingVisitAtAnotherLocation";

private final Logger log = LoggerFactory.getLogger(getClass());

public Object create(@SpringBean("adtService") AdtService adtService,
@SpringBean("adminService") AdministrationService administrationService,
@RequestParam("patientId") Patient patient,
@RequestParam("locationId") Location location,
@RequestParam("startDate") Date startDate,
Expand All @@ -48,7 +53,15 @@ public Object create(@SpringBean("adtService") AdtService adtService,
}
}

String propValue = administrationService.getGlobalProperty(ALLOW_OVERLAPPING_VISIT_AT_ANOTHER_LOCATION_GP);
boolean allowOverlappingAtAnotherLocation = !"false".equalsIgnoreCase(propValue);

if (!allowOverlappingAtAnotherLocation) {
List<SimpleObject> conflictingVisits = getConflictsAcrossAllLocations(adtService, patient, startDate, stopDate, ui);
if (!conflictingVisits.isEmpty()) {
return conflictingVisits;
}
}

try {
VisitDomainWrapper createdVisit = adtService.createRetrospectiveVisit(patient, location, startDate, stopDate);
Expand All @@ -62,29 +75,52 @@ public Object create(@SpringBean("adtService") AdtService adtService,
catch (ExistingVisitDuringTimePeriodException e) {

// if there are existing visit(s), return these existing visits
List<SimpleObject> simpleVisits = new ArrayList<SimpleObject>();
List<SimpleObject> conflictingVisits = new ArrayList<SimpleObject>();
List<VisitDomainWrapper> visits = adtService.getVisits(patient, location, startDate, stopDate);

if (visits != null) {
for (VisitDomainWrapper visit : visits) {
if(ui.convertTimezones()){
simpleVisits.add(SimpleObject.create("startDate", ui.formatDateWithClientTimezone(new DateTime(visit.getVisit().getStartDatetime()).toDate()),
"stopDate", ui.formatDateWithClientTimezone(new DateTime(visit.getVisit().getStopDatetime()).toDate()),
"id", visit.getVisit().getId(), "uuid", visit.getVisit().getUuid()));
}else{
simpleVisits.add(SimpleObject.create("startDate", ui.format(new DateTime(visit.getVisit().getStartDatetime()).toDateMidnight().toDate()),
"stopDate", ui.format(new DateTime(visit.getVisit().getStopDatetime()).toDateMidnight().toDate()),
"id", visit.getVisit().getId(), "uuid", visit.getVisit().getUuid()));
}
conflictingVisits.add(formatVisit(visit, ui));
}
}

return simpleVisits;
return conflictingVisits;
}
catch (Exception e) {
log.error("Unable to add retrospective visit", e);
return new FailureResult(ui.message(e.getMessage()));
}
}

private List<SimpleObject> getConflictsAcrossAllLocations(AdtService adtService, Patient patient,
Date startDate, Date stopDate, UiUtils ui) {
List<SimpleObject> conflictingVisits = new ArrayList<SimpleObject>();
List<Location> allLocations = adtService.getAllLocationsThatSupportVisits();
if (allLocations != null) {
for (Location loc : allLocations) {
List<VisitDomainWrapper> visits = adtService.getVisits(patient, loc, startDate, stopDate);
if (visits != null) {
for (VisitDomainWrapper visit : visits) {
conflictingVisits.add(formatVisit(visit, ui));
}
}
}
}
return conflictingVisits;
}

private SimpleObject formatVisit(VisitDomainWrapper visit, UiUtils ui) {
if (ui.convertTimezones()) {
return SimpleObject.create(
"startDate", ui.formatDateWithClientTimezone(new DateTime(visit.getVisit().getStartDatetime()).toDate()),
"stopDate", ui.formatDateWithClientTimezone(new DateTime(visit.getVisit().getStopDatetime()).toDate()),
"id", visit.getVisit().getId(), "uuid", visit.getVisit().getUuid());
} else {
return SimpleObject.create(
"startDate", ui.format(new DateTime(visit.getVisit().getStartDatetime()).toDateMidnight().toDate()),
"stopDate", ui.format(new DateTime(visit.getVisit().getStopDatetime()).toDateMidnight().toDate()),
"id", visit.getVisit().getId(), "uuid", visit.getVisit().getUuid());
}
}

}
10 changes: 10 additions & 0 deletions omod/src/main/resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@
<!-- /Internationalization -->

<!-- Global Properties -->
<globalProperty>
<property>coreapps.addPastVisitsAllowOverlappingVisitAtAnotherLocation</property>
<defaultValue>true</defaultValue>
<description>
When adding a past (retrospective) visit, controls whether a visit is allowed if the patient already has
a visit at a different location during that time period. When set to false, the patient must have no visits
at any location during the requested time period.
</description>
</globalProperty>

<globalProperty>
<property>coreapps.defaultPatientIdentifierLocation</property>
<defaultValue></defaultValue>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.openmrs.Location;
import org.openmrs.Patient;
import org.openmrs.Visit;
import org.openmrs.api.AdministrationService;
import org.openmrs.module.appui.AppUiConstants;
import org.openmrs.module.emrapi.adt.AdtService;
import org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException;
Expand All @@ -16,6 +17,7 @@

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
Expand All @@ -38,6 +40,8 @@ public class RetrospectiveVisitFragmentControllerTest {

AdtService adtService;

AdministrationService administrationService;

UiUtils ui;

HttpServletRequest request;
Expand All @@ -48,11 +52,15 @@ public class RetrospectiveVisitFragmentControllerTest {
public void setup() {
controller = new RetrospectiveVisitFragmentController();
adtService = mock(AdtService.class);
administrationService = mock(AdministrationService.class);
ui = mock(UiUtils.class);
request = mock(HttpServletRequest.class);
session = mock(HttpSession.class);

when(request.getSession()).thenReturn(session);
// default: property not set, so overlapping at another location is allowed
when(administrationService.getGlobalProperty(RetrospectiveVisitFragmentController.ALLOW_OVERLAPPING_VISIT_AT_ANOTHER_LOCATION_GP))
.thenReturn(null);
}

@Test
Expand All @@ -74,7 +82,7 @@ public void test_shouldCreateNewRetrospectiveVisit() throws Exception {

when(adtService.createRetrospectiveVisit(patient, location, expectedStartDate, expectedStopDate)).thenReturn(new VisitDomainWrapper(visit));

SimpleObject result = (SimpleObject) controller.create(adtService, patient, location, startDate, stopDate, request, ui);
SimpleObject result = (SimpleObject) controller.create(adtService, administrationService, patient, location, startDate, stopDate, request, ui);

verify(session).setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE,
ui.message("coreapps.retrospectiveVisit.addedVisitMessage"));
Expand Down Expand Up @@ -102,7 +110,7 @@ public void test_shouldCreateNewRetrospectiveVisit_whenNoStopDateSpecified() thr

when(adtService.createRetrospectiveVisit(patient, location, expectedStartDate, expectedStopDate)).thenReturn(new VisitDomainWrapper(visit));

SimpleObject result = (SimpleObject) controller.create(adtService, patient, location, startDate, null, request, ui);
SimpleObject result = (SimpleObject) controller.create(adtService, administrationService, patient, location, startDate, null, request, ui);

verify(session).setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE,
ui.message("coreapps.retrospectiveVisit.addedVisitMessage"));
Expand Down Expand Up @@ -136,7 +144,7 @@ public void test_shouldReturnConflictingVisits() throws Exception {

when(ui.format(any())).thenReturn("someDate");

List<SimpleObject> result = (List<SimpleObject>) controller.create(adtService, patient, location, startDate, null, request, ui);
List<SimpleObject> result = (List<SimpleObject>) controller.create(adtService, administrationService, patient, location, startDate, null, request, ui);

assertThat(result.size(), is(1));
assertThat(result.get(0).toJson(), is("{\"startDate\":\"someDate\",\"stopDate\":\"someDate\",\"id\":null,\"uuid\":\"" + conflictingVisit.getUuid() + "\"}"));
Expand All @@ -159,7 +167,7 @@ public void test_shouldSetEndDateToCurrentTimeIfEndDateIsCurrentDay() throws Exc
when(adtService.createRetrospectiveVisit(eq(patient), eq(location), eq(startDate), any(Date.class))).thenReturn(new VisitDomainWrapper(mockVisit)); // to prevent against NPE when generating success message

Date expectedMinDateValue = new Date();
controller.create(adtService, patient, location, startDate, endDate, request, ui);
controller.create(adtService, administrationService, patient, location, startDate, endDate, request, ui);
Date expectedMaxDateValue = new Date();

verify(adtService).createRetrospectiveVisit(eq(patient), eq(location), eq(startDate), argThat(new IsWithinDateRange(expectedMinDateValue, expectedMaxDateValue)));
Expand All @@ -184,7 +192,7 @@ public void test_shouldCreateNewRetrospectiveVisitWithTimezoneAndDontChangeEndDa

when(adtService.createRetrospectiveVisit(eq(patient), eq(location), eq(startDate), any(Date.class))).thenReturn(new VisitDomainWrapper(mockVisit)); // to prevent against NPE when generating success message

controller.create(adtService, patient, location, startDate, endDate, request, ui);
controller.create(adtService, administrationService, patient, location, startDate, endDate, request, ui);

verify(adtService).createRetrospectiveVisit(eq(patient), eq(location), eq(startDate), eq(endDate));

Expand All @@ -207,12 +215,72 @@ public void test_shouldNotSetEndDateToCurrentTimeIfEndDateIsCurrentDay() throws

when(adtService.createRetrospectiveVisit(eq(patient), eq(location), eq(startDate), any(Date.class))).thenReturn(new VisitDomainWrapper(mockVisit)); // to prevent against NPE when generating success message

controller.create(adtService, patient, location, startDate, endDate, request, ui);
controller.create(adtService, administrationService, patient, location, startDate, endDate, request, ui);

verify(adtService).createRetrospectiveVisit(eq(patient), eq(location), eq(startDate), eq(endDate));

}

@Test
public void test_shouldReturnConflictingVisitsAtAnyLocationWhenPropertySetToFalse() throws Exception {

when(administrationService.getGlobalProperty(RetrospectiveVisitFragmentController.ALLOW_OVERLAPPING_VISIT_AT_ANOTHER_LOCATION_GP))
.thenReturn("false");

Patient patient = new Patient();
Location requestedLocation = new Location();
Location otherLocation = new Location();
Date startDate = new DateTime(2012, 1, 1, 12, 12, 12).toDate();

Date expectedStartDate = new DateTime(2012, 1, 1, 0, 0, 0, 0).toDate();
Date expectedStopDate = null;

Visit conflictingVisit = new Visit();
conflictingVisit.setStartDatetime(new DateTime(2012, 1, 1, 0, 0, 0, 0).toDate());
conflictingVisit.setStopDatetime(new DateTime(2012, 1, 3, 0, 0, 0, 999).toDate());

when(adtService.getAllLocationsThatSupportVisits()).thenReturn(Arrays.asList(requestedLocation, otherLocation));
when(adtService.getVisits(patient, requestedLocation, expectedStartDate, expectedStopDate)).thenReturn(Collections.emptyList());
when(adtService.getVisits(patient, otherLocation, expectedStartDate, expectedStopDate))
.thenReturn(Collections.singletonList(new VisitDomainWrapper(conflictingVisit)));

when(ui.format(any())).thenReturn("someDate");

List<SimpleObject> result = (List<SimpleObject>) controller.create(adtService, administrationService, patient, requestedLocation, startDate, null, request, ui);

assertThat(result.size(), is(1));
assertThat(result.get(0).toJson(), is("{\"startDate\":\"someDate\",\"stopDate\":\"someDate\",\"id\":null,\"uuid\":\"" + conflictingVisit.getUuid() + "\"}"));
}

@Test
public void test_shouldCreateVisitWhenPropertySetToFalseAndNoConflictAtAnyLocation() throws Exception {

when(administrationService.getGlobalProperty(RetrospectiveVisitFragmentController.ALLOW_OVERLAPPING_VISIT_AT_ANOTHER_LOCATION_GP))
.thenReturn("false");
when(ui.message("coreapps.retrospectiveVisit.addedVisitMessage")).thenReturn("success message");

Patient patient = createPatient();
Location location = new Location();
Location otherLocation = new Location();
Date startDate = new DateTime(2012, 1, 1, 12, 12, 12).toDate();
Date stopDate = new DateTime(2012, 1, 2, 13, 13, 13).toDate();

Date expectedStartDate = new DateTime(2012, 1, 1, 0, 0, 0, 0).toDate();
Date expectedStopDate = new DateTime(2012, 1, 2, 23, 59, 59, 999).toDate();

Visit visit = createVisit();

when(adtService.getAllLocationsThatSupportVisits()).thenReturn(Arrays.asList(location, otherLocation));
when(adtService.getVisits(patient, location, expectedStartDate, expectedStopDate)).thenReturn(Collections.emptyList());
when(adtService.getVisits(patient, otherLocation, expectedStartDate, expectedStopDate)).thenReturn(Collections.emptyList());
when(adtService.createRetrospectiveVisit(patient, location, expectedStartDate, expectedStopDate)).thenReturn(new VisitDomainWrapper(visit));

SimpleObject result = (SimpleObject) controller.create(adtService, administrationService, patient, location, startDate, stopDate, request, ui);

assertTrue((Boolean) result.get("success"));
assertThat((String) result.get("id"), is(visit.getId().toString()));
}

private Visit createVisit() {
Visit visit = new Visit();
visit.setId(1);
Expand Down