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/SectionRoleResolver.java $
003: * $Id: SectionRoleResolver.java 20714 2007-01-26 19:47:36Z 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.Enrollment;
032: import org.sakaiproject.coursemanagement.api.EnrollmentSet;
033: import org.sakaiproject.coursemanagement.api.Membership;
034: import org.sakaiproject.coursemanagement.api.Section;
035:
036: /**
037: * Resolves users roles in sections.
038: *
039: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
040: *
041: */
042: public class SectionRoleResolver implements RoleResolver {
043: private static final Log log = LogFactory
044: .getLog(SectionRoleResolver.class);
045:
046: /** Map of CM section roles to Sakai roles */
047: protected Map<String, String> roleMap;
048:
049: /** The Sakai role to use for official instructors of EnrollmentSets */
050: protected String officialInstructorRole;
051:
052: /** The Sakai roles to use for official enrollments in EnrollmentSets,keyed on the enrollment status */
053: protected Map<String, String> enrollmentStatusRoleMap;
054:
055: /**
056: * {@inheritDoc}
057: */
058: public Map<String, String> getUserRoles(
059: CourseManagementService cmService, Section section) {
060: Map<String, String> userRoleMap = new HashMap<String, String>();
061:
062: EnrollmentSet enrSet = section.getEnrollmentSet();
063: if (log.isDebugEnabled())
064: log.debug("EnrollmentSet " + enrSet
065: + " is attached to section " + section.getEid());
066: if (enrSet != null) {
067: // Check for official instructors
068: Set<String> officialInstructors = cmService
069: .getInstructorsOfRecordIds(enrSet.getEid());
070: for (Iterator<String> iter = officialInstructors.iterator(); iter
071: .hasNext();) {
072: userRoleMap.put(iter.next(), officialInstructorRole);
073: }
074:
075: // Check for enrollments
076: Set enrollments = cmService.getEnrollments(section
077: .getEnrollmentSet().getEid());
078: for (Iterator iter = enrollments.iterator(); iter.hasNext();) {
079: Enrollment enr = (Enrollment) iter.next();
080: if (enr.isDropped()) {
081: continue;
082: }
083: String roleFromEnrollmentStatus = (String) enrollmentStatusRoleMap
084: .get(enr.getEnrollmentStatus());
085:
086: // Only add the enrollment if it's not dropped and it has an enrollment role mapping
087: // Defer to the official instructor status
088: if (!userRoleMap.containsKey(enr.getUserId())
089: && roleFromEnrollmentStatus != null
090: && !enr.isDropped()) {
091: userRoleMap.put(enr.getUserId(),
092: roleFromEnrollmentStatus);
093: }
094: }
095: }
096:
097: // Check for memberships
098: Set memberships = cmService.getSectionMemberships(section
099: .getEid());
100: for (Iterator iter = memberships.iterator(); iter.hasNext();) {
101: Membership membership = (Membership) iter.next();
102: // Only add the membership role if the user isn't enrolled or an official instructor(these take precedence)
103: if (!userRoleMap.containsKey(membership.getUserId())) {
104: String convertedRole = convertRole(membership.getRole());
105: if (convertedRole != null) {
106: userRoleMap.put(membership.getUserId(),
107: convertedRole);
108: }
109: }
110: }
111: return userRoleMap;
112: }
113:
114: public Map<String, String> getGroupRoles(
115: CourseManagementService cmService, String userEid) {
116: // Start with the sectionEid->role map
117: Map<String, String> groupRoleMap = cmService
118: .findSectionRoles(userEid);
119:
120: // Convert these roles to Sakai roles
121: Set<String> iterSet = new HashSet<String>(groupRoleMap.keySet());
122: for (Iterator iter = iterSet.iterator(); iter.hasNext();) {
123: String key = (String) iter.next();
124: groupRoleMap.put(key, convertRole((String) groupRoleMap
125: .get(key)));
126: }
127:
128: // Next add all enrollments to the sectionEid->role map, overriding memberships
129: Set enrolledSections = cmService.findEnrolledSections(userEid);
130: if (log.isDebugEnabled())
131: log.debug("Found " + enrolledSections.size()
132: + " currently enrolled sections for user "
133: + userEid);
134: for (Iterator secIter = enrolledSections.iterator(); secIter
135: .hasNext();) {
136: Section section = (Section) secIter.next();
137: if (log.isDebugEnabled())
138: log
139: .debug(userEid
140: + " is enrolled in an enrollment set attached to section "
141: + section.getEid());
142: // TODO Calling this for every section is inefficient -- add new method to CM service?
143: Enrollment enr = cmService.findEnrollment(userEid, section
144: .getEnrollmentSet().getEid());
145: String roleFromEnrollmentStatus = enrollmentStatusRoleMap
146: .get(enr.getEnrollmentStatus());
147:
148: // Only add the enrollment if it's not dropped and it has an enrollment role mapping
149: if (roleFromEnrollmentStatus != null && !enr.isDropped()) {
150: groupRoleMap.put(section.getEid(),
151: roleFromEnrollmentStatus);
152: }
153: }
154:
155: // Finally, add the official instructors, overriding any other roles if necessary
156: Set instructingSections = cmService
157: .findInstructingSections(userEid);
158: for (Iterator iter = instructingSections.iterator(); iter
159: .hasNext();) {
160: Section instructingSection = (Section) iter.next();
161: groupRoleMap.put(instructingSection.getEid(),
162: officialInstructorRole);
163: }
164:
165: if (log.isDebugEnabled()) {
166: for (Iterator iter = groupRoleMap.keySet().iterator(); iter
167: .hasNext();) {
168: String sectionEid = (String) iter.next();
169: log.debug("User " + userEid + " has role "
170: + groupRoleMap.get(sectionEid) + " in "
171: + sectionEid);
172: }
173: }
174: return groupRoleMap;
175: }
176:
177: public String convertRole(String cmRole) {
178: if (cmRole == null) {
179: log.warn("Can not convert CM role 'null' to a sakai role.");
180: return null;
181: }
182: String sakaiRole = (String) roleMap.get(cmRole);
183: if (sakaiRole == null) {
184: log.warn("Unable to find sakai role for CM role " + cmRole);
185: return null;
186: } else {
187: return sakaiRole;
188: }
189: }
190:
191: // Dependency injection
192:
193: public void setRoleMap(Map<String, String> roleMap) {
194: this .roleMap = roleMap;
195: }
196:
197: public void setOfficialInstructorRole(String officialInstructorRole) {
198: this .officialInstructorRole = officialInstructorRole;
199: }
200:
201: public void setEnrollmentStatusRoleMap(
202: Map<String, String> enrollmentStatusRoleMap) {
203: this.enrollmentStatusRoleMap = enrollmentStatusRoleMap;
204: }
205: }
|