StringBuilder sb = new StringBuilder();
// iterate through all the widgets and set their values based on the values in the last submission
// if there is no value in the last submission, explicitly set the value as empty to override any default values
for (Map.Entry<Widget, String> entry : context.getFieldNames().entrySet()) {
Widget widgetType = entry.getKey();
String widgetFieldName = entry.getValue();
String val = lastSubmission.getParameter(widgetFieldName);
// note that for each widget we set, we also trigger the change event on that widget
// this is so any custom change handlers that a widget or tag may configure are called
// when we set a value here; this is specifically used to make sure we trigger the change
// handlers configured by the <exitFromCare> tag
if (val != null) {
// special case to set the display field when autocomplete is used
if (AutocompleteWidget.class.isAssignableFrom(widgetType.getClass())) {
Class widgetClass = ((AutocompleteWidget) widgetType).getOptionClass();
if (widgetClass != null) {
Object returnedObj = HtmlFormEntryUtil.convertToType(val.trim(), widgetClass);
if (widgetClass.getSimpleName().equals("Location")) {
Location location = null;
if (returnedObj != null) {
location = (Location) returnedObj;
} else {
//This should typically never happen,why is there no location with this id, we
//should set val(locationId) to blank so that the hidden form field is blank too
val = "";
}
sb.append("$j('#" + widgetFieldName + "').val(\""
+ (location == null ? "" : JavaScriptUtils.javaScriptEscape(location.getName())) + "\");\n");
sb.append("$j('#" + widgetFieldName + "_hid" + "').val(\""
+ (location == null ? "" : JavaScriptUtils.javaScriptEscape(location.getId().toString())) + "\");\n");
sb.append("$j('#" + widgetFieldName + "').change();\n");
} else if (widgetClass.getSimpleName().equals("Person")) {
Person provider = null;
if (returnedObj != null) {
provider = (Person) returnedObj;
} else {
//This should typically never happen,why is there no provider with this id, we
//should set val(providerid) to blank so that the hidden form field is blank too
val = "";
}
sb.append("$j('#" + widgetFieldName + "').val(\""
+ (provider == null ? "" : JavaScriptUtils.javaScriptEscape(provider.getPersonName().getFullName())) + "\");\n");
sb.append("$j('#" + widgetFieldName + "_hid" + "').val(\""
+ (provider == null ? "" : JavaScriptUtils.javaScriptEscape(provider.getId().toString())) + "\");\n");
sb.append("$j('#" + widgetFieldName + "').change();\n");
}
}
}
// special case to set the display field of the obs value widget when autocomplete is used with <obs> tag
else if (ConceptSearchAutocompleteWidget.class.isAssignableFrom(entry.getKey().getClass())) {
String conveptVal = lastSubmission.getParameter(widgetFieldName + "_hid");
Object returnedObj = HtmlFormEntryUtil.convertToType(conveptVal.trim(), Concept.class);
Concept concept = null;
if (returnedObj != null) {
concept = (Concept) returnedObj;
} else {
//This should typically never happen,why if there no obs with this id, we
//should set val(obsId) to blank so that the hidden form field is blank too
val = "";
}
sb.append("$j('#" + widgetFieldName + "').val(\""
+ (concept == null ? "" : JavaScriptUtils.javaScriptEscape(concept.getDisplayString())) + "\");\n");
sb.append("$j('#" + widgetFieldName + "_hid" + "').val(\"" + (concept == null ? "" : JavaScriptUtils.javaScriptEscape(concept.getId().toString())) + "\");\n");
sb.append("$j('#" + widgetFieldName + "').change();\n");
} else {
// set the value of the widget based on it's name
sb.append("setValueByName('" + widgetFieldName + "', '" + JavaScriptUtils.javaScriptEscape(val)
+ "');\n");
sb.append("$j('#" + widgetFieldName + "').change();\n");
}
} else {
if (AutocompleteWidget.class.isAssignableFrom(widgetType.getClass())) {
sb.append("$j('#" + widgetFieldName + "').val('');\n");
sb.append("$j('#" + widgetFieldName + "_hid" + "').val('');\n");
sb.append("$j('#" + widgetFieldName + "').change();\n");
} else if (ConceptSearchAutocompleteWidget.class.isAssignableFrom(widgetType.getClass())) {
sb.append("$j('#" + widgetFieldName + "').val('');\n");
sb.append("$j('#" + widgetFieldName + "_hid" + "').val('');\n");
sb.append("$j('#" + widgetFieldName + "').change();\n");
} else {
sb.append("setValueByName('" + widgetFieldName + "', '');\n");