final String selRoleKey = ((ModifyRoleForm)form).getUserRole();
assert null != selRoleKey;
final UserRoleService service = getService(UserRoleService.class);
//find the selected role
UserRoleVo selectedRole = null;
{
final List<UserRoleVo> userRoles = service.loadAllUserRoles();
for(final UserRoleVo urvo: userRoles)
{
if( selRoleKey.equals(urvo.getUserRole()) )
{
selectedRole = urvo;
break;
}
}
}
if( null == selectedRole )
{
throw new RuntimeException("Application Error: selected role has key='" + selRoleKey + "' not in the list returned by service.loadAllUserRoles()");
}
//get privileges for the selected role, and also prepare the read-write allowed list
final String[] selPrivs = new String[selectedRole.getUserRolePrivileges().size()];
final String[] writeAllowedPrivs = new String[selectedRole.getUserRolePrivileges().size()];
final Map selPrivMap = new HashMap();
{
for(int i = 0; i < selectedRole.getUserRolePrivileges().size(); i++)
{
final RolePrivilegeVo rpvo = selectedRole.getUserRolePrivileges().get(i);
selPrivs[i] = rpvo.getUserRolePrivilege();
selPrivMap.put(rpvo.getUserRolePrivilege(), "true");
if(PrivilegeActionType.Key.READ_WRITE_KEY.equals(rpvo.getPrivilegeActions())) {
writeAllowedPrivs[i] = rpvo.getUserRolePrivilege();
}
}
}
//prepare also the read-only list which covers all privileges
final List<UserPrivilegeType> privTypes = (List<UserPrivilegeType>)getServletContext().getAttribute(UserPrivilegeType.class.getSimpleName());
final String[] readAllowedPrivs = new String[privTypes.size()];
{
for(int i = 0; i < readAllowedPrivs.length; i++)
{
readAllowedPrivs[i] = privTypes.get(i).getKey();
}
}
//now set them in the form
((ModifyRoleForm)form).setUserRolePrivileges( selPrivs );
((ModifyRoleForm)form).setPrivilegesWithWriteAllowed( writeAllowedPrivs );
((ModifyRoleForm)form).setPrivilegesWithReadAllowed( readAllowedPrivs );
//set also the user role
((ModifyRoleForm)form).setUserRoleLabel(selectedRole.getUserRoleLabel());
((ModifyRoleForm)form).setUserRole(selectedRole.getUserRole());
//and set the label to be displayed on next screen
request.setAttribute("selectedRole", selectedRole.getUserRoleLabel());
request.setAttribute("selectedPrivMap", selPrivMap);
return mapping.findForward("view");
}