/**
  * Retrieve an implementation of {@code IPersonAttributesGroupDefinition} with the given {@code
  * name} from the JPA DAO. There are two assumptions. First, that the DAO handles caching, so
  * caching is not implemented here. Second, that group names are unique. A warning will be logged
  * if more than one group is found with the same name.
  *
  * @param name group name used to search for group definition
  * @return {@code IPersonAttributesGroupDefinition} of named group or null
  * @see IPersonAttributesGroupDefinitionDao#getPersonAttributesGroupDefinitionByName(String)
  * @see IPersonAttributesGroupDefinition
  */
 private IPersonAttributesGroupDefinition getPagsGroupDefByName(String name) {
   Set<IPersonAttributesGroupDefinition> pagsGroups =
       personAttributesGroupDefinitionDao.getPersonAttributesGroupDefinitionByName(name);
   if (pagsGroups.size() > 1) {
     logger.error("More than one PAGS group with name {} found.", name);
   }
   final IPersonAttributesGroupDefinition rslt =
       pagsGroups.isEmpty() ? null : pagsGroups.iterator().next();
   return rslt;
 }
  private Iterator<IEntityGroup> findParentGroupsForEntity(IEntity member) throws GroupsException {

    Set<IPersonAttributesGroupDefinition> pagsGroups =
        personAttributesGroupDefinitionDao.getPersonAttributesGroupDefinitions();
    List<IEntityGroup> results = new ArrayList<IEntityGroup>();
    for (IPersonAttributesGroupDefinition pagsGroup : pagsGroups) {
      IEntityGroup group = convertPagsGroupToEntity(pagsGroup);
      if (contains(group, member)) {
        results.add(group);
      }
    }
    return results.iterator();
  }
 @Override
 public IEntityGroup find(String name) throws GroupsException {
   Set<IPersonAttributesGroupDefinition> groups =
       personAttributesGroupDefinitionDao.getPersonAttributesGroupDefinitionByName(name);
   if (groups.size() == 0) {
     logger.error(
         "No PAGS group with name {} found. Check your PAGS group definitions for possible error"
             + " in member group name",
         name);
     return null;
   }
   IPersonAttributesGroupDefinition pagsGroup = groups.iterator().next();
   return convertPagsGroupToEntity(pagsGroup);
 }
 @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[] {});
 }
 private Set<IEntityGroup> getParentGroups(String name, Set<IEntityGroup> groups)
     throws GroupsException {
   logger.debug("Looking up containing groups for {}", name);
   IPersonAttributesGroupDefinition pagsGroup = getPagsGroupDefByName(name);
   Set<IPersonAttributesGroupDefinition> pagsParentGroups =
       personAttributesGroupDefinitionDao.getParentPersonAttributesGroupDefinitions(pagsGroup);
   for (IPersonAttributesGroupDefinition pagsParent : pagsParentGroups) {
     IEntityGroup parent = convertPagsGroupToEntity(pagsParent);
     if (!groups.contains(parent)) {
       groups.add(parent);
       getParentGroups(pagsParent.getName(), groups);
     } else {
       throw new RuntimeException(
           "Recursive grouping detected! for " + name + " and parent " + pagsParent.getName());
     }
   }
   return groups;
 }