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