Skip to content
Closed
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
@@ -1,18 +1,23 @@
package org.openmrs.module.htmlformentry.element;

import javax.servlet.http.HttpServletRequest;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.openmrs.Concept;
import org.openmrs.Obs;
import org.openmrs.api.context.Context;
import org.openmrs.module.appointments.model.AppointmentServiceDefinition;
import org.openmrs.module.appointments.model.Speciality;
import org.openmrs.module.appointments.service.AppointmentServiceDefinitionService;
import org.openmrs.module.appointments.service.SpecialityService;
import org.openmrs.module.htmlformentry.BadFormDesignException;
import org.openmrs.module.htmlformentry.FormEntryContext;
import org.openmrs.module.htmlformentry.FormEntryContext.Mode;
Expand All @@ -37,6 +42,14 @@ public class AppointmentServiceObsElement implements HtmlGeneratorElement, FormS

private String tagControlId;

private static final String ATTRIBUTE_SPECIALITIES = "specialities";
private static final String ATTRIBUTE_SORT_BY = "sortBy";

private static final String ATTRIBUTE_SORT_BY_SPECIALITIES_ORDER_THEN_SERVICE_NAME = "specialitiesOrderThenServiceName";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in love with this attribute name. Can we change this so that sortBy can take a comma-delimited list of options, so the default would be "sortBy=serviceName", but in our forms it would be "sortBy=specialty,serviceName"?


@SuppressWarnings("unused")
private static final String ATTRIBUTE_SORT_BY_SERVICE_NAME = "serviceName"; // default sort order

public AppointmentServiceObsElement(FormEntryContext context, Map<String, String> parameters) throws BadFormDesignException{
String conceptId = parameters.get("conceptId");
if (conceptId == null) {
Expand Down Expand Up @@ -68,20 +81,35 @@ public AppointmentServiceObsElement(FormEntryContext context, Map<String, String
}
String existingValue = existingObs != null ? existingObs.getValueText() : null;

List<String> specialityFilter = TagUtil.parseListParameter(parameters, "specialities", String.class).stream()
List<String> specialityFilterStr = TagUtil.parseListParameter(parameters, ATTRIBUTE_SPECIALITIES, String.class).stream()
.map(String::toLowerCase).map(String::trim).collect(Collectors.toList());

List<Speciality> allSpecialities = Context.getService(SpecialityService.class).getAllSpecialities();
List<String> specialityFilter = specialityFilterStr.stream()
.map(s -> allSpecialities.stream().filter(sp -> sp.getName().equalsIgnoreCase(s) || sp.getUuid().equalsIgnoreCase(s)).findFirst())
.filter(Optional::isPresent)
.map(Optional::get)
.map(Speciality::getUuid)
.collect(Collectors.toList());


Comparator<AppointmentServiceDefinition> compareServicesByServiceName = Comparator.comparing(AppointmentServiceDefinition::getName, String.CASE_INSENSITIVE_ORDER);
Comparator<AppointmentServiceDefinition> compareServicesBySpecialityOrder = Comparator.comparingInt(svc -> {
Speciality speciality = svc.getSpeciality();
return speciality != null ? specialityFilter.indexOf(speciality.getUuid()) : -1;
});
List<AppointmentServiceDefinition> services = Context.getService(AppointmentServiceDefinitionService.class)
.getAllAppointmentServices(false);
services.sort((a, b) -> a.getName().compareToIgnoreCase(b.getName()));

if (!specialityFilter.isEmpty()) {
services = services.stream()
.filter(s -> s.getSpeciality() != null
&& (specialityFilter.contains(s.getSpeciality().getName().toLowerCase().trim())
|| specialityFilter.contains(s.getSpeciality().getUuid().toLowerCase().trim())))
.collect(Collectors.toList());
}

String sortBy = parameters.get(ATTRIBUTE_SORT_BY);
boolean sortBySpecialitiesOrder = ATTRIBUTE_SORT_BY_SPECIALITIES_ORDER_THEN_SERVICE_NAME.equalsIgnoreCase(sortBy);
services = services.stream()
.filter(service -> specialityFilterStr.isEmpty()
|| (service.getSpeciality() != null && specialityFilter.contains(service.getSpeciality().getUuid())))
.sorted(sortBySpecialitiesOrder
? compareServicesBySpecialityOrder.thenComparing(compareServicesByServiceName)
: compareServicesByServiceName)
.collect(Collectors.toList());

serviceWidget = new DropdownWidget();
errorWidget = new ErrorWidget();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openmrs.module.appointments.model.AppointmentServiceDefinition;
import org.openmrs.module.appointments.model.Speciality;
import org.openmrs.module.appointments.service.AppointmentServiceDefinitionService;
import org.openmrs.module.appointments.service.SpecialityService;
import org.openmrs.module.htmlformentry.BadFormDesignException;
import org.openmrs.module.htmlformentry.FormEntryContext;
import org.openmrs.module.htmlformentry.FormEntryContext.Mode;
Expand All @@ -60,6 +61,9 @@ public class AppointmentServiceObsElementTest {
@Mock
private AppointmentServiceDefinitionService appointmentServiceDefinitionService;

@Mock
private SpecialityService specialityService;

private FormSubmissionActions actions;

private MockedStatic<Context> mockedContext;
Expand All @@ -82,7 +86,12 @@ public void setup() throws Exception {
mockedHtmlFormEntryUtil.when(() -> HtmlFormEntryUtil.createObs(any(Concept.class), any(), any(), any())).thenCallRealMethod();
mockedContext.when(() -> Context.getService(AppointmentServiceDefinitionService.class))
.thenReturn(appointmentServiceDefinitionService);
mockedContext.when(() -> Context.getService(SpecialityService.class))
.thenReturn(specialityService);
mockedContext.when(Context::getLocale).thenReturn(Locale.ENGLISH);
when(specialityService.getAllSpecialities()).thenReturn(
Arrays.asList(makeSpeciality("Cardiology", "spec-uuid-cardio"),
makeSpeciality("Orthopaedics", "spec-uuid-ortho")));

when(appointmentServiceDefinitionService.getAllAppointmentServices(false))
.thenReturn(Collections.singletonList(makeService("Cardiology", 1, SERVICE_UUID, null)));
Expand Down Expand Up @@ -161,6 +170,28 @@ public void constructor_shouldMatchSpecialityFilterCaseInsensitively() throws Ex
assertThat(html.contains("uuid-cardio"), is(true));
}

@Test
public void constructor_shouldSortServicesBySpecialityOrderThenByName() throws Exception {
Speciality cardiology = makeSpeciality("Cardiology", "spec-uuid-cardio");
Speciality orthopaedics = makeSpeciality("Orthopaedics", "spec-uuid-ortho");
when(specialityService.getAllSpecialities()).thenReturn(Arrays.asList(cardiology, orthopaedics));
when(appointmentServiceDefinitionService.getAllAppointmentServices(false)).thenReturn(
Arrays.asList(
makeService("Cardio B", 1, "uuid-cardio-b", cardiology),
makeService("Ortho A", 2, "uuid-ortho-a", orthopaedics),
makeService("Cardio A", 3, "uuid-cardio-a", cardiology)));

Map<String, String> p = params("conceptId", CONCEPT_ID);
p.put("specialities", "Cardiology,Orthopaedics");
p.put("sortBy", "specialitiesOrder");
AppointmentServiceObsElement element = new AppointmentServiceObsElement(context, p);
stubFieldName(element, "field");
String html = element.generateHtml(context);

assertThat(html.indexOf("uuid-cardio-a") < html.indexOf("uuid-cardio-b"), is(true));
assertThat(html.indexOf("uuid-cardio-b") < html.indexOf("uuid-ortho-a"), is(true));
}

@Test
public void constructor_shouldFilterServicesBySpecialityUuid() throws Exception {
when(appointmentServiceDefinitionService.getAllAppointmentServices(false)).thenReturn(
Expand Down