code: @Programmatic
      public ApplicationPermission findByUserAndPermissionValue(final String username, final ApplicationPermissionValue permissionValue) {
  
  
  
          // obtain all permissions for this user, map by its value, and
          // put into query cache (so that this method can be safely called in a tight loop)
          final Map<ApplicationPermissionValue, List<ApplicationPermission>> permissions =
              queryResultsCache.execute(new Callable<Map<ApplicationPermissionValue, List<ApplicationPermission>>>() {
                  @Override
                  public Map<ApplicationPermissionValue, List<ApplicationPermission>> call() throws Exception {
  
                      final List<ApplicationPermission> applicationPermissions = findByUser(username);
                      final ImmutableListMultimap<ApplicationPermissionValue, ApplicationPermission> index = Multimaps
                              .index(applicationPermissions, ApplicationPermission.Functions.AS_VALUE);
  
                      return Multimaps.asMap(index);
                  }
                  // note: it is correct that only username (and not permissionValue) is the key
                  // (we are obtaining all the perms for this user)
              }, ApplicationPermissionRepository.class, "findByUserAndPermissionValue", username);
  
          // now simply return the permission from the required value (if it exists)
          final List<ApplicationPermission> applicationPermissions = permissions.get(permissionValue);
          return applicationPermissions != null && !applicationPermissions.isEmpty()
                      ? applicationPermissions.get(0)
                      : null;
      }
docs: Uses the {@link QueryResultsCache} in order to support
  multiple lookups from <code>org.isisaddons.module.security.app.user.UserPermissionViewModel</code>.