Using JSF EL expressions.
485096Dec 2 2006 — edited Dec 4 2006I wonder if any one could help:
The Use case:
A user starts the home page with a default user role otherwise if the user logs in, then the user role is used to determine whether a field is displayed or not.
The user role or #{user.userRole} is compared to the field role threshold to determine whether the field can be rendered or not
Example of a field role threshold in my ListResourceBundle file is as follows:
public class UIResources extends ListResourceBundle{
private static final Object[][] messageStrings = new String[][] {
{"address_addressType_role","0"},
{"address_building_role","4"},
{"address_street_role","3"},
{"address_townOrCity_role","0"},
{"address_stateOrProvince_role","0"},
…
}
Problem situation:
So far so good except that the render core attribute seems to fall back to the default or "true" value. Testing shows that the render attribute remains "true" even when the get() method (see below) does return "false", which is the problem, I am having. I wonder if this is to do with the JSF lifecycle or something else. I need your help.
Current Solutions:
1. In my page, I have for each field something like:
rendered= "#{res['address_townOrCity_role :ge #{userInfo.userRole}']}"
2. Based on oracle.jheadstart.controller.jsf.util. MessageFactoryMap, I re-wrote the
get(Object key) method as follows:
public class MessageFactoryMap extends HashMap {
private MessageFactory messageFactory;
public MessageFactoryMap() {
}
public Object get(Object key) {
// first split the colon-separated message key and arguments
String[] keyAndArgs = StringUtil.stringToStringArray(String.valueOf(key), ":");
String messageKey = keyAndArgs[0];
String operator = null;
String rightArg = null;
if (keyAndArgs.length == 1) {
try {
return getMessageFactory().getMessageFromBundle(messageKey);
} catch (MissingResourceException me) {
// not found in this bundle, go to next bundle
JSFUtil.getInstance().warn(me.getMessage());
}
} else if (keyAndArgs.length > 1) {
operator = keyAndArgs[1].trim().substring(0, 2);
rightArg = keyAndArgs[1].trim().substring(2);
if (getOperators().contains(operator)){
return getOperationResult(messageKey, operator, rightArg);
} else{
return getMessageByKeyAndArg(messageKey, keyAndArgs[1]);
}
}
throw new NullPointerException("No valid expression");
}
private ArrayList<String> getOperators() {
ArrayList<String> operator = new ArrayList<String>(3);
operator.add("eq");
operator.add("le");
operator.add("ge");
return operator;
}
/**
* This method returns the translated message text for the message key
* passed into this method.
* <pre>
"#{res['address_building_role :ge #{userInfo.userRole'] }
"#{res['address_building_role :le #{userInfo.userRole'] }
"#{res['address_building_role :eq #{userInfo.userRole'] }
* </pre>
* @param messageKey
* @param operator
* @param rightArg
* @return
*/
public String getOperationResult(String messageKey, String operator,
String rightArg) {
String leftValue = null;
String rightValue = null;
rightArg = rightArg.trim();
if (getOperators().contains(operator) && rightArg.length() > 1)
try {
...
leftValue = getMessageFactory().getMessageFromBundle(messageKey);
rightValue = String.valueOf(JSFUtil.getInstance().getExpressionValue(rightArg));
if (leftValue != null && rightValue != null) {
Long lvalue = new Long(leftValue);
Long rvalue = new Long(rightValue);
if (operator.equals("eq")) {
return (lvalue == rvalue) ? "true" : "false";
} else if (operator.equals("le")) {
return (lvalue <= rvalue) ? "true" : "false";
} else if (operator.equals("ge")) {
String result = (lvalue >= rvalue) ? "true" : "false";
return (lvalue >= rvalue) ? "false" : "true";
}
}
} catch (java.lang.NullPointerException npe) {
JSFUtil.warn(npe.getMessage());
} catch (java.lang.NumberFormatException nfe) {
JSFUtil.warn(nfe.getMessage());
} catch (MissingResourceException mre) {
// not found in this bundle, go to next bundle
JSFUtil.getInstance().warn(mre.getMessage());
}
return "false";
}
Thanks in advance.