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