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 * @author  Tomáš Müller
028 * @version ExamTT 1.3 (Examination Timetabling)<br>
029 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
030 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
031 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
032 * <br>
033 *          This library is free software; you can redistribute it and/or modify
034 *          it under the terms of the GNU Lesser General Public License as
035 *          published by the Free Software Foundation; either version 3 of the
036 *          License, or (at your option) any later version. <br>
037 * <br>
038 *          This library is distributed in the hope that it will be useful, but
039 *          WITHOUT ANY WARRANTY; without even the implied warranty of
040 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
041 *          Lesser General Public License for more details. <br>
042 * <br>
043 *          You should have received a copy of the GNU Lesser General Public
044 *          License along with this library; if not see
045 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
046 */
047public class ExamCourseSectionAssignments {
048    private ExamModel iModel = null;
049
050    /**
051     * Constructor
052     * 
053     * @param model
054     *            examination timetabling model
055     */
056    public ExamCourseSectionAssignments(ExamModel model) {
057        iModel = model;
058    }
059
060    /**
061     * generate report
062     * @param assignment current assignment
063     * @return resultant report
064     */
065    public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
066        CSVFile csv = new CSVFile();
067        csv.setHeader(new CSVField[] { new CSVField("Section/Course"), new CSVField("Enrl"), new CSVField("Alt"),
068                new CSVField("Period"), new CSVField("Date"), new CSVField("Time"), new CSVField("Room"),
069                new CSVField("Cap") });
070        for (Exam exam : iModel.variables()) {
071            ExamPlacement placement = assignment.getValue(exam);
072            for (ExamOwner owner : exam.getOwners()) {
073                List<CSVField> fields = new ArrayList<CSVField>();
074                fields.add(new CSVField(owner.getName()));
075                fields.add(new CSVField(owner.getStudents().size()));
076                fields.add(new CSVField(exam.hasAltSeating() ? "Yes" : "No"));
077                if (placement == null) {
078                    fields.add(new CSVField(""));
079                    fields.add(new CSVField(""));
080                    fields.add(new CSVField(""));
081                    fields.add(new CSVField(""));
082                    fields.add(new CSVField(""));
083                } else {
084                    fields.add(new CSVField(placement.getPeriod().getIndex() + 1));
085                    fields.add(new CSVField(placement.getPeriod().getDayStr()));
086                    fields.add(new CSVField(placement.getPeriod().getTimeStr()));
087                    String rooms = "";
088                    String roomSizes = "";
089                    for (Iterator<ExamRoomPlacement> i = placement.getRoomPlacements().iterator(); i.hasNext();) {
090                        ExamRoomPlacement room = i.next();
091                        rooms += room.getName();
092                        roomSizes += room.getSize(exam.hasAltSeating());
093                        if (i.hasNext()) {
094                            rooms += ", ";
095                            roomSizes += ", ";
096                        }
097                    }
098                    fields.add(new CSVField(rooms));
099                    fields.add(new CSVField(roomSizes));
100                }
101                csv.addLine(fields);
102            }
103        }
104        return csv;
105    }
106}