public void toPdfTable(
      OutputStream out,
      HttpServletRequest request,
      SessionContext context,
      Collection distPrefs,
      Long examTypeId)
      throws Exception {
    WebTable.setOrder(context, "examDistPrefsTable.ord", request.getParameter("order"), 4);

    PdfWebTable tbl =
        new PdfWebTable(
            4,
            ExamTypeDAO.getInstance().get(examTypeId).getLabel()
                + " Examination Distribution Preferences",
            null,
            new String[] {" Preference ", " Type ", " Exam ", " Class/Course "},
            new String[] {"left", "left", "left", "left"},
            new boolean[] {true, true, true, true});

    int nrPrefs = 0;

    for (Iterator i1 = distPrefs.iterator(); i1.hasNext(); ) {
      DistributionPref dp = (DistributionPref) i1.next();

      if (!context.hasPermission(dp, Right.ExaminationDistributionPreferenceDetail)) continue;

      nrPrefs++;

      String examStr = "";
      String objStr = "";

      for (Iterator i2 = dp.getOrderedSetOfDistributionObjects().iterator(); i2.hasNext(); ) {
        DistributionObject dO = (DistributionObject) i2.next();
        Exam exam = (Exam) dO.getPrefGroup();
        examStr += dO.preferenceText();
        for (Iterator i3 = exam.getOwners().iterator(); i3.hasNext(); ) {
          ExamOwner owner = (ExamOwner) i3.next();
          objStr += owner.getLabel();
          if (i3.hasNext()) {
            examStr += "\n";
            objStr += "\n";
          }
        }
        if (i2.hasNext()) {
          examStr += "\n";
          objStr += "\n";
        }
      }

      String distType = dp.getDistributionType().getLabel();
      String prefLevel = dp.getPrefLevel().getPrefName();

      tbl.addLine(
          null,
          new String[] {prefLevel, distType, examStr, objStr},
          new Comparable[] {null, distType, examStr, objStr});
    }

    if (nrPrefs == 0) tbl.addLine(null, new String[] {"No preferences found", "", "", ""}, null);

    int ord = WebTable.getOrder(context, "examDistPrefsTable.ord");
    ord = (ord > 0 ? 1 : -1) * (1 + Math.abs(ord));

    PdfPTable table = tbl.printPdfTable(ord);

    float width = tbl.getWidth();

    Document doc = new Document(new Rectangle(60f + width, 60f + 1.30f * width), 30, 30, 30, 30);

    PdfWriter iWriter = PdfWriter.getInstance(doc, out);
    iWriter.setPageEvent(new PdfEventHandler());
    doc.open();

    if (tbl.getName() != null) doc.add(new Paragraph(tbl.getName(), PdfFont.getBigFont(true)));

    doc.add(table);

    doc.close();
  }
示例#2
0
  protected void saveSolution(org.hibernate.Session hibSession) {
    TimetableManager owner =
        TimetableManager.findByExternalId(
            getModel().getProperties().getProperty("General.OwnerPuid"));
    Collection exams = org.unitime.timetable.model.Exam.findAll(iSessionId, iExamTypeId);
    Hashtable<Long, ExamEvent> examEvents = new Hashtable();
    for (Iterator i =
            hibSession
                .createQuery(
                    "select e from ExamEvent e where e.exam.session.uniqueId=:sessionId and e.exam.examType.uniqueId=:examTypeId")
                .setLong("sessionId", iSessionId)
                .setLong("examTypeId", iExamTypeId)
                .iterate();
        i.hasNext(); ) {
      ExamEvent e = (ExamEvent) i.next();
      examEvents.put(e.getExam().getUniqueId(), e);
    }
    iProgress.setPhase("Saving assignments...", exams.size());
    Hashtable examTable = new Hashtable();
    for (Iterator i = exams.iterator(); i.hasNext(); ) {
      iProgress.incProgress();
      org.unitime.timetable.model.Exam exam = (org.unitime.timetable.model.Exam) i.next();
      ExamAssignment oldAssignment = new ExamAssignment(exam);
      exam.setAssignedPeriod(null);
      exam.setAssignedPreference(null);
      exam.getAssignedRooms().clear();
      ExamEvent event = examEvents.get(exam.getUniqueId());
      if (event != null) hibSession.delete(event);
      for (Iterator j = exam.getConflicts().iterator(); j.hasNext(); ) {
        ExamConflict conf = (ExamConflict) j.next();
        hibSession.delete(conf);
        j.remove();
      }
      Exam examVar = null;
      for (Exam x : getModel().variables()) {
        if (exam.getUniqueId().equals(x.getId())) {
          examVar = x;
          break;
        }
      }
      if (examVar == null) {
        iProgress.warn("Exam " + getExamLabel(exam) + " was not loaded.");
        if (oldAssignment.getPeriodId() != null) {
          SubjectArea subject = null;
          Department dept = null;
          for (Iterator j = new TreeSet(exam.getOwners()).iterator(); j.hasNext(); ) {
            ExamOwner xo = (ExamOwner) j.next();
            subject = xo.getCourse().getSubjectArea();
            dept = subject.getDepartment();
            break;
          }
          ChangeLog.addChange(
              hibSession,
              owner,
              exam.getSession(),
              exam,
              exam.getName()
                  + " ("
                  + oldAssignment.getPeriodAbbreviation()
                  + " "
                  + oldAssignment.getRoomsName(", ")
                  + " &rarr; N/A)",
              ChangeLog.Source.EXAM_SOLVER,
              ChangeLog.Operation.UNASSIGN,
              subject,
              dept);
        }
        continue;
      }
      examTable.put(examVar.getId(), exam);
      ExamPlacement placement = getAssignment().getValue(examVar);
      if (placement == null) {
        iProgress.warn("Exam " + getExamLabel(exam) + " has no assignment.");
        if (oldAssignment.getPeriodId() != null) {
          SubjectArea subject = null;
          Department dept = null;
          for (Iterator j = new TreeSet(exam.getOwners()).iterator(); j.hasNext(); ) {
            ExamOwner xo = (ExamOwner) j.next();
            subject = xo.getCourse().getSubjectArea();
            dept = subject.getDepartment();
            break;
          }
          ChangeLog.addChange(
              hibSession,
              owner,
              exam.getSession(),
              exam,
              exam.getName()
                  + " ("
                  + oldAssignment.getPeriodAbbreviation()
                  + " "
                  + oldAssignment.getRoomsName(", ")
                  + " &rarr; N/A)",
              ChangeLog.Source.EXAM_SOLVER,
              ChangeLog.Operation.UNASSIGN,
              subject,
              dept);
        }
        continue;
      }
      ExamPeriod period = new ExamPeriodDAO().get(placement.getPeriod().getId());
      if (period == null) {
        iProgress.warn(
            "Examination period "
                + placement.getPeriod().getDayStr()
                + " "
                + placement.getPeriod().getTimeStr()
                + " not found.");
        if (oldAssignment.getPeriodId() != null) {
          SubjectArea subject = null;
          Department dept = null;
          for (Iterator j = new TreeSet(exam.getOwners()).iterator(); j.hasNext(); ) {
            ExamOwner xo = (ExamOwner) j.next();
            subject = xo.getCourse().getSubjectArea();
            dept = subject.getDepartment();
            break;
          }
          ChangeLog.addChange(
              hibSession,
              owner,
              exam.getSession(),
              exam,
              exam.getName()
                  + " ("
                  + oldAssignment.getPeriodAbbreviation()
                  + " "
                  + oldAssignment.getRoomsName(", ")
                  + " &rarr; N/A)",
              ChangeLog.Source.EXAM_SOLVER,
              ChangeLog.Operation.UNASSIGN,
              subject,
              dept);
        }
        continue;
      }
      exam.setAssignedPeriod(period);
      for (Iterator j = placement.getRoomPlacements().iterator(); j.hasNext(); ) {
        ExamRoomPlacement room = (ExamRoomPlacement) j.next();
        Location location = new LocationDAO().get(room.getId());
        if (location == null) {
          iProgress.warn("Location " + room.getName() + " (id:" + room.getId() + ") not found.");
          continue;
        }
        exam.getAssignedRooms().add(location);
      }
      exam.setAssignedPreference(
          new ExamAssignment(placement, getAssignment()).getAssignedPreferenceString());

      hibSession.saveOrUpdate(exam);
      ExamAssignment newAssignment = new ExamAssignment(exam);
      if (!ToolBox.equals(newAssignment.getPeriodId(), oldAssignment.getPeriodId())
          || !ToolBox.equals(newAssignment.getRooms(), oldAssignment.getRooms())) {
        SubjectArea subject = null;
        Department dept = null;
        for (Iterator j = new TreeSet(exam.getOwners()).iterator(); j.hasNext(); ) {
          ExamOwner xo = (ExamOwner) j.next();
          subject = xo.getCourse().getSubjectArea();
          dept = subject.getDepartment();
          break;
        }
        ChangeLog.addChange(
            hibSession,
            owner,
            exam.getSession(),
            exam,
            exam.getName()
                + " ("
                + (oldAssignment.getPeriod() == null
                    ? "N/A"
                    : oldAssignment.getPeriodAbbreviation()
                        + " "
                        + oldAssignment.getRoomsName(", "))
                + " &rarr; "
                + newAssignment.getPeriodAbbreviation()
                + " "
                + newAssignment.getRoomsName(", ")
                + ")",
            ChangeLog.Source.EXAM_SOLVER,
            ChangeLog.Operation.ASSIGN,
            subject,
            dept);
      }
    }
    iProgress.setPhase("Saving conflicts...", getAssignment().nrAssignedVariables());
    for (Exam examVar : getAssignment().assignedVariables()) {
      iProgress.incProgress();
      org.unitime.timetable.model.Exam exam =
          (org.unitime.timetable.model.Exam) examTable.get(examVar.getId());
      if (exam == null) continue;
      ExamPlacement placement = (ExamPlacement) getAssignment().getValue(examVar);
      ExamAssignmentInfo info = new ExamAssignmentInfo(placement, getAssignment());
      for (Iterator i = info.getDirectConflicts().iterator(); i.hasNext(); ) {
        ExamAssignmentInfo.DirectConflict dc = (ExamAssignmentInfo.DirectConflict) i.next();
        if (dc.getOtherExam() == null) continue;
        if (examVar.getId() < dc.getOtherExam().getExamId().longValue()) {
          org.unitime.timetable.model.Exam otherExam =
              (org.unitime.timetable.model.Exam) examTable.get(dc.getOtherExam().getExamId());
          if (otherExam == null) {
            iProgress.warn(
                "Exam "
                    + dc.getOtherExam().getExamName()
                    + " (id:"
                    + dc.getOtherExam().getExamId()
                    + ") not found.");
            continue;
          }
          ExamConflict conf = new ExamConflict();
          conf.setConflictType(ExamConflict.sConflictTypeDirect);
          conf.setStudents(getStudents(hibSession, dc.getStudents()));
          conf.setNrStudents(conf.getStudents().size());
          hibSession.save(conf);
          exam.getConflicts().add(conf);
          otherExam.getConflicts().add(conf);
          iProgress.debug(
              "Direct conflict of "
                  + dc.getStudents().size()
                  + " students between "
                  + exam.getLabel()
                  + " and "
                  + otherExam.getLabel());
        }
      }
      for (Iterator i = info.getBackToBackConflicts().iterator(); i.hasNext(); ) {
        ExamAssignmentInfo.BackToBackConflict btb =
            (ExamAssignmentInfo.BackToBackConflict) i.next();
        if (examVar.getId() < btb.getOtherExam().getExamId().longValue()) {
          org.unitime.timetable.model.Exam otherExam =
              (org.unitime.timetable.model.Exam) examTable.get(btb.getOtherExam().getExamId());
          if (otherExam == null) {
            iProgress.warn(
                "Exam "
                    + btb.getOtherExam().getExamName()
                    + " (id:"
                    + btb.getOtherExam().getExamId()
                    + ") not found.");
            continue;
          }
          ExamConflict conf = new ExamConflict();
          conf.setConflictType(
              btb.isDistance()
                  ? ExamConflict.sConflictTypeBackToBackDist
                  : ExamConflict.sConflictTypeBackToBack);
          conf.setDistance(btb.getDistance());
          conf.setStudents(getStudents(hibSession, btb.getStudents()));
          conf.setNrStudents(conf.getStudents().size());
          exam.getConflicts().add(conf);
          otherExam.getConflicts().add(conf);
          hibSession.save(conf);
          iProgress.debug(
              "Back-to-back conflict of "
                  + btb.getStudents().size()
                  + " students between "
                  + exam.getLabel()
                  + " and "
                  + otherExam.getLabel());
        }
      }
      m2d:
      for (Iterator i = info.getMoreThanTwoADaysConflicts().iterator(); i.hasNext(); ) {
        ExamAssignmentInfo.MoreThanTwoADayConflict m2d =
            (ExamAssignmentInfo.MoreThanTwoADayConflict) i.next();
        HashSet confExams = new HashSet();
        confExams.add(exam);
        for (Iterator j = m2d.getOtherExams().iterator(); j.hasNext(); ) {
          ExamAssignment otherExamAsg = (ExamAssignment) j.next();
          if (examVar.getId() >= otherExamAsg.getExamId().longValue()) continue m2d;
          org.unitime.timetable.model.Exam otherExam =
              (org.unitime.timetable.model.Exam) examTable.get(otherExamAsg.getExamId());
          if (otherExam == null) {
            iProgress.warn(
                "Exam "
                    + otherExamAsg.getExamName()
                    + " (id:"
                    + otherExamAsg.getExamId()
                    + ") not found.");
            continue;
          }
          confExams.add(otherExam);
        }
        if (confExams.size() >= 3) {
          ExamConflict conf = new ExamConflict();
          conf.setConflictType(ExamConflict.sConflictTypeMoreThanTwoADay);
          conf.setStudents(getStudents(hibSession, m2d.getStudents()));
          conf.setNrStudents(conf.getStudents().size());
          hibSession.save(conf);
          for (Iterator j = confExams.iterator(); j.hasNext(); )
            ((org.unitime.timetable.model.Exam) j.next()).getConflicts().add(conf);
          iProgress.debug(
              "More than 2 a day conflict of "
                  + m2d.getStudents().size()
                  + " students between "
                  + exam.getLabel()
                  + " and "
                  + m2d.getOtherExams());
        }
      }

      for (Iterator i = info.getInstructorDirectConflicts().iterator(); i.hasNext(); ) {
        ExamAssignmentInfo.DirectConflict dc = (ExamAssignmentInfo.DirectConflict) i.next();
        if (dc.getOtherExam() == null) continue;
        if (examVar.getId() < dc.getOtherExam().getExamId().longValue()) {
          org.unitime.timetable.model.Exam otherExam =
              (org.unitime.timetable.model.Exam) examTable.get(dc.getOtherExam().getExamId());
          if (otherExam == null) {
            iProgress.warn(
                "Exam "
                    + dc.getOtherExam().getExamName()
                    + " (id:"
                    + dc.getOtherExam().getExamId()
                    + ") not found.");
            continue;
          }
          ExamConflict conf = new ExamConflict();
          conf.setConflictType(ExamConflict.sConflictTypeDirect);
          conf.setInstructors(getInstructors(hibSession, dc.getStudents()));
          conf.setNrInstructors(conf.getInstructors().size());
          hibSession.save(conf);
          exam.getConflicts().add(conf);
          otherExam.getConflicts().add(conf);
          iProgress.debug(
              "Direct conflict of "
                  + dc.getStudents().size()
                  + " instructors between "
                  + exam.getLabel()
                  + " and "
                  + otherExam.getLabel());
        }
      }
      for (Iterator i = info.getInstructorBackToBackConflicts().iterator(); i.hasNext(); ) {
        ExamAssignmentInfo.BackToBackConflict btb =
            (ExamAssignmentInfo.BackToBackConflict) i.next();
        if (examVar.getId() < btb.getOtherExam().getExamId().longValue()) {
          org.unitime.timetable.model.Exam otherExam =
              (org.unitime.timetable.model.Exam) examTable.get(btb.getOtherExam().getExamId());
          if (otherExam == null) {
            iProgress.warn(
                "Exam "
                    + btb.getOtherExam().getExamName()
                    + " (id:"
                    + btb.getOtherExam().getExamId()
                    + ") not found.");
            continue;
          }
          ExamConflict conf = new ExamConflict();
          conf.setConflictType(
              btb.isDistance()
                  ? ExamConflict.sConflictTypeBackToBackDist
                  : ExamConflict.sConflictTypeBackToBack);
          conf.setDistance(btb.getDistance());
          conf.setInstructors(getInstructors(hibSession, btb.getStudents()));
          conf.setNrInstructors(conf.getInstructors().size());
          exam.getConflicts().add(conf);
          otherExam.getConflicts().add(conf);
          hibSession.save(conf);
          iProgress.debug(
              "Back-to-back conflict of "
                  + btb.getStudents().size()
                  + " instructors between "
                  + exam.getLabel()
                  + " and "
                  + otherExam.getLabel());
        }
      }
      m2d:
      for (Iterator i = info.getInstructorMoreThanTwoADaysConflicts().iterator(); i.hasNext(); ) {
        ExamAssignmentInfo.MoreThanTwoADayConflict m2d =
            (ExamAssignmentInfo.MoreThanTwoADayConflict) i.next();
        HashSet confExams = new HashSet();
        confExams.add(exam);
        for (Iterator j = m2d.getOtherExams().iterator(); j.hasNext(); ) {
          ExamAssignment otherExamAsg = (ExamAssignment) j.next();
          if (examVar.getId() >= otherExamAsg.getExamId().longValue()) continue m2d;
          org.unitime.timetable.model.Exam otherExam =
              (org.unitime.timetable.model.Exam) examTable.get(otherExamAsg.getExamId());
          if (otherExam == null) {
            iProgress.warn(
                "Exam "
                    + otherExamAsg.getExamName()
                    + " (id:"
                    + otherExamAsg.getExamId()
                    + ") not found.");
            continue;
          }
          confExams.add(otherExam);
        }
        if (confExams.size() >= 3) {
          ExamConflict conf = new ExamConflict();
          conf.setConflictType(ExamConflict.sConflictTypeMoreThanTwoADay);
          conf.setInstructors(getInstructors(hibSession, m2d.getStudents()));
          conf.setNrInstructors(conf.getInstructors().size());
          hibSession.save(conf);
          for (Iterator j = confExams.iterator(); j.hasNext(); )
            ((org.unitime.timetable.model.Exam) j.next()).getConflicts().add(conf);
          iProgress.debug(
              "More than 2 a day conflict of "
                  + m2d.getStudents().size()
                  + " instructors between "
                  + exam.getLabel()
                  + " and "
                  + m2d.getOtherExams());
        }
      }
    }
    iProgress.setPhase("Saving events...", getAssignment().nrAssignedVariables());
    String ownerPuid = getModel().getProperties().getProperty("General.OwnerPuid");
    EventContact contact = EventContact.findByExternalUniqueId(ownerPuid);
    if (contact == null) {
      TimetableManager manager = TimetableManager.findByExternalId(ownerPuid);
      contact = new EventContact();
      contact.setFirstName(manager.getFirstName());
      contact.setMiddleName(manager.getMiddleName());
      contact.setLastName(manager.getLastName());
      contact.setExternalUniqueId(manager.getExternalUniqueId());
      contact.setEmailAddress(manager.getEmailAddress());
      hibSession.save(contact);
    }
    for (Exam examVar : getAssignment().assignedVariables()) {
      iProgress.incProgress();
      org.unitime.timetable.model.Exam exam =
          (org.unitime.timetable.model.Exam) examTable.get(examVar.getId());
      if (exam == null) continue;
      ExamEvent event = exam.generateEvent(null, true);
      if (event != null) {
        event.setEventName(examVar.getName());
        event.setMinCapacity(examVar.getSize());
        event.setMaxCapacity(examVar.getSize());
        event.setMainContact(contact);
        hibSession.saveOrUpdate(event);
      }
      if (event != null || !exam.getConflicts().isEmpty()) hibSession.saveOrUpdate(exam);
    }
  }
  /**
   * Build a html table with the list representing distribution prefs
   *
   * @param distPrefs
   * @param ordCol
   * @param editable
   * @return
   */
  public String toHtmlTable(
      HttpServletRequest request, SessionContext context, Collection distPrefs, String title)
      throws Exception {

    String backId =
        ("PreferenceGroup".equals(request.getParameter("backType"))
            ? request.getParameter("backId")
            : null);

    WebTable.setOrder(context, "examDistPrefsTable.ord", request.getParameter("order"), 4);

    WebTable tbl =
        new WebTable(
            3,
            title,
            "examDistributionPrefs.do?order=%%",
            new String[] {" Type ", " Exam ", " Class/Course "},
            new String[] {"left", "left", "left"},
            new boolean[] {true, true, true});

    int nrPrefs = 0;

    for (Iterator i1 = distPrefs.iterator(); i1.hasNext(); ) {
      DistributionPref dp = (DistributionPref) i1.next();

      if (!context.hasPermission(dp, Right.ExaminationDistributionPreferenceDetail)) continue;

      boolean prefEditable = context.hasPermission(dp, Right.ExaminationDistributionPreferenceEdit);

      nrPrefs++;

      String examStr = "";
      String objStr = "";

      for (Iterator i2 = dp.getOrderedSetOfDistributionObjects().iterator(); i2.hasNext(); ) {
        DistributionObject dO = (DistributionObject) i2.next();
        Exam exam = (Exam) dO.getPrefGroup();
        examStr += dO.preferenceText();
        for (Iterator i3 = exam.getOwners().iterator(); i3.hasNext(); ) {
          ExamOwner owner = (ExamOwner) i3.next();
          objStr += owner.getLabel();
          if (i3.hasNext()) {
            examStr += "<BR>";
            objStr += "<BR>";
          }
        }
        if (i2.hasNext()) {
          examStr += "<BR>";
          objStr += "<BR>";
        }
      }

      String distType = dp.getDistributionType().getLabel();
      String prefLevel = dp.getPrefLevel().getPrefName();
      String prefColor = dp.getPrefLevel().prefcolor();
      if (PreferenceLevel.sNeutral.equals(dp.getPrefLevel().getPrefProlog())) prefColor = "gray";
      String onClick = null;

      boolean gray = false;

      if (prefEditable) {
        onClick =
            "onClick=\"document.location='examDistributionPrefs.do"
                + "?dp="
                + dp.getUniqueId().toString()
                + "&op=view'\"";
      } // else gray = true;

      boolean back = dp.getUniqueId().toString().equals(backId);

      tbl.addLine(
          onClick,
          new String[] {
            (back ? "<A name=\"back\"</A>" : "")
                + (gray
                    ? "<span style='color:gray;'>"
                    : "<span style='color:"
                        + prefColor
                        + ";font-weight:bold;' title='"
                        + prefLevel
                        + " "
                        + distType
                        + "'>")
                + distType
                + "</span>",
            (gray ? "<span style='color:gray;'>" : "") + examStr + (gray ? "</span>" : ""),
            (gray ? "<span style='color:gray;'>" : "") + objStr + (gray ? "</span>" : "")
          },
          new Comparable[] {distType, examStr, objStr});
    }

    if (nrPrefs == 0) tbl.addLine(null, new String[] {"No preferences found", "", ""}, null);

    return tbl.printTable(WebTable.getOrder(context, "examDistPrefsTable.ord"));
  }