@Override
 @PreAuthorize("checkPermission('StudentSchedulingStatusTypeEdit')")
 public void save(Record record, SessionContext context, Session hibSession) {
   StudentSectioningStatus status = new StudentSectioningStatus();
   int value = 0;
   for (int i = 0; i < StatusOption.values().length; i++)
     if ("true".equals(record.getField(2 + i)))
       value += StatusOption.values()[i].getOption().toggle();
   status.setTypes(new HashSet<CourseType>());
   List<CourseType> courseTypes = CourseTypeDAO.getInstance().findAll(Order.asc("reference"));
   if (!courseTypes.isEmpty()) {
     for (int i = 0; i < courseTypes.size(); i++)
       if ("true".equals(record.getField(3 + StatusOption.values().length + i)))
         status.getTypes().add(courseTypes.get(i));
     if (!"true".equals(record.getField(3 + StatusOption.values().length + courseTypes.size())))
       value += StudentSectioningStatus.Option.notype.toggle();
   }
   status.setReference(record.getField(0));
   status.setLabel(record.getField(1));
   status.setStatus(value);
   status.setMessage(record.getField(2 + StatusOption.values().length));
   record.setUniqueId((Long) hibSession.save(status));
   ChangeLog.addChange(
       hibSession,
       context,
       status,
       status.getReference() + " " + status.getLabel(),
       Source.SIMPLE_EDIT,
       Operation.CREATE,
       null,
       null);
 }
 protected void delete(
     StudentSectioningStatus status, SessionContext context, Session hibSession) {
   if (status == null) return;
   ChangeLog.addChange(
       hibSession,
       context,
       status,
       status.getReference() + " " + status.getLabel(),
       Source.SIMPLE_EDIT,
       Operation.DELETE,
       null,
       null);
   hibSession.delete(status);
 }
  protected boolean dropFeature(
      Long featureId,
      Long sessionId,
      org.hibernate.Session hibSession,
      SessionContext context,
      boolean future) {
    RoomFeature rf = lookupFeature(hibSession, featureId, future, sessionId);
    if (rf == null) {
      if (!future) throw new GwtRpcException(MESSAGES.errorRoomFeatureDoesNotExist(featureId));
      return false;
    }

    if (rf instanceof GlobalRoomFeature) context.checkPermission(rf, Right.GlobalRoomFeatureDelete);
    else context.checkPermission(rf, Right.DepartmenalRoomFeatureDelete);

    ChangeLog.addChange(
        hibSession,
        context,
        rf,
        ChangeLog.Source.ROOM_FEATURE_EDIT,
        ChangeLog.Operation.DELETE,
        null,
        rf instanceof DepartmentRoomFeature ? ((DepartmentRoomFeature) rf).getDepartment() : null);

    for (Location location : rf.getRooms()) {
      location.getFeatures().remove(rf);
      hibSession.saveOrUpdate(location);
    }

    for (RoomFeaturePref p :
        (List<RoomFeaturePref>)
            hibSession
                .createQuery("from RoomFeaturePref p where p.roomFeature.uniqueId = :id")
                .setLong("id", rf.getUniqueId())
                .list()) {
      p.getOwner().getPreferences().remove(p);
      hibSession.delete(p);
      hibSession.saveOrUpdate(p.getOwner());
    }

    hibSession.delete(rf);
    return true;
  }
 protected void update(
     StudentSectioningStatus status, Record record, SessionContext context, Session hibSession) {
   if (status == null) return;
   int value = 0;
   for (int i = 0; i < StatusOption.values().length; i++)
     if ("true".equals(record.getField(2 + i)))
       value += StatusOption.values()[i].getOption().toggle();
   Set<CourseType> types = new HashSet<CourseType>();
   List<CourseType> courseTypes = CourseTypeDAO.getInstance().findAll(Order.asc("reference"));
   if (!courseTypes.isEmpty()) {
     for (int i = 0; i < courseTypes.size(); i++)
       if ("true".equals(record.getField(3 + StatusOption.values().length + i)))
         types.add(courseTypes.get(i));
     if (!"true".equals(record.getField(3 + StatusOption.values().length + courseTypes.size())))
       value += StudentSectioningStatus.Option.notype.toggle();
   }
   boolean changed =
       !ToolBox.equals(status.getReference(), record.getField(0))
           || !ToolBox.equals(status.getLabel(), record.getField(1))
           || !ToolBox.equals(status.getStatus(), value)
           || !ToolBox.equals(status.getTypes(), types)
           || !ToolBox.equals(
               status.getMessage(), record.getField(2 + StatusOption.values().length));
   status.setReference(record.getField(0));
   status.setLabel(record.getField(1));
   status.setStatus(value);
   status.setTypes(types);
   status.setMessage(record.getField(2 + StatusOption.values().length));
   hibSession.saveOrUpdate(status);
   if (changed)
     ChangeLog.addChange(
         hibSession,
         context,
         status,
         status.getReference() + " " + status.getLabel(),
         Source.SIMPLE_EDIT,
         Operation.UPDATE,
         null,
         null);
 }
示例#5
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);
    }
  }
  protected RoomFeature createOrUpdateFeature(
      FeatureInterface feature,
      List<Long> add,
      List<Long> drop,
      Long sessionId,
      org.hibernate.Session hibSession,
      SessionContext context,
      boolean future) {
    Department d =
        feature.isDepartmental()
            ? lookuDepartment(hibSession, feature.getDepartment(), future, sessionId)
            : null;
    if (feature.isDepartmental() && d == null) return null;

    RoomFeature rf =
        (feature.getId() == null ? null : lookupFeature(hibSession, feature, future, sessionId));

    if (rf == null) {
      if (!future && feature.getId() != null)
        throw new GwtRpcException(MESSAGES.errorRoomFeatureDoesNotExist(feature.getId()));
      if (d == null) {
        context.checkPermission(Right.GlobalRoomFeatureAdd);
        rf = new GlobalRoomFeature();
        ((GlobalRoomFeature) rf).setSession(SessionDAO.getInstance().get(sessionId));
      } else {
        context.checkPermission(d, Right.DepartmentRoomFeatureAdd);
        rf = new DepartmentRoomFeature();
        ((DepartmentRoomFeature) rf).setDepartment(d);
      }
      rf.setRooms(new HashSet<Location>());
    } else {
      if (rf instanceof GlobalRoomFeature) {
        context.checkPermission(rf, Right.GlobalRoomFeatureEdit);
      } else {
        context.checkPermission(rf, Right.DepartmenalRoomFeatureEdit);
        ((DepartmentRoomFeature) rf).setDepartment(d);
      }
    }

    for (Iterator i = RoomFeature.getAllGlobalRoomFeatures(sessionId).iterator(); i.hasNext(); ) {
      RoomFeature x = (RoomFeature) i.next();
      if ((x.getLabel().equalsIgnoreCase(feature.getLabel())
              || x.getAbbv().equalsIgnoreCase(feature.getAbbreviation()))
          && !x.getUniqueId().equals(rf.getUniqueId()))
        throw new GwtRpcException(
            MESSAGES.errorRoomFeatureAlreadyExists(
                feature.getLabel(), SessionDAO.getInstance().get(sessionId).getLabel()));
    }

    if (rf instanceof DepartmentRoomFeature) {
      for (Iterator i = RoomFeature.getAllDepartmentRoomFeatures(d).iterator(); i.hasNext(); ) {
        RoomFeature x = (RoomFeature) i.next();
        if ((x.getLabel().equalsIgnoreCase(feature.getLabel())
                || x.getAbbv().equalsIgnoreCase(feature.getAbbreviation()))
            && !x.getUniqueId().equals(rf.getUniqueId()))
          throw new GwtRpcException(
              MESSAGES.errorRoomFeatureAlreadyExists(
                  feature.getLabel(), d.getSession().getLabel()));
      }
    }

    rf.setAbbv(feature.getAbbreviation());
    rf.setLabel(feature.getLabel());
    rf.setFeatureType(
        feature.getType() == null
            ? null
            : RoomFeatureTypeDAO.getInstance().get(feature.getType().getId(), hibSession));

    hibSession.saveOrUpdate(rf);

    if (add != null && !add.isEmpty())
      for (Location location : lookupLocations(hibSession, add, future, sessionId)) {
        rf.getRooms().add(location);
        location.getFeatures().add(rf);
        hibSession.saveOrUpdate(location);
      }

    if (drop != null && !drop.isEmpty())
      for (Location location : lookupLocations(hibSession, drop, future, sessionId)) {
        rf.getRooms().remove(location);
        location.getFeatures().remove(rf);
        hibSession.saveOrUpdate(location);
      }

    hibSession.saveOrUpdate(rf);

    ChangeLog.addChange(
        hibSession,
        context,
        rf,
        ChangeLog.Source.ROOM_FEATURE_EDIT,
        (feature.getId() == null ? ChangeLog.Operation.CREATE : ChangeLog.Operation.UPDATE),
        null,
        rf instanceof DepartmentRoomFeature ? ((DepartmentRoomFeature) rf).getDepartment() : null);

    return rf;
  }
  public void loadXml(Element root) throws Exception {
    String trimLeadingZeros =
        ApplicationProperties.getProperty("tmtbl.data.exchange.trim.externalId", "false");
    if (trimLeadingZeros.equals("true")) {
      trimLeadingZerosFromExternalId = true;
    }
    try {
      String rootElementName = "lastLikeCourseDemand";
      if (!root.getName().equalsIgnoreCase(rootElementName)) {
        throw new Exception("Given XML file is not a Course Offerings load file.");
      }

      String campus = root.attributeValue("campus");
      String year = root.attributeValue("year");
      String term = root.attributeValue("term");
      String created = getOptionalStringAttribute(root, "created");
      Session session = Session.getSessionUsingInitiativeYearTerm(campus, year, term);
      if (session == null) {
        throw new Exception("No session found for the given campus, year, and term.");
      }
      loadSubjectAreas(session.getSessionId());
      loadCourseOfferings(session.getSessionId());

      beginTransaction();
      if (created != null) {
        ChangeLog.addChange(
            getHibSession(),
            getManager(),
            session,
            session,
            created,
            ChangeLog.Source.DATA_IMPORT_LASTLIKE_DEMAND,
            ChangeLog.Operation.UPDATE,
            null,
            null);
      }

      getHibSession()
          .createQuery(
              "delete LastLikeCourseDemand ll where ll.subjectArea.uniqueId in "
                  + "(select s.uniqueId from SubjectArea s where s.session.uniqueId=:sessionId)")
          .setLong("sessionId", session.getUniqueId())
          .executeUpdate();

      flush(true);

      for (Iterator it = root.elementIterator(); it.hasNext(); ) {
        Element element = (Element) it.next();
        String externalId = element.attributeValue("externalId");
        if (trimLeadingZerosFromExternalId) {
          try {
            Integer num = new Integer(externalId);
            externalId = num.toString();
          } catch (Exception e) {
            // do nothing
          }
        }
        Student student = fetchStudent(externalId, session.getSessionId());
        if (student == null) {
          student = new Student();
          student.setFirstName("Unknown");
          student.setLastName("Student");
          student.setExternalUniqueId(externalId);
          student.setFreeTimeCategory(new Integer(0));
          student.setSchedulePreference(new Integer(0));
          student.setSession(session);
          getHibSession().save(student);
          getHibSession().flush();
          getHibSession().refresh(student);
        }
        loadCourses(element, student, session);
        flushIfNeeded(true);
      }

      flush(true);

      getHibSession()
          .createQuery(
              "update CourseOffering c set c.demand="
                  + "(select count(distinct d.student) from LastLikeCourseDemand d where "
                  + "(c.subjectArea=d.subjectArea and c.courseNbr=d.courseNbr)) where "
                  + "c.permId is null and c.subjectArea.uniqueId in (select sa.uniqueId from SubjectArea sa where sa.session.uniqueId=:sessionId)")
          .setLong("sessionId", session.getUniqueId())
          .executeUpdate();

      getHibSession()
          .createQuery(
              "update CourseOffering c set c.demand="
                  + "(select count(distinct d.student) from LastLikeCourseDemand d where "
                  + "d.student.session=c.subjectArea.session and c.permId=d.coursePermId) where "
                  + "c.permId is not null and c.subjectArea.uniqueId in (select sa.uniqueId from SubjectArea sa where sa.session.uniqueId=:sessionId)")
          .setLong("sessionId", session.getUniqueId())
          .executeUpdate();

      commitTransaction();
    } catch (Exception e) {
      fatal("Exception: " + e.getMessage(), e);
      rollbackTransaction();
      throw e;
    }
  }