001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/msgcntr/trunk/messageforums-component-impl/src/java/org/sakaiproject/component/app/messageforums/MembershipManagerImpl.java $
003: * $Id: MembershipManagerImpl.java 9227 2006-05-15 15:02:42Z cwen@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006, 2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.component.app.messageforums;
021:
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.Collection;
025: import java.util.HashMap;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Set;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.sakaiproject.api.app.messageforums.MembershipManager;
035: import org.sakaiproject.api.privacy.PrivacyManager;
036: import org.sakaiproject.authz.api.AuthzGroup;
037: import org.sakaiproject.authz.api.AuthzGroupService;
038: import org.sakaiproject.authz.api.GroupNotDefinedException;
039: import org.sakaiproject.authz.api.Member;
040: import org.sakaiproject.authz.api.Role;
041: import org.sakaiproject.authz.api.SecurityService;
042: import org.sakaiproject.exception.IdUnusedException;
043: import org.sakaiproject.site.api.Group;
044: import org.sakaiproject.site.api.Site;
045: import org.sakaiproject.site.api.SiteService;
046: import org.sakaiproject.tool.api.ToolManager;
047: import org.sakaiproject.user.api.User;
048: import org.sakaiproject.user.api.UserDirectoryService;
049: import org.sakaiproject.user.api.UserNotDefinedException;
050:
051: public class MembershipManagerImpl implements MembershipManager {
052:
053: private static final Log LOG = LogFactory
054: .getLog(MembershipManagerImpl.class);
055:
056: private SiteService siteService;
057: private UserDirectoryService userDirectoryService;
058: private AuthzGroupService authzGroupService;
059: private ToolManager toolManager;
060: private SecurityService securityService;
061: private PrivacyManager privacyManager;
062:
063: public void init() {
064: LOG.info("init()");
065: ;
066: }
067:
068: /**
069: *
070: * @param privacyManager
071: */
072: public void setPrivacyManager(PrivacyManager privacyManager) {
073: this .privacyManager = privacyManager;
074: }
075:
076: /**
077: * sets users' privacy status so can be filtered out
078: *
079: * @param allCourseUsers - used to get user ids so can call PrivacyManager
080: * @param courseUserMap - map of all course users
081: *
082: * @return Map of all course users with privacy status set
083: */
084: private Map setPrivacyStatus(List allCourseUsers, Map courseUserMap) {
085:
086: List userIds = new ArrayList();
087: Map results = new HashMap();
088:
089: Collection userCollection = courseUserMap.values();
090:
091: for (Iterator usersIter = allCourseUsers.iterator(); usersIter
092: .hasNext();) {
093: MembershipItem memberItem = (MembershipItem) usersIter
094: .next();
095:
096: if (memberItem.getUser() != null) {
097: userIds.add(memberItem.getUser().getId());
098: }
099: }
100:
101: // set privacy status
102: Set memberSet = null;
103:
104: memberSet = privacyManager.findViewable(("/site/" + toolManager
105: .getCurrentPlacement().getContext()), new HashSet(
106: userIds));
107:
108: /** look through the members again to pick out Member objects corresponding
109: to only those who are visible (as well as current user) */
110: for (Iterator userIterator = userCollection.iterator(); userIterator
111: .hasNext();) {
112: MembershipItem memberItem = (MembershipItem) userIterator
113: .next();
114:
115: if (memberItem.getUser() != null) {
116: memberItem.setViewable(memberSet.contains(memberItem
117: .getUser().getId()));
118: } else {
119: // want groups to be displayed
120: memberItem.setViewable(true);
121: }
122:
123: results.put(memberItem.getId(), memberItem);
124: }
125:
126: return results;
127: }
128:
129: /**
130: * @see org.sakaiproject.api.app.messageforums.MembershipManager#getFilteredCourseMembers(boolean)
131: */
132: public Map getFilteredCourseMembers(boolean filterFerpa) {
133:
134: List allCourseUsers = getAllCourseUsers();
135:
136: Set membershipRoleSet = new HashSet();
137:
138: /** generate set of roles which has members */
139: for (Iterator i = allCourseUsers.iterator(); i.hasNext();) {
140: MembershipItem item = (MembershipItem) i.next();
141: if (item.getRole() != null) {
142: membershipRoleSet.add(item.getRole());
143: }
144: }
145:
146: /** filter member map */
147: Map memberMap = getAllCourseMembers(filterFerpa, true, true);
148:
149: // if (filterFerpa) {
150: // memberMap = setPrivacyStatus(allCourseUsers, memberMap);
151: // }
152:
153: for (Iterator i = memberMap.entrySet().iterator(); i.hasNext();) {
154:
155: Map.Entry entry = (Map.Entry) i.next();
156: MembershipItem item = (MembershipItem) entry.getValue();
157:
158: if (MembershipItem.TYPE_ROLE.equals(item.getType())) {
159: /** if no member belongs to role, filter role */
160: if (!membershipRoleSet.contains(item.getRole())) {
161: i.remove();
162: }
163: } else if (MembershipItem.TYPE_GROUP.equals(item.getType())) {
164: /** if no member belongs to group, filter group */
165: if (item.getGroup().getMembers().size() == 0) {
166: i.remove();
167: }
168: } else {
169: ;
170: }
171: }
172:
173: return memberMap;
174: }
175:
176: /**
177: * @see org.sakaiproject.api.app.messageforums.MembershipManager#getAllCourseMembers(boolean, boolean, boolean)
178: */
179: public Map getAllCourseMembers(boolean filterFerpa,
180: boolean includeRoles, boolean includeAllParticipantsMember) {
181: Map returnMap = new HashMap();
182: String realmId = getContextSiteId();
183: Site currentSite = null;
184:
185: /** add all participants */
186: if (includeAllParticipantsMember) {
187: MembershipItem memberAll = MembershipItem.getInstance();
188: memberAll.setType(MembershipItem.TYPE_ALL_PARTICIPANTS);
189: memberAll.setName(MembershipItem.ALL_PARTICIPANTS_DESC);
190: returnMap.put(memberAll.getId(), memberAll);
191: }
192:
193: AuthzGroup realm = null;
194: try {
195: realm = authzGroupService.getAuthzGroup(realmId);
196: currentSite = siteService.getSite(toolManager
197: .getCurrentPlacement().getContext());
198: } catch (IdUnusedException e) {
199: LOG.debug(e.getMessage(), e);
200: return returnMap;
201: } catch (GroupNotDefinedException e) {
202: // TODO Auto-generated catch block
203: e.printStackTrace();
204: }
205:
206: /** handle groups */
207: Collection groups = currentSite.getGroups();
208: for (Iterator groupIterator = groups.iterator(); groupIterator
209: .hasNext();) {
210: Group currentGroup = (Group) groupIterator.next();
211: MembershipItem member = MembershipItem.getInstance();
212: member.setType(MembershipItem.TYPE_GROUP);
213: member.setName(currentGroup.getTitle() + " Group");
214: member.setGroup(currentGroup);
215: returnMap.put(member.getId(), member);
216: }
217:
218: /** handle roles */
219: if (includeRoles && realm != null) {
220: Set roles = realm.getRoles();
221: for (Iterator roleIterator = roles.iterator(); roleIterator
222: .hasNext();) {
223: Role role = (Role) roleIterator.next();
224: MembershipItem member = MembershipItem.getInstance();
225: member.setType(MembershipItem.TYPE_ROLE);
226: String roleId = role.getId();
227: if (roleId != null && roleId.length() > 0) {
228: roleId = roleId.substring(0, 1).toUpperCase()
229: + roleId.substring(1);
230: }
231: member.setName(roleId + " Role");
232: member.setRole(role);
233: returnMap.put(member.getId(), member);
234: }
235: }
236:
237: /** handle users */
238: Set users = realm.getMembers();
239:
240: /** create our HashSet of user ids */
241: for (Iterator userIterator = users.iterator(); userIterator
242: .hasNext();) {
243: Member member = (Member) userIterator.next();
244: String userId = member.getUserId();
245: Role userRole = member.getRole();
246:
247: User user = null;
248: try {
249: if (realm.getMember(userId) != null
250: && realm.getMember(userId).isActive()) {
251: user = userDirectoryService.getUser(userId);
252: }
253: } catch (UserNotDefinedException e) {
254: // TODO Auto-generated catch block
255: // e.printStackTrace();
256: LOG.warn(" User " + userId + " not defined");
257: }
258:
259: if (user != null) {
260: MembershipItem memberItem = MembershipItem
261: .getInstance();
262: memberItem.setType(MembershipItem.TYPE_USER);
263: memberItem.setName(user.getSortName());
264: memberItem.setUser(user);
265: memberItem.setRole(userRole);
266:
267: // Don't want admin as part of the list
268: if (!(userId).equals("admin")) {
269: returnMap.put(memberItem.getId(), memberItem);
270: }
271: }
272: }
273:
274: // set FERPA status for all items in map - allCourseUsers
275: // needed by PrivacyManager to determine status
276: return setPrivacyStatus(getAllCourseUsers(), returnMap);
277: }
278:
279: /**
280: * @see org.sakaiproject.api.app.messageforums.MembershipManager#getAllCourseUsers()
281: */
282: public List getAllCourseUsers() {
283: Map userMap = new HashMap();
284: String realmId = getContextSiteId();
285:
286: AuthzGroup realm = null;
287: try {
288: realm = authzGroupService.getAuthzGroup(realmId);
289: } catch (GroupNotDefinedException e) {
290: // TODO Auto-generated catch block
291: e.printStackTrace();
292: }
293:
294: /** handle users */
295: Set users = realm.getMembers();
296: for (Iterator userIterator = users.iterator(); userIterator
297: .hasNext();) {
298: Member member = (Member) userIterator.next();
299: String userId = member.getUserId();
300: Role userRole = member.getRole();
301:
302: User user = null;
303: try {
304: if (realm.getMember(userId) != null
305: && realm.getMember(userId).isActive()) {
306: user = userDirectoryService.getUser(userId);
307: }
308: } catch (UserNotDefinedException e) {
309: // TODO Auto-generated catch block
310: // e.printStackTrace();
311: LOG.warn(" User " + userId + " not defined");
312: }
313:
314: if (user != null) {
315: MembershipItem memberItem = MembershipItem
316: .getInstance();
317: memberItem.setType(MembershipItem.TYPE_USER);
318: memberItem.setName(user.getSortName());
319: memberItem.setUser(user);
320: memberItem.setRole(userRole);
321:
322: if (!(userId).equals("admin")) {
323: userMap.put(memberItem.getId(), memberItem);
324: }
325: }
326: }
327:
328: return convertMemberMapToList(userMap);
329: }
330:
331: /**
332: * @see org.sakaiproject.api.app.messageforums.MembershipManager#convertMemberMapToList(java.util.Map)
333: */
334: public List convertMemberMapToList(Map memberMap) {
335:
336: MembershipItem[] membershipArray = new MembershipItem[memberMap
337: .size()];
338: membershipArray = (MembershipItem[]) memberMap.values()
339: .toArray(membershipArray);
340: Arrays.sort(membershipArray);
341:
342: return Arrays.asList(membershipArray);
343: }
344:
345: /**
346: * get site reference
347: * @return siteId
348: */
349: public String getContextSiteId() {
350: return ("/site/" + toolManager.getCurrentPlacement()
351: .getContext());
352: }
353:
354: /** setters */
355: public void setSiteService(SiteService siteService) {
356: this .siteService = siteService;
357: }
358:
359: public void setUserDirectoryService(
360: UserDirectoryService userDirectoryService) {
361: this .userDirectoryService = userDirectoryService;
362: }
363:
364: public void setAuthzGroupService(AuthzGroupService authzGroupService) {
365: this .authzGroupService = authzGroupService;
366: }
367:
368: public void setToolManager(ToolManager toolManager) {
369: this .toolManager = toolManager;
370: }
371:
372: public void setSecurityService(SecurityService securityService) {
373: this.securityService = securityService;
374: }
375:
376: }
|