001    package net.sf.cpsolver.exam.reports;
002    
003    import java.util.ArrayList;
004    import java.util.Iterator;
005    import java.util.List;
006    
007    import net.sf.cpsolver.exam.model.Exam;
008    import net.sf.cpsolver.exam.model.ExamModel;
009    import net.sf.cpsolver.exam.model.ExamOwner;
010    import net.sf.cpsolver.exam.model.ExamPlacement;
011    import net.sf.cpsolver.exam.model.ExamRoomPlacement;
012    import net.sf.cpsolver.ifs.util.CSVFile;
013    import net.sf.cpsolver.ifs.util.CSVFile.CSVField;
014    
015    /**
016     * Export exam time and room assignments into a CSV file. Similar to
017     * {@link ExamAssignments}, however, a line is created for each course/section. <br>
018     * <br>
019     * Usage:<br>
020     * <code>
021     * &nbsp;&nbsp;&nbsp;&nbsp;new ExamCourseSectionAssignments(model).report().save(file);
022     * </code> <br>
023     * <br>
024     * 
025     * @version ExamTT 1.2 (Examination Timetabling)<br>
026     *          Copyright (C) 2008 - 2010 Tomas Muller<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     */
044    public class ExamCourseSectionAssignments {
045        private ExamModel iModel = null;
046    
047        /**
048         * Constructor
049         * 
050         * @param model
051         *            examination timetabling model
052         */
053        public ExamCourseSectionAssignments(ExamModel model) {
054            iModel = model;
055        }
056    
057        /**
058         * generate report
059         */
060        public CSVFile report() {
061            CSVFile csv = new CSVFile();
062            csv.setHeader(new CSVField[] { new CSVField("Section/Course"), new CSVField("Enrl"), new CSVField("Alt"),
063                    new CSVField("Period"), new CSVField("Date"), new CSVField("Time"), new CSVField("Room"),
064                    new CSVField("Cap") });
065            for (Exam exam : iModel.variables()) {
066                ExamPlacement placement = exam.getAssignment();
067                for (ExamOwner owner : exam.getOwners()) {
068                    List<CSVField> fields = new ArrayList<CSVField>();
069                    fields.add(new CSVField(owner.getName()));
070                    fields.add(new CSVField(owner.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.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            }
099            return csv;
100        }
101    }