001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/providers/tags/sakai_2-4-1/cm/cm-authz-provider/src/java/org/sakaiproject/coursemanagement/impl/provider/CourseSetRoleResolver.java $
003: * $Id: CourseSetRoleResolver.java 20453 2007-01-19 00:26:56Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006 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.coursemanagement.impl.provider;
021:
022: import java.util.HashMap;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.coursemanagement.api.CanonicalCourse;
031: import org.sakaiproject.coursemanagement.api.CourseManagementService;
032: import org.sakaiproject.coursemanagement.api.CourseOffering;
033: import org.sakaiproject.coursemanagement.api.Membership;
034: import org.sakaiproject.coursemanagement.api.Section;
035: import org.sakaiproject.coursemanagement.api.exception.IdNotFoundException;
036:
037: /**
038: * Resolves user roles in CourseOfferings.
039: *
040: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
041: *
042: */
043: public class CourseSetRoleResolver implements RoleResolver {
044: private static final Log log = LogFactory
045: .getLog(CourseSetRoleResolver.class);
046:
047: /** Map of CM course set roles to Sakai roles */
048: Map roleMap;
049:
050: /**
051: * {@inheritDoc}
052: */
053: public Map<String, String> getUserRoles(
054: CourseManagementService cmService, Section section) {
055: Map<String, String> userRoleMap = new HashMap<String, String>();
056:
057: Set csEids = getCourseSetEids(cmService, section);
058: if (csEids.isEmpty()) {
059: if (log.isDebugEnabled())
060: log
061: .debug("There are no course sets associated with section "
062: + section.getEid());
063: return new HashMap<String, String>();
064: }
065:
066: // Iterate over the course set EIDs. If the user is a member of any of the
067: // course sets, add that role to the map.
068:
069: // TODO: We need to account for cases where a user has different roles in multiple course sets.
070:
071: for (Iterator iter = csEids.iterator(); iter.hasNext();) {
072: String eid = (String) iter.next();
073: Set csMembers = cmService.getCourseSetMemberships(eid);
074: if (csMembers == null) {
075: if (log.isDebugEnabled())
076: log.debug("CourseSet " + eid
077: + " has a null set of members");
078: continue;
079: }
080: if (log.isDebugEnabled())
081: log.debug("CourseSet " + eid + " has "
082: + csMembers.size() + " members");
083: for (Iterator memberIter = csMembers.iterator(); memberIter
084: .hasNext();) {
085: Membership membership = (Membership) memberIter.next();
086: String sakaiRole = convertRole(membership.getRole());
087: if (sakaiRole != null) {
088: if (log.isDebugEnabled())
089: log.debug("Adding user "
090: + membership.getUserId()
091: + " to userRoleMap with role "
092: + sakaiRole);
093: userRoleMap.put(membership.getUserId(), sakaiRole);
094: }
095: }
096: }
097: return userRoleMap;
098: }
099:
100: /**
101: * {@inheritDoc}
102: */
103: public Map<String, String> getGroupRoles(
104: CourseManagementService cmService, String userEid) {
105: Map<String, String> sectionRoles = new HashMap<String, String>();
106: Map courseSetRoles = cmService.findCourseSetRoles(userEid);
107:
108: // Look at each of the course sets for which this user has a role
109: for (Iterator csIter = courseSetRoles.keySet().iterator(); csIter
110: .hasNext();) {
111: String csEid = (String) csIter.next();
112: String csRole = (String) courseSetRoles.get(csEid);
113:
114: // If this course set role shouldn't be added to the site, ignore this course set
115: String sakaiRole = convertRole(csRole);
116: if (sakaiRole == null) {
117: continue;
118: }
119:
120: // Look at each of the course offerings in the course set
121: Set courseOfferings = cmService
122: .getCourseOfferingsInCourseSet(csEid);
123: for (Iterator coIter = courseOfferings.iterator(); coIter
124: .hasNext();) {
125: CourseOffering co = (CourseOffering) coIter.next();
126: // Get the sections in each course offering
127: Set sections = cmService.getSections(co.getEid());
128: for (Iterator secIter = sections.iterator(); secIter
129: .hasNext();) {
130: // Add the section EIDs and *CourseSet* role to the sectionRoles map
131: Section section = (Section) secIter.next();
132: sectionRoles.put(section.getEid(), sakaiRole);
133: }
134: }
135: }
136: return sectionRoles;
137: }
138:
139: private Set<String> getCourseSetEids(
140: CourseManagementService cmService, Section section) {
141: // Look up the hierarchy for any course sets
142: CourseOffering co;
143: CanonicalCourse cc;
144: try {
145: co = cmService.getCourseOffering(section
146: .getCourseOfferingEid());
147: cc = cmService.getCanonicalCourse(co
148: .getCanonicalCourseEid());
149: } catch (IdNotFoundException ide) {
150: if (log.isDebugEnabled())
151: log.debug("Unable to find CM objects: " + ide);
152: return new HashSet<String>();
153: }
154:
155: if (log.isDebugEnabled())
156: log.debug("Found course offering " + co);
157: if (log.isDebugEnabled())
158: log.debug("Found canonical course " + cc);
159:
160: // Now that we have the CourseOffering, check for cross-listed courses
161: Set xListedCourseOfferings = cmService
162: .getEquivalentCourseOfferings(co.getEid());
163: Set xListedCanonCourses = cmService
164: .getEquivalentCanonicalCourses(cc.getEid());
165:
166: // Collect all of the CourseSet EIDs connected to this course or an equivalent
167: Set<String> csEids = co.getCourseSetEids();
168: if (log.isDebugEnabled())
169: log.debug("Course offering " + co.getEid()
170: + " is a member of " + csEids.size()
171: + " course sets");
172:
173: // Collect all of the CourseSet EIDs for which these cross listed course offerings are a member
174: for (Iterator coIter = xListedCourseOfferings.iterator(); coIter
175: .hasNext();) {
176: CourseOffering xListCo = (CourseOffering) coIter.next();
177: String xListCcEid = xListCo.getCanonicalCourseEid();
178: CanonicalCourse xListCc = cmService
179: .getCanonicalCourse(xListCcEid);
180: csEids.addAll(xListCc.getCourseSetEids());
181: }
182: for (Iterator ccIter = xListedCanonCourses.iterator(); ccIter
183: .hasNext();) {
184: CanonicalCourse xListCc = (CanonicalCourse) ccIter.next();
185: csEids.addAll(xListCc.getCourseSetEids());
186: }
187: if (log.isDebugEnabled())
188: log.debug("Found " + csEids.size()
189: + " course sets for section " + section.getEid());
190: return csEids;
191: }
192:
193: public String convertRole(String cmRole) {
194: if (cmRole == null) {
195: log.warn("Can not convert CM role 'null' to a sakai role.");
196: return null;
197: }
198: String sakaiRole = (String) roleMap.get(cmRole);
199: if (sakaiRole == null) {
200: log.warn("Unable to find sakai role for CM role " + cmRole);
201: return null;
202: } else {
203: return sakaiRole;
204: }
205: }
206:
207: // Dependency injection
208:
209: public void setRoleMap(Map roleMap) {
210: this.roleMap = roleMap;
211: }
212: }
|