0001: /**********************************************************************************
0002: * $URL: https://source.sakaiproject.org/svn/course-management/tags/sakai_2-4-1/cm-impl/hibernate-impl/impl/src/java/org/sakaiproject/coursemanagement/impl/CourseManagementServiceFederatedImpl.java $
0003: * $Id: CourseManagementServiceFederatedImpl.java 21050 2007-02-06 16:12:06Z jholtzman@berkeley.edu $
0004: ***********************************************************************************
0005: *
0006: * Copyright (c) 2006 The Sakai Foundation.
0007: *
0008: * Licensed under the Educational Community License, Version 1.0 (the "License");
0009: * you may not use this file except in compliance with the License.
0010: * You may obtain a copy of the License at
0011: *
0012: * http://www.opensource.org/licenses/ecl1.php
0013: *
0014: * Unless required by applicable law or agreed to in writing, software
0015: * distributed under the License is distributed on an "AS IS" BASIS,
0016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0017: * See the License for the specific language governing permissions and
0018: * limitations under the License.
0019: *
0020: **********************************************************************************/package org.sakaiproject.coursemanagement.impl;
0021:
0022: import java.util.ArrayList;
0023: import java.util.Collections;
0024: import java.util.Comparator;
0025: import java.util.HashMap;
0026: import java.util.HashSet;
0027: import java.util.Iterator;
0028: import java.util.List;
0029: import java.util.Locale;
0030: import java.util.Map;
0031: import java.util.Set;
0032: import java.util.Map.Entry;
0033:
0034: import org.apache.commons.logging.Log;
0035: import org.apache.commons.logging.LogFactory;
0036: import org.sakaiproject.coursemanagement.api.AcademicSession;
0037: import org.sakaiproject.coursemanagement.api.CanonicalCourse;
0038: import org.sakaiproject.coursemanagement.api.CourseManagementService;
0039: import org.sakaiproject.coursemanagement.api.CourseOffering;
0040: import org.sakaiproject.coursemanagement.api.CourseSet;
0041: import org.sakaiproject.coursemanagement.api.Enrollment;
0042: import org.sakaiproject.coursemanagement.api.EnrollmentSet;
0043: import org.sakaiproject.coursemanagement.api.Membership;
0044: import org.sakaiproject.coursemanagement.api.Section;
0045: import org.sakaiproject.coursemanagement.api.exception.IdNotFoundException;
0046:
0047: /**
0048: * <p>
0049: * Federates multiple CourseManagementService implementations. Each individual implementation
0050: * must follow the following pattern to participate in federation:
0051: * </p>
0052: *
0053: * <p>
0054: * If the implementation doesn't have any information about a particular method and would
0055: * like to defer to other impls in the chain, it should:
0056: *
0057: * <ul>
0058: * <li>Throw an IdNotFoundException if the return type is an object and the method throws this exception</li>
0059: * <li>Return null if the return type is an object and the method does not throw IdNotFoundException</li>
0060: * <li>Throw UnsupportedOperationException if the return type is a primitive</li>
0061: * </ul>
0062: *
0063: * Please ensure that your implementation is internally consistent. If you implement
0064: * getEnrollments(String enrollmentSetEid), for instance, you should also implement
0065: * the isEnrolled() methods. If you implement one but not the other, the data
0066: * provided by the CourseManagementService will be dependent on <i>how</i>
0067: * the client calls the API, rather than getting a consistent picture no matter which
0068: * methods are called.
0069: * </p>
0070: *
0071: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
0072: *
0073: */
0074: public class CourseManagementServiceFederatedImpl implements
0075: CourseManagementService {
0076:
0077: private static final Log log = LogFactory
0078: .getLog(CourseManagementServiceFederatedImpl.class);
0079:
0080: private List<CourseManagementService> implList;
0081:
0082: /**
0083: * Sets the list of implementations to consult. Implementations earlier in the list will override later ones.
0084: * @param implList
0085: */
0086: public void setImplList(List<CourseManagementService> implList) {
0087: this .implList = implList;
0088: }
0089:
0090: public Set<CourseOffering> findCourseOfferings(String courseSetEid,
0091: String academicSessionEid) throws IdNotFoundException {
0092: Set<CourseOffering> resultSet = new HashSet<CourseOffering>();
0093: int exceptions = 0;
0094: for (Iterator implIter = implList.iterator(); implIter
0095: .hasNext();) {
0096: CourseManagementService cm = (CourseManagementService) implIter
0097: .next();
0098: Set<CourseOffering> set = null;
0099: try {
0100: set = cm.findCourseOfferings(courseSetEid,
0101: academicSessionEid);
0102: if (set != null) {
0103: resultSet.addAll(set);
0104: }
0105: } catch (IdNotFoundException ide) {
0106: if (log.isDebugEnabled())
0107: log.debug(cm + " could not find course set "
0108: + courseSetEid);
0109: exceptions++;
0110: continue;
0111: }
0112: }
0113: if (exceptions == implList.size()) {
0114: throw new IdNotFoundException(
0115: "Could not find a CM impl with knowledge of academic session "
0116: + academicSessionEid + " and course set "
0117: + courseSetEid);
0118: }
0119: return resultSet;
0120: }
0121:
0122: public List<CourseSet> findCourseSets(String category) {
0123: List<CourseSet> resultSet = new ArrayList<CourseSet>();
0124: for (Iterator implIter = implList.iterator(); implIter
0125: .hasNext();) {
0126: CourseManagementService cm = (CourseManagementService) implIter
0127: .next();
0128: List<CourseSet> list = cm.findCourseSets(category);
0129: if (list != null) {
0130: resultSet.addAll(list);
0131: }
0132: }
0133:
0134: // The federated list should be sorted by title.
0135: Collections.sort(resultSet, new Comparator<CourseSet>() {
0136: public int compare(CourseSet cs1, CourseSet cs2) {
0137: return cs1.getTitle().compareTo(cs2.getTitle());
0138: }
0139: });
0140: return resultSet;
0141: }
0142:
0143: public Set<EnrollmentSet> findCurrentlyEnrolledEnrollmentSets(
0144: String userId) {
0145: Set<EnrollmentSet> resultSet = new HashSet<EnrollmentSet>();
0146: for (Iterator implIter = implList.iterator(); implIter
0147: .hasNext();) {
0148: CourseManagementService cm = (CourseManagementService) implIter
0149: .next();
0150: Set<EnrollmentSet> set = cm
0151: .findCurrentlyEnrolledEnrollmentSets(userId);
0152: if (set != null) {
0153: resultSet.addAll(set);
0154: }
0155: }
0156: return resultSet;
0157: }
0158:
0159: public Set<EnrollmentSet> findCurrentlyInstructingEnrollmentSets(
0160: String userId) {
0161: Set<EnrollmentSet> resultSet = new HashSet<EnrollmentSet>();
0162: for (Iterator implIter = implList.iterator(); implIter
0163: .hasNext();) {
0164: CourseManagementService cm = (CourseManagementService) implIter
0165: .next();
0166: Set<EnrollmentSet> set = cm
0167: .findCurrentlyInstructingEnrollmentSets(userId);
0168: if (set != null) {
0169: resultSet.addAll(set);
0170: }
0171: }
0172: return resultSet;
0173: }
0174:
0175: public Enrollment findEnrollment(String userId,
0176: String enrollmentSetEid) {
0177: for (Iterator implIter = implList.iterator(); implIter
0178: .hasNext();) {
0179: CourseManagementService cm = (CourseManagementService) implIter
0180: .next();
0181: Enrollment enr = cm
0182: .findEnrollment(userId, enrollmentSetEid);
0183: if (enr != null) {
0184: return enr;
0185: }
0186: }
0187: return null;
0188: }
0189:
0190: public Set<Section> findInstructingSections(String userId) {
0191: Set<Section> resultSet = new HashSet<Section>();
0192: for (Iterator implIter = implList.iterator(); implIter
0193: .hasNext();) {
0194: CourseManagementService cm = (CourseManagementService) implIter
0195: .next();
0196: Set<Section> set = cm.findInstructingSections(userId);
0197: if (set != null) {
0198: resultSet.addAll(set);
0199: }
0200: }
0201: return resultSet;
0202: }
0203:
0204: public Set<Section> findInstructingSections(String userId,
0205: String academicSessionEid) throws IdNotFoundException {
0206: Set<Section> resultSet = new HashSet<Section>();
0207: int exceptions = 0;
0208: for (Iterator implIter = implList.iterator(); implIter
0209: .hasNext();) {
0210: CourseManagementService cm = (CourseManagementService) implIter
0211: .next();
0212: Set<Section> set = null;
0213: try {
0214: set = cm.findInstructingSections(userId,
0215: academicSessionEid);
0216: } catch (IdNotFoundException ide) {
0217: exceptions++;
0218: if (log.isDebugEnabled())
0219: log.debug(cm + " could not find academic session "
0220: + academicSessionEid);
0221: }
0222: if (set != null) {
0223: resultSet.addAll(set);
0224: }
0225: }
0226: // If none of the impls could find the academic session, throw an IdNotFoundException.
0227: if (exceptions == implList.size()) {
0228: throw new IdNotFoundException(academicSessionEid,
0229: AcademicSession.class.getName());
0230: }
0231: return resultSet;
0232: }
0233:
0234: public AcademicSession getAcademicSession(String eid)
0235: throws IdNotFoundException {
0236: for (Iterator implIter = implList.iterator(); implIter
0237: .hasNext();) {
0238: CourseManagementService cm = (CourseManagementService) implIter
0239: .next();
0240: try {
0241: return cm.getAcademicSession(eid);
0242: } catch (IdNotFoundException ide) {
0243: if (log.isDebugEnabled())
0244: log.debug(cm
0245: + " could not locate academic session "
0246: + eid);
0247: }
0248: }
0249: throw new IdNotFoundException(eid, AcademicSession.class
0250: .getName());
0251: }
0252:
0253: public List<AcademicSession> getAcademicSessions() {
0254: List<AcademicSession> resultSet = new ArrayList<AcademicSession>();
0255:
0256: for (Iterator implIter = implList.iterator(); implIter
0257: .hasNext();) {
0258: CourseManagementService cm = (CourseManagementService) implIter
0259: .next();
0260: List<AcademicSession> list = cm.getAcademicSessions();
0261: if (list != null) {
0262: resultSet.addAll(list);
0263: }
0264: }
0265: // The federated list should be sorted by start date.
0266: Collections.sort(resultSet, startDateComparator);
0267: return resultSet;
0268: }
0269:
0270: public CanonicalCourse getCanonicalCourse(String canonicalCourseEid)
0271: throws IdNotFoundException {
0272: for (Iterator implIter = implList.iterator(); implIter
0273: .hasNext();) {
0274: CourseManagementService cm = (CourseManagementService) implIter
0275: .next();
0276: try {
0277: return cm.getCanonicalCourse(canonicalCourseEid);
0278: } catch (IdNotFoundException ide) {
0279: if (log.isDebugEnabled())
0280: log.debug(cm
0281: + " could not locate canonical course "
0282: + canonicalCourseEid);
0283: }
0284: }
0285: throw new IdNotFoundException(canonicalCourseEid,
0286: CanonicalCourse.class.getName());
0287: }
0288:
0289: public Set<CanonicalCourse> getCanonicalCourses(String courseSetEid)
0290: throws IdNotFoundException {
0291: Set<CanonicalCourse> resultSet = new HashSet<CanonicalCourse>();
0292: int exceptions = 0;
0293: for (Iterator implIter = implList.iterator(); implIter
0294: .hasNext();) {
0295: CourseManagementService cm = (CourseManagementService) implIter
0296: .next();
0297: Set<CanonicalCourse> set = null;
0298: try {
0299: set = cm.getCanonicalCourses(courseSetEid);
0300: } catch (IdNotFoundException ide) {
0301: exceptions++;
0302: if (log.isDebugEnabled())
0303: log.debug(cm + " could not find course set "
0304: + courseSetEid);
0305: }
0306: if (set != null) {
0307: resultSet.addAll(set);
0308: }
0309: }
0310: // If none of the impls could find the course set, throw an IdNotFoundException.
0311: if (exceptions == implList.size()) {
0312: throw new IdNotFoundException(courseSetEid, CourseSet.class
0313: .getName());
0314: }
0315: return resultSet;
0316: }
0317:
0318: public Set<CourseSet> getChildCourseSets(String parentCourseSetEid)
0319: throws IdNotFoundException {
0320: Set<CourseSet> resultSet = new HashSet<CourseSet>();
0321: int exceptions = 0;
0322: for (Iterator implIter = implList.iterator(); implIter
0323: .hasNext();) {
0324: CourseManagementService cm = (CourseManagementService) implIter
0325: .next();
0326: Set<CourseSet> set = null;
0327: try {
0328: set = cm.getChildCourseSets(parentCourseSetEid);
0329: } catch (IdNotFoundException ide) {
0330: exceptions++;
0331: if (log.isDebugEnabled())
0332: log.debug(cm
0333: + " could not locate parent course set "
0334: + parentCourseSetEid);
0335: }
0336: if (set != null) {
0337: resultSet.addAll(set);
0338: }
0339: }
0340: // If none of the impls could find the course set, throw an IdNotFoundException.
0341: if (exceptions == implList.size()) {
0342: throw new IdNotFoundException(parentCourseSetEid,
0343: CourseSet.class.getName());
0344: }
0345: return resultSet;
0346: }
0347:
0348: public Set<Section> getChildSections(String parentSectionEid)
0349: throws IdNotFoundException {
0350: Set<Section> resultSet = new HashSet<Section>();
0351: int exceptions = 0;
0352: for (Iterator implIter = implList.iterator(); implIter
0353: .hasNext();) {
0354: CourseManagementService cm = (CourseManagementService) implIter
0355: .next();
0356: Set<Section> set = null;
0357: try {
0358: set = cm.getChildSections(parentSectionEid);
0359: } catch (IdNotFoundException ide) {
0360: exceptions++;
0361: if (log.isDebugEnabled())
0362: log.debug(cm + " could not locate parent section "
0363: + parentSectionEid);
0364: }
0365: if (set != null) {
0366: resultSet.addAll(set);
0367: }
0368: }
0369: // If none of the impls could find the section, throw an IdNotFoundException.
0370: if (exceptions == implList.size()) {
0371: throw new IdNotFoundException(parentSectionEid,
0372: Section.class.getName());
0373: }
0374: return resultSet;
0375: }
0376:
0377: public CourseOffering getCourseOffering(String courseOfferingEid)
0378: throws IdNotFoundException {
0379: for (Iterator implIter = implList.iterator(); implIter
0380: .hasNext();) {
0381: CourseManagementService cm = (CourseManagementService) implIter
0382: .next();
0383: try {
0384: return cm.getCourseOffering(courseOfferingEid);
0385: } catch (IdNotFoundException ide) {
0386: if (log.isDebugEnabled())
0387: log.debug(cm + " could not locate course offering "
0388: + courseOfferingEid);
0389: }
0390: }
0391: throw new IdNotFoundException(courseOfferingEid,
0392: CanonicalCourse.class.getName());
0393: }
0394:
0395: public Set<Membership> getCourseOfferingMemberships(
0396: String courseOfferingEid) throws IdNotFoundException {
0397: Set<Membership> resultSet = new HashSet<Membership>();
0398: int exceptions = 0;
0399: for (Iterator implIter = implList.iterator(); implIter
0400: .hasNext();) {
0401: CourseManagementService cm = (CourseManagementService) implIter
0402: .next();
0403: Set<Membership> set = null;
0404: try {
0405: set = cm
0406: .getCourseOfferingMemberships(courseOfferingEid);
0407: } catch (IdNotFoundException ide) {
0408: exceptions++;
0409: if (log.isDebugEnabled())
0410: log.debug(cm + " could not locate course offering "
0411: + courseOfferingEid);
0412: }
0413: if (set != null) {
0414: resultSet.addAll(set);
0415: }
0416: }
0417: // If none of the impls could find the course offering, throw an IdNotFoundException.
0418: if (exceptions == implList.size()) {
0419: throw new IdNotFoundException(courseOfferingEid,
0420: CourseOffering.class.getName());
0421: }
0422: return resultSet;
0423: }
0424:
0425: public Set<CourseOffering> getCourseOfferingsInCourseSet(
0426: String courseSetEid) throws IdNotFoundException {
0427: Set<CourseOffering> resultSet = new HashSet<CourseOffering>();
0428: int exceptions = 0;
0429: for (Iterator implIter = implList.iterator(); implIter
0430: .hasNext();) {
0431: CourseManagementService cm = (CourseManagementService) implIter
0432: .next();
0433: Set<CourseOffering> set = null;
0434: try {
0435: set = cm.getCourseOfferingsInCourseSet(courseSetEid);
0436: } catch (IdNotFoundException ide) {
0437: exceptions++;
0438: if (log.isDebugEnabled())
0439: log.debug(cm + " could not locate course set "
0440: + courseSetEid);
0441: }
0442: if (set != null) {
0443: resultSet.addAll(set);
0444: }
0445: }
0446: if (exceptions == implList.size()) {
0447: // all of the impls threw an IdNotFoundException, so the course set doesn't exist anywhere
0448: throw new IdNotFoundException(courseSetEid, CourseSet.class
0449: .getName());
0450: }
0451: return resultSet;
0452: }
0453:
0454: public CourseSet getCourseSet(String eid)
0455: throws IdNotFoundException {
0456: for (Iterator implIter = implList.iterator(); implIter
0457: .hasNext();) {
0458: CourseManagementService cm = (CourseManagementService) implIter
0459: .next();
0460: try {
0461: return cm.getCourseSet(eid);
0462: } catch (IdNotFoundException ide) {
0463: if (log.isDebugEnabled())
0464: log.debug(cm + " could not locate course set "
0465: + eid);
0466: }
0467: }
0468: throw new IdNotFoundException(eid, CourseSet.class.getName());
0469: }
0470:
0471: public Set<Membership> getCourseSetMemberships(String courseSetEid)
0472: throws IdNotFoundException {
0473: Set<Membership> resultSet = new HashSet<Membership>();
0474: int exceptions = 0;
0475: for (Iterator implIter = implList.iterator(); implIter
0476: .hasNext();) {
0477: CourseManagementService cm = (CourseManagementService) implIter
0478: .next();
0479: Set<Membership> set = null;
0480: try {
0481: set = cm.getCourseSetMemberships(courseSetEid);
0482: } catch (IdNotFoundException ide) {
0483: exceptions++;
0484: if (log.isDebugEnabled())
0485: log.debug(cm + " could not locate course set "
0486: + courseSetEid);
0487: }
0488: if (set != null) {
0489: resultSet.addAll(set);
0490: }
0491: }
0492: // If none of the impls could find the course set, throw an IdNotFoundException.
0493: if (exceptions == implList.size()) {
0494: throw new IdNotFoundException(courseSetEid, CourseSet.class
0495: .getName());
0496: }
0497: return resultSet;
0498: }
0499:
0500: public Set<CourseSet> getCourseSets() {
0501: Set<CourseSet> resultSet = new HashSet<CourseSet>();
0502: for (Iterator implIter = implList.iterator(); implIter
0503: .hasNext();) {
0504: CourseManagementService cm = (CourseManagementService) implIter
0505: .next();
0506: Set<CourseSet> set = cm.getCourseSets();
0507: if (set != null) {
0508: resultSet.addAll(set);
0509: }
0510: }
0511: return resultSet;
0512: }
0513:
0514: public List<AcademicSession> getCurrentAcademicSessions() {
0515: List<AcademicSession> resultSet = new ArrayList<AcademicSession>();
0516: for (Iterator implIter = implList.iterator(); implIter
0517: .hasNext();) {
0518: CourseManagementService cm = (CourseManagementService) implIter
0519: .next();
0520: List<AcademicSession> list = cm
0521: .getCurrentAcademicSessions();
0522: if (list != null) {
0523: resultSet.addAll(list);
0524: }
0525: }
0526: // Sort the academic sessions by start date
0527: Collections.sort(resultSet, startDateComparator);
0528: return resultSet;
0529: }
0530:
0531: public EnrollmentSet getEnrollmentSet(String enrollmentSetEid)
0532: throws IdNotFoundException {
0533: for (Iterator implIter = implList.iterator(); implIter
0534: .hasNext();) {
0535: CourseManagementService cm = (CourseManagementService) implIter
0536: .next();
0537: try {
0538: return cm.getEnrollmentSet(enrollmentSetEid);
0539: } catch (IdNotFoundException ide) {
0540: if (log.isDebugEnabled())
0541: log.debug(cm + " could not locate enrollmentSet "
0542: + enrollmentSetEid);
0543: }
0544: }
0545: throw new IdNotFoundException(enrollmentSetEid,
0546: EnrollmentSet.class.getName());
0547: }
0548:
0549: public Set<EnrollmentSet> getEnrollmentSets(String courseOfferingEid)
0550: throws IdNotFoundException {
0551: Set<EnrollmentSet> resultSet = new HashSet<EnrollmentSet>();
0552: int exceptions = 0;
0553: for (Iterator implIter = implList.iterator(); implIter
0554: .hasNext();) {
0555: CourseManagementService cm = (CourseManagementService) implIter
0556: .next();
0557: Set<EnrollmentSet> set = null;
0558: try {
0559: set = cm.getEnrollmentSets(courseOfferingEid);
0560: } catch (IdNotFoundException ide) {
0561: exceptions++;
0562: if (log.isDebugEnabled())
0563: log.debug(cm + " could not locate course offering "
0564: + courseOfferingEid);
0565: }
0566: if (set != null) {
0567: resultSet.addAll(set);
0568: }
0569: }
0570: // If none of the impls could find the course offering, throw an IdNotFoundException.
0571: if (exceptions == implList.size()) {
0572: throw new IdNotFoundException(courseOfferingEid,
0573: CourseOffering.class.getName());
0574: }
0575: return resultSet;
0576: }
0577:
0578: public Set<Enrollment> getEnrollments(String enrollmentSetEid)
0579: throws IdNotFoundException {
0580: Set<Enrollment> resultSet = new HashSet<Enrollment>();
0581: int exceptions = 0;
0582: for (Iterator implIter = implList.iterator(); implIter
0583: .hasNext();) {
0584: CourseManagementService cm = (CourseManagementService) implIter
0585: .next();
0586: Set<Enrollment> set = null;
0587: try {
0588: set = cm.getEnrollments(enrollmentSetEid);
0589: } catch (IdNotFoundException ide) {
0590: if (log.isDebugEnabled())
0591: log.debug(cm + " could not locate enrollment set "
0592: + enrollmentSetEid);
0593: }
0594: if (set != null) {
0595: resultSet.addAll(set);
0596: }
0597: }
0598: // If none of the impls could find the enrollment set, throw an IdNotFoundException.
0599: if (exceptions == implList.size()) {
0600: throw new IdNotFoundException(enrollmentSetEid,
0601: EnrollmentSet.class.getName());
0602: }
0603: return resultSet;
0604: }
0605:
0606: public Set<CanonicalCourse> getEquivalentCanonicalCourses(
0607: String canonicalCourseEid) throws IdNotFoundException {
0608: Set<CanonicalCourse> resultSet = new HashSet<CanonicalCourse>();
0609: int exceptions = 0;
0610: for (Iterator implIter = implList.iterator(); implIter
0611: .hasNext();) {
0612: CourseManagementService cm = (CourseManagementService) implIter
0613: .next();
0614: Set<CanonicalCourse> set = null;
0615: try {
0616: set = cm
0617: .getEquivalentCanonicalCourses(canonicalCourseEid);
0618: } catch (IdNotFoundException ide) {
0619: exceptions++;
0620: if (log.isDebugEnabled())
0621: log.debug(cm
0622: + " could not locate canonical course "
0623: + canonicalCourseEid);
0624: }
0625: if (set != null) {
0626: resultSet.addAll(set);
0627: }
0628: }
0629: // If none of the impls could find the canonical course, throw an IdNotFoundException.
0630: if (exceptions == implList.size()) {
0631: throw new IdNotFoundException(canonicalCourseEid,
0632: CanonicalCourse.class.getName());
0633: }
0634: return resultSet;
0635: }
0636:
0637: public Set<CourseOffering> getEquivalentCourseOfferings(
0638: String courseOfferingEid) throws IdNotFoundException {
0639: Set<CourseOffering> resultSet = new HashSet<CourseOffering>();
0640: int exceptions = 0;
0641: for (Iterator implIter = implList.iterator(); implIter
0642: .hasNext();) {
0643: CourseManagementService cm = (CourseManagementService) implIter
0644: .next();
0645: Set<CourseOffering> set = null;
0646: try {
0647: set = cm
0648: .getEquivalentCourseOfferings(courseOfferingEid);
0649: } catch (IdNotFoundException ide) {
0650: exceptions++;
0651: if (log.isDebugEnabled())
0652: log.debug(cm + " could not locate course offering "
0653: + courseOfferingEid);
0654: }
0655: if (set != null) {
0656: resultSet.addAll(set);
0657: }
0658: }
0659: // If none of the impls could find the course offering, throw an IdNotFoundException.
0660: if (exceptions == implList.size()) {
0661: throw new IdNotFoundException(courseOfferingEid,
0662: CourseOffering.class.getName());
0663: }
0664: return resultSet;
0665: }
0666:
0667: public Set<String> getInstructorsOfRecordIds(String enrollmentSetEid)
0668: throws IdNotFoundException {
0669: Set<String> resultSet = new HashSet<String>();
0670: int exceptions = 0;
0671: for (Iterator implIter = implList.iterator(); implIter
0672: .hasNext();) {
0673: CourseManagementService cm = (CourseManagementService) implIter
0674: .next();
0675: Set<String> set = null;
0676: try {
0677: set = cm.getInstructorsOfRecordIds(enrollmentSetEid);
0678: } catch (IdNotFoundException ide) {
0679: exceptions++;
0680: if (log.isDebugEnabled())
0681: log.debug(cm + " could not locate enrollment set "
0682: + enrollmentSetEid);
0683: }
0684: if (set != null) {
0685: resultSet.addAll(set);
0686: }
0687: }
0688: // If none of the impls could find the enrollment set, throw an IdNotFoundException.
0689: if (exceptions == implList.size()) {
0690: throw new IdNotFoundException(enrollmentSetEid,
0691: EnrollmentSet.class.getName());
0692: }
0693: return resultSet;
0694: }
0695:
0696: public Section getSection(String sectionEid)
0697: throws IdNotFoundException {
0698: for (Iterator implIter = implList.iterator(); implIter
0699: .hasNext();) {
0700: CourseManagementService cm = (CourseManagementService) implIter
0701: .next();
0702: try {
0703: return cm.getSection(sectionEid);
0704: } catch (IdNotFoundException ide) {
0705: if (log.isDebugEnabled())
0706: log.debug(cm + " could not locate section "
0707: + sectionEid);
0708: }
0709: }
0710: throw new IdNotFoundException(sectionEid, Section.class
0711: .getName());
0712: }
0713:
0714: public Set<Membership> getSectionMemberships(String sectionEid)
0715: throws IdNotFoundException {
0716: Set<Membership> resultSet = new HashSet<Membership>();
0717: int exceptions = 0;
0718: for (Iterator implIter = implList.iterator(); implIter
0719: .hasNext();) {
0720: CourseManagementService cm = (CourseManagementService) implIter
0721: .next();
0722: Set<Membership> set = null;
0723: try {
0724: set = cm.getSectionMemberships(sectionEid);
0725: } catch (IdNotFoundException ide) {
0726: exceptions++;
0727: if (log.isDebugEnabled())
0728: log.debug(cm + " could not locate section "
0729: + sectionEid);
0730: }
0731: if (set != null) {
0732: resultSet.addAll(set);
0733: }
0734: }
0735: // If none of the impls could find the section, throw an IdNotFoundException.
0736: if (exceptions == implList.size()) {
0737: throw new IdNotFoundException(sectionEid, Section.class
0738: .getName());
0739: }
0740: return resultSet;
0741: }
0742:
0743: public Set<Section> getSections(String courseOfferingEid)
0744: throws IdNotFoundException {
0745: Set<Section> resultSet = new HashSet<Section>();
0746: int exceptions = 0;
0747: for (Iterator implIter = implList.iterator(); implIter
0748: .hasNext();) {
0749: CourseManagementService cm = (CourseManagementService) implIter
0750: .next();
0751: Set<Section> set = null;
0752: try {
0753: set = cm.getSections(courseOfferingEid);
0754: } catch (IdNotFoundException ide) {
0755: exceptions++;
0756: if (log.isDebugEnabled())
0757: log.debug(cm + " could not locate course offering "
0758: + courseOfferingEid);
0759: }
0760: if (set != null) {
0761: resultSet.addAll(set);
0762: }
0763: }
0764: // If none of the impls could find the course offering, throw an IdNotFoundException.
0765: if (exceptions == implList.size()) {
0766: throw new IdNotFoundException(courseOfferingEid,
0767: CourseOffering.class.getName());
0768: }
0769: return resultSet;
0770: }
0771:
0772: public boolean isEmpty(String courseSetEid) {
0773: for (Iterator implIter = implList.iterator(); implIter
0774: .hasNext();) {
0775: CourseManagementService cm = (CourseManagementService) implIter
0776: .next();
0777: try {
0778: // If any implementation says that the course set is not empty, it's not empty!
0779: if (!cm.isEmpty(courseSetEid)) {
0780: return false;
0781: }
0782: } catch (UnsupportedOperationException uso) {
0783: if (log.isDebugEnabled())
0784: log.debug(cm + " doesn't know whether "
0785: + courseSetEid + " is empty");
0786: }
0787: }
0788: return true;
0789: }
0790:
0791: public boolean isEnrolled(String userId,
0792: Set<String> enrollmentSetEids) {
0793: for (Iterator implIter = implList.iterator(); implIter
0794: .hasNext();) {
0795: CourseManagementService cm = (CourseManagementService) implIter
0796: .next();
0797: try {
0798: // If any implementation says that this user is enrolled, they are enrolled!
0799: if (cm.isEnrolled(userId, enrollmentSetEids)) {
0800: return true;
0801: }
0802: } catch (UnsupportedOperationException uso) {
0803: if (log.isDebugEnabled())
0804: log
0805: .debug(cm
0806: + " doesn't know whether "
0807: + userId
0808: + " is enrolled in any of these enrollment sets: "
0809: + enrollmentSetEids);
0810: }
0811: }
0812: return false;
0813: }
0814:
0815: public boolean isEnrolled(String userId, String enrollmentSetEid) {
0816: for (Iterator implIter = implList.iterator(); implIter
0817: .hasNext();) {
0818: CourseManagementService cm = (CourseManagementService) implIter
0819: .next();
0820: try {
0821: // If any implementation says that this user is enrolled, they are enrolled!
0822: if (cm.isEnrolled(userId, enrollmentSetEid)) {
0823: return true;
0824: }
0825: } catch (UnsupportedOperationException uso) {
0826: if (log.isDebugEnabled())
0827: log.debug(cm + " doesn't know whether " + userId
0828: + " is enrolled in enrollment sets: "
0829: + enrollmentSetEid);
0830: }
0831: }
0832: return false;
0833: }
0834:
0835: protected static Comparator<AcademicSession> startDateComparator = new Comparator<AcademicSession>() {
0836: public int compare(AcademicSession as1, AcademicSession as2) {
0837: if (as1.getStartDate() == null
0838: && as2.getStartDate() == null) {
0839: return 0;
0840: }
0841: if (as1.getStartDate() == null
0842: && as2.getStartDate() != null) {
0843: return -1;
0844: }
0845: if (as1.getStartDate() != null
0846: && as2.getStartDate() == null) {
0847: return 1;
0848: }
0849: return as1.getStartDate().compareTo(as2.getStartDate());
0850: }
0851: };
0852:
0853: public Set<Section> findEnrolledSections(String userId) {
0854: Set<Section> resultSet = new HashSet<Section>();
0855: for (Iterator implIter = implList.iterator(); implIter
0856: .hasNext();) {
0857: CourseManagementService cm = (CourseManagementService) implIter
0858: .next();
0859: Set<Section> set = cm.findEnrolledSections(userId);
0860: if (set != null) {
0861: resultSet.addAll(set);
0862: }
0863: }
0864: return resultSet;
0865: }
0866:
0867: public Map<String, String> findCourseOfferingRoles(String userEid) {
0868: Map<String, String> courseOfferingRoleMap = new HashMap<String, String>();
0869: for (Iterator implIter = implList.iterator(); implIter
0870: .hasNext();) {
0871: CourseManagementService cm = (CourseManagementService) implIter
0872: .next();
0873: Map map = cm.findCourseOfferingRoles(userEid);
0874: if (map == null) {
0875: continue;
0876: }
0877: for (Iterator mapIter = map.keySet().iterator(); mapIter
0878: .hasNext();) {
0879: String courseSetEid = (String) mapIter.next();
0880: String role = (String) map.get(courseSetEid);
0881: // Earlier impls take precedence, so don't overwrite what's in the map
0882: if (!courseOfferingRoleMap.containsKey(courseSetEid)) {
0883: courseOfferingRoleMap.put(courseSetEid, role);
0884: }
0885: }
0886: }
0887: return courseOfferingRoleMap;
0888: }
0889:
0890: public Map<String, String> findCourseSetRoles(String userEid) {
0891: Map<String, String> courseSetRoleMap = new HashMap<String, String>();
0892: for (Iterator implIter = implList.iterator(); implIter
0893: .hasNext();) {
0894: CourseManagementService cm = (CourseManagementService) implIter
0895: .next();
0896: Map map = cm.findCourseSetRoles(userEid);
0897: if (map == null) {
0898: continue;
0899: }
0900: for (Iterator mapIter = map.keySet().iterator(); mapIter
0901: .hasNext();) {
0902: String courseSetEid = (String) mapIter.next();
0903: String role = (String) map.get(courseSetEid);
0904: // Earlier impls take precedence, so don't overwrite what's in the map
0905: if (!courseSetRoleMap.containsKey(courseSetEid)) {
0906: courseSetRoleMap.put(courseSetEid, role);
0907: }
0908: }
0909: }
0910: return courseSetRoleMap;
0911: }
0912:
0913: public Map<String, String> findSectionRoles(String userEid) {
0914: Map<String, String> sectionRoleMap = new HashMap<String, String>();
0915: for (Iterator implIter = implList.iterator(); implIter
0916: .hasNext();) {
0917: CourseManagementService cm = (CourseManagementService) implIter
0918: .next();
0919: Map map = cm.findSectionRoles(userEid);
0920: if (map == null) {
0921: continue;
0922: }
0923: for (Iterator mapIter = map.keySet().iterator(); mapIter
0924: .hasNext();) {
0925: String sectionEid = (String) mapIter.next();
0926: String role = (String) map.get(sectionEid);
0927: // Earlier impls take precedence, so don't overwrite what's in the map
0928: if (!sectionRoleMap.containsKey(sectionEid)) {
0929: sectionRoleMap.put(sectionEid, role);
0930: }
0931: }
0932: }
0933: return sectionRoleMap;
0934: }
0935:
0936: public Set<CourseOffering> getCourseOfferingsInCanonicalCourse(
0937: String canonicalCourseEid) throws IdNotFoundException {
0938: Set<CourseOffering> resultSet = new HashSet<CourseOffering>();
0939: int exceptions = 0;
0940: for (Iterator implIter = implList.iterator(); implIter
0941: .hasNext();) {
0942: CourseManagementService cm = (CourseManagementService) implIter
0943: .next();
0944: Set<CourseOffering> set = null;
0945: try {
0946: set = cm
0947: .getCourseOfferingsInCanonicalCourse(canonicalCourseEid);
0948: } catch (IdNotFoundException ide) {
0949: if (log.isDebugEnabled())
0950: log.debug(cm + " could not find canonical course "
0951: + canonicalCourseEid);
0952: exceptions++;
0953: }
0954: if (set != null) {
0955: resultSet.addAll(set);
0956: }
0957: }
0958: if (exceptions == implList.size()) {
0959: // all of the impls threw an IdNotFoundException, so the canonical course doesn't exist anywhere
0960: throw new IdNotFoundException(canonicalCourseEid,
0961: CanonicalCourse.class.getName());
0962: }
0963: return resultSet;
0964: }
0965:
0966: public boolean isAcademicSessionDefined(String eid) {
0967: for (Iterator implIter = implList.iterator(); implIter
0968: .hasNext();) {
0969: CourseManagementService cm = (CourseManagementService) implIter
0970: .next();
0971: try {
0972: // If any implementation says that the object exists, it exists!
0973: if (cm.isAcademicSessionDefined(eid)) {
0974: return true;
0975: }
0976: } catch (UnsupportedOperationException uso) {
0977: if (log.isDebugEnabled())
0978: log.debug(cm
0979: + " doesn't know whether academic session "
0980: + eid + " exists");
0981: }
0982: }
0983: return false;
0984: }
0985:
0986: public boolean isCanonicalCourseDefined(String eid) {
0987: for (Iterator implIter = implList.iterator(); implIter
0988: .hasNext();) {
0989: CourseManagementService cm = (CourseManagementService) implIter
0990: .next();
0991: try {
0992: // If any implementation says that the object exists, it exists!
0993: if (cm.isCanonicalCourseDefined(eid)) {
0994: return true;
0995: }
0996: } catch (UnsupportedOperationException uso) {
0997: if (log.isDebugEnabled())
0998: log.debug(cm
0999: + " doesn't know whether canonical course "
1000: + eid + " exists");
1001: }
1002: }
1003: return false;
1004: }
1005:
1006: public boolean isCourseOfferingDefined(String eid) {
1007: for (Iterator implIter = implList.iterator(); implIter
1008: .hasNext();) {
1009: CourseManagementService cm = (CourseManagementService) implIter
1010: .next();
1011: try {
1012: // If any implementation says that the object exists, it exists!
1013: if (cm.isCourseOfferingDefined(eid)) {
1014: return true;
1015: }
1016: } catch (UnsupportedOperationException uso) {
1017: if (log.isDebugEnabled())
1018: log.debug(cm
1019: + " doesn't know whether course offering "
1020: + eid + " exists");
1021: }
1022: }
1023: return false;
1024: }
1025:
1026: public boolean isCourseSetDefined(String eid) {
1027: for (Iterator implIter = implList.iterator(); implIter
1028: .hasNext();) {
1029: CourseManagementService cm = (CourseManagementService) implIter
1030: .next();
1031: try {
1032: // If any implementation says that the object exists, it exists!
1033: if (cm.isCourseSetDefined(eid)) {
1034: return true;
1035: }
1036: } catch (UnsupportedOperationException uso) {
1037: if (log.isDebugEnabled())
1038: log.debug(cm + " doesn't know whether course set "
1039: + eid + " exists");
1040: }
1041: }
1042: return false;
1043: }
1044:
1045: public boolean isEnrollmentSetDefined(String eid) {
1046: for (Iterator implIter = implList.iterator(); implIter
1047: .hasNext();) {
1048: CourseManagementService cm = (CourseManagementService) implIter
1049: .next();
1050: try {
1051: // If any implementation says that the object exists, it exists!
1052: if (cm.isEnrollmentSetDefined(eid)) {
1053: return true;
1054: }
1055: } catch (UnsupportedOperationException uso) {
1056: if (log.isDebugEnabled())
1057: log.debug(cm
1058: + " doesn't know whether enrollment set "
1059: + eid + " exists");
1060: }
1061: }
1062: return false;
1063: }
1064:
1065: public boolean isSectionDefined(String eid) {
1066: for (Iterator implIter = implList.iterator(); implIter
1067: .hasNext();) {
1068: CourseManagementService cm = (CourseManagementService) implIter
1069: .next();
1070: try {
1071: // If any implementation says that the object exists, it exists!
1072: if (cm.isSectionDefined(eid)) {
1073: return true;
1074: }
1075: } catch (UnsupportedOperationException uso) {
1076: if (log.isDebugEnabled())
1077: log.debug(cm + " doesn't know whether section "
1078: + eid + " exists");
1079: }
1080: }
1081: return false;
1082: }
1083:
1084: public List<String> getSectionCategories() {
1085: List<String> resultSet = new ArrayList<String>();
1086: for (Iterator implIter = implList.iterator(); implIter
1087: .hasNext();) {
1088: CourseManagementService cm = (CourseManagementService) implIter
1089: .next();
1090: List<String> list = cm.getSectionCategories();
1091: if (list != null) {
1092: resultSet.addAll(list);
1093: }
1094: }
1095:
1096: // The federated list should be sorted as a single collection.
1097: Collections.sort(resultSet, new Comparator<String>() {
1098: public int compare(String cat1, String cat2) {
1099: return cat1.compareTo(cat2);
1100: }
1101: });
1102: return resultSet;
1103: }
1104:
1105: public String getSectionCategoryDescription(String categoryCode) {
1106: for (Iterator implIter = implList.iterator(); implIter
1107: .hasNext();) {
1108: CourseManagementService cm = (CourseManagementService) implIter
1109: .next();
1110: String descr = cm
1111: .getSectionCategoryDescription(categoryCode);
1112: if (descr != null) {
1113: return descr;
1114: }
1115: }
1116: return null;
1117: }
1118:
1119: public Map<String, String> getEnrollmentStatusDescriptions(
1120: Locale locale) {
1121: Map<String, String> statusMap = new HashMap<String, String>();
1122: for (Iterator<CourseManagementService> implIter = implList
1123: .iterator(); implIter.hasNext();) {
1124: CourseManagementService cm = implIter.next();
1125: Map<String, String> map = cm
1126: .getEnrollmentStatusDescriptions(locale);
1127: if (map == null) {
1128: continue;
1129: }
1130: for (Iterator<Entry<String, String>> mapIter = map
1131: .entrySet().iterator(); mapIter.hasNext();) {
1132: Entry<String, String> entry = mapIter.next();
1133: // Earlier impls take precedence, so don't overwrite what's in the map
1134: if (!statusMap.containsKey(entry.getKey())) {
1135: statusMap.put(entry.getKey(), entry.getValue());
1136: }
1137: }
1138: }
1139: return statusMap;
1140: }
1141:
1142: public Map<String, String> getGradingSchemeDescriptions(
1143: Locale locale) {
1144: Map<String, String> gradingSchemeMap = new HashMap<String, String>();
1145: for (Iterator<CourseManagementService> implIter = implList
1146: .iterator(); implIter.hasNext();) {
1147: CourseManagementService cm = implIter.next();
1148: Map<String, String> map = cm
1149: .getGradingSchemeDescriptions(locale);
1150: if (map == null) {
1151: continue;
1152: }
1153: for (Iterator<Entry<String, String>> mapIter = map
1154: .entrySet().iterator(); mapIter.hasNext();) {
1155: Entry<String, String> entry = mapIter.next();
1156: // Earlier impls take precedence, so don't overwrite what's in the map
1157: if (!gradingSchemeMap.containsKey(entry.getKey())) {
1158: gradingSchemeMap.put(entry.getKey(), entry
1159: .getValue());
1160: }
1161: }
1162: }
1163: return gradingSchemeMap;
1164: }
1165:
1166: public Map<String, String> getMembershipStatusDescriptions(
1167: Locale locale) {
1168: Map<String, String> statusMap = new HashMap<String, String>();
1169: for (Iterator<CourseManagementService> implIter = implList
1170: .iterator(); implIter.hasNext();) {
1171: CourseManagementService cm = implIter.next();
1172: Map<String, String> map = cm
1173: .getMembershipStatusDescriptions(locale);
1174: if (map == null) {
1175: continue;
1176: }
1177: for (Iterator<Entry<String, String>> mapIter = map
1178: .entrySet().iterator(); mapIter.hasNext();) {
1179: Entry<String, String> entry = mapIter.next();
1180: // Earlier impls take precedence, so don't overwrite what's in the map
1181: if (!statusMap.containsKey(entry.getKey())) {
1182: statusMap.put(entry.getKey(), entry.getValue());
1183: }
1184: }
1185: }
1186: return statusMap;
1187: }
1188: }
|