001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-app-util/src/java/org/sakaiproject/tool/section/decorator/EnrollmentDecorator.java $
003: * $Id: EnrollmentDecorator.java 20507 2007-01-20 21:33:16Z louis@media.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.tool.section.decorator;
021:
022: import java.io.Serializable;
023: import java.util.Comparator;
024: import java.util.Map;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.sakaiproject.section.api.coursemanagement.CourseSection;
029: import org.sakaiproject.section.api.coursemanagement.EnrollmentRecord;
030: import org.sakaiproject.section.api.coursemanagement.User;
031:
032: /**
033: * Decorates an EnrollmentRecord for display in the UI.
034: *
035: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
036: *
037: */
038: public class EnrollmentDecorator implements Serializable {
039: private static final long serialVersionUID = 1L;
040: private static final Log log = LogFactory
041: .getLog(EnrollmentDecorator.class);
042:
043: protected EnrollmentRecord enrollment;
044:
045: // A Map of categoryId -> Enrolled Section
046: protected Map categoryToSectionMap;
047:
048: public EnrollmentDecorator(EnrollmentRecord enrollment,
049: Map categoryToSectionMap) {
050: this .enrollment = enrollment;
051: this .categoryToSectionMap = categoryToSectionMap;
052: }
053:
054: public static final Comparator<EnrollmentDecorator> getNameComparator(
055: final boolean sortAscending) {
056: return new Comparator<EnrollmentDecorator>() {
057: public int compare(EnrollmentDecorator enr1,
058: EnrollmentDecorator enr2) {
059: int comparison = enr1.getUser().getSortName()
060: .compareTo(enr2.getUser().getSortName());
061: return sortAscending ? comparison : (-1 * comparison);
062: }
063: };
064: }
065:
066: public static final Comparator<EnrollmentDecorator> getDisplayIdComparator(
067: final boolean sortAscending) {
068: return new Comparator<EnrollmentDecorator>() {
069: public int compare(EnrollmentDecorator enr1,
070: EnrollmentDecorator enr2) {
071: int comparison = enr1.getUser().getDisplayId()
072: .compareTo(enr2.getUser().getDisplayId());
073: return sortAscending ? comparison : (-1 * comparison);
074: }
075: };
076: }
077:
078: public static final Comparator<EnrollmentDecorator> getCategoryComparator(
079: final String categoryId, final boolean sortAscending) {
080: if (log.isDebugEnabled())
081: log.debug("Comparing enrollment decorators by "
082: + categoryId);
083: return new Comparator<EnrollmentDecorator>() {
084: public int compare(EnrollmentDecorator enr1,
085: EnrollmentDecorator enr2) {
086: CourseSection section1 = (CourseSection) enr1
087: .getCategoryToSectionMap().get(categoryId);
088: CourseSection section2 = (CourseSection) enr2
089: .getCategoryToSectionMap().get(categoryId);
090: if (section1 == null && section2 != null) {
091: return sortAscending ? -1 : 1;
092: }
093: if (section1 != null && section2 == null) {
094: return sortAscending ? 1 : -1;
095: }
096: if (section1 == null && section2 == null) {
097: return getNameComparator(sortAscending).compare(
098: enr1, enr2);
099: }
100:
101: int comparison = 0;
102: if (section1.getTitle().equals(section2.getTitle())) {
103: // Use the student name for comparison if the titles are equal
104: comparison = enr1.getUser().getSortName()
105: .compareTo(enr2.getUser().getSortName());
106: } else {
107: // Use the section title for comparison if the titles are different
108: comparison = section1.getTitle().compareTo(
109: section2.getTitle());
110: }
111: return sortAscending ? comparison : (-1 * comparison);
112: }
113: };
114: }
115:
116: public String getStatus() {
117: return enrollment.getStatus();
118: }
119:
120: public User getUser() {
121: return enrollment.getUser();
122: }
123:
124: public Map getCategoryToSectionMap() {
125: return categoryToSectionMap;
126: }
127:
128: public String getCategory() {
129: return null;
130: }
131:
132: public String getCatgeoryName() {
133: return null;
134: }
135:
136: }
|