001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-impl/standalone/src/java/org/sakaiproject/component/section/SectionEnrollmentsImpl.java $
003: * $Id: SectionEnrollmentsImpl.java 18134 2006-11-14 18:59:25Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Regents of the University of California and The Regents of the University of Michigan
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.section;
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import org.apache.commons.lang.builder.ToStringBuilder;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.sakaiproject.section.api.coursemanagement.CourseSection;
032: import org.sakaiproject.section.api.coursemanagement.EnrollmentRecord;
033: import org.sakaiproject.section.api.coursemanagement.SectionEnrollments;
034:
035: /**
036: * A data structure that keeps the UI layer from needing to know how the
037: * SectionManager service packages up the section enrollment information for
038: * a course.
039: *
040: * This is an effort to aviod MOP (Map Oriented Programming).
041: *
042: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
043: *
044: */
045: public class SectionEnrollmentsImpl implements SectionEnrollments {
046: private static Log log = LogFactory
047: .getLog(SectionEnrollmentsImpl.class);
048:
049: protected Map studentToMap;
050:
051: public SectionEnrollmentsImpl(List enrollmentRecords) {
052: studentToMap = new HashMap();
053: for (Iterator iter = enrollmentRecords.iterator(); iter
054: .hasNext();) {
055: EnrollmentRecord enrollment = (EnrollmentRecord) iter
056: .next();
057: String userUid = enrollment.getUser().getUserUid();
058: CourseSection section = (CourseSection) enrollment
059: .getLearningContext();
060: Map sectionMap;
061: if (studentToMap.get(userUid) == null) {
062: // Insert a new entry into the map
063: sectionMap = new HashMap();
064: studentToMap.put(userUid, sectionMap);
065: } else {
066: sectionMap = (Map) studentToMap.get(userUid);
067: }
068: sectionMap.put(section.getCategory(), section);
069: }
070: }
071:
072: public CourseSection getSection(String studentUid, String categoryId) {
073: Map sectionMap = (Map) studentToMap.get(studentUid);
074: if (sectionMap == null) {
075: if (log.isDebugEnabled())
076: log
077: .debug("Student "
078: + studentUid
079: + " is not represented in this SectionEnrollments data structure");
080: return null;
081: }
082: CourseSection section = (CourseSection) sectionMap
083: .get(categoryId);
084: if (section == null) {
085: if (log.isDebugEnabled())
086: log.debug("Student " + studentUid
087: + " is not enrolled in a " + categoryId);
088: }
089: return section;
090: }
091:
092: public Set getStudentUuids() {
093: return studentToMap.keySet();
094: }
095:
096: public String toString() {
097: return new ToStringBuilder(this).append(studentToMap)
098: .toString();
099: }
100: }
|