@Override
 public EntityIdentifier[] searchForGroups(String query, int method, Class leaftype)
     throws GroupsException {
   if (leaftype != IPERSON_CLASS) {
     return EMPTY_SEARCH_RESULTS;
   }
   Set<IPersonAttributesGroupDefinition> pagsGroups =
       personAttributesGroupDefinitionDao.getPersonAttributesGroupDefinitions();
   List<EntityIdentifier> results = new ArrayList<EntityIdentifier>();
   switch (method) {
     case IS:
       for (IPersonAttributesGroupDefinition pagsGroup : pagsGroups) {
         IEntityGroup g = convertPagsGroupToEntity(pagsGroup);
         if (g.getName().equalsIgnoreCase(query)) {
           results.add(g.getEntityIdentifier());
         }
       }
       break;
     case STARTS_WITH:
       for (IPersonAttributesGroupDefinition pagsGroup : pagsGroups) {
         IEntityGroup g = convertPagsGroupToEntity(pagsGroup);
         if (g.getName().toUpperCase().startsWith(query.toUpperCase())) {
           results.add(g.getEntityIdentifier());
         }
       }
       break;
     case ENDS_WITH:
       for (IPersonAttributesGroupDefinition pagsGroup : pagsGroups) {
         IEntityGroup g = convertPagsGroupToEntity(pagsGroup);
         if (g.getName().toUpperCase().endsWith(query.toUpperCase())) {
           results.add(g.getEntityIdentifier());
         }
       }
       break;
     case CONTAINS:
       for (IPersonAttributesGroupDefinition pagsGroup : pagsGroups) {
         IEntityGroup g = convertPagsGroupToEntity(pagsGroup);
         if (g.getName().toUpperCase().indexOf(query.toUpperCase()) != -1) {
           results.add(g.getEntityIdentifier());
         }
       }
       break;
   }
   return results.toArray(new EntityIdentifier[] {});
 }
  @Override
  public boolean contains(IEntityGroup group, IGroupMember member) {

    /*
     * This method has the potential to be called A LOT, especially if
     * there's a lot of portal data (portlets & groups).  It's important
     * not to waste time on nonsensical checks.
     */

    if (!IPERSON_CLASS.equals(member.getLeafType())) {
      // Maybe this call to contains() shouldn't even happen, since
      // group.getLeafType() is (presumably) IPerson.class.
      return false;
    }

    if (member.isGroup()) {
      // PAGS groups may only contain other PAGS groups (and people, of course)
      final IEntityGroup ieg = (IEntityGroup) member;
      if (!PagsService.SERVICE_NAME_PAGS.equals(ieg.getServiceName().toString())) {
        return false;
      }
    }

    final MembershipCacheKey cacheKey =
        new MembershipCacheKey(group.getEntityIdentifier(), member.getEntityIdentifier());
    Element element = membershipCache.get(cacheKey);
    if (element == null) {

      logger.debug(
          "Checking if group {} contains member {}/{}",
          group.getName(),
          member.getKey(),
          member.getLeafType().getSimpleName());

      boolean answer = false; // default
      final PagsGroup groupDef = convertEntityToGroupDef(group);
      if (member.isGroup()) {
        final String key = ((IEntityGroup) member).getLocalKey();
        answer = groupDef.hasMember(key);
      } else {
        try {
          final IPersonAttributeDao pa = PersonAttributeDaoLocator.getPersonAttributeDao();
          final IPersonAttributes personAttributes = pa.getPerson(member.getKey());

          if (personAttributes != null) {
            final RestrictedPerson rp = PersonFactory.createRestrictedPerson();
            rp.setAttributes(personAttributes.getAttributes());
            answer = groupDef.contains(rp);
          }
        } catch (Exception ex) {
          logger.error(
              "Exception acquiring attributes for member "
                  + member
                  + " while checking if group "
                  + group
                  + " contains this member.",
              ex);
          return false;
        }
      }

      element = new Element(cacheKey, answer);
      membershipCache.put(element);
    }

    return (Boolean) element.getObjectValue();
  }