001    package net.sf.cpsolver.exam.reports;
002    
003    import java.util.Collections;
004    import java.util.Comparator;
005    import java.util.Enumeration;
006    import java.util.Vector;
007    
008    import net.sf.cpsolver.exam.model.Exam;
009    import net.sf.cpsolver.exam.model.ExamModel;
010    import net.sf.cpsolver.exam.model.ExamPeriod;
011    import net.sf.cpsolver.exam.model.ExamPlacement;
012    import net.sf.cpsolver.exam.model.ExamRoom;
013    import net.sf.cpsolver.ifs.util.CSVFile;
014    import net.sf.cpsolver.ifs.util.CSVFile.CSVField;
015    
016    /**
017     * Export schedule for each room into a CSV file.
018     * <br><br>
019     * Usage:<br>
020     * <code>
021     * &nbsp;&nbsp;&nbsp;&nbsp;new ExamRoomSchedule(model).report().save(file);
022     * </code>
023     * <br><br>
024     * 
025     * @version
026     * ExamTT 1.1 (Examination Timetabling)<br>
027     * Copyright (C) 2008 Tomáš Müller<br>
028     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
029     * Lazenska 391, 76314 Zlin, Czech Republic<br>
030     * <br>
031     * This library is free software; you can redistribute it and/or
032     * modify it under the terms of the GNU Lesser General Public
033     * License as published by the Free Software Foundation; either
034     * version 2.1 of the License, or (at your option) any later version.
035     * <br><br>
036     * This library is distributed in the hope that it will be useful,
037     * but WITHOUT ANY WARRANTY; without even the implied warranty of
038     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
039     * Lesser General Public License for more details.
040     * <br><br>
041     * You should have received a copy of the GNU Lesser General Public
042     * License along with this library; if not, write to the Free Software
043     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
044     */
045    public class ExamRoomSchedule {
046        ExamModel iModel = null;
047        
048        /**
049         * Constructor
050         * @param model examination timetabling model
051         */
052        public ExamRoomSchedule(ExamModel model) {
053            iModel = model;
054        }
055        
056        public CSVFile report() {
057            CSVFile csv = new CSVFile();
058            csv.setHeader(new CSVField[] {
059                    new CSVField("Room"),
060                    new CSVField("Cap"),
061                    new CSVField("AltCap"),
062                    new CSVField("Period"),
063                    new CSVField("Date"),
064                    new CSVField("Time"),
065                    new CSVField("Exam"),
066                    new CSVField("Enrl")
067            });
068            Vector rooms = new Vector(iModel.getRooms());
069            Collections.sort(rooms, new Comparator() {
070                public int compare(Object o1, Object o2) {
071                    ExamRoom r1 = (ExamRoom)o1;
072                    ExamRoom r2 = (ExamRoom)o2;
073                    int cmp = -Double.compare(r1.getSize(),r2.getSize());
074                    if (cmp!=0) return cmp;
075                    cmp = -Double.compare(r1.getAltSize(),r2.getAltSize());
076                    if (cmp!=0) return cmp;
077                    return r1.compareTo(r2);
078                }
079            });
080            for (Enumeration e=rooms.elements();e.hasMoreElements();) {
081                ExamRoom room = (ExamRoom)e.nextElement();
082                boolean first = true;
083                int day = -1;
084                for (Enumeration f=iModel.getPeriods().elements();f.hasMoreElements();) {
085                    ExamPeriod period = (ExamPeriod)f.nextElement();
086                    ExamPlacement placement = room.getPlacement(period);
087                    if (placement==null) continue;
088                    Exam exam = (Exam)placement.variable();
089                    csv.addLine(new CSVField[] {
090                       new CSVField(first?room.getName():""),
091                       new CSVField(first?""+room.getSize():""),
092                       new CSVField(first?""+room.getAltSize():""),
093                       new CSVField(period.getIndex()+1),
094                       new CSVField(day==period.getDay()?"":period.getDayStr()),
095                       new CSVField(period.getTimeStr()),
096                       new CSVField(exam.getName()),
097                       new CSVField(exam.getStudents().size())
098                    });
099                    first=false;
100                    day = period.getDay();
101                }
102            }
103            return csv;
104        }
105    }