001    package net.sf.cpsolver.studentsct.check;
002    
003    import java.util.Enumeration;
004    import java.util.Hashtable;
005    import java.util.Iterator;
006    
007    import net.sf.cpsolver.coursett.model.TimeLocation;
008    import net.sf.cpsolver.studentsct.StudentSectioningModel;
009    import net.sf.cpsolver.studentsct.model.Assignment;
010    import net.sf.cpsolver.studentsct.model.Enrollment;
011    import net.sf.cpsolver.studentsct.model.Request;
012    import net.sf.cpsolver.studentsct.model.Student;
013    
014    /**
015     * This class looks and reports cases when a student is enrolled into two sections that are 
016     * overlapping in time.  
017     *  
018     * <br><br>
019     * 
020     * Usage: if (new OverlapCheck(model).check()) ...
021     * 
022     * <br><br>
023     * 
024     * @version
025     * StudentSct 1.1 (Student Sectioning)<br>
026     * Copyright (C) 2007 Tomáš Müller<br>
027     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
028     * Lazenska 391, 76314 Zlin, Czech Republic<br>
029     * <br>
030     * This library is free software; you can redistribute it and/or
031     * modify it under the terms of the GNU Lesser General Public
032     * License as published by the Free Software Foundation; either
033     * version 2.1 of the License, or (at your option) any later version.
034     * <br><br>
035     * This library is distributed in the hope that it will be useful,
036     * but WITHOUT ANY WARRANTY; without even the implied warranty of
037     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
038     * Lesser General Public License for more details.
039     * <br><br>
040     * You should have received a copy of the GNU Lesser General Public
041     * License along with this library; if not, write to the Free Software
042     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
043     */
044    public class OverlapCheck {
045        private static org.apache.log4j.Logger sLog = org.apache.log4j.Logger.getLogger(OverlapCheck.class);
046        private StudentSectioningModel iModel;
047        
048        /** Constructor
049         * @param model student sectioning model
050         */
051        public OverlapCheck(StudentSectioningModel model) {
052            iModel = model;
053        }
054        
055        /** Return student sectioning model */
056        public StudentSectioningModel getModel() {
057            return iModel;
058        }
059        
060        /** Check for overlapping sections that are attended by the same student
061         * @return false, if there is such a case
062         */
063        public boolean check() {
064            sLog.info("Checking for overlaps...");
065            boolean ret = true;
066            for (Enumeration e=getModel().getStudents().elements();e.hasMoreElements();) {
067                Student student = (Student)e.nextElement();
068                Hashtable times = new Hashtable();
069                for (Enumeration f=student.getRequests().elements();f.hasMoreElements();) {
070                    Request request = (Request)f.nextElement();
071                    Enrollment enrollment = (Enrollment)request.getAssignment();
072                    if (enrollment==null) continue;
073                    for (Iterator g=enrollment.getAssignments().iterator();g.hasNext();) {
074                        Assignment assignment = (Assignment)g.next();
075                        if (assignment.getTime()==null) continue;
076                        for (Enumeration h=times.keys();h.hasMoreElements();) {
077                            TimeLocation time = (TimeLocation)h.nextElement();
078                            if (time.hasIntersection(assignment.getTime())) {
079                                sLog.error("Student "+student+" assignment "+assignment+" overlaps with "+times.get(time));
080                                ret = false;
081                            }
082                        }
083                        times.put(assignment.getTime(),assignment);
084                    }
085                }
086            }
087            return ret;
088        }
089    
090    }