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