Search in sources :

Example 1 with GroupedEntry

use of com.zimbra.cs.account.GroupedEntry in project zm-mailbox by Zimbra.

the class LdapProvisioning method getAllDirectDLs.

/**
     * -  Get list of ids from EntryCacheDataKey.GROUPEDENTRY_DIRECT_GROUPIDS for "entry"
     *    -  Entry not cached:
     *       -  Get all addresses of this entry that can be identified as a member in a static group.
     *       -  Get direct groups for those addresses using filterFactory.distributionListsByMemberAddrs
     *          i.e. using an OR filter on Provisioning.A_zimbraMailForwardingAddress
     *       -  See if prov's Group Cache has this group already.  If so, use that! otherwise add it
     *       -  Put list of ids to EntryCacheDataKey.GROUPEDENTRY_DIRECT_GROUPIDS for "entry"
     *    -  Entry is cached:
     *       -  Get all the direct groups.  If any have been deleted, update the cache to reflect that
     * @param prov
     * @param entry
     * @return
     * @throws ServiceException
     */
private List<DistributionList> getAllDirectDLs(LdapProvisioning prov, Entry entry) throws ServiceException {
    if (!(entry instanceof GroupedEntry)) {
        throw ServiceException.FAILURE("internal error", null);
    }
    EntryCacheDataKey cacheKey = EntryCacheDataKey.GROUPEDENTRY_DIRECT_GROUPIDS;
    @SuppressWarnings("unchecked") List<String> directGroupIds = (List<String>) entry.getCachedData(cacheKey);
    List<DistributionList> directGroups = null;
    if (directGroupIds == null) {
        String[] addrs = ((GroupedEntry) entry).getAllAddrsAsGroupMember();
        // fetch from LDAP
        directGroups = prov.getAllDistributionListsForAddresses(addrs, true);
        // - build the group id list and cache it on the entry
        // - add each group in cache only if it is not already in.
        //   we do not want to overwrite the entry in cache, because it
        //   might already have all its direct group ids cached on it.
        // - if the group is already in cache, return the cached instance
        //   instead of the instance we just fetched, because the cached
        //   instance might have its direct group ids cached on it.
        directGroupIds = new ArrayList<String>(directGroups.size());
        List<DistributionList> directGroupsToReturn = new ArrayList<DistributionList>(directGroups.size());
        for (DistributionList group : directGroups) {
            String groupId = group.getId();
            directGroupIds.add(groupId);
            DistributionList cached = getDLFromCache(Key.DistributionListBy.id, groupId);
            if (cached == null) {
                putInGroupCache(group);
                directGroupsToReturn.add(group);
            } else {
                directGroupsToReturn.add(cached);
            }
        }
        entry.setCachedData(cacheKey, directGroupIds);
        return directGroupsToReturn;
    } else {
        /*
             * The entry already have direct group ids cached.
             * Go through each of them and fetch the groups,
             * eithr from cache or from LDAP (prov.getDLBasic).
             */
        directGroups = new ArrayList<DistributionList>();
        Set<String> idsToRemove = null;
        for (String groupId : directGroupIds) {
            DistributionList group = prov.getDLBasic(Key.DistributionListBy.id, groupId);
            if (group == null) {
                // remove it from our direct group id cache on the entry
                if (idsToRemove == null) {
                    idsToRemove = new HashSet<String>();
                }
                idsToRemove.add(groupId);
            } else {
                directGroups.add(group);
            }
        }
        // update our direct group id cache if needed
        if (idsToRemove != null) {
            // create a new object, do *not* update directly on the cached copy
            List<String> updatedDirectGroupIds = new ArrayList<String>();
            for (String id : directGroupIds) {
                if (!idsToRemove.contains(id)) {
                    updatedDirectGroupIds.add(id);
                }
            }
            // swap in the new data
            entry.setCachedData(cacheKey, updatedDirectGroupIds);
        }
    }
    return directGroups;
}
Also used : EntryCacheDataKey(com.zimbra.cs.account.EntryCacheDataKey) ArrayList(java.util.ArrayList) GroupedEntry(com.zimbra.cs.account.GroupedEntry) LdapDistributionList(com.zimbra.cs.account.ldap.entry.LdapDistributionList) ArrayList(java.util.ArrayList) List(java.util.List) DistributionList(com.zimbra.cs.account.DistributionList) LdapDistributionList(com.zimbra.cs.account.ldap.entry.LdapDistributionList) DistributionList(com.zimbra.cs.account.DistributionList)

Aggregations

DistributionList (com.zimbra.cs.account.DistributionList)1 EntryCacheDataKey (com.zimbra.cs.account.EntryCacheDataKey)1 GroupedEntry (com.zimbra.cs.account.GroupedEntry)1 LdapDistributionList (com.zimbra.cs.account.ldap.entry.LdapDistributionList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1