/**
  * Calculate sum of estimated times for tasks in the given list
  *
  * @param tasks
  */
 public static long extractEstimatedTime(List tasks) {
   long rtn = 0L;
   for (Object task1 : tasks) {
     Task task = (Task) task1;
     rtn += ConvertUtils.intVal(task.getEstTime());
   }
   return rtn;
 }
 /**
  * Find task with max of worker required and return the number
  *
  * @param tasks
  * @return
  * @throws BusinessException
  */
 public static int extractMaxWorkersRequired(List tasks) throws BusinessException {
   int max = 0;
   for (Object task1 : tasks) {
     Task task = (Task) task1;
     if (task.getNumberOfWorkers() == null) {
       throw new BusinessException(
           "Number of workers is not set for task " + task.getTaskDescription());
     } else if (max < task.getNumberOfWorkers()) {
       max = task.getNumberOfWorkers();
     }
   }
   return max;
 }
  /**
   * Finf highest skill level from the given task list
   *
   * @param tasks
   * @throws BusinessException
   */
  public static int extractSkillLevel(List tasks) throws BusinessException {
    int skillLevel = 0;
    for (Object task1 : tasks) {
      Task task = (Task) task1;
      if (ConvertUtils.intVal(task.getSkillLevelId()) > skillLevel) {
        skillLevel = ConvertUtils.intVal(task.getSkillLevelId());
      }
    }
    // not sure if its a violation if(skillLevel==0) throw new BusinessException("Failed to
    // determine job skill level. All supplied tasks have no skill level set!");

    return skillLevel;
  }
  /**
   * Finf highest priority from task list
   *
   * @param tasks
   * @return
   * @throws BusinessException
   */
  public static int extractPriority(List tasks) throws BusinessException {
    int priorityId = 0;
    for (Object task1 : tasks) {
      Task task = (Task) task1;
      if (ConvertUtils.intVal(task.getPriorityId()) > priorityId) {
        priorityId = ConvertUtils.intVal(task.getPriorityId());
      }
    }
    if (priorityId == 0)
      throw new BusinessException(
          "Failed to determine job priority. All supplied tasks have no priority set!");

    return priorityId;
  }
 private static Date calculateLastestStartDateOfTask(Task task) {
   Date latestStartDate = null;
   Integer expiryTypeId = task.getExpiryTypeId();
   String expiryType = TaskExpiryTypeRef.getCode(expiryTypeId);
   if (TaskExpiryTypeRef.Status.NEX.toString().equals(expiryType)) {
     latestStartDate = null;
   } else if (TaskExpiryTypeRef.Status.EOY.toString().equals(expiryType)) {
     Calendar cal = Calendar.getInstance();
     cal.setTime(new Date());
     cal.set(Calendar.MONTH, Calendar.DECEMBER);
     cal.set(Calendar.DAY_OF_MONTH, 31);
     latestStartDate = new java.sql.Date(cal.getTimeInMillis());
   } else if (TaskExpiryTypeRef.Status.EOM.toString().equals(expiryType)) {
     Calendar cal = Calendar.getInstance();
     cal.setTime(new Date());
     int max = cal.getMaximum(Calendar.DAY_OF_MONTH);
     int actualMax = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
     cal.set(Calendar.DAY_OF_MONTH, actualMax);
     latestStartDate = new java.sql.Date(cal.getTimeInMillis());
   } else if (TaskExpiryTypeRef.Status.EOW.toString().equals(expiryType)) {
     // Assumes Sunday is beginning of week
     Calendar cal = Calendar.getInstance();
     cal.setTime(new Date());
     int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
     if (dayOfWeek == Calendar.SATURDAY) {
       // Today is Saturday, no need to expire it today. It should expire next Saturday
       cal.add(Calendar.DATE, 7);
       latestStartDate = new java.sql.Date(cal.getTimeInMillis());
       return latestStartDate;
     }
     while (dayOfWeek != Calendar.SATURDAY) {
       cal.add(Calendar.DATE, 1);
       dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
     }
     latestStartDate = new java.sql.Date(cal.getTimeInMillis());
   } else if (TaskExpiryTypeRef.Status.REL.toString().equals(expiryType)) {
     Calendar cal = Calendar.getInstance();
     cal.setTime(new Date());
     cal.add(Calendar.DAY_OF_MONTH, task.getExpiryNumOfDays().intValue());
     latestStartDate = new java.sql.Date(cal.getTimeInMillis());
   }
   return latestStartDate;
 }
 /**
  * Validate that objectId is the same for all tasks
  *
  * @param tasks
  * @return
  * @throws BusinessException
  */
 public static Long validateObjectId(List tasks) throws BusinessException {
   Long objectId = null;
   for (Object task1 : tasks) {
     Task task = (Task) task1;
     if (task.getObject() == null) {
       throw new BusinessException("objectId is null for task " + task.getTaskDescription());
     } else if (objectId != null && !objectId.equals(task.getObject().getId())) {
       throw new BusinessException(
           "objectId must be identical for all tasks in the job " + task.getTaskDescription());
     } else {
       objectId = task.getObject().getId();
     }
   }
   return objectId;
 }
 public static void validateEstimatedTime(Task vo) throws BusinessException {
   if (vo.getEstTime() == null || vo.getEstTime() <= 0) {
     throw new BusinessException("Estimated time must be defined and greater than zero");
   }
 }
 /**
  * Same as above in another words: Routine tasks must have a frequence defined. Frequency can be
  * Time based or Meter based Routine task is meter based if one (only one) of getFreqDays or
  * getFreqMonths are set Routine task is Time based if getRunHoursThreshInc is defined. All
  * conditions are eclusive. No mixing is allowed
  *
  * @param vo
  * @throws BusinessException
  */
 public static void validateTaskType(Task vo) throws BusinessException {
   if (EqualsUtils.areEqual(
       Integer.valueOf(TaskTypeRef.getIdByCode(TaskTypeRef.ROUTINE_MAINT)), vo.getTaskTypeId())) {
     // first check if Meter based
     if (vo.getRunHoursThreshInc() != null) { // Meter based
       if (vo.getFrequencyId() != null || vo.getFreqMonths() != null || vo.getFreqDays() != null)
         throw new BusinessException(
             "Meter based frequency is not compatible with Time based settings!");
     } else { // Frequency based
       if (vo.getFrequencyId() == null && vo.getFreqMonths() == null && vo.getFreqDays() == null)
         throw new BusinessException("Routine Tasks must have frequency!");
     }
   } else {
     if (vo.getFrequencyId() != null
         || vo.getRunHoursThreshInc() != null
         || vo.getFreqDays() != null
         || vo.getFreqDays() != null)
       throw new BusinessException("Non Routine Tasks may not have frequency!");
   }
 }