Exemplo n.º 1
0
  /**
   * Calculates how many hours a given timesheet should get
   *
   * @param ts the timesheet of which you want the hours
   * @return the hours
   * @throws SQLException if an error has occured executing the sql query
   */
  public int getHoursForTimeSheeet(Time_Sheet ts) throws SQLException {
    ArrayList<Time_Sheet> conflicts = getAllConflicts(ts);

    Collections.sort(conflicts);
    ArrayList<Time_Sheet> currentTimesheets =
        new ArrayList(); // A list of timesheets that are still active
    ArrayList<Time_Sheet> primaryTimesheets =
        new ArrayList(); // A list of timesheets that are currently getting the hours
    for (int i = 0; i < conflicts.size(); i++) {
      Time_Sheet currentTs = conflicts.get(i);

      /**
       * If the current timesheet is the first timesheet in conflicts, or if the current timesheet
       * has the same startime, as the current active timesheets the add it to the currently active
       * timesheets
       */
      if (primaryTimesheets.isEmpty()
          || primaryTimesheets.get(0).getStartTime().getTime()
              == currentTs.getStartTime().getTime()) {
        primaryTimesheets.add(currentTs);

      } else {

        removeOldTimeSheets(currentTimesheets, currentTs.getStartTime());
        addTimeToPrimaryTimeSheets(primaryTimesheets, currentTimesheets, currentTs.getStartTime());
        primaryTimesheets.add(currentTs);
      }
    }
    cleanUpRemainingTimeSheets(primaryTimesheets, currentTimesheets);

    // calculate the total amount of minutes
    int minutes = 0;
    for (Time_Sheet myTS : conflicts) {
      minutes += myTS.getMinutes();
    }
    // calculate the total amount of hours
    int totalHours;
    if (minutes % 60 == 0) totalHours = (int) (minutes / 60);
    else totalHours = (int) (minutes / 60) + 1;

    if (totalHours < 2) totalHours = 2;
    for (Time_Sheet myTS : conflicts) {
      while (myTS.getMinutes() >= 60) {
        myTS.addHours(1);
        myTS.removeMinute(60);
        totalHours--;
      }
    }

    sortTimeSheetListByHours(conflicts, 0, conflicts.size() - 1);

    while (totalHours > 0) {
      conflicts.get(conflicts.size() - 1).addHours(1);
      totalHours--;
      conflicts.get(conflicts.size() - 1).removeMinute(60);
      sortTimeSheetListByHours(conflicts, 0, conflicts.size() - 1);
    }
    int res = ts.getHours();
    for (Time_Sheet myTS : conflicts) {
      myTS.clearHoursAndMinutes();
    }

    return res;
  }