001package org.cpsolver.studentsct.reservation;
002
003import java.util.Collection;
004
005import org.cpsolver.studentsct.model.Course;
006import org.cpsolver.studentsct.model.CourseRequest;
007import org.cpsolver.studentsct.model.Request;
008import org.cpsolver.studentsct.model.Student;
009
010/**
011 * Learning Community reservation. This is a combination of {@link GroupReservation}
012 * and {@link CourseReservation}. Space is reserved for students of a group but only when
013 * they enroll into the offering through the given course.
014 * 
015 * <br>
016 * <br>
017 * 
018 * @version StudentSct 1.3 (Student Sectioning)<br>
019 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
020 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
021 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
022 * <br>
023 *          This library is free software; you can redistribute it and/or modify
024 *          it under the terms of the GNU Lesser General Public License as
025 *          published by the Free Software Foundation; either version 3 of the
026 *          License, or (at your option) any later version. <br>
027 * <br>
028 *          This library is distributed in the hope that it will be useful, but
029 *          WITHOUT ANY WARRANTY; without even the implied warranty of
030 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
031 *          Lesser General Public License for more details. <br>
032 * <br>
033 *          You should have received a copy of the GNU Lesser General Public
034 *          License along with this library; if not see
035 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
036 */
037public class LearningCommunityReservation extends GroupReservation {
038    private Course iCourse;
039    
040    /**
041     * Learning Community reservations are just a below student group reservations.
042     */
043    public static final int DEFAULT_PRIORITY = 250;
044    /**
045     * Learning Community reservation must be used (unless it is expired)
046     */
047    public static final boolean DEFAULT_MUST_BE_USED = true;
048    /**
049     * Learning Community reservations cannot be assigned over the limit.
050     */
051    public static final boolean DEFAULT_CAN_ASSIGN_OVER_LIMIT = false;
052    /**
053     * Overlaps are not allowed for Learning Community reservations. 
054     */
055    public static final boolean DEFAULT_ALLOW_OVERLAP = false;
056
057    /**
058     * Constructor
059     * @param id unique id
060     * @param limit reservation limit (-1 for unlimited)
061     * @param course course offering for which the reservation is
062     * @param studentIds one or more students
063     */
064    public LearningCommunityReservation(long id, double limit, Course course, Long... studentIds) {
065        super(id, limit, course.getOffering(), DEFAULT_PRIORITY, DEFAULT_MUST_BE_USED, DEFAULT_CAN_ASSIGN_OVER_LIMIT, DEFAULT_ALLOW_OVERLAP, studentIds);
066        iCourse = course;
067    }
068    
069    /**
070     * Constructor
071     * @param id unique id
072     * @param limit reservation limit (-1 for unlimited)
073     * @param course course offering for which the reservation is
074     * @param studentIds one or more students
075     */
076    public LearningCommunityReservation(long id, double limit, Course course, Collection<Long> studentIds) {
077        super(id, limit, course.getOffering(), DEFAULT_PRIORITY, DEFAULT_MUST_BE_USED, DEFAULT_CAN_ASSIGN_OVER_LIMIT, DEFAULT_ALLOW_OVERLAP, studentIds);
078        iCourse = course;
079    }
080
081    /**
082     * Reservation limit (-1 for unlimited)
083     */
084    @Override
085    public double getReservationLimit() {
086        if (super.getReservationLimit() < 0.0)
087            return iCourse.getLimit(); // no group limit >> return course limit
088        else if (iCourse.getLimit()  < 0.0)
089            return super.getReservationLimit(); // course unlimited >> return group limit
090        else
091            return Math.min(super.getReservationLimit(), iCourse.getLimit()); // return smaller of the two limits
092    }
093    
094    /**
095     * Course offering
096     * @return course offering
097     */
098    public Course getCourse() {
099        return iCourse;
100    }
101    
102    /**
103     * Check the student group and the course
104     */
105    @Override
106    public boolean isApplicable(Student student) {
107        if (!super.isApplicable(student)) return false;
108        for (Request r: student.getRequests()) {
109            if (r instanceof CourseRequest) {
110                for (Course course: ((CourseRequest) r).getCourses()) {
111                    if (course.equals(getCourse())) return true;
112                }
113            }
114        }
115        return false;
116    }
117}