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/CourseOfferingRoleResolver.java $
003: * $Id: CourseOfferingRoleResolver.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.CourseManagementService;
031: import org.sakaiproject.coursemanagement.api.CourseOffering;
032: import org.sakaiproject.coursemanagement.api.Membership;
033: import org.sakaiproject.coursemanagement.api.Section;
034:
035: /**
036: * Resolves user roles in CourseOfferings.
037: *
038: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
039: *
040: */
041: public class CourseOfferingRoleResolver implements RoleResolver {
042: private static final Log log = LogFactory
043: .getLog(CourseOfferingRoleResolver.class);
044:
045: /** Map of CM course offering roles to Sakai roles */
046: Map roleMap;
047:
048: /**
049: * {@inheritDoc}
050: */
051: public Map<String, String> getUserRoles(
052: CourseManagementService cmService, Section section) {
053: Map<String, String> userRoleMap = new HashMap<String, String>();
054: String coEid = section.getCourseOfferingEid();
055:
056: // Get the members of this course offering
057: Set<Membership> coMembers = cmService
058: .getCourseOfferingMemberships(coEid);
059: Set<Membership> equivMembers = new HashSet<Membership>();
060:
061: // Get the members of equivalent course offerings, and add them to the set of equivalent memberships
062: Set<CourseOffering> equivalentCourseOfferings = cmService
063: .getEquivalentCourseOfferings(coEid);
064: for (Iterator<CourseOffering> iter = equivalentCourseOfferings
065: .iterator(); iter.hasNext();) {
066: CourseOffering equivCo = iter.next();
067: equivMembers.addAll(cmService
068: .getCourseOfferingMemberships(equivCo.getEid()));
069: }
070:
071: // Add the course offering memebers
072: addMemberRoles(userRoleMap, coMembers);
073:
074: // Add the equivalent course offering members (but don't override any roles in the original course offering)
075: addMemberRoles(userRoleMap, equivMembers);
076:
077: if (coMembers != null) {
078: }
079: return userRoleMap;
080: }
081:
082: private void addMemberRoles(Map<String, String> userRoleMap,
083: Set<Membership> coMembers) {
084: for (Iterator<Membership> iter = coMembers.iterator(); iter
085: .hasNext();) {
086: Membership membership = iter.next();
087: if (userRoleMap.containsKey(membership.getUserId())) {
088: // Don't override existing roles in the map.
089: } else {
090: String sakaiRole = convertRole(membership.getRole());
091: if (sakaiRole != null) {
092: userRoleMap.put(membership.getUserId(), sakaiRole);
093: }
094: }
095: }
096: }
097:
098: /**
099: * {@inheritDoc}
100: */
101: public Map<String, String> getGroupRoles(
102: CourseManagementService cmService, String userEid) {
103: Map<String, String> sectionRoles = new HashMap<String, String>();
104:
105: // Find all of the course offerings for which this user is a member
106: Map<String, String> courseOfferingRoles = cmService
107: .findCourseOfferingRoles(userEid);
108: if (log.isDebugEnabled())
109: log.debug("Found " + courseOfferingRoles.size()
110: + " course offering roles for " + userEid);
111:
112: // Add all of the equivalent course offerings-> role mappings for this user
113: Set<String> coEids = new HashSet(courseOfferingRoles.keySet());
114: for (Iterator<String> coIter = coEids.iterator(); coIter
115: .hasNext();) {
116: String coEid = coIter.next();
117: Set<CourseOffering> equivOfferings = cmService
118: .getEquivalentCourseOfferings(coEid);
119: for (Iterator<CourseOffering> equivIter = equivOfferings
120: .iterator(); equivIter.hasNext();) {
121: CourseOffering equiv = equivIter.next();
122: // Use the role in the original course offering for this equivalent course offering
123: courseOfferingRoles.put(equiv.getEid(),
124: courseOfferingRoles.get(coEid));
125: }
126: }
127:
128: for (Iterator<String> coIter = courseOfferingRoles.keySet()
129: .iterator(); coIter.hasNext();) {
130: String coEid = coIter.next();
131: String coRole = courseOfferingRoles.get(coEid);
132:
133: // If this role shouldn't be part of the site, ignore the membership in this course offering
134: String sakaiRole = convertRole(coRole);
135: if (sakaiRole == null) {
136: if (log.isDebugEnabled())
137: log
138: .debug("Course offering role "
139: + coRole
140: + " is not mapped to a sakai role. Skipping this membership.");
141: continue;
142: }
143:
144: if (log.isDebugEnabled())
145: log.debug(userEid + " has role=" + coRole
146: + " in course offering " + coEid);
147: // Get the sections in each course offering
148: Set<Section> sections = cmService.getSections(coEid);
149: for (Iterator<Section> secIter = sections.iterator(); secIter
150: .hasNext();) {
151: // Add the section EIDs and the converted *CourseOffering* role to the sectionRoles map
152: Section section = secIter.next();
153: sectionRoles.put(section.getEid(), sakaiRole);
154: }
155: }
156: return sectionRoles;
157: }
158:
159: public String convertRole(String cmRole) {
160: if (cmRole == null) {
161: log.warn("Can not convert CM role 'null' to a sakai role.");
162: return null;
163: }
164: String sakaiRole = (String) roleMap.get(cmRole);
165: if (sakaiRole == null) {
166: log.warn("Unable to find sakai role for CM role " + cmRole);
167: return null;
168: } else {
169: return sakaiRole;
170: }
171: }
172:
173: // Dependency injection
174:
175: public void setRoleMap(Map roleMap) {
176: this.roleMap = roleMap;
177: }
178:
179: }
|