    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 * @author  Tomáš Müller
    025 * @version ExamTT 1.3 (Examination Timetabling)<br>
    026 *          Copyright (C) 2008 - 2014 Tomáš Müller<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 */
    044public class ExamAssignments {
    045    private ExamModel iModel = null;
    046
    047    /**
    048     * Constructor
    049     *
    050     * @param model
    051     *            examination timetabling model
    052     */
    053    public ExamAssignments(ExamModel model) {
    054        iModel = model;
    055    }
    056
    057    /**
    058     * generate report
    059     * @param assignment current assignment
    060     * @return resultant report
    061     */
    062    public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
    063        CSVFile csv = new CSVFile();
    064        csv.setHeader(new CSVField[] { new CSVField("Exam"), new CSVField("Enrl"), new CSVField("Alt"),
    065                new CSVField("Period"), new CSVField("Date"), new CSVField("Time"), new CSVField("Room"),
    066                new CSVField("Cap") });
    067        for (Exam exam : iModel.variables()) {
    068            ExamPlacement placement = assignment.getValue(exam);
    069            ArrayList<CSVField> fields = new ArrayList<CSVField>();
    070            fields.add(new CSVField(exam.getName()));
    071            fields.add(new CSVField(exam.getStudents().size()));
    072            fields.add(new CSVField(exam.hasAltSeating() ? "Yes" : "No"));
    073            if (placement == null) {
    074                fields.add(new CSVField(""));
    075                fields.add(new CSVField(""));
    076                fields.add(new CSVField(""));
    077                fields.add(new CSVField(""));
    078                fields.add(new CSVField(""));
    079            } else {
    080                fields.add(new CSVField(placement.getPeriod().getIndex() + 1));
    081                fields.add(new CSVField(placement.getPeriod().getDayStr()));
    082                fields.add(new CSVField(placement.getPeriod().getTimeStr()));
    083                String rooms = "";
    084                String roomSizes = "";
    085                for (Iterator<ExamRoomPlacement> i = placement.getRoomPlacements().iterator(); i.hasNext();) {
    086                    ExamRoomPlacement room = i.next();
    087                    rooms += room.getRoom().getName();
    088                    roomSizes += room.getSize(exam.hasAltSeating());
    089                    if (i.hasNext()) {
    090                        rooms += ", ";
    091                        roomSizes += ", ";
    092                    }
    093                }
    094                fields.add(new CSVField(rooms));
    095                fields.add(new CSVField(roomSizes));
    096            }
    097            csv.addLine(fields);
    098        }
    099        return csv;
    100    }
    101}