states = new LinkedHashMap<String, ProgramWorkflowState>();
if (tagParams.getStateIds() != null) {
for (int i = 0; i < tagParams.getStateIds().size(); i++) {
String stateId = tagParams.getStateIds().get(i);
ProgramWorkflowState state = HtmlFormEntryUtil.getState(stateId, workflow);
if (state == null) {
throw new IllegalArgumentException("workflow with id " + workflow.getId() + " does not have state id "
+ stateId);
}
String label = state.getConcept().getName().getName();
if (tagParams.getStateLabels() != null) {
label = tagParams.getStateLabels().get(i);
}
states.put(label, state);
}
} else {
for (ProgramWorkflowState state : workflow.getStates()) {
if (state.isRetired()) {
continue;
}
String label = state.getConcept().getName().getName();
states.put(label, state);
}
}
Date encounterDatetime = new Date();
if (context.getExistingEncounter() != null) {
encounterDatetime = context.getExistingEncounter().getEncounterDatetime();
}
ProgramWorkflowState currentState = null;
activePatientState = getActivePatientState(context.getExistingPatient(), encounterDatetime, workflow);
if (activePatientState != null) {
currentState = activePatientState.getState();
}
if (currentState == null) {
// Remove other than initial states
for (Iterator<ProgramWorkflowState> it = states.values().iterator(); it.hasNext();) {
ProgramWorkflowState state = it.next();
if (Boolean.FALSE.equals(state.getInitial())) {
it.remove();
}
}
}
if (tagParams.getLabelCode() != null) {
label = context.getTranslator().translate(Context.getLocale().toString(), tagParams.getLabelCode());
}
if (label == null && tagParams.getLabelText() != null) {
label = tagParams.getLabelText();
}
if (tagParams.getType().equals("hidden")) {
widget = new HiddenFieldWidget();
// there is only one state
Entry<String, ProgramWorkflowState> state = states.entrySet().iterator().next();
widget.setInitialValue(state.getValue().getUuid());
} else if (tagParams.getType().equals("checkbox")) {
// there is only one state
Entry<String, ProgramWorkflowState> state = states.entrySet().iterator().next();
widget = new CheckboxWidget(state.getKey(), state.getValue().getUuid());
} else {
SingleOptionWidget singleOption;
if (tagParams.getType().equals("dropdown")) {
singleOption = new DropdownWidget();
singleOption.addOption(new Option("", "", false));
} else {
singleOption = new RadioButtonsWidget();
}
for (Entry<String, ProgramWorkflowState> state : states.entrySet()) {
boolean select = state.getValue().equals(currentState);
singleOption.addOption(new Option(state.getKey(), state.getValue().getUuid(), select));
}
widget = singleOption;
}