001package org.cpsolver.studentsct.reservation;
002
003import java.util.Collection;
004import java.util.HashSet;
005import java.util.Set;
006
007import org.cpsolver.studentsct.model.AcademicAreaCode;
008import org.cpsolver.studentsct.model.AreaClassificationMajor;
009import org.cpsolver.studentsct.model.Offering;
010import org.cpsolver.studentsct.model.Student;
011
012
013/**
014 * Curriculum reservation. Students are matched based on their academic area.
015 * If classifications and/or majors are included, student must match on them as well.  
016 * 
017 * <br>
018 * <br>
019 * 
020 * @version StudentSct 1.3 (Student Sectioning)<br>
021 *          Copyright (C) 2007 - 2014 Tomas Muller<br>
022 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
023 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
024 * <br>
025 *          This library is free software; you can redistribute it and/or modify
026 *          it under the terms of the GNU Lesser General Public License as
027 *          published by the Free Software Foundation; either version 3 of the
028 *          License, or (at your option) any later version. <br>
029 * <br>
030 *          This library is distributed in the hope that it will be useful, but
031 *          WITHOUT ANY WARRANTY; without even the implied warranty of
032 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
033 *          Lesser General Public License for more details. <br>
034 * <br>
035 *          You should have received a copy of the GNU Lesser General Public
036 *          License along with this library; if not see
037 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
038 */
039public class CurriculumReservation extends Reservation {
040    private double iLimit;
041    private String iAcadArea;
042    private Set<String> iClassifications = new HashSet<String>();
043    private Set<String> iMajors = new HashSet<String>();
044    
045    /**
046     * Reservation priority (lower than individual and group reservations)
047     */
048    public static final int DEFAULT_PRIORITY = 500;
049    /**
050     * Curriculum reservation does not need to be used
051     */
052    public static final boolean DEFAULT_MUST_BE_USED = false;
053    /**
054     * Curriculum reservations can not assign over the limit.
055     */
056    public static final boolean DEFAULT_CAN_ASSIGN_OVER_LIMIT = false;
057    /**
058     * Overlaps are not allowed for curriculum reservations. 
059     */
060    public static final boolean DEFAULT_ALLOW_OVERLAP = false;
061    
062    /**
063     * Constructor
064     * @param id unique id
065     * @param limit reservation limit (-1 for unlimited)
066     * @param offering instructional offering on which the reservation is set
067     * @param acadArea academic area
068     * @param classifications zero or more classifications (classifications must match if not empty)
069     * @param majors zero or more majors (majors must match if not empty)
070     */
071    public CurriculumReservation(long id, double limit, Offering offering, String acadArea, Collection<String> classifications, Collection<String> majors) {
072        super(id, offering, DEFAULT_PRIORITY, DEFAULT_MUST_BE_USED, DEFAULT_CAN_ASSIGN_OVER_LIMIT, DEFAULT_ALLOW_OVERLAP);
073        iLimit = limit;
074        iAcadArea = acadArea;
075        if (classifications != null)
076            iClassifications.addAll(classifications);
077        if (majors != null)
078            iMajors.addAll(majors);
079    }
080    
081    /**
082     * Constructor
083     * @param id unique id
084     * @param limit reservation limit (-1 for unlimited)
085     * @param offering instructional offering on which the reservation is set
086     * @param acadArea academic area
087     * @param classifications zero or more classifications (classifications must match if not empty)
088     * @param majors zero or more majors (majors must match if not empty)
089     * @param priority reservation priority
090     * @param mustBeUsed must this reservation be used
091     * @param canAssignOverLimit can assign over class / configuration / course limit
092     * @param allowOverlap does this reservation allow for overlaps
093     */
094    protected CurriculumReservation(long id, double limit, Offering offering, String acadArea, Collection<String> classifications, Collection<String> majors,
095            int priority, boolean mustBeUsed, boolean canAssignOverLimit, boolean allowOverlap) {
096        super(id, offering, priority, mustBeUsed, canAssignOverLimit, allowOverlap);
097        iLimit = limit;
098        iAcadArea = acadArea;
099        if (classifications != null)
100            iClassifications.addAll(classifications);
101        if (majors != null)
102            iMajors.addAll(majors);
103    }
104
105    /**
106     * Reservation limit (-1 for unlimited)
107     */
108    @Override
109    public double getReservationLimit() {
110        return iLimit;
111    }
112
113    /**
114     * Set reservation limit (-1 for unlimited)
115     * @param limit reservation limit, -1 for unlimited
116     */
117    public void setReservationLimit(double limit) {
118        iLimit = limit;
119    }
120
121    
122    /**
123     * Academic area
124     * @return selected academic area
125     */
126    public String getAcademicArea() {
127        return iAcadArea;
128    }
129    
130    /**
131     * Majors
132     * @return selected majors
133     */
134    public Set<String> getMajors() {
135        return iMajors;
136    }
137    
138    /**
139     * Academic classifications
140     * @return selected academic classifications
141     */
142    public Set<String> getClassifications() {
143        return iClassifications;
144    }
145
146    /**
147     * Check the area, classifications and majors
148     */
149    @Override
150    public boolean isApplicable(Student student) {
151        boolean match = false;
152        for (AreaClassificationMajor acm: student.getAreaClassificationMajors())
153            if (getAcademicArea().equals(acm.getArea()) &&
154                (getClassifications().isEmpty() || getClassifications().contains(acm.getClassification())) &&
155                (getMajors().isEmpty() || getMajors().contains(acm.getMajor())))
156                    return true;
157        if (student.getAcademicAreaClasiffications() == null) return false;
158        for (AcademicAreaCode aac: student.getAcademicAreaClasiffications()) {
159            if (getAcademicArea().equals(aac.getArea())) {
160                if (getClassifications().isEmpty() || getClassifications().contains(aac.getCode())) {
161                    match = true; break;
162                }
163            }
164        }
165        if (!match) return false;
166        for (AcademicAreaCode aac: student.getMajors()) {
167            if (getAcademicArea().equals(aac.getArea())) {
168                if (getMajors().isEmpty() || getMajors().contains(aac.getCode()))
169                    return true;
170            }
171        }
172        return getMajors().isEmpty();
173    }
174    
175
176}