/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
*/
package com.sun.portal.desktop.context;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sun.portal.community.mc.CMCException;
import com.sun.portal.community.mc.CMCNode;
import com.sun.portal.community.mc.CMCPrincipal;
import com.sun.portal.community.mc.ConfigTable;
import com.sun.portal.community.mc.CMCRolePrincipal;
import com.sun.portal.community.mc.CMCFactory;
import com.sun.portal.desktop.context.DesktopAppContext;
import com.sun.portal.log.common.PortalLogger;
public class CommunityDesktopMembership {
private static Logger logger = PortalLogger.getLogger(CommunityDesktopMembership.class);
public static Set getDesktopMembership(Set realMembership, Set currentCommunityPrincipals) throws CMCException {
logger.log(Level.FINEST, "PSDT_CSPDC0039", realMembership);
logger.log(Level.FINEST, "PSDT_CSPDC0040", currentCommunityPrincipals);
//
// get the special cmc contributor as set in propertirs file
// it is set to "jdo".
DesktopAppContext desktopAppContext =
DesktopAppContextThreadLocalizer.get();
List communityContributorTypes =
desktopAppContext.getCommunityContributorTypes();
// Make a copy of realMmebership.
// This is what we'll return when we are done
// manipulating.
Set effectiveMembership = Collections.EMPTY_SET;
if ( realMembership != null) {
effectiveMembership = new TreeSet(realMembership);
}
// We need to find out the communities in currentCommunityPrincipals
// for which current user needs implicit VISITOR role.
// Here is how we'll find it.
// We are initializing visitorCommunities with copying the
// currentCommunityPrincipals.
// As we go through the realMembership, we remove all entries for
// which user has explicit role
// so that we are left only with communities for which we need to
// arrange visitor roles.
Set workingVisitorCommunities = Collections.EMPTY_SET;
if ( currentCommunityPrincipals != null) {
workingVisitorCommunities = new TreeSet(currentCommunityPrincipals);
}
//
// A Map that keeps the roles where user has explicit membership
//
Map realMembershipInCurrentCommunities = new HashMap();
//
// Go through the realMmebership that has am and jdo membership
//
Iterator iterator = realMembership.iterator();
while (iterator.hasNext()) {
ConfigTable.ConfigKey configKey =
(ConfigTable.ConfigKey) iterator.next();
if (communityContributorTypes.contains(
configKey.getCommunityPrincipal().getType())) {
// kill all the jdo related memberhsips from its copy
// called effectiveMembership.
// By the way, "jdo" type is not
// hardcoded it is returned from
// desktopAppContext.getCommunityContributorTypes.
effectiveMembership.remove(configKey);
// While we are killing jdo memberships in the
// effectiveMembership - find out if anyone
// of them belonged to our interested communities in
// currentCommunityPrincipals.
if ((currentCommunityPrincipals != null) &&
(currentCommunityPrincipals.contains(
configKey.getCommunityPrincipal().toString()))) {
// Store all roles for any such community in a separate
// Map, whose key is the community principal.
// This map is realMembershipInCurrentCommunities.
// We are going to process it once we are done
// iterating all the realMembership elements.
Set temp = (Set)realMembershipInCurrentCommunities.get(
configKey.getCommunityPrincipal());
if ( temp == null ) {
temp = new TreeSet();
realMembershipInCurrentCommunities.put(
configKey.getCommunityPrincipal(), temp);
}
temp.add(configKey);
// Since there is real membership for this community,
// it doesn't need
// implicit VISITOR role, so lets remove it from
// visitorCommunities.
workingVisitorCommunities.remove(
configKey.getCommunityPrincipal().toString());
}
}
}
// For communities with real membership - get the effective membership
// and add them to big bucket of effectiveMembership that we plan to
// return
Iterator iter = realMembershipInCurrentCommunities.keySet().iterator();
while(iter.hasNext()) {
CMCPrincipal community = (CMCPrincipal) iter.next();
Set temp = (Set) realMembershipInCurrentCommunities.get(community);
effectiveMembership.addAll(
getEffectiveMembershipForSingleCommunity(community, temp));
}
//
// For communities that need VISITOR role, first get all the community
// roles add the visitor role and then let the same logic run as above.
iter = workingVisitorCommunities.iterator();
CMCFactory cf = CMCFactory.getInstance();
while ( iter.hasNext()) {
CMCPrincipal communityPrincipal =
new CMCPrincipal((String) iter.next());
CMCNode communityNode = cf.getCMCNode(communityPrincipal);
Set temp = communityNode.getRoles();
Set configKeySet = new TreeSet();
if ( temp != null) {
Iterator tempIter = temp.iterator();
while(tempIter.hasNext()) {
CMCRolePrincipal role = (CMCRolePrincipal) tempIter.next();
configKeySet.add(
new ConfigTable.ConfigKey(communityPrincipal, role));
}
}
effectiveMembership.addAll(
getEffectiveMembershipForSingleCommunity(communityPrincipal,
configKeySet));
}
logger.log(Level.FINEST, "PSDT_CSPDC0041", effectiveMembership);
return (effectiveMembership.size() == 0) ? null : effectiveMembership;
}
private static Set getEffectiveMembershipForSingleCommunity(CMCPrincipal communityPrincipal, Set configKeys) {
ConfigTable.ConfigKey deletedConfigKey = null;
ConfigTable.ConfigKey disabledConfigKey = null;
ConfigTable.ConfigKey bannedConfigKey = null;
ConfigTable.ConfigKey memberConfigKey = null;
if ( configKeys != null && configKeys.size() != 0) {
Iterator iterator = configKeys.iterator();
while (configKeys != null && iterator.hasNext()) {
ConfigTable.ConfigKey configKey =
(ConfigTable.ConfigKey)iterator.next();
CMCRolePrincipal role = configKey.getRolePrincipal();
if (role.equals(CMCRolePrincipal.DELETED_ROLE)) {
deletedConfigKey = configKey;
}
else if (role.equals(CMCRolePrincipal.DISABLED_ROLE)) {
disabledConfigKey = configKey;
}
else if (role.equals(CMCRolePrincipal.BANNED_ROLE)) {
bannedConfigKey = configKey;
}
else if ( role.equals(CMCRolePrincipal.MEMBER_ROLE)) {
memberConfigKey = configKey;
}
}
}
Set resultSet = new TreeSet();
//
// whenever these roles are there, we dont send any other roles
//
if ( deletedConfigKey != null) {
resultSet.add(deletedConfigKey);
}
else if ( disabledConfigKey != null) {
resultSet.add(disabledConfigKey);
}
else if ( bannedConfigKey != null) {
//unless a community is disabled/deleted -
// it should always let everyone see the
// visitor view.
resultSet.add(bannedConfigKey);
resultSet.add(getVisitorConfigKey(communityPrincipal));
}
//
// whenever above roles are there, we dont send any other roles
// For the remaining normal cases ( normal visitor or normal member)
// we do the following
if ( resultSet.size() == 0) {
//
// add visitor role if not a member
// unless a community is disabled/deleted - it should always
// let everyone see the visitor view.
//
if (memberConfigKey == null) {
resultSet.add(getVisitorConfigKey(communityPrincipal));
}
//
// send whatever else is there
//
if ( configKeys != null) {
resultSet.addAll(configKeys);
}
}
logger.log(Level.FINEST, "PSDT_CSPDC0042", communityPrincipal + ":" + configKeys);
logger.log(Level.FINEST, "PSDT_CSPDC0043", communityPrincipal + ":" + resultSet);
return resultSet;
}
private static ConfigTable.ConfigKey getVisitorConfigKey(CMCPrincipal community) {
return new ConfigTable.ConfigKey(community, CMCRolePrincipal.VISITOR_ROLE);
}
}
|