try {
cols = Integer.valueOf(parameters.get("cols"));
}
catch (Exception ex) {}
if (rows != null || cols != null || "textarea".equals(parameters.get("style"))) {
valueWidget = new TextFieldWidget(rows, cols);
} else {
Integer textFieldSize = null;
try {
textFieldSize = Integer.valueOf(parameters.get("size"));
}
catch (Exception ex) {}
valueWidget = new TextFieldWidget(textFieldSize);
}
((TextFieldWidget) valueWidget).setPlaceholder(parameters.get("placeholder"));
} else {
if ("radio".equals(parameters.get("style"))) {
valueWidget = new RadioButtonsWidget();
if (answerSeparator != null) {
((RadioButtonsWidget) valueWidget).setAnswerSeparator(answerSeparator);
}
} else { // dropdown
valueWidget =buildDropdownWidget(size);
}
// need to make sure we have the initialValue too
String lookFor = existingObs == null ? null : existingObs.getValueText();
for (int i = 0; i < textAnswers.size(); ++i) {
String s = textAnswers.get(i);
if (lookFor != null && lookFor.equals(s))
lookFor = null;
String label = null;
if (answerLabels != null && i < answerLabels.size()) {
label = answerLabels.get(i);
} else {
label = s;
}
((SingleOptionWidget) valueWidget).addOption(new Option(label, s, false));
}
// if lookFor is still non-null, we need to add it directly
// as an option:
if (lookFor != null)
((SingleOptionWidget) valueWidget).addOption(new Option(lookFor, lookFor, true));
}
}
if (initialValue != null) {
if (isLocationObs) {
Location l = HtmlFormEntryUtil.getLocation(initialValue, context);
if (l == null) {
throw new RuntimeException("Cannot find Location: " + initialValue);
}
valueWidget.setInitialValue(l);
} else if ("person".equals(parameters.get("style"))) {
Person p = HtmlFormEntryUtil.getPerson(initialValue);
if (p == null) {
throw new RuntimeException("Cannot find Person: " + initialValue);
}
valueWidget.setInitialValue(new PersonStub(p));
} else {
valueWidget.setInitialValue(initialValue);
}
}
} else if (concept.getDatatype().isCoded()) {
if (parameters.get("answerConceptIds") != null) {
try {
for (StringTokenizer st = new StringTokenizer(parameters.get("answerConceptIds"), ","); st
.hasMoreTokens();) {
Concept c = HtmlFormEntryUtil.getConcept(st.nextToken());
if (c == null)
throw new RuntimeException("Cannot find concept " + st.nextToken());
conceptAnswers.add(c);
}
}
catch (Exception ex) {
throw new RuntimeException("Error in answer list for concept " + concept.getConceptId() + " ("
+ ex.toString() + "): " + conceptAnswers);
}
} else if (parameters.get("answerClasses") != null && !"autocomplete".equals(parameters.get("style"))) {
try {
for (StringTokenizer st = new StringTokenizer(parameters.get("answerClasses"), ","); st
.hasMoreTokens();) {
String className = st.nextToken().trim();
ConceptClass cc = Context.getConceptService().getConceptClassByName(className);
if (cc == null) {
throw new RuntimeException("Cannot find concept class " + className);
}
conceptAnswers.addAll(Context.getConceptService().getConceptsByClass(cc));
}
Collections.sort(conceptAnswers, conceptNameComparator);
}
catch (Exception ex) {
throw new RuntimeException("Error in answer class list for concept " + concept.getConceptId() + " ("
+ ex.toString() + "): " + conceptAnswers);
}
}
if (answerConcept != null) {
// if there's also an answer concept specified, this is a single
// checkbox
answerLabel = parameters.get("answerLabel");
if (answerLabel == null) {
String answerCode = parameters.get("answerCode");
if (answerCode != null) {
answerLabel = context.getTranslator().translate(userLocaleStr, answerCode);
} else {
answerLabel = answerConcept.getBestName(Context.getLocale()).getName();
}
}
valueWidget = new CheckboxWidget(answerLabel, answerConcept.getConceptId().toString());
if (existingObsList != null && !existingObsList.isEmpty()) {
for (int i = 0; i < existingObsList.size(); i++) {
((DynamicAutocompleteWidget)valueWidget).addInitialValue(existingObsList.get(i).getValueCoded());
}
} else if (existingObs != null) {
valueWidget.setInitialValue(existingObs.getValueCoded());
} else if (defaultValue != null && Mode.ENTER.equals(context.getMode())) {
Concept initialValue = HtmlFormEntryUtil.getConcept(defaultValue);
if (initialValue == null) {
throw new IllegalArgumentException("Invalid default value. Cannot find concept: " + defaultValue);
}
if (!answerConcept.equals(initialValue)) {
throw new IllegalArgumentException("Invalid default value: " + defaultValue
+ ". The only allowed answer is: " + answerConcept.getId());
}
valueWidget.setInitialValue(initialValue);
}
} else if ("true".equals(parameters.get("multiple"))) {
// if this is a select-multi, we need a group of checkboxes
throw new RuntimeException("Multi-select coded questions are not yet implemented");
} else {
// allow selecting one of multiple possible coded values
// if no answers are specified explicitly (by conceptAnswers or conceptClasses), get them from concept.answers.
if (!parameters.containsKey("answerConceptIds") && !parameters.containsKey("answerClasses") && !parameters.containsKey("answerDrugs")) {
conceptAnswers = new ArrayList<Concept>();
for (ConceptAnswer ca : concept.getAnswers(false)) {
conceptAnswers.add(ca.getAnswerConcept());
}
Collections.sort(conceptAnswers, conceptNameComparator);
}
if ("autocomplete".equals(parameters.get("style"))) {
List<ConceptClass> cptClasses = new ArrayList<ConceptClass>();
if (parameters.get("answerClasses") != null) {
for (StringTokenizer st = new StringTokenizer(parameters.get("answerClasses"), ","); st
.hasMoreTokens();) {
String className = st.nextToken().trim();
ConceptClass cc = Context.getConceptService().getConceptClassByName(className);
cptClasses.add(cc);
}
}
if ((conceptAnswers == null || conceptAnswers.isEmpty())
&& (cptClasses == null || cptClasses.isEmpty())) {
throw new RuntimeException(
"style \"autocomplete\" but there are no possible answers. Looked for answerConcepts and answerClasses attributes, and answers for concept "
+ concept.getConceptId());
}
if ("true".equals(parameters.get("selectMulti"))) {
valueWidget = new DynamicAutocompleteWidget(conceptAnswers, cptClasses);
}
else {
valueWidget = new ConceptSearchAutocompleteWidget(conceptAnswers, cptClasses);
}
} else if (parameters.get("answerDrugs") != null) {
// we support searching through all drugs via AJAX
RemoteJsonAutocompleteWidget widget = new RemoteJsonAutocompleteWidget("/" + WebConstants.WEBAPP_NAME + "/module/htmlformentry/drugSearch.form");
widget.setValueTemplate("Drug:{{id}}");
if (parameters.get("displayTemplate") != null) {
widget.setDisplayTemplate(parameters.get("displayTemplate"));
} else {
widget.setDisplayTemplate("{{name}}");
}
if (existingObs != null && existingObs.getValueDrug() != null) {
widget.setInitialValue(new Option(existingObs.getValueDrug().getName(), existingObs.getValueDrug().getDrugId().toString(), true));
}
valueWidget = widget;
} else {
// Show Radio Buttons if specified, otherwise default to Drop
// Down
boolean isRadio = "radio".equals(parameters.get("style"));
if (isRadio) {
valueWidget = new RadioButtonsWidget();
if (answerSeparator != null) {
((RadioButtonsWidget) valueWidget).setAnswerSeparator(answerSeparator);
}
} else {
valueWidget = buildDropdownWidget(size);
}
for (int i = 0; i < conceptAnswers.size(); ++i) {
Concept c = conceptAnswers.get(i);
String label = null;
if (answerLabels != null && i < answerLabels.size()) {
label = answerLabels.get(i);
} else {
label = c.getBestName(Context.getLocale()).getName();
}
((SingleOptionWidget) valueWidget).addOption(new Option(label, c.getConceptId().toString(),
false));
}
}
if (existingObsList != null && !existingObsList.isEmpty()) {
for (int i = 0; i < existingObsList.size(); i++) {
((DynamicAutocompleteWidget)valueWidget).addInitialValue(existingObsList.get(i).getValueCoded());
}
}
if (existingObs != null) {
if (existingObs.getValueDrug() != null) {
valueWidget.setInitialValue(existingObs.getValueDrug());
} else {
valueWidget.setInitialValue(existingObs.getValueCoded());
}
} else if (defaultValue != null && Mode.ENTER.equals(context.getMode())) {
Concept initialValue = HtmlFormEntryUtil.getConcept(defaultValue);
if (initialValue == null) {
throw new IllegalArgumentException("Invalid default value. Cannot find concept: " + defaultValue);
}
if (!conceptAnswers.contains(initialValue)) {
String allowedIds = "";
for (Concept conceptAnswer : conceptAnswers) {
allowedIds += conceptAnswer.getId() + ", ";
}
allowedIds = allowedIds.substring(0, allowedIds.length() - 2);
throw new IllegalArgumentException("Invalid default value: " + defaultValue
+ ". The only allowed answers are: " + allowedIds);
}
valueWidget.setInitialValue(initialValue);
}
}
} else if (concept.getDatatype().isBoolean()) {
String noStr = parameters.get("noLabel");
if (StringUtils.isEmpty(noStr)) {
noStr = context.getTranslator().translate(userLocaleStr, "general.no");
}
String yesStr = parameters.get("yesLabel");
if (StringUtils.isEmpty(yesStr)) {
yesStr = context.getTranslator().translate(userLocaleStr, "general.yes");
}
if ("checkbox".equals(parameters.get("style"))) {
if (parameters.get("toggle") != null) {
ToggleWidget toggleWidget = new ToggleWidget(parameters.get("toggle"));
valueWidget = new CheckboxWidget(valueLabel, parameters.get("value") != null ? parameters.get("value") : "true", toggleWidget.getTargetId(), toggleWidget.isToggleDim());
} else {
valueWidget = new CheckboxWidget(valueLabel, parameters.get("value") != null ? parameters.get("value") : "true", parameters.get("toggle"));
}
valueLabel = "";
} else if ("no_yes".equals(parameters.get("style"))) {
valueWidget = new RadioButtonsWidget();
((RadioButtonsWidget) valueWidget).addOption(new Option(noStr, "false", false));
((RadioButtonsWidget) valueWidget).addOption(new Option(yesStr, "true", false));
} else if ("yes_no".equals(parameters.get("style"))) {
valueWidget = new RadioButtonsWidget();
((RadioButtonsWidget) valueWidget).addOption(new Option(yesStr, "true", false));
((RadioButtonsWidget) valueWidget).addOption(new Option(noStr, "false", false));
} else if ("no_yes_dropdown".equals(parameters.get("style"))) {
valueWidget = new DropdownWidget();
((DropdownWidget) valueWidget).addOption(new Option());
((DropdownWidget) valueWidget).addOption(new Option(noStr, "false", false));
((DropdownWidget) valueWidget).addOption(new Option(yesStr, "true", false));
} else if ("yes_no_dropdown".equals(parameters.get("style"))) {
valueWidget = new DropdownWidget();
((DropdownWidget) valueWidget).addOption(new Option());
((DropdownWidget) valueWidget).addOption(new Option(yesStr, "true", false));
((DropdownWidget) valueWidget).addOption(new Option(noStr, "false", false));
} else {
throw new RuntimeException("Boolean with style = " + parameters.get("style")
+ " not yet implemented (concept = " + concept.getConceptId() + ")");
}
if (existingObs != null) {
valueWidget.setInitialValue(existingObs.getValueAsBoolean());
} else if (defaultValue != null && Mode.ENTER.equals(context.getMode())) {
defaultValue = defaultValue.trim();
//Check the default value. Do not use Boolean.valueOf as it only tests for 'true'.
Boolean initialValue = null;
if (defaultValue.equalsIgnoreCase(Boolean.TRUE.toString())) {
initialValue = true;
} else if (defaultValue.equalsIgnoreCase(Boolean.FALSE.toString())) {
initialValue = false;
} else if (defaultValue.isEmpty()) {
initialValue = null;
} else {
throw new IllegalArgumentException("Invalid default value " + defaultValue
+ ". Must be 'true', 'false' or ''.");
}
valueWidget.setInitialValue(initialValue);
}
// TODO: in 1.7-compatible version of the module, we can replace the H17 checks
// used below with the new isDate, isTime, and isDatetime
} else {
DateWidget dateWidget = null;
TimeWidget timeWidget = null;
boolean disableTime = "false".equalsIgnoreCase(parameters.get("allowTime"));
if (ConceptDatatype.DATE.equals(concept.getDatatype().getHl7Abbreviation()) || (ConceptDatatype.DATETIME.equals(concept.getDatatype().getHl7Abbreviation()) && disableTime)) {
valueWidget = new DateWidget();
} else if (ConceptDatatype.TIME.equals(concept.getDatatype().getHl7Abbreviation())) {
valueWidget = new TimeWidget();
} else if (ConceptDatatype.DATETIME.equals(concept.getDatatype().getHl7Abbreviation())) {
dateWidget = new DateWidget();
timeWidget = new TimeWidget();
valueWidget = new DateTimeWidget(dateWidget, timeWidget);
} else {
throw new RuntimeException("Cannot handle datatype: " + concept.getDatatype().getName()
+ " (for concept " + concept.getConceptId() + ")");
}
if (defaultValue != null && parameters.get("defaultDatetime") != null) {
throw new IllegalArgumentException("Cannot set defaultDatetime and defaultValue at the same time.");
} else if (defaultValue == null) {
defaultValue = parameters.get("defaultDatetime");
}
if (existingObs != null) {
valueWidget.setInitialValue(existingObs.getValueDatetime());
} else if (defaultValue != null && Mode.ENTER.equals(context.getMode())) {
valueWidget.setInitialValue(HtmlFormEntryUtil
.translateDatetimeParam(defaultValue, defaultDatetimeFormat));
}
if (dateWidget != null) {
context.registerWidget(dateWidget);
}
if (timeWidget != null) {
context.registerWidget(timeWidget);
}
}
}
context.registerWidget(valueWidget);
context.registerErrorWidget(valueWidget, errorWidget);
if (parameters.get("showUnits") != null) {
if ("true".equalsIgnoreCase(parameters.get("showUnits"))) {
showUnits = true;
} else if (!"false".equalsIgnoreCase(parameters.get("showUnits"))) {
showUnits = true;
unitsCode = parameters.get("showUnits");
}
}
if (parameters.get("unitsCssClass") != null) {
unitsCssClass = parameters.get("unitsCssClass");
}
// if a date is requested, do that too
if ("true".equals(parameters.get("showDate")) || parameters.containsKey("dateLabel")) {
if (parameters.containsKey("dateLabel")){
dateLabel = parameters.get("dateLabel");
}
dateWidget = new DateWidget();
context.registerWidget(dateWidget);
context.registerErrorWidget(dateWidget, errorWidget);
if (existingObs != null) {
dateWidget.setInitialValue(existingObs.getObsDatetime());
} else if (parameters.get("defaultObsDatetime") != null) {
// Make sure this format continues to match
// the <obs> attribute defaultObsDatetime documentation at
// https://wiki.openmrs.org/display/docs/HTML+Form+Entry+Module+HTML+Reference#HTMLFormEntryModuleHTMLReference-%3Cobs%3E
String supportedDateFormat = "yyyy-MM-dd-HH-mm";
dateWidget.setInitialValue(HtmlFormEntryUtil.translateDatetimeParam(parameters.get("defaultObsDatetime"),
supportedDateFormat));
}
}
// if an accessionNumber is requested, do that too
if ("true".equals(parameters.get("showAccessionNumber")) || parameters.containsKey("accessionNumberLabel")) {
if (parameters.containsKey("accessionNumberLabel")) {
accessionNumberLabel = parameters.get("accessionNumberLabel");
}
accessionNumberWidget = new TextFieldWidget();
context.registerWidget(accessionNumberWidget);
context.registerErrorWidget(accessionNumberWidget, errorWidget);
if (existingObs != null) {
accessionNumberWidget.setInitialValue(existingObs.getAccessionNumber());
}
}
// if a comment is requested, do that too
if ("true".equals(parameters.get("showCommentField")) || parameters.containsKey("commentFieldLabel")) {
if(parameters.containsKey("commentFieldLabel")){
commentFieldLabel = parameters.get("commentFieldLabel");
}
commentFieldWidget = new TextFieldWidget();
context.registerWidget(commentFieldWidget);
context.registerErrorWidget(commentFieldWidget, errorWidget);
if (existingObs != null) {
commentFieldWidget.setInitialValue(existingObs.getComment());
}