/*
(non-Javadoc)
@see org.apereo.portal.layout.dlm.remoting.IGroupListHelper#getEntity(org.apereo.portal.groups.IGroupMember)
349302
@Override
    public JsonEntityBean getEntity(IGroupMember member) {

        // get the type of this member entity
        EntityEnum entityEnum = getEntityType(member);

        // construct a new entity bean for this entity
        JsonEntityBean entity;
        if (entityEnum.isGroup()) {
            entity = new JsonEntityBean((IEntityGroup) member, entityEnum);
        } else {
            entity = new JsonEntityBean(member, entityEnum);
        }

        // if the name hasn't been set yet, look up the entity name
        if (entity.getName() == null) {
            entity.setName(lookupEntityName(entity));
        }

        if (EntityEnum.GROUP.equals(entity.getEntityType())
                || EntityEnum.PERSON.equals(entity.getEntityType())) {
            IAuthorizationPrincipal principal = getPrincipalForEntity(entity);
            entity.setPrincipalString(principal.getPrincipalString());
        }
        return entity;
    }
978913
@Override
    public IEntity getEntity(String key, Class type) throws GroupsException {
        return getEntity(key, type, null);
    }
795937
@Override
    public Iterator findEntitiesForGroup(IEntityGroup group) throws GroupsException {

        if (isTreeRefreshRequired()) {
            refreshTree();
        }

        log.debug("Invoking findEntitiesForGroup() for group:  {}", group.getLocalKey());

        // We only deal w/ group-group relationships here...
        // return findMemberGroups(group);

        List<IEntity> rslt = new ArrayList<IEntity>();
        if (displayPersonMembers) {
            List<String> list = groupsTree.getPersonChildren().get(group.getLocalKey());
            if (list != null) {
                // should only reach this code if its a SmartLdap managed group...
                for (String s : list) {
                    rslt.add(persons.get(s));
                }
            }
        }
        return rslt.iterator();
    }
1188672
@SuppressWarnings("unchecked")
    private <T extends IEntity> T retrieveEntity(IntuitMessage intuitMessage) {
        T returnEntity = null;
        IntuitResponse intuitResponse = (IntuitResponse) intuitMessage.getResponseElements().getResponse();
        if (intuitResponse != null) {
            JAXBElement<? extends IntuitEntity> intuitObject = intuitResponse.getIntuitObject();
            if (intuitObject != null) {
                returnEntity = ((T) intuitObject.getValue());
            }
        }
        return returnEntity;
    }
583516
public Response getEntityListByType(String entityType) {
        try {
            Preconditions.checkNotNull(entityType, "Entity type cannot be null");

            if (LOG.isDebugEnabled()) {
                LOG.debug("Fetching entity list for type={} ", entityType);
            }

            final List<String> entityList = metadataService.getEntityList(entityType);

            JSONObject response = new JSONObject();
            response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
            response.put(AtlasClient.TYPENAME, entityType);
            response.put(AtlasClient.RESULTS, new JSONArray(entityList));
            response.put(AtlasClient.COUNT, entityList.size());

            return Response.ok(response).build();
        } catch (NullPointerException e) {
            LOG.error("Entity type cannot be null", e);
            throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
        } catch (AtlasException | IllegalArgumentException e) {
            LOG.error("Unable to get entity list for type {}", entityType, e);
            throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
        } catch (WebApplicationException e) {
            LOG.error("Unable to get entity list for type {}", entityType, e);
            throw e;
        } catch (Throwable e) {
            LOG.error("Unable to get entity list for type {}", entityType, e);
            throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
        }
    }
653997
public EntityType getEntityType(Class<?> entityClass) {
    String className = entityClass.getName();
    EntityType type = entityTypes.get(className);
    if (null != type) { return type; }

    type = classEntityTypes.get(className);
    if (null == type) {
      List<EntityType> matched = CollectUtils.newArrayList();
      for (EntityType entityType : entityTypes.values()) {
        if (className.equals(entityType.getEntityName())
            || className.equals(entityType.getEntityClass().getName())) {
          matched.add(entityType);
        }
      }
      if (matched.size() > 1) { throw new RuntimeException("multi-entityName for class:" + className); }
      if (matched.isEmpty()) {
        EntityType tmp = new EntityType(entityClass);
        classEntityTypes.put(className, tmp);
        return tmp;
      } else {
        classEntityTypes.put(className, matched.get(0));
        type = (EntityType) matched.get(0);
      }
    }
    return type;
  }
115205
@Override
    public JsonEntityBean getEntity(String entityType, String entityId, boolean populateChildren) {

        // get the EntityEnum for the specified entity type
        EntityEnum entityEnum = EntityEnum.getEntityEnum(entityType);
        if (entityEnum == null) {
            throw new IllegalArgumentException(
                    String.format("Parameter entityType has an unknown value of [%s]", entityType));
        }
        // if the entity type is a group, use the group service's findGroup method
        // to locate it
        if (entityEnum.isGroup()) {
            // attempt to find the entity
            IEntityGroup entity = GroupService.findGroup(entityId);
            if (entity == null) {
                return null;
            } else {
                JsonEntityBean jsonBean = new JsonEntityBean(entity, entityEnum);
                if (populateChildren) {
                    Iterator<IGroupMember> members = entity.getChildren().iterator();
                    jsonBean = populateChildren(jsonBean, members);
                }
                if (jsonBean.getEntityType().isGroup()
                        || EntityEnum.PERSON.equals(jsonBean.getEntityType())) {
                    IAuthorizationPrincipal principal = getPrincipalForEntity(jsonBean);
                    jsonBean.setPrincipalString(principal.getPrincipalString());
                }
                return jsonBean;
            }
        }

        // otherwise use the getGroupMember method
        else {
            IGroupMember entity = GroupService.getGroupMember(entityId, entityEnum.getClazz());
            if (entity == null || entity instanceof IEntityGroup) {
                return null;
            }
            JsonEntityBean jsonBean = new JsonEntityBean(entity, entityEnum);

            // the group member interface doesn't include the entity name, so
            // we'll need to look that up manually
            jsonBean.setName(lookupEntityName(jsonBean));
            if (EntityEnum.GROUP.equals(jsonBean.getEntityType())
                    || EntityEnum.PERSON.equals(jsonBean.getEntityType())) {
                IAuthorizationPrincipal principal = getPrincipalForEntity(jsonBean);
                jsonBean.setPrincipalString(principal.getPrincipalString());
            }
            return jsonBean;
        }
    }
272429
public EntityAnnotation getEntityAnnotation(final String entityUri) {
        return Iterables.find(getEntityAnnotations(),
                new Predicate<EntityAnnotation>() {

                    @Override
                    public boolean apply(EntityAnnotation ea) {
                        return ea.getEntityReference().getUri()
                                .equals(entityUri);
                    }

                }
        );
    }
561488
private <T extends IEntity> String getEntityName(T entity) {
		if (entity != null) {
			return entity.getClass().getSimpleName().toLowerCase();
		}
		return null;
	}
744246
@Override
    public boolean contains(IEntityGroup group, IGroupMember member) throws GroupsException {
        return (member.isGroup())
                ? containsGroup(group, (IEntityGroup) member)
                : containsEntity(group, member);
    }
512608
public GroupEntity getGroup(String groupName) {
		return restClient.get("groups/" + groupName, GroupEntity.class, new HashMap<String, String>());
	}
751330
private IEntityGroup instanceFromResultSet(ResultSet rs) throws SQLException, GroupsException {
        IEntityGroup eg = null;

        String key = rs.getString(1);
        String creatorID = rs.getString(2);
        Integer entityTypeID = rs.getInt(3);
        Class entityType = EntityTypesLocator.getEntityTypes().getEntityTypeFromID(entityTypeID);
        String groupName = rs.getString(4);
        String description = rs.getString(5);

        if (key != null) {
            eg = newInstance(key, entityType, creatorID, groupName, description);
        }

        return eg;
    }
95959
public String lookupEntityName(Entity entity) {
        if (entity == null) {
            return null;
        }
        EntityEnum entityEnum = EntityEnum.getEntityEnum(entity.getEntityType());
        return lookupEntityName(entityEnum, entity.getId());
    }
1358740
public Annotation getFirstEntity() {
    if (Cooccurrence_Type.featOkTst && ((Cooccurrence_Type)jcasType).casFeat_firstEntity == null)
      jcasType.jcas.throwFeatMissing("firstEntity", "ch.epfl.bbp.uima.types.Cooccurrence");
    return (Annotation)(jcasType.ll_cas.ll_getFSForRef(jcasType.ll_cas.ll_getRefValue(addr, ((Cooccurrence_Type)jcasType).casFeatCode_firstEntity)));}
70354
protected IEntity getEntityFromCache(String key) throws CachingException {
        return (IEntity)
                EntityCachingService.instance().get(ICompositeGroupService.LEAF_ENTITY_TYPE, key);
    }
1118568
@Override
    @SuppressWarnings("unchecked")
    public Iterator findEntitiesForGroup(IEntityGroup group) throws GroupsException {

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Searching Grouper for members of the group with key: " + group.getKey());
        }

        try {

            // execute a search for members of the specified group
            GcGetMembers getGroupsMembers = new GcGetMembers();
            getGroupsMembers.addGroupName(group.getLocalKey());
            getGroupsMembers.assignIncludeSubjectDetail(true);
            WsGetMembersResults results = getGroupsMembers.execute();

            if (results == null
                    || results.getResults() == null
                    || results.getResults().length == 0
                    || results.getResults()[0].getWsSubjects() == null) {
                LOGGER.debug("No members found for Grouper group with key " + group.getLocalKey());
                return Collections.<IGroupMember>emptyList().iterator();
            }

            WsSubject[] gInfos = results.getResults()[0].getWsSubjects();
            final List<IGroupMember> members = new ArrayList<IGroupMember>(gInfos.length);

            // add each result to the member list
            for (WsSubject gInfo : gInfos) {

                // if the member is not a group (aka person)
                if (!StringUtils.equals(gInfo.getSourceId(), "g:gsa")) {

                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(
                                "creating leaf member:"
                                        + gInfo.getId()
                                        + " and name: "
                                        + gInfo.getName()
                                        + " from group: "
                                        + group.getLocalKey());
                    }
                    // use the name instead of id as it shows better in the display
                    IGroupMember member = new EntityImpl(gInfo.getName(), IPerson.class);
                    members.add(member);
                }
            }

            // return an iterator for the assembled group
            return members.iterator();

        } catch (Exception e) {
            LOGGER.warn(
                    "Exception while attempting to retrieve "
                            + "member entities of group with key "
                            + group.getKey()
                            + " from Grouper web services: "
                            + e.getMessage());
            return Collections.<IGroupMember>emptyList().iterator();
        }
    }
722335
private boolean isMember(IGroupMember groupMember, String groupName) {
        try {
            IEntityGroup group = GroupService.findGroup(groupName);
            if (group != null) {
                return groupMember.isDeepMemberOf(group);
            }

            final EntityIdentifier[] results =
                    GroupService.searchForGroups(
                            groupName, GroupService.SearchMethod.DISCRETE, IPerson.class);
            if (results == null || results.length == 0) {
                this.logger.warn(
                        "No portal group found for '{}' no users will be placed in that group for analytics",
                        groupName);
                return false;
            }

            if (results.length > 1) {
                this.logger.warn(
                        "{} groups were found for groupName '{}'. The first result will be used.",
                        results.length,
                        groupName);
            }

            group = (IEntityGroup) GroupService.getGroupMember(results[0]);
            return groupMember.isDeepMemberOf(group);
        } catch (Exception e) {
            this.logger.warn(
                    "Failed to determine if {} is a member of {}, returning false",
                    groupMember,
                    groupName,
                    e);
            return false;
        }
    }