001package org.cpsolver.studentsct;
002
003import java.io.File;
004import java.io.FileOutputStream;
005import java.io.IOException;
006import java.math.RoundingMode;
007import java.text.DecimalFormat;
008import java.text.DecimalFormatSymbols;
009import java.util.BitSet;
010import java.util.Date;
011import java.util.Locale;
012import java.util.Map;
013import java.util.Set;
014import java.util.TreeSet;
015
016import org.cpsolver.coursett.IdConvertor;
017import org.cpsolver.coursett.model.RoomLocation;
018import org.cpsolver.coursett.model.TimeLocation;
019import org.cpsolver.ifs.solver.Solver;
020import org.cpsolver.ifs.util.Progress;
021import org.cpsolver.studentsct.constraint.LinkedSections;
022import org.cpsolver.studentsct.model.AcademicAreaCode;
023import org.cpsolver.studentsct.model.AreaClassificationMajor;
024import org.cpsolver.studentsct.model.Choice;
025import org.cpsolver.studentsct.model.Config;
026import org.cpsolver.studentsct.model.Course;
027import org.cpsolver.studentsct.model.CourseRequest;
028import org.cpsolver.studentsct.model.Enrollment;
029import org.cpsolver.studentsct.model.FreeTimeRequest;
030import org.cpsolver.studentsct.model.Instructor;
031import org.cpsolver.studentsct.model.Offering;
032import org.cpsolver.studentsct.model.Request;
033import org.cpsolver.studentsct.model.RequestGroup;
034import org.cpsolver.studentsct.model.Section;
035import org.cpsolver.studentsct.model.Student;
036import org.cpsolver.studentsct.model.Subpart;
037import org.cpsolver.studentsct.model.Unavailability;
038import org.cpsolver.studentsct.reservation.CourseReservation;
039import org.cpsolver.studentsct.reservation.CurriculumReservation;
040import org.cpsolver.studentsct.reservation.DummyReservation;
041import org.cpsolver.studentsct.reservation.GroupReservation;
042import org.cpsolver.studentsct.reservation.IndividualReservation;
043import org.cpsolver.studentsct.reservation.Reservation;
044import org.cpsolver.studentsct.reservation.ReservationOverride;
045import org.dom4j.Document;
046import org.dom4j.DocumentHelper;
047import org.dom4j.Element;
048import org.dom4j.io.OutputFormat;
049import org.dom4j.io.XMLWriter;
050
051
052/**
053 * Save student sectioning solution into an XML file.
054 * 
055 * <br>
056 * <br>
057 * Parameters:
058 * <table border='1' summary='Related Solver Parameters'>
059 * <tr>
060 * <th>Parameter</th>
061 * <th>Type</th>
062 * <th>Comment</th>
063 * </tr>
064 * <tr>
065 * <td>General.Output</td>
066 * <td>{@link String}</td>
067 * <td>Folder with the output solution in XML format (solution.xml)</td>
068 * </tr>
069 * <tr>
070 * <td>Xml.ConvertIds</td>
071 * <td>{@link Boolean}</td>
072 * <td>If true, ids are converted (to be able to make input data public)</td>
073 * </tr>
074 * <tr>
075 * <td>Xml.ShowNames</td>
076 * <td>{@link Boolean}</td>
077 * <td>If false, names are not exported (to be able to make input data public)</td>
078 * </tr>
079 * <tr>
080 * <td>Xml.SaveBest</td>
081 * <td>{@link Boolean}</td>
082 * <td>If true, best solution is saved.</td>
083 * </tr>
084 * <tr>
085 * <td>Xml.SaveInitial</td>
086 * <td>{@link Boolean}</td>
087 * <td>If true, initial solution is saved.</td>
088 * </tr>
089 * <tr>
090 * <td>Xml.SaveCurrent</td>
091 * <td>{@link Boolean}</td>
092 * <td>If true, current solution is saved.</td>
093 * </tr>
094 * <tr>
095 * <td>Xml.SaveOnlineSectioningInfo</td>
096 * <td>{@link Boolean}</td>
097 * <td>If true, save online sectioning info (i.e., expected and held space of
098 * each section)</td>
099 * </tr>
100 * <tr>
101 * <td>Xml.SaveStudentInfo</td>
102 * <td>{@link Boolean}</td>
103 * <td>If true, save student information (i.e., academic area classification,
104 * major, minor)</td>
105 * </tr>
106 * </table>
107 * <br>
108 * <br>
109 * Usage:
110 * <pre><code>
111 * new StudentSectioningXMLSaver(solver).save(new File("solution.xml")); 
112 * </code></pre>
113 * 
114 * @version StudentSct 1.3 (Student Sectioning)<br>
115 *          Copyright (C) 2007 - 2014 Tomas Muller<br>
116 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
117 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
118 * <br>
119 *          This library is free software; you can redistribute it and/or modify
120 *          it under the terms of the GNU Lesser General Public License as
121 *          published by the Free Software Foundation; either version 3 of the
122 *          License, or (at your option) any later version. <br>
123 * <br>
124 *          This library is distributed in the hope that it will be useful, but
125 *          WITHOUT ANY WARRANTY; without even the implied warranty of
126 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
127 *          Lesser General Public License for more details. <br>
128 * <br>
129 *          You should have received a copy of the GNU Lesser General Public
130 *          License along with this library; if not see
131 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
132 */
133
134public class StudentSectioningXMLSaver extends StudentSectioningSaver {
135    private static org.apache.log4j.Logger sLogger = org.apache.log4j.Logger.getLogger(StudentSectioningXMLSaver.class);
136    private static DecimalFormat[] sDF = { new DecimalFormat(""), new DecimalFormat("0"), new DecimalFormat("00"),
137            new DecimalFormat("000"), new DecimalFormat("0000"), new DecimalFormat("00000"),
138            new DecimalFormat("000000"), new DecimalFormat("0000000") };
139    private static DecimalFormat sStudentWeightFormat = new DecimalFormat("0.0000", new DecimalFormatSymbols(Locale.US));
140    private File iOutputFolder = null;
141
142    private boolean iSaveBest = false;
143    private boolean iSaveInitial = false;
144    private boolean iSaveCurrent = false;
145    private boolean iSaveOnlineSectioningInfo = false;
146    private boolean iSaveStudentInfo = true;
147
148    private boolean iConvertIds = false;
149    private boolean iShowNames = false;
150    
151    static {
152        sStudentWeightFormat.setRoundingMode(RoundingMode.DOWN);
153    }
154
155    /**
156     * Constructor
157     * 
158     * @param solver
159     *            student sectioning solver
160     */
161    public StudentSectioningXMLSaver(Solver<Request, Enrollment> solver) {
162        super(solver);
163        iOutputFolder = new File(getModel().getProperties().getProperty("General.Output",
164                "." + File.separator + "output"));
165        iSaveBest = getModel().getProperties().getPropertyBoolean("Xml.SaveBest", true);
166        iSaveInitial = getModel().getProperties().getPropertyBoolean("Xml.SaveInitial", true);
167        iSaveCurrent = getModel().getProperties().getPropertyBoolean("Xml.SaveCurrent", false);
168        iSaveOnlineSectioningInfo = getModel().getProperties().getPropertyBoolean("Xml.SaveOnlineSectioningInfo", true);
169        iSaveStudentInfo = getModel().getProperties().getPropertyBoolean("Xml.SaveStudentInfo", true);
170        iShowNames = getModel().getProperties().getPropertyBoolean("Xml.ShowNames", true);
171        iConvertIds = getModel().getProperties().getPropertyBoolean("Xml.ConvertIds", false);
172    }
173
174    /** Convert bitset to a bit string */
175    private static String bitset2string(BitSet b) {
176        StringBuffer sb = new StringBuffer();
177        for (int i = 0; i < b.length(); i++)
178            sb.append(b.get(i) ? "1" : "0");
179        return sb.toString();
180    }
181
182    /** Generate id for given object with the given id */
183    private String getId(String type, String id) {
184        if (!iConvertIds)
185            return id.toString();
186        return IdConvertor.getInstance().convert(type, id);
187    }
188
189    /** Generate id for given object with the given id */
190    private String getId(String type, Number id) {
191        return getId(type, id.toString());
192    }
193
194    /** Generate id for given object with the given id */
195    private String getId(String type, long id) {
196        return getId(type, String.valueOf(id));
197    }
198
199    /** Save an XML file */
200    @Override
201    public void save() throws Exception {
202        save(null);
203    }
204
205    /**
206     * Save an XML file
207     * 
208     * @param outFile
209     *            output file
210     * @throws Exception thrown when the save fails
211     */
212    public void save(File outFile) throws Exception {
213        if (outFile == null) {
214            outFile = new File(iOutputFolder, "solution.xml");
215        } else if (outFile.getParentFile() != null) {
216            outFile.getParentFile().mkdirs();
217        }
218        sLogger.debug("Writting XML data to:" + outFile);
219
220        Document document = DocumentHelper.createDocument();
221        document.addComment("Student Sectioning");
222        
223        populate(document);
224
225        FileOutputStream fos = null;
226        try {
227            fos = new FileOutputStream(outFile);
228            (new XMLWriter(fos, OutputFormat.createPrettyPrint())).write(document);
229            fos.flush();
230            fos.close();
231            fos = null;
232        } finally {
233            try {
234                if (fos != null)
235                    fos.close();
236            } catch (IOException e) {
237            }
238        }
239
240        if (iConvertIds)
241            IdConvertor.getInstance().save();
242    }
243    
244    public Document saveDocument() {
245        Document document = DocumentHelper.createDocument();
246        document.addComment("Student Sectioning");
247        
248        populate(document);
249
250        return document;
251    }
252
253    /**
254     * Fill in all the data into the given document
255     * @param document document to be populated
256     */
257    protected void populate(Document document) {
258        if (iSaveCurrent || iSaveBest) {
259            StringBuffer comments = new StringBuffer("Solution Info:\n");
260            Map<String, String> solutionInfo = (getSolution() == null ? getModel().getExtendedInfo(getAssignment()) : getSolution().getExtendedInfo());
261            for (String key : new TreeSet<String>(solutionInfo.keySet())) {
262                String value = solutionInfo.get(key);
263                comments.append("    " + key + ": " + value + "\n");
264            }
265            document.addComment(comments.toString());
266        }
267
268        Element root = document.addElement("sectioning");
269        root.addAttribute("version", "1.0");
270        root.addAttribute("initiative", getModel().getProperties().getProperty("Data.Initiative"));
271        root.addAttribute("term", getModel().getProperties().getProperty("Data.Term"));
272        root.addAttribute("year", getModel().getProperties().getProperty("Data.Year"));
273        root.addAttribute("created", String.valueOf(new Date()));
274
275        saveOfferings(root);
276
277        saveStudents(root);
278        
279        saveLinkedSections(root);
280        
281        saveTravelTimes(root);
282
283        if (iShowNames) {
284            Progress.getInstance(getModel()).save(root);
285        }
286    }
287    
288    /**
289     * Save offerings
290     * @param root document root
291     */
292    protected void saveOfferings(Element root) {
293        Element offeringsEl = root.addElement("offerings");
294        for (Offering offering : getModel().getOfferings()) {
295            Element offeringEl = offeringsEl.addElement("offering");
296            saveOffering(offeringEl, offering);
297            saveReservations(offeringEl, offering);
298        }
299    }
300    
301    /**
302     * Save given offering
303     * @param offeringEl offering element to be populated
304     * @param offering offering to be saved
305     */
306    protected void saveOffering(Element offeringEl, Offering offering) {
307        offeringEl.addAttribute("id", getId("offering", offering.getId()));
308        if (iShowNames)
309            offeringEl.addAttribute("name", offering.getName());
310        for (Course course : offering.getCourses()) {
311            Element courseEl = offeringEl.addElement("course");
312            saveCourse(courseEl, course);
313        }
314        for (Config config : offering.getConfigs()) {
315            Element configEl = offeringEl.addElement("config");
316            saveConfig(configEl, config);
317        }
318    }
319    
320    /**
321     * Save given course
322     * @param courseEl course element to be populated
323     * @param course course to be saved
324     */
325    protected void saveCourse(Element courseEl, Course course) {
326        courseEl.addAttribute("id", getId("course", course.getId()));
327        if (iShowNames)
328            courseEl.addAttribute("subjectArea", course.getSubjectArea());
329        if (iShowNames)
330            courseEl.addAttribute("courseNbr", course.getCourseNumber());
331        if (iShowNames && course.getLimit() >= 0)
332            courseEl.addAttribute("limit", String.valueOf(course.getLimit()));
333        if (iShowNames && course.getProjected() != 0)
334            courseEl.addAttribute("projected", String.valueOf(course.getProjected()));
335        if (iShowNames && course.getCredit() != null)
336            courseEl.addAttribute("credit", course.getCredit());
337    }
338    
339    /**
340     * Save given config
341     * @param configEl config element to be populated
342     * @param config config to be saved
343     */
344    protected void saveConfig(Element configEl, Config config) {
345        configEl.addAttribute("id", getId("config", config.getId()));
346        if (config.getLimit() >= 0)
347            configEl.addAttribute("limit", String.valueOf(config.getLimit()));
348        if (iShowNames)
349            configEl.addAttribute("name", config.getName());
350        for (Subpart subpart : config.getSubparts()) {
351            Element subpartEl = configEl.addElement("subpart");
352            saveSubpart(subpartEl, subpart);
353        }
354        if (config.getInstructionalMethodId() != null) {
355            Element imEl = configEl.addElement("instructional-method");
356            imEl.addAttribute("id", getId("instructional-method", config.getInstructionalMethodId()));
357            if (iShowNames && config.getInstructionalMethodName() != null)
358                imEl.addAttribute("name", config.getInstructionalMethodName());
359        }
360    }
361    
362    /**
363     * Save scheduling subpart
364     * @param subpartEl subpart element to be populated
365     * @param subpart subpart to be saved
366     */
367    protected void saveSubpart(Element subpartEl, Subpart subpart) {
368        subpartEl.addAttribute("id", getId("subpart", subpart.getId()));
369        subpartEl.addAttribute("itype", subpart.getInstructionalType());
370        if (subpart.getParent() != null)
371            subpartEl.addAttribute("parent", getId("subpart", subpart.getParent().getId()));
372        if (iShowNames) {
373            subpartEl.addAttribute("name", subpart.getName());
374            if (subpart.getCredit() != null)
375                subpartEl.addAttribute("credit", subpart.getCredit());
376        }
377        if (subpart.isAllowOverlap())
378            subpartEl.addAttribute("allowOverlap", "true");
379        for (Section section : subpart.getSections()) {
380            Element sectionEl = subpartEl.addElement("section");
381            saveSection(sectionEl, section);
382        }
383    }
384    
385    /**
386     * Save section
387     * @param sectionEl section element to be populated
388     * @param section section to be saved
389     */
390    protected void saveSection(Element sectionEl, Section section) {
391        sectionEl.addAttribute("id", getId("section", section.getId()));
392        sectionEl.addAttribute("limit", String.valueOf(section.getLimit()));
393        if (section.isCancelled())
394            sectionEl.addAttribute("cancelled", "true");
395        if (iShowNames && section.getNameByCourse() != null)
396            for (Map.Entry<Long, String> entry: section.getNameByCourse().entrySet())
397                sectionEl.addElement("cname").addAttribute("id", getId("course", entry.getKey())).setText(entry.getValue());
398        if (section.getParent() != null)
399            sectionEl.addAttribute("parent", getId("section", section.getParent().getId()));
400        if (section.hasInstructors()) {
401            for (Instructor instructor: section.getInstructors()) {
402                Element instructorEl = sectionEl.addElement("instructor");
403                instructorEl.addAttribute("id", getId("instructor", instructor.getId()));
404                if (iShowNames && instructor.getName() != null)
405                    instructorEl.addAttribute("name", instructor.getName());
406                if (iShowNames && instructor.getExternalId() != null)
407                    instructorEl.addAttribute("externalId", instructor.getExternalId());
408                if (iShowNames && instructor.getEmail() != null)
409                    instructorEl.addAttribute("email", instructor.getExternalId());
410            }
411        }
412        if (iShowNames)
413            sectionEl.addAttribute("name", section.getName());
414        if (section.getPlacement() != null) {
415            TimeLocation tl = section.getPlacement().getTimeLocation();
416            if (tl != null) {
417                Element timeLocationEl = sectionEl.addElement("time");
418                timeLocationEl.addAttribute("days", sDF[7].format(Long.parseLong(Integer
419                        .toBinaryString(tl.getDayCode()))));
420                timeLocationEl.addAttribute("start", String.valueOf(tl.getStartSlot()));
421                timeLocationEl.addAttribute("length", String.valueOf(tl.getLength()));
422                if (tl.getBreakTime() != 0)
423                    timeLocationEl.addAttribute("breakTime", String.valueOf(tl.getBreakTime()));
424                if (iShowNames && tl.getTimePatternId() != null)
425                    timeLocationEl.addAttribute("pattern", getId("timePattern", tl.getTimePatternId()));
426                if (iShowNames && tl.getDatePatternId() != null)
427                    timeLocationEl.addAttribute("datePattern", tl.getDatePatternId().toString());
428                if (iShowNames && tl.getDatePatternName() != null
429                        && tl.getDatePatternName().length() > 0)
430                    timeLocationEl.addAttribute("datePatternName", tl.getDatePatternName());
431                timeLocationEl.addAttribute("dates", bitset2string(tl.getWeekCode()));
432                if (iShowNames)
433                    timeLocationEl.setText(tl.getLongName(true));
434            }
435            for (RoomLocation rl : section.getRooms()) {
436                Element roomLocationEl = sectionEl.addElement("room");
437                roomLocationEl.addAttribute("id", getId("room", rl.getId()));
438                if (iShowNames && rl.getBuildingId() != null)
439                    roomLocationEl.addAttribute("building", getId("building", rl.getBuildingId()));
440                if (iShowNames && rl.getName() != null)
441                    roomLocationEl.addAttribute("name", rl.getName());
442                roomLocationEl.addAttribute("capacity", String.valueOf(rl.getRoomSize()));
443                if (rl.getPosX() != null && rl.getPosY() != null)
444                    roomLocationEl.addAttribute("location", rl.getPosX() + "," + rl.getPosY());
445                if (rl.getIgnoreTooFar())
446                    roomLocationEl.addAttribute("ignoreTooFar", "true");
447            }
448        }
449        if (iSaveOnlineSectioningInfo) {
450            if (section.getSpaceHeld() != 0.0)
451                sectionEl.addAttribute("hold", sStudentWeightFormat.format(section.getSpaceHeld()));
452            if (section.getSpaceExpected() != 0.0)
453                sectionEl.addAttribute("expect", sStudentWeightFormat
454                        .format(section.getSpaceExpected()));
455        }
456        if (section.getIgnoreConflictWithSectionIds() != null && !section.getIgnoreConflictWithSectionIds().isEmpty()) {
457            Element ignoreEl = sectionEl.addElement("no-conflicts");
458            for (Long sectionId: section.getIgnoreConflictWithSectionIds())
459                ignoreEl.addElement("section").addAttribute("id", getId("section", sectionId));
460        }
461    }
462    
463    /**
464     * Save reservations of the given offering
465     * @param offeringEl offering element to be populated with reservations
466     * @param offering offering which reservations are to be saved
467     */
468    protected void saveReservations(Element offeringEl, Offering offering) {
469        if (!offering.getReservations().isEmpty()) {
470            for (Reservation r: offering.getReservations()) {
471                saveReservation(offeringEl.addElement("reservation"), r);
472            }
473        }
474    }
475    
476    /**
477     * Save reservation
478     * @param reservationEl reservation element to be populated
479     * @param reservation reservation to be saved
480     */
481    protected void saveReservation(Element reservationEl, Reservation reservation) {
482        reservationEl.addAttribute("id", getId("reservation", reservation.getId()));
483        reservationEl.addAttribute("expired", reservation.isExpired() ? "true" : "false");
484        if (reservation instanceof GroupReservation) {
485            GroupReservation gr = (GroupReservation)reservation;
486            reservationEl.addAttribute("type", "group");
487            for (Long studentId: gr.getStudentIds())
488                reservationEl.addElement("student").addAttribute("id", getId("student", studentId));
489            if (gr.getReservationLimit() >= 0.0)
490                reservationEl.addAttribute("limit", String.valueOf(gr.getReservationLimit()));
491        } else if (reservation instanceof ReservationOverride) {
492            reservationEl.addAttribute("type", "override");
493            ReservationOverride o = (ReservationOverride)reservation;
494            for (Long studentId: o.getStudentIds())
495                reservationEl.addElement("student").addAttribute("id", getId("student", studentId));
496        } else if (reservation instanceof IndividualReservation) {
497            reservationEl.addAttribute("type", "individual");
498            for (Long studentId: ((IndividualReservation)reservation).getStudentIds())
499                reservationEl.addElement("student").addAttribute("id", getId("student", studentId));
500        } else if (reservation instanceof CurriculumReservation) {
501            reservationEl.addAttribute("type", "curriculum");
502            CurriculumReservation cr = (CurriculumReservation)reservation;
503            if (cr.getReservationLimit() >= 0.0)
504                reservationEl.addAttribute("limit", String.valueOf(cr.getReservationLimit()));
505            reservationEl.addAttribute("area", cr.getAcademicArea());
506            for (String clasf: cr.getClassifications())
507                reservationEl.addElement("classification").addAttribute("code", clasf);
508            for (String major: cr.getMajors())
509                reservationEl.addElement("major").addAttribute("code", major);
510        } else if (reservation instanceof CourseReservation) {
511            reservationEl.addAttribute("type", "course");
512            CourseReservation cr = (CourseReservation)reservation;
513            reservationEl.addAttribute("course", getId("course",cr.getCourse().getId()));
514        } else if (reservation instanceof DummyReservation) {
515            reservationEl.addAttribute("type", "dummy");
516        }
517        reservationEl.addAttribute("priority", String.valueOf(reservation.getPriority()));
518        reservationEl.addAttribute("mustBeUsed", reservation.mustBeUsed() ? "true" : "false");
519        reservationEl.addAttribute("allowOverlap", reservation.isAllowOverlap() ? "true" : "false");
520        reservationEl.addAttribute("canAssignOverLimit", reservation.canAssignOverLimit() ? "true" : "false");
521        for (Config config: reservation.getConfigs())
522            reservationEl.addElement("config").addAttribute("id", getId("config", config.getId()));
523        for (Map.Entry<Subpart, Set<Section>> entry: reservation.getSections().entrySet()) {
524            for (Section section: entry.getValue()) {
525                reservationEl.addElement("section").addAttribute("id", getId("section", section.getId()));
526            }
527        }
528    }
529    
530    /**
531     * Save students
532     * @param root document root
533     */
534    protected void saveStudents(Element root) {
535        Element studentsEl = root.addElement("students");
536        for (Student student : getModel().getStudents()) {
537            Element studentEl = studentsEl.addElement("student");
538            saveStudent(studentEl, student);
539            for (Request request : student.getRequests()) {
540                saveRequest(studentEl, request);
541            }
542        }
543    }
544    
545    /**
546     * Save student
547     * @param studentEl student element to be populated
548     * @param student student to be saved
549     */
550    protected void saveStudent(Element studentEl, Student student) {
551        studentEl.addAttribute("id", getId("student", student.getId()));
552        if (iShowNames) {
553            if (student.getExternalId() != null && !student.getExternalId().isEmpty())
554                studentEl.addAttribute("externalId", student.getExternalId());
555            if (student.getName() != null && !student.getName().isEmpty())
556                studentEl.addAttribute("name", student.getName());
557            if (student.getStatus() != null && !student.getStatus().isEmpty())
558                studentEl.addAttribute("status", student.getStatus());
559        }
560        if (student.isDummy())
561            studentEl.addAttribute("dummy", "true");
562        if (student.isNeedShortDistances())
563            studentEl.addAttribute("shortDistances", "true");
564        if (iSaveStudentInfo) {
565            for (AcademicAreaCode aac : student.getAcademicAreaClasiffications()) {
566                Element aacEl = studentEl.addElement("classification");
567                if (aac.getArea() != null)
568                    aacEl.addAttribute("area", aac.getArea());
569                if (aac.getCode() != null)
570                    aacEl.addAttribute("code", aac.getCode());
571            }
572            for (AcademicAreaCode aac : student.getMajors()) {
573                Element aacEl = studentEl.addElement("major");
574                if (aac.getArea() != null)
575                    aacEl.addAttribute("area", aac.getArea());
576                if (aac.getCode() != null)
577                    aacEl.addAttribute("code", aac.getCode());
578            }
579            for (AcademicAreaCode aac : student.getMinors()) {
580                Element aacEl = studentEl.addElement("minor");
581                if (aac.getArea() != null)
582                    aacEl.addAttribute("area", aac.getArea());
583                if (aac.getCode() != null)
584                    aacEl.addAttribute("code", aac.getCode());
585            }
586            for (AreaClassificationMajor acm : student.getAreaClassificationMajors()) {
587                Element acmEl = studentEl.addElement("acm");
588                if (acm.getArea() != null)
589                    acmEl.addAttribute("area", acm.getArea());
590                if (acm.getArea() != null)
591                    acmEl.addAttribute("classification", acm.getClassification());
592                if (acm.getArea() != null)
593                    acmEl.addAttribute("major", acm.getMajor());
594            }
595        }
596        for (Unavailability unavailability: student.getUnavailabilities()) {
597            Element unavEl = studentEl.addElement("unavailability");
598            unavEl.addAttribute("offering", getId("offering", unavailability.getSection().getSubpart().getConfig().getOffering().getId()));
599            unavEl.addAttribute("section", getId("section", unavailability.getSection().getId()));
600            if (unavailability.isAllowOverlap()) unavEl.addAttribute("allowOverlap", "true");
601        }
602    }
603    
604    /**
605     * Save request
606     * @param studentEl student element to be populated
607     * @param request request to be saved
608     */
609    protected void saveRequest(Element studentEl, Request request) {
610        if (request instanceof FreeTimeRequest) {
611            saveFreeTimeRequest(studentEl.addElement("freeTime"), (FreeTimeRequest) request);
612        } else if (request instanceof CourseRequest) {
613            saveCourseRequest(studentEl.addElement("course"), (CourseRequest) request);
614        }
615    }
616    
617    /**
618     * Save free time request
619     * @param requestEl request element to be populated
620     * @param request free time request to be saved 
621     */
622    protected void saveFreeTimeRequest(Element requestEl, FreeTimeRequest request) {
623        requestEl.addAttribute("id", getId("request", request.getId()));
624        requestEl.addAttribute("priority", String.valueOf(request.getPriority()));
625        if (request.isAlternative())
626            requestEl.addAttribute("alternative", "true");
627        if (request.getWeight() != 1.0)
628            requestEl.addAttribute("weight", sStudentWeightFormat.format(request.getWeight()));
629        TimeLocation tl = request.getTime();
630        if (tl != null) {
631            requestEl.addAttribute("days", sDF[7].format(Long.parseLong(Integer.toBinaryString(tl
632                    .getDayCode()))));
633            requestEl.addAttribute("start", String.valueOf(tl.getStartSlot()));
634            requestEl.addAttribute("length", String.valueOf(tl.getLength()));
635            if (iShowNames && tl.getDatePatternId() != null)
636                requestEl.addAttribute("datePattern", tl.getDatePatternId().toString());
637            requestEl.addAttribute("dates", bitset2string(tl.getWeekCode()));
638            if (iShowNames)
639                requestEl.setText(tl.getLongName(true));
640        }
641        if (iSaveInitial && request.getInitialAssignment() != null) {
642            requestEl.addElement("initial");
643        }
644        if (iSaveCurrent && getAssignment().getValue(request) != null) {
645            requestEl.addElement("current");
646        }
647        if (iSaveBest && request.getBestAssignment() != null) {
648            requestEl.addElement("best");
649        }
650    }
651    
652    /**
653     * Save course request 
654     * @param requestEl request element to be populated
655     * @param request course request to be saved
656     */
657    protected void saveCourseRequest(Element requestEl, CourseRequest request) {
658        requestEl.addAttribute("id", getId("request", request.getId()));
659        requestEl.addAttribute("priority", String.valueOf(request.getPriority()));
660        if (request.isAlternative())
661            requestEl.addAttribute("alternative", "true");
662        if (request.getWeight() != 1.0)
663            requestEl.addAttribute("weight", sStudentWeightFormat.format(request.getWeight()));
664        requestEl.addAttribute("waitlist", request.isWaitlist() ? "true" : "false");
665        if (request.getTimeStamp() != null)
666            requestEl.addAttribute("timeStamp", request.getTimeStamp().toString());
667        boolean first = true;
668        for (Course course : request.getCourses()) {
669            if (first)
670                requestEl.addAttribute("course", getId("course", course.getId()));
671            else
672                requestEl.addElement("alternative").addAttribute("course", getId("course", course.getId()));
673            first = false;
674        }
675        for (Choice choice : request.getWaitlistedChoices()) {
676            Element choiceEl = requestEl.addElement("waitlisted");
677            choiceEl.addAttribute("offering", getId("offering", choice.getOffering().getId()));
678            choiceEl.setText(choice.getId());
679        }
680        for (Choice choice : request.getSelectedChoices()) {
681            Element choiceEl = requestEl.addElement("selected");
682            choiceEl.addAttribute("offering", getId("offering", choice.getOffering().getId()));
683            choiceEl.setText(choice.getId());
684        }
685        if (iSaveInitial && request.getInitialAssignment() != null) {
686            saveEnrollment(requestEl.addElement("initial"), request.getInitialAssignment());
687        }
688        if (iSaveCurrent && getAssignment().getValue(request) != null) {
689            saveEnrollment(requestEl.addElement("current"), getAssignment().getValue(request));
690        }
691        if (iSaveBest && request.getBestAssignment() != null) {
692            saveEnrollment(requestEl.addElement("best"), request.getBestAssignment());
693        }
694        for (RequestGroup g: request.getRequestGroups()) {
695            Element groupEl = requestEl.addElement("group").addAttribute("id", getId("group", g.getId())).addAttribute("course", getId("course", g.getCourse().getId()));
696            if (iShowNames)
697                groupEl.addAttribute("name", g.getName());
698        }
699    }
700    
701    /**
702     * Save enrollment
703     * @param assignmentEl assignment element to be populated
704     * @param enrollment enrollment to be saved
705     */
706    protected void saveEnrollment(Element assignmentEl, Enrollment enrollment) {
707        if (enrollment.getReservation() != null)
708            assignmentEl.addAttribute("reservation", getId("reservation", enrollment.getReservation().getId()));
709        for (Section section : enrollment.getSections()) {
710            Element sectionEl = assignmentEl.addElement("section").addAttribute("id",
711                    getId("section", section.getId()));
712            if (iShowNames)
713                sectionEl.setText(section.getName() + " " +
714                        (section.getTime() == null ? " Arr Hrs" : " " + section.getTime().getLongName(true)) +
715                        (section.getNrRooms() == 0 ? "" : " " + section.getPlacement().getRoomName(",")) +
716                        (section.hasInstructors() ? " " + section.getInstructorNames(",") : ""));
717        }
718    }
719    
720    /**
721     * Save linked sections
722     * @param root document root
723     */
724    protected void saveLinkedSections(Element root) {
725        Element constrainstEl = root.addElement("constraints");
726        for (LinkedSections linkedSections: getModel().getLinkedSections()) {
727            Element linkEl = constrainstEl.addElement("linked-sections");
728            linkEl.addAttribute("mustBeUsed", linkedSections.isMustBeUsed() ? "true" : "false");
729            for (Offering offering: linkedSections.getOfferings())
730                for (Subpart subpart: linkedSections.getSubparts(offering))
731                    for (Section section: linkedSections.getSections(subpart))
732                        linkEl.addElement("section")
733                            .addAttribute("offering", getId("offering", offering.getId()))
734                            .addAttribute("id", getId("section", section.getId()));
735        }
736    }
737    
738    /**
739     * Save travel times
740     * @param root document root
741     */
742    protected void saveTravelTimes(Element root) {
743        if (getModel().getDistanceConflict() != null) {
744            Map<Long, Map<Long, Integer>> travelTimes = getModel().getDistanceConflict().getDistanceMetric().getTravelTimes();
745            if (travelTimes != null) {
746                Element travelTimesEl = root.addElement("travel-times");
747                for (Map.Entry<Long, Map<Long, Integer>> e1: travelTimes.entrySet())
748                    for (Map.Entry<Long, Integer> e2: e1.getValue().entrySet())
749                        travelTimesEl.addElement("travel-time")
750                            .addAttribute("id1", getId("room", e1.getKey().toString()))
751                            .addAttribute("id2", getId("room", e2.getKey().toString()))
752                            .addAttribute("minutes", e2.getValue().toString());
753            }
754        }
755    }
756}