public boolean isTimeslotFreeForRoom(Room r, Calendar time, int hours) {
   Calendar endTime = (Calendar) time.clone();
   endTime.add(Calendar.HOUR_OF_DAY, hours);
   for (WeeklyEvent we : r.getWeeklyEvents()) {
     if (DateFunctions.doDateRangesOverlap(
         time, endTime, we.getStartTime(), we.getEndTime(), true)) {
       return false;
     }
   }
   return true;
 }
 public boolean isTimeslotFreeForCourse(Course c, Calendar time, int hours) {
   List<Room> rooms = findRoomsForCourse(c);
   Calendar end = (Calendar) time.clone();
   end.add(Calendar.HOUR_OF_DAY, hours);
   for (CurriculumSemester cs : c.getCurriculumSemesters()) {
     for (Course co : cs.getCourses()) {
       WeeklyEvent we = co.getWeeklyEvent();
       if (we != null
           && DateFunctions.doDateRangesOverlap(
               time, end, we.getStartTime(), we.getEndTime(), true)) {
         return false;
       }
     }
   }
   return true;
 }
 public boolean generateWeeklyEventForAllCourses() {
   HibernateSupport.beginTransaction();
   for (WeeklyEvent we : etm.getWeeklyEvents()) {
     we.deleteFromDB();
   }
   HibernateSupport.commitTransaction();
   etm.clearWeeklyEvents();
   List<Course> courses = bc.getCoc().getCourseTableModel().getAllCourses();
   boolean result = true;
   for (Course c : courses) {
     if (!generateWeeklyEventFor(c)) {
       LoggingFunctions.printError("Could not generate weekly event for " + c.getId(), "-");
       result = false;
     }
   }
   return result;
 }
  public boolean generateWeeklyEventFor(Course c) {
    List<Room> rooms = findRoomsForCourse(c);
    Room r = null;
    if (rooms.size() < 1) {
      LoggingFunctions.printError(
          "Could not generate weekly event for course " + c.getId(), "No suitable rooms found");
      return false;
    }
    Calendar cal = DateFunctions.calculateNextValidStartTime();
    cal.add(Calendar.HOUR_OF_DAY, -1);
    int hours = (int) c.getSemesterHours();

    // While there is another valid start time
    while (DateFunctions.calculateNextValidStartTime(cal, hours)) {
      // If the course can be done on that start time
      if (isTimeslotFreeForCourse(c, cal, hours)) {
        r = getFreeRoomForTimeslot(rooms, cal, hours);
        // If there is a free room at that time
        if (r != null) {
          WeeklyEvent we = new WeeklyEvent();
          we.setEvent(cal, hours, c, r);
          etm.addWeeklyEvent(we);
          we.saveToDB();
          System.out.println("");
          System.out.println("Saved new weekly event:");
          System.out.println("Course: " + c.getName());
          System.out.println(
              "From: " + cal.get(Calendar.HOUR_OF_DAY) + "" + DateFunctions.getDayOfWeek(cal));
          System.out.println(
              "To: "
                  + cal.get(Calendar.HOUR_OF_DAY)
                  + hours
                  + ""
                  + DateFunctions.getDayOfWeek(cal));
          System.out.println("Room: " + r.getRoomName());
          System.out.println("");
          return true;
        }
      }
    }

    return false;
  }