[rules-users] Verify if an objects attribute exists

Mark Proctor mproctor at codehaus.org
Tue Sep 14 06:21:06 EDT 2010


  Pattern( field != null, eval( someFunc( field ) ) )

Mark
On 14/09/2010 05:18, lnguyen wrote:
> Sorry here is some additional details...I hope I'm not making this sound more
> complicated than it really is but I am attempting to restrict access to view
> an account based on the logged in user.
>
> public class User
> {
> 	private Long id;
>
> 	...
> 	
> 	private UserProfile userProfile;
>
> }
>
>
> public class UserProfile
> {
>
> 	private Long id;
>
> 	....
> 	
> 	private User supervisor;
>
> }
>
>
> So I created a function called isUserMatching to determine if the
> currentUser is looking at their own account or if their supervisor is
> looking at an employees account:
>
> function boolean isUserMatching(User user)
> {
>      User currentUser = (User) Component.getInstance("currentUser");
>
>      User supervisor = (User) user.getUserProfile().getSupervisor();
>
>      boolean result = false;	
>
>
>      if(currentUser == user)
>      {
>          System.out.println("###################### currentUser = user is
> TRUE #####");
>          result = true;
>      }
>      else if((supervisor != null)&&
> (currentUser.getId().equals(supervisor.getId())))
>      {	
> 	System.out.println("###################### currentUser = supervisor is TRUE
> #####");
>          result = true;
>      }
>      else
>      {
>          System.out.println("###################### currentUser/supervisor !=
> user is FALSE #####");
>      }
>
>      return result;
>
>
> }
>
> This function works as desired but not all users have a supervisor. What I'd
> like to know is how can I check if a supervisor is null so that I can
> prevent passing null values into functions?
>
> Doing this "eval (isUserMatching($supervisor))" results in an runtime error
> of course when there is no supervisor.





More information about the rules-users mailing list