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