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 } 324 325 /** 326 * Save given config 327 * @param configEl config element to be populated 328 * @param config config to be saved 329 */ 330 protected void saveConfig(Element configEl, Config config) { 331 configEl.addAttribute("id", getId("config", config.getId())); 332 if (config.getLimit() >= 0) 333 configEl.addAttribute("limit", String.valueOf(config.getLimit())); 334 if (iShowNames) 335 configEl.addAttribute("name", config.getName()); 336 for (Subpart subpart : config.getSubparts()) { 337 Element subpartEl = configEl.addElement("subpart"); 338 saveSubpart(subpartEl, subpart); 339 } 340 } 341 342 /** 343 * Save scheduling subpart 344 * @param subpartEl subpart element to be populated 345 * @param subpart subpart to be saved 346 */ 347 protected void saveSubpart(Element subpartEl, Subpart subpart) { 348 subpartEl.addAttribute("id", getId("subpart", subpart.getId())); 349 subpartEl.addAttribute("itype", subpart.getInstructionalType()); 350 if (subpart.getParent() != null) 351 subpartEl.addAttribute("parent", getId("subpart", subpart.getParent().getId())); 352 if (iShowNames) 353 subpartEl.addAttribute("name", subpart.getName()); 354 if (subpart.isAllowOverlap()) 355 subpartEl.addAttribute("allowOverlap", "true"); 356 for (Section section : subpart.getSections()) { 357 Element sectionEl = subpartEl.addElement("section"); 358 saveSection(sectionEl, section); 359 } 360 } 361 362 /** 363 * Save section 364 * @param sectionEl section element to be populated 365 * @param section section to be saved 366 */ 367 protected void saveSection(Element sectionEl, Section section) { 368 sectionEl.addAttribute("id", getId("section", section.getId())); 369 sectionEl.addAttribute("limit", String.valueOf(section.getLimit())); 370 if (section.getNameByCourse() != null) 371 for (Map.Entry<Long, String> entry: section.getNameByCourse().entrySet()) 372 sectionEl.addElement("cname").addAttribute("id", entry.getKey().toString()).setText(entry.getValue()); 373 if (section.getParent() != null) 374 sectionEl.addAttribute("parent", getId("section", section.getParent().getId())); 375 if (iShowNames && section.getChoice().getInstructorIds() != null) 376 sectionEl.addAttribute("instructorIds", section.getChoice().getInstructorIds()); 377 if (iShowNames && section.getChoice().getInstructorNames() != null) 378 sectionEl.addAttribute("instructorNames", section.getChoice().getInstructorNames()); 379 if (iShowNames) 380 sectionEl.addAttribute("name", section.getName()); 381 if (section.getPlacement() != null) { 382 TimeLocation tl = section.getPlacement().getTimeLocation(); 383 if (tl != null) { 384 Element timeLocationEl = sectionEl.addElement("time"); 385 timeLocationEl.addAttribute("days", sDF[7].format(Long.parseLong(Integer 386 .toBinaryString(tl.getDayCode())))); 387 timeLocationEl.addAttribute("start", String.valueOf(tl.getStartSlot())); 388 timeLocationEl.addAttribute("length", String.valueOf(tl.getLength())); 389 if (tl.getBreakTime() != 0) 390 timeLocationEl.addAttribute("breakTime", String.valueOf(tl.getBreakTime())); 391 if (iShowNames && tl.getTimePatternId() != null) 392 timeLocationEl.addAttribute("pattern", getId("timePattern", tl.getTimePatternId())); 393 if (iShowNames && tl.getDatePatternId() != null) 394 timeLocationEl.addAttribute("datePattern", tl.getDatePatternId().toString()); 395 if (iShowNames && tl.getDatePatternName() != null 396 && tl.getDatePatternName().length() > 0) 397 timeLocationEl.addAttribute("datePatternName", tl.getDatePatternName()); 398 timeLocationEl.addAttribute("dates", bitset2string(tl.getWeekCode())); 399 if (iShowNames) 400 timeLocationEl.setText(tl.getLongName(true)); 401 } 402 for (RoomLocation rl : section.getRooms()) { 403 Element roomLocationEl = sectionEl.addElement("room"); 404 roomLocationEl.addAttribute("id", getId("room", rl.getId())); 405 if (iShowNames && rl.getBuildingId() != null) 406 roomLocationEl.addAttribute("building", getId("building", rl.getBuildingId())); 407 if (iShowNames && rl.getName() != null) 408 roomLocationEl.addAttribute("name", rl.getName()); 409 roomLocationEl.addAttribute("capacity", String.valueOf(rl.getRoomSize())); 410 if (rl.getPosX() != null && rl.getPosY() != null) 411 roomLocationEl.addAttribute("location", rl.getPosX() + "," + rl.getPosY()); 412 if (rl.getIgnoreTooFar()) 413 roomLocationEl.addAttribute("ignoreTooFar", "true"); 414 } 415 } 416 if (iSaveOnlineSectioningInfo) { 417 if (section.getSpaceHeld() != 0.0) 418 sectionEl.addAttribute("hold", sStudentWeightFormat.format(section.getSpaceHeld())); 419 if (section.getSpaceExpected() != 0.0) 420 sectionEl.addAttribute("expect", sStudentWeightFormat 421 .format(section.getSpaceExpected())); 422 } 423 if (section.getIgnoreConflictWithSectionIds() != null && !section.getIgnoreConflictWithSectionIds().isEmpty()) { 424 Element ignoreEl = sectionEl.addElement("no-conflicts"); 425 for (Long sectionId: section.getIgnoreConflictWithSectionIds()) 426 ignoreEl.addElement("section").addAttribute("id", getId("section", sectionId)); 427 } 428 } 429 430 /** 431 * Save reservations of the given offering 432 * @param offeringEl offering element to be populated with reservations 433 * @param offering offering which reservations are to be saved 434 */ 435 protected void saveReservations(Element offeringEl, Offering offering) { 436 if (!offering.getReservations().isEmpty()) { 437 for (Reservation r: offering.getReservations()) { 438 saveReservation(offeringEl.addElement("reservation"), r); 439 } 440 } 441 } 442 443 /** 444 * Save reservation 445 * @param reservationEl reservation element to be populated 446 * @param reservation reservation to be saved 447 */ 448 protected void saveReservation(Element reservationEl, Reservation reservation) { 449 reservationEl.addAttribute("id", getId("reservation", reservation.getId())); 450 reservationEl.addAttribute("expired", reservation.isExpired() ? "true" : "false"); 451 if (reservation instanceof GroupReservation) { 452 GroupReservation gr = (GroupReservation)reservation; 453 reservationEl.addAttribute("type", "group"); 454 for (Long studentId: gr.getStudentIds()) 455 reservationEl.addElement("student").addAttribute("id", getId("student", studentId)); 456 if (gr.getReservationLimit() >= 0.0) 457 reservationEl.addAttribute("limit", String.valueOf(gr.getReservationLimit())); 458 } else if (reservation instanceof ReservationOverride) { 459 reservationEl.addAttribute("type", "override"); 460 ReservationOverride o = (ReservationOverride)reservation; 461 for (Long studentId: o.getStudentIds()) 462 reservationEl.addElement("student").addAttribute("id", getId("student", studentId)); 463 } else if (reservation instanceof IndividualReservation) { 464 reservationEl.addAttribute("type", "individual"); 465 for (Long studentId: ((IndividualReservation)reservation).getStudentIds()) 466 reservationEl.addElement("student").addAttribute("id", getId("student", studentId)); 467 } else if (reservation instanceof CurriculumReservation) { 468 reservationEl.addAttribute("type", "curriculum"); 469 CurriculumReservation cr = (CurriculumReservation)reservation; 470 if (cr.getReservationLimit() >= 0.0) 471 reservationEl.addAttribute("limit", String.valueOf(cr.getReservationLimit())); 472 reservationEl.addAttribute("area", cr.getAcademicArea()); 473 for (String clasf: cr.getClassifications()) 474 reservationEl.addElement("classification").addAttribute("code", clasf); 475 for (String major: cr.getMajors()) 476 reservationEl.addElement("major").addAttribute("code", major); 477 } else if (reservation instanceof CourseReservation) { 478 reservationEl.addAttribute("type", "course"); 479 CourseReservation cr = (CourseReservation)reservation; 480 reservationEl.addAttribute("course", getId("course",cr.getCourse().getId())); 481 } else if (reservation instanceof DummyReservation) { 482 reservationEl.addAttribute("type", "dummy"); 483 } 484 reservationEl.addAttribute("priority", String.valueOf(reservation.getPriority())); 485 reservationEl.addAttribute("mustBeUsed", reservation.mustBeUsed() ? "true" : "false"); 486 reservationEl.addAttribute("allowOverlap", reservation.isAllowOverlap() ? "true" : "false"); 487 reservationEl.addAttribute("canAssignOverLimit", reservation.canAssignOverLimit() ? "true" : "false"); 488 for (Config config: reservation.getConfigs()) 489 reservationEl.addElement("config").addAttribute("id", getId("config", config.getId())); 490 for (Map.Entry<Subpart, Set<Section>> entry: reservation.getSections().entrySet()) { 491 for (Section section: entry.getValue()) { 492 reservationEl.addElement("section").addAttribute("id", getId("section", section.getId())); 493 } 494 } 495 } 496 497 /** 498 * Save students 499 * @param root document root 500 */ 501 protected void saveStudents(Element root) { 502 Element studentsEl = root.addElement("students"); 503 for (Student student : getModel().getStudents()) { 504 Element studentEl = studentsEl.addElement("student"); 505 saveStudent(studentEl, student); 506 for (Request request : student.getRequests()) { 507 saveRequest(studentEl, request); 508 } 509 } 510 } 511 512 /** 513 * Save student 514 * @param studentEl student element to be populated 515 * @param student student to be saved 516 */ 517 protected void saveStudent(Element studentEl, Student student) { 518 studentEl.addAttribute("id", getId("student", student.getId())); 519 if (iShowNames) { 520 if (student.getExternalId() != null && !student.getExternalId().isEmpty()) 521 studentEl.addAttribute("externalId", student.getExternalId()); 522 if (student.getName() != null && !student.getName().isEmpty()) 523 studentEl.addAttribute("name", student.getName()); 524 if (student.getStatus() != null && !student.getStatus().isEmpty()) 525 studentEl.addAttribute("status", student.getStatus()); 526 } 527 if (student.isDummy()) 528 studentEl.addAttribute("dummy", "true"); 529 if (iSaveStudentInfo) { 530 for (AcademicAreaCode aac : student.getAcademicAreaClasiffications()) { 531 Element aacEl = studentEl.addElement("classification"); 532 if (aac.getArea() != null) 533 aacEl.addAttribute("area", aac.getArea()); 534 if (aac.getCode() != null) 535 aacEl.addAttribute("code", aac.getCode()); 536 } 537 for (AcademicAreaCode aac : student.getMajors()) { 538 Element aacEl = studentEl.addElement("major"); 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.getMinors()) { 545 Element aacEl = studentEl.addElement("minor"); 546 if (aac.getArea() != null) 547 aacEl.addAttribute("area", aac.getArea()); 548 if (aac.getCode() != null) 549 aacEl.addAttribute("code", aac.getCode()); 550 } 551 } 552 } 553 554 /** 555 * Save request 556 * @param studentEl student element to be populated 557 * @param request request to be saved 558 */ 559 protected void saveRequest(Element studentEl, Request request) { 560 if (request instanceof FreeTimeRequest) { 561 saveFreeTimeRequest(studentEl.addElement("freeTime"), (FreeTimeRequest) request); 562 } else if (request instanceof CourseRequest) { 563 saveCourseRequest(studentEl.addElement("course"), (CourseRequest) request); 564 } 565 } 566 567 /** 568 * Save free time request 569 * @param requestEl request element to be populated 570 * @param request free time request to be saved 571 */ 572 protected void saveFreeTimeRequest(Element requestEl, FreeTimeRequest request) { 573 requestEl.addAttribute("id", getId("request", request.getId())); 574 requestEl.addAttribute("priority", String.valueOf(request.getPriority())); 575 if (request.isAlternative()) 576 requestEl.addAttribute("alternative", "true"); 577 if (request.getWeight() != 1.0) 578 requestEl.addAttribute("weight", sStudentWeightFormat.format(request.getWeight())); 579 TimeLocation tl = request.getTime(); 580 if (tl != null) { 581 requestEl.addAttribute("days", sDF[7].format(Long.parseLong(Integer.toBinaryString(tl 582 .getDayCode())))); 583 requestEl.addAttribute("start", String.valueOf(tl.getStartSlot())); 584 requestEl.addAttribute("length", String.valueOf(tl.getLength())); 585 if (iShowNames && tl.getDatePatternId() != null) 586 requestEl.addAttribute("datePattern", tl.getDatePatternId().toString()); 587 requestEl.addAttribute("dates", bitset2string(tl.getWeekCode())); 588 if (iShowNames) 589 requestEl.setText(tl.getLongName(true)); 590 } 591 if (iSaveInitial && request.getInitialAssignment() != null) { 592 requestEl.addElement("initial"); 593 } 594 if (iSaveCurrent && getAssignment().getValue(request) != null) { 595 requestEl.addElement("current"); 596 } 597 if (iSaveBest && request.getBestAssignment() != null) { 598 requestEl.addElement("best"); 599 } 600 } 601 602 /** 603 * Save course request 604 * @param requestEl request element to be populated 605 * @param request course request to be saved 606 */ 607 protected void saveCourseRequest(Element requestEl, CourseRequest request) { 608 requestEl.addAttribute("id", getId("request", request.getId())); 609 requestEl.addAttribute("priority", String.valueOf(request.getPriority())); 610 if (request.isAlternative()) 611 requestEl.addAttribute("alternative", "true"); 612 if (request.getWeight() != 1.0) 613 requestEl.addAttribute("weight", sStudentWeightFormat.format(request.getWeight())); 614 requestEl.addAttribute("waitlist", request.isWaitlist() ? "true" : "false"); 615 if (request.getTimeStamp() != null) 616 requestEl.addAttribute("timeStamp", request.getTimeStamp().toString()); 617 boolean first = true; 618 for (Course course : request.getCourses()) { 619 if (first) 620 requestEl.addAttribute("course", getId("course", course.getId())); 621 else 622 requestEl.addElement("alternative").addAttribute("course", getId("course", course.getId())); 623 first = false; 624 } 625 for (Choice choice : request.getWaitlistedChoices()) { 626 Element choiceEl = requestEl.addElement("waitlisted"); 627 choiceEl.addAttribute("offering", getId("offering", choice.getOffering().getId())); 628 choiceEl.setText(choice.getId()); 629 } 630 for (Choice choice : request.getSelectedChoices()) { 631 Element choiceEl = requestEl.addElement("selected"); 632 choiceEl.addAttribute("offering", getId("offering", choice.getOffering().getId())); 633 choiceEl.setText(choice.getId()); 634 } 635 if (iSaveInitial && request.getInitialAssignment() != null) { 636 saveEnrollment(requestEl.addElement("initial"), request.getInitialAssignment()); 637 } 638 if (iSaveCurrent && getAssignment().getValue(request) != null) { 639 saveEnrollment(requestEl.addElement("current"), getAssignment().getValue(request)); 640 } 641 if (iSaveBest && request.getBestAssignment() != null) { 642 saveEnrollment(requestEl.addElement("best"), request.getBestAssignment()); 643 } 644 for (RequestGroup g: request.getRequestGroups()) { 645 Element groupEl = requestEl.addElement("group").addAttribute("id", getId("group", g.getId())).addAttribute("course", getId("course", g.getCourse().getId())); 646 if (iShowNames) 647 groupEl.addAttribute("name", g.getName()); 648 } 649 } 650 651 /** 652 * Save enrollment 653 * @param assignmentEl assignment element to be populated 654 * @param enrollment enrollment to be saved 655 */ 656 protected void saveEnrollment(Element assignmentEl, Enrollment enrollment) { 657 if (enrollment.getReservation() != null) 658 assignmentEl.addAttribute("reservation", getId("reservation", enrollment.getReservation().getId())); 659 for (Section section : enrollment.getSections()) { 660 Element sectionEl = assignmentEl.addElement("section").addAttribute("id", 661 getId("section", section.getId())); 662 if (iShowNames) 663 sectionEl.setText(section.getName() + " " + 664 (section.getTime() == null ? " Arr Hrs" : " " + section.getTime().getLongName(true)) + 665 (section.getNrRooms() == 0 ? "" : " " + section.getPlacement().getRoomName(",")) + 666 (section.getChoice().getInstructorNames() == null ? "" : " " + section.getChoice().getInstructorNames())); 667 } 668 } 669 670 /** 671 * Save linked sections 672 * @param root document root 673 */ 674 protected void saveLinkedSections(Element root) { 675 Element constrainstEl = root.addElement("constraints"); 676 for (LinkedSections linkedSections: getModel().getLinkedSections()) { 677 Element linkEl = constrainstEl.addElement("linked-sections"); 678 for (Offering offering: linkedSections.getOfferings()) 679 for (Subpart subpart: linkedSections.getSubparts(offering)) 680 for (Section section: linkedSections.getSections(subpart)) 681 linkEl.addElement("section") 682 .addAttribute("offering", getId("offering", offering.getId())) 683 .addAttribute("id", getId("section", section.getId())); 684 } 685 } 686 687 /** 688 * Save travel times 689 * @param root document root 690 */ 691 protected void saveTravelTimes(Element root) { 692 if (getModel().getDistanceConflict() != null) { 693 Map<Long, Map<Long, Integer>> travelTimes = getModel().getDistanceConflict().getDistanceMetric().getTravelTimes(); 694 if (travelTimes != null) { 695 Element travelTimesEl = root.addElement("travel-times"); 696 for (Map.Entry<Long, Map<Long, Integer>> e1: travelTimes.entrySet()) 697 for (Map.Entry<Long, Integer> e2: e1.getValue().entrySet()) 698 travelTimesEl.addElement("travel-time") 699 .addAttribute("id1", getId("room", e1.getKey().toString())) 700 .addAttribute("id2", getId("room", e2.getKey().toString())) 701 .addAttribute("minutes", e2.getValue().toString()); 702 } 703 } 704 } 705}