}
}
if (defaultProvider != null) {
providerWidget.setInitialValue(new PersonStub(defaultProvider));
}
Collections.sort(providerOptions,new OptionComparator());
if (("autocomplete").equals(parameters.get("type"))) {
providerWidget.addOption(new Option());
if (!providerOptions.isEmpty()) {
providerWidget.setOptions(providerOptions);
}
} else {
// if initialValueIsSet=false, no initial/default provider, hence this shows the 'select input' field as first option
boolean initialValueIsSet = !(providerWidget.getInitialValue() == null);
providerWidget.addOption(new Option
(Context.getMessageSourceService().getMessage("htmlformentry.chooseAProvider"), "", !initialValueIsSet)); // if no initial or default value
if (!providerOptions.isEmpty()) {
for (Option option : providerOptions) {
providerWidget.addOption(option);
}
}
}
context.registerWidget(providerWidget);
context.registerErrorWidget(providerWidget, providerErrorWidget);
}
if (Boolean.TRUE.equals(parameters.get("encounterType"))) {
encounterTypeWidget = new EncounterTypeWidget();
encounterTypeErrorWidget = new ErrorWidget();
if (parameters.get("types") != null) {
List<EncounterType> encounterTypes = new ArrayList<EncounterType>();
String[] temp = ((String) parameters.get("types")).split(",");
for (String s : temp) {
EncounterType type = HtmlFormEntryUtil.getEncounterType(s);
if (type == null) {
throw new RuntimeException("Cannot find encounter type: " + s);
}
encounterTypes.add(type);
}
encounterTypeWidget.setOptions(encounterTypes);
}
// Set default values
EncounterType defaultEncounterType = null;
if (context.getExistingEncounter() != null) {
defaultEncounterType = context.getExistingEncounter().getEncounterType();
} else {
String defaultTypeId = (String) parameters.get("default");
if (StringUtils.hasText(defaultTypeId)) {
defaultEncounterType = HtmlFormEntryUtil.getEncounterType(defaultTypeId);
}
}
encounterTypeWidget.setInitialValue(defaultEncounterType);
context.registerWidget(encounterTypeWidget);
context.registerErrorWidget(encounterTypeWidget, encounterTypeErrorWidget);
}
// Register Location widgets, if appropriate
if (Boolean.TRUE.equals(parameters.get("location"))) {
locationErrorWidget = new ErrorWidget();
List<Location> locations = new ArrayList<Location>();
List<Option> locationOptions = new ArrayList<Option>();
if ("autocomplete".equals(parameters.get("type"))) {
locationWidget = new AutocompleteWidget(Location.class);
} else {
locationWidget = new DropdownWidget();
}
if (parameters.get("tags") != null && parameters.get("orders") != null) {
throw new RuntimeException("Using both \"order\" and \"tags\" attribute in an encounterLocation tag is not currently supported");
}
// if the "tags" attribute has been specified, load all the locations referenced by tag
if (parameters.get("tags") != null) {
List<LocationTag> tags = new ArrayList<LocationTag>();
String temp[] = ((String) parameters.get("tags")).split(",");
for (String s : temp) {
LocationTag tag = HtmlFormEntryUtil.getLocationTag(s);
if (tag == null) {
throw new RuntimeException("Cannot find tag: " + tag);
}
tags.add(tag);
}
locations.addAll(Context.getLocationService().getLocationsHavingAnyTag(tags));
}
// If the "order" attribute is passed in, limit to the specified locations in order
else if (parameters.get("order") != null) {
String[] temp = ((String) parameters.get("order")).split(",");
for (String s : temp) {
Location loc = HtmlFormEntryUtil.getLocation(s, context);
if (loc == null) {
throw new RuntimeException("Cannot find location: " + loc);
}
locations.add(loc);
}
}
// if no locations have been specified by the order attribute, use all non-retired locations
if (locations.isEmpty()) {
locations = Context.getLocationService().getAllLocations(false);
}
// Set default values
Location defaultLocation = null;
if (context.getExistingEncounter() != null) {
defaultLocation = context.getExistingEncounter().getLocation();
} else {
String defaultLocId = (String) parameters.get("default");
if (StringUtils.hasText(defaultLocId)) {
defaultLocation = HtmlFormEntryUtil.getLocation(defaultLocId, context);
}
}
defaultLocation = defaultLocation == null ? context.getDefaultLocation() : defaultLocation;
locationWidget.setInitialValue(defaultLocation);
// if in EDIT mode, make sure that the default/selected location is one of the location options, so we don't accidentally lose it
if (defaultLocation != null && context.getMode().equals(Mode.EDIT)) {
if (!locations.contains(defaultLocation)) {
locations.add(defaultLocation);
}
}
// now create the actual location options
for (Location location : locations) {
String label = HtmlFormEntryUtil.format(location);
Option option = new Option(label, location.getId().toString(), location.equals(defaultLocation));
locationOptions.add(option);
}
// sort options (if a specific order hasn't been specified
if (parameters.get("order") == null) {
Collections.sort(locationOptions, new OptionComparator());
}
if ("autocomplete".equals(parameters.get("type"))) {
locationWidget.addOption(new Option());
if (!locationOptions.isEmpty()) {