// If specific persons are specified, display only those persons in order
String personsParam = (String) parameters.get("persons");
if (personsParam != null) {
for (String s : personsParam.split(",")) {
Person p = HtmlFormEntryUtil.getPerson(s);
if (p == null) {
throw new RuntimeException("Cannot find Person: " + s);
}
options.add(new PersonStub(p));
}
}
// Only if specific person ids are not passed in do we get by user Role
if (options.isEmpty()) {
List<PersonStub> users = new ArrayList<PersonStub>();
// If the "role" attribute is passed in, limit to users with this role
if (parameters.get("role") != null) {
Role role = Context.getUserService().getRole((String) parameters.get("role"));
if (role == null) {
throw new RuntimeException("Cannot find role: " + parameters.get("role"));
} else {
users = Context.getService(HtmlFormEntryService.class).getUsersAsPersonStubs(role.getRole());
}
}
// Otherwise, limit to users with the default OpenMRS PROVIDER role,
else {
String defaultRole = OpenmrsConstants.PROVIDER_ROLE;
Role role = Context.getUserService().getRole(defaultRole);
if (role != null) {
users = Context.getService(HtmlFormEntryService.class).getUsersAsPersonStubs(role.getRole());
}
// If this role isn't used, default to all Users
if (users.isEmpty()) {
users = Context.getService(HtmlFormEntryService.class).getUsersAsPersonStubs(null);
}
}
options.addAll(users);
// sortOptions = true;
}
valueWidget = new PersonStubWidget(options);
} else {
if (textAnswers.size() == 0) {
Integer rows = null;
Integer cols = null;
try {
rows = Integer.valueOf(parameters.get("rows"));
}
catch (Exception ex) {}
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 {