001    package net.sf.cpsolver.exam.reports;
002    
003    import java.util.ArrayList;
004    import java.util.Collections;
005    import java.util.Comparator;
006    import java.util.List;
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. <br>
018     * <br>
019     * Usage:<br>
020     * <code>
021     * &nbsp;&nbsp;&nbsp;&nbsp;new ExamRoomSchedule(model).report().save(file);
022     * </code> <br>
023     * <br>
024     * 
025     * @version ExamTT 1.2 (Examination Timetabling)<br>
026     *          Copyright (C) 2008 - 2010 Tomas Muller<br>
027     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
028     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
029     * <br>
030     *          This library is free software; you can redistribute it and/or modify
031     *          it under the terms of the GNU Lesser General Public License as
032     *          published by the Free Software Foundation; either version 3 of the
033     *          License, or (at your option) any later version. <br>
034     * <br>
035     *          This library is distributed in the hope that it will be useful, but
036     *          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. <br>
039     * <br>
040     *          You should have received a copy of the GNU Lesser General Public
041     *          License along with this library; if not see
042     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
043     */
044    public class ExamRoomSchedule {
045        ExamModel iModel = null;
046    
047        /**
048         * Constructor
049         * 
050         * @param model
051         *            examination timetabling model
052         */
053        public ExamRoomSchedule(ExamModel model) {
054            iModel = model;
055        }
056    
057        public CSVFile report() {
058            CSVFile csv = new CSVFile();
059            csv.setHeader(new CSVField[] { new CSVField("Room"), new CSVField("Cap"), new CSVField("AltCap"),
060                    new CSVField("Period"), new CSVField("Date"), new CSVField("Time"), new CSVField("Exam"),
061                    new CSVField("Enrl") });
062            List<ExamRoom> rooms = new ArrayList<ExamRoom>(iModel.getRooms());
063            Collections.sort(rooms, new Comparator<ExamRoom>() {
064                @Override
065                public int compare(ExamRoom r1, ExamRoom r2) {
066                    int cmp = -Double.compare(r1.getSize(), r2.getSize());
067                    if (cmp != 0)
068                        return cmp;
069                    cmp = -Double.compare(r1.getAltSize(), r2.getAltSize());
070                    if (cmp != 0)
071                        return cmp;
072                    return r1.compareTo(r2);
073                }
074            });
075            for (ExamRoom room : rooms) {
076                boolean first = true;
077                int day = -1;
078                for (ExamPeriod period : iModel.getPeriods()) {
079                    for (ExamPlacement placement: room.getPlacements(period)) {
080                        Exam exam = placement.variable();
081                        csv.addLine(new CSVField[] { new CSVField(first ? room.getName() : ""),
082                                new CSVField(first ? "" + room.getSize() : ""),
083                                new CSVField(first ? "" + room.getAltSize() : ""), new CSVField(period.getIndex() + 1),
084                                new CSVField(day == period.getDay() ? "" : period.getDayStr()),
085                                new CSVField(period.getTimeStr()), new CSVField(exam.getName()),
086                                new CSVField(exam.getStudents().size()) });
087                        first = false;
088                        day = period.getDay();
089                    }
090                }
091            }
092            return csv;
093        }
094    }