} 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())) {