    001package org.cpsolver.exam.reports;
    002
    003import java.util.ArrayList;
    004import java.util.Iterator;
    005import java.util.List;
    006
    007import org.cpsolver.exam.model.Exam;
    008import org.cpsolver.exam.model.ExamModel;
    009import org.cpsolver.exam.model.ExamOwner;
    010import org.cpsolver.exam.model.ExamPlacement;
    011import org.cpsolver.exam.model.ExamRoomPlacement;
    012import org.cpsolver.ifs.assignment.Assignment;
    013import org.cpsolver.ifs.util.CSVFile;
    014import org.cpsolver.ifs.util.CSVFile.CSVField;
    015
    016
    017/**
    018 * Export exam time and room assignments into a CSV file. Similar to
    019 * {@link ExamAssignments}, however, a line is created for each course/section. <br>
    020 * <br>
    021 * Usage:
    022 * <pre><code>
    023 * &nbsp;&nbsp;&nbsp;&nbsp;new ExamCourseSectionAssignments(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 ExamCourseSectionAssignments {
    047    private ExamModel iModel = null;
    048
    049    /**
    050     * Constructor
    051     *
    052     * @param model
    053     *            examination timetabling model
    054     */
    055    public ExamCourseSectionAssignments(ExamModel model) {
    056        iModel = model;
    057    }
    058
    059    /**
    060     * generate report
    061     * @param assignment current assignment
    062     * @return resultant report
    063     */
    064    public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
    065        CSVFile csv = new CSVFile();
    066        csv.setHeader(new CSVField[] { new CSVField("Section/Course"), new CSVField("Enrl"), new CSVField("Alt"),
    067                new CSVField("Period"), new CSVField("Date"), new CSVField("Time"), new CSVField("Room"),
    068                new CSVField("Cap") });
    069        for (Exam exam : iModel.variables()) {
    070            ExamPlacement placement = assignment.getValue(exam);
    071            for (ExamOwner owner : exam.getOwners()) {
    072                List<CSVField> fields = new ArrayList<CSVField>();
    073                fields.add(new CSVField(owner.getName()));
    074                fields.add(new CSVField(owner.getStudents().size()));
    075                fields.add(new CSVField(exam.hasAltSeating() ? "Yes" : "No"));
    076                if (placement == null) {
    077                    fields.add(new CSVField(""));
    078                    fields.add(new CSVField(""));
    079                    fields.add(new CSVField(""));
    080                    fields.add(new CSVField(""));
    081                    fields.add(new CSVField(""));
    082                } else {
    083                    fields.add(new CSVField(placement.getPeriod().getIndex() + 1));
    084                    fields.add(new CSVField(placement.getPeriod().getDayStr()));
    085                    fields.add(new CSVField(placement.getPeriod().getTimeStr()));
    086                    String rooms = "";
    087                    String roomSizes = "";
    088                    for (Iterator<ExamRoomPlacement> i = placement.getRoomPlacements().iterator(); i.hasNext();) {
    089                        ExamRoomPlacement room = i.next();
    090                        rooms += room.getName();
    091                        roomSizes += room.getSize(exam.hasAltSeating());
    092                        if (i.hasNext()) {
    093                            rooms += ", ";
    094                            roomSizes += ", ";
    095                        }
    096                    }
    097                    fields.add(new CSVField(rooms));
    098                    fields.add(new CSVField(roomSizes));
    099                }
    100                csv.addLine(fields);
    101            }
    102        }
    103        return csv;
    104    }
    105}