{
//load again user roles with privileges
final List<UserRolePrivilege> userRoles = userRoleDao.getAllUserRoles();
//find the one we have to update
UserRolePrivilege foundRolePriv = null;
{
for( final UserRolePrivilege urp : userRoles )
{
if( urp.getUserRoleType().getKey().equals(vo.getUserRole()) )
{
foundRolePriv = urp;
break;
}
}
}
if( null == foundRolePriv )
{
throw new RuntimeException(" Unexpected error: could not find UserRolePrivilege with key='" + vo.getUserRole() + "'");
}
//and update/add role privileges for the user role we found
{
for(final RolePrivilegeVo rpvo: vo.getUserRolePrivileges())
{
//see whether this user privilege is for update or for add
UserPrivilege foundUserPrivilege = null;
for(final UserPrivilege up: foundRolePriv.getUserPrivileges())
{
if( up.getUserPrivilegeType().getKey().equals(rpvo.getUserRolePrivilege()) )
{
foundUserPrivilege = up;
break;
}
}
if( null == foundUserPrivilege )
{
foundUserPrivilege = new UserPrivilege();
UserPrivilegeType.Key uptKey = UserPrivilegeType.Key.findKey(rpvo.getUserRolePrivilege());
foundUserPrivilege.setUserPrivilegeType(UserPrivilegeType.forKey(uptKey));
foundRolePriv.getUserPrivileges().add(foundUserPrivilege);
}
PrivilegeActionType.Key patKey = PrivilegeActionType.Key.findKey(rpvo.getPrivilegeActions());
foundUserPrivilege.setPrivilegeActionType(PrivilegeActionType.forKey(patKey));
}
}
//and delete the ones that weren't updated or added
{
final Iterator it = foundRolePriv.getUserPrivileges().iterator();
while(it.hasNext())
{
final UserPrivilege urp = (UserPrivilege)it.next();
//see whether it was in vo