*/
protected void loadOptionList() {
// Determine whether option list should be loaded
if (getOptionList().size() == 1) {
Option option = (Option) getOptionList().get(0);
if (option.getValue().equals(Option.EMPTY_OPTION.getValue())) {
// continue and load option list
} else {
// Don't load list
return;
}
} else if (getOptionList().size() > 1) {
// Don't load list
return;
}
CayenneForm form = (CayenneForm) getForm();
DataContext dataContext = form.getDataContext();
try {
List list = null;
if (getSelectQuery() != null) {
SelectQuery query = getSelectQuery();
if (!query.getOrderings().isEmpty()) {
orderingApplied = true;
} else if (getOrdering() != null && !orderingApplied) {
query.addOrdering(getOrdering());
orderingApplied = true;
} else if (getOptionLabel() != null && !orderingApplied) {
query.addOrdering(getOptionLabel(), true);
orderingApplied = true;
}
list = dataContext.performQuery(query);
} else if (getNamedQuery() != null) {
list = dataContext.performQuery(getNamedQuery());
} else if (getQueryName() != null) {
list = dataContext.performQuery(getQueryName(), false);
} else {
Class doClass = form.getDataObjectClass();
String getterName = ClickUtils.toGetterName(getName());
Method method = doClass.getMethod(getterName);
Class propertyClass = method.getReturnType();
SelectQuery query = new SelectQuery(propertyClass);
if (getOrdering() != null && !orderingApplied) {
query.addOrdering(getOrdering());
orderingApplied = true;
} else if (getOptionLabel() != null && !orderingApplied) {
query.addOrdering(getOptionLabel(), true);
orderingApplied = true;
}
list = dataContext.performQuery(query);
}
if (isRequired() && getOptionList().isEmpty() || isOptional()) {
getOptionList().add(Option.EMPTY_OPTION);
}
Map cache = new HashMap();
for (int i = 0; i < list.size(); i++) {
DataObject dataObject = (DataObject) list.get(i);
String value = DataObjectUtils.pkForObject(dataObject).toString();
Object label = null;
if (getDecorator() != null) {
label = getDecorator().render(dataObject, getContext());
} else {
if (getOptionLabel() == null) {
String msg =
"optionLabel not defined for PropertySelect: " + getName();
throw new IllegalStateException(msg);
}
label = PropertyUtils.getValue(dataObject, getOptionLabel(), cache);
}
Option option = null;
if (label != null) {
option = new Option(value, label.toString());
} else {
option = new Option(value);
}
add(option);
}