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