001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.context;
006:
007: import java.util.Set;
008: import java.util.Map;
009: import java.util.HashMap;
010: import java.util.Collections;
011: import java.util.Iterator;
012: import java.util.Set;
013: import java.util.TreeSet;
014: import java.util.List;
015: import java.util.logging.Level;
016: import java.util.logging.Logger;
017:
018: import com.sun.portal.community.mc.CMCException;
019: import com.sun.portal.community.mc.CMCNode;
020: import com.sun.portal.community.mc.CMCPrincipal;
021: import com.sun.portal.community.mc.ConfigTable;
022: import com.sun.portal.community.mc.CMCRolePrincipal;
023: import com.sun.portal.community.mc.CMCFactory;
024:
025: import com.sun.portal.desktop.context.DesktopAppContext;
026: import com.sun.portal.log.common.PortalLogger;
027:
028: public class CommunityDesktopMembership {
029:
030: private static Logger logger = PortalLogger
031: .getLogger(CommunityDesktopMembership.class);
032:
033: public static Set getDesktopMembership(Set realMembership,
034: Set currentCommunityPrincipals) throws CMCException {
035:
036: logger.log(Level.FINEST, "PSDT_CSPDC0039", realMembership);
037: logger.log(Level.FINEST, "PSDT_CSPDC0040",
038: currentCommunityPrincipals);
039:
040: //
041: // get the special cmc contributor as set in propertirs file
042: // it is set to "jdo".
043:
044: DesktopAppContext desktopAppContext = DesktopAppContextThreadLocalizer
045: .get();
046: List communityContributorTypes = desktopAppContext
047: .getCommunityContributorTypes();
048:
049: // Make a copy of realMmebership.
050: // This is what we'll return when we are done
051: // manipulating.
052:
053: Set effectiveMembership = Collections.EMPTY_SET;
054: if (realMembership != null) {
055: effectiveMembership = new TreeSet(realMembership);
056: }
057:
058: // We need to find out the communities in currentCommunityPrincipals
059: // for which current user needs implicit VISITOR role.
060: // Here is how we'll find it.
061: // We are initializing visitorCommunities with copying the
062: // currentCommunityPrincipals.
063: // As we go through the realMembership, we remove all entries for
064: // which user has explicit role
065: // so that we are left only with communities for which we need to
066: // arrange visitor roles.
067:
068: Set workingVisitorCommunities = Collections.EMPTY_SET;
069: if (currentCommunityPrincipals != null) {
070: workingVisitorCommunities = new TreeSet(
071: currentCommunityPrincipals);
072: }
073:
074: //
075: // A Map that keeps the roles where user has explicit membership
076: //
077:
078: Map realMembershipInCurrentCommunities = new HashMap();
079:
080: //
081: // Go through the realMmebership that has am and jdo membership
082: //
083:
084: Iterator iterator = realMembership.iterator();
085: while (iterator.hasNext()) {
086:
087: ConfigTable.ConfigKey configKey = (ConfigTable.ConfigKey) iterator
088: .next();
089:
090: if (communityContributorTypes.contains(configKey
091: .getCommunityPrincipal().getType())) {
092:
093: // kill all the jdo related memberhsips from its copy
094: // called effectiveMembership.
095: // By the way, "jdo" type is not
096: // hardcoded it is returned from
097: // desktopAppContext.getCommunityContributorTypes.
098:
099: effectiveMembership.remove(configKey);
100:
101: // While we are killing jdo memberships in the
102: // effectiveMembership - find out if anyone
103: // of them belonged to our interested communities in
104: // currentCommunityPrincipals.
105:
106: if ((currentCommunityPrincipals != null)
107: && (currentCommunityPrincipals
108: .contains(configKey
109: .getCommunityPrincipal()
110: .toString()))) {
111:
112: // Store all roles for any such community in a separate
113: // Map, whose key is the community principal.
114: // This map is realMembershipInCurrentCommunities.
115: // We are going to process it once we are done
116: // iterating all the realMembership elements.
117:
118: Set temp = (Set) realMembershipInCurrentCommunities
119: .get(configKey.getCommunityPrincipal());
120: if (temp == null) {
121: temp = new TreeSet();
122: realMembershipInCurrentCommunities
123: .put(configKey.getCommunityPrincipal(),
124: temp);
125: }
126: temp.add(configKey);
127:
128: // Since there is real membership for this community,
129: // it doesn't need
130: // implicit VISITOR role, so lets remove it from
131: // visitorCommunities.
132:
133: workingVisitorCommunities.remove(configKey
134: .getCommunityPrincipal().toString());
135: }
136: }
137: }
138:
139: // For communities with real membership - get the effective membership
140: // and add them to big bucket of effectiveMembership that we plan to
141: // return
142:
143: Iterator iter = realMembershipInCurrentCommunities.keySet()
144: .iterator();
145: while (iter.hasNext()) {
146: CMCPrincipal community = (CMCPrincipal) iter.next();
147: Set temp = (Set) realMembershipInCurrentCommunities
148: .get(community);
149: effectiveMembership
150: .addAll(getEffectiveMembershipForSingleCommunity(
151: community, temp));
152: }
153:
154: //
155: // For communities that need VISITOR role, first get all the community
156: // roles add the visitor role and then let the same logic run as above.
157:
158: iter = workingVisitorCommunities.iterator();
159:
160: CMCFactory cf = CMCFactory.getInstance();
161: while (iter.hasNext()) {
162: CMCPrincipal communityPrincipal = new CMCPrincipal(
163: (String) iter.next());
164: CMCNode communityNode = cf.getCMCNode(communityPrincipal);
165: Set temp = communityNode.getRoles();
166: Set configKeySet = new TreeSet();
167: if (temp != null) {
168: Iterator tempIter = temp.iterator();
169: while (tempIter.hasNext()) {
170: CMCRolePrincipal role = (CMCRolePrincipal) tempIter
171: .next();
172: configKeySet.add(new ConfigTable.ConfigKey(
173: communityPrincipal, role));
174: }
175: }
176:
177: effectiveMembership
178: .addAll(getEffectiveMembershipForSingleCommunity(
179: communityPrincipal, configKeySet));
180:
181: }
182:
183: logger.log(Level.FINEST, "PSDT_CSPDC0041", effectiveMembership);
184:
185: return (effectiveMembership.size() == 0) ? null
186: : effectiveMembership;
187:
188: }
189:
190: private static Set getEffectiveMembershipForSingleCommunity(
191: CMCPrincipal communityPrincipal, Set configKeys) {
192:
193: ConfigTable.ConfigKey deletedConfigKey = null;
194: ConfigTable.ConfigKey disabledConfigKey = null;
195: ConfigTable.ConfigKey bannedConfigKey = null;
196: ConfigTable.ConfigKey memberConfigKey = null;
197:
198: if (configKeys != null && configKeys.size() != 0) {
199: Iterator iterator = configKeys.iterator();
200: while (configKeys != null && iterator.hasNext()) {
201: ConfigTable.ConfigKey configKey = (ConfigTable.ConfigKey) iterator
202: .next();
203: CMCRolePrincipal role = configKey.getRolePrincipal();
204: if (role.equals(CMCRolePrincipal.DELETED_ROLE)) {
205: deletedConfigKey = configKey;
206: } else if (role.equals(CMCRolePrincipal.DISABLED_ROLE)) {
207: disabledConfigKey = configKey;
208: } else if (role.equals(CMCRolePrincipal.BANNED_ROLE)) {
209: bannedConfigKey = configKey;
210: } else if (role.equals(CMCRolePrincipal.MEMBER_ROLE)) {
211: memberConfigKey = configKey;
212: }
213: }
214: }
215:
216: Set resultSet = new TreeSet();
217:
218: //
219: // whenever these roles are there, we dont send any other roles
220: //
221: if (deletedConfigKey != null) {
222: resultSet.add(deletedConfigKey);
223: } else if (disabledConfigKey != null) {
224: resultSet.add(disabledConfigKey);
225: } else if (bannedConfigKey != null) {
226: //unless a community is disabled/deleted -
227: // it should always let everyone see the
228: // visitor view.
229: resultSet.add(bannedConfigKey);
230: resultSet.add(getVisitorConfigKey(communityPrincipal));
231: }
232:
233: //
234: // whenever above roles are there, we dont send any other roles
235: // For the remaining normal cases ( normal visitor or normal member)
236: // we do the following
237:
238: if (resultSet.size() == 0) {
239:
240: //
241: // add visitor role if not a member
242: // unless a community is disabled/deleted - it should always
243: // let everyone see the visitor view.
244: //
245: if (memberConfigKey == null) {
246: resultSet.add(getVisitorConfigKey(communityPrincipal));
247: }
248: //
249: // send whatever else is there
250: //
251: if (configKeys != null) {
252: resultSet.addAll(configKeys);
253: }
254: }
255:
256: logger.log(Level.FINEST, "PSDT_CSPDC0042", communityPrincipal
257: + ":" + configKeys);
258: logger.log(Level.FINEST, "PSDT_CSPDC0043", communityPrincipal
259: + ":" + resultSet);
260: return resultSet;
261:
262: }
263:
264: private static ConfigTable.ConfigKey getVisitorConfigKey(
265: CMCPrincipal community) {
266: return new ConfigTable.ConfigKey(community,
267: CMCRolePrincipal.VISITOR_ROLE);
268: }
269:
270: }
|