protected <T> String getNextRunTime(ScheduledTask<T> task) {
    String nextRunTime = "n/a";

    // Run now type tasks should never have a next run time
    if (!task.getSchedule().getClass().isAssignableFrom(RunNowSchedule.class)
        && task.getNextRun() != null) {
      nextRunTime = task.getNextRun().toString();
    }

    return nextRunTime;
  }
  @Override
  public boolean allowConcurrentSubmission(Map<String, List<ScheduledTask<?>>> activeTasks) {
    if (activeTasks.containsKey(ID)) {
      for (ScheduledTask<?> scheduledTask : activeTasks.get(ID)) {
        if (isSubmitted(scheduledTask)
            && conflictsWith((GenerateMetadataTask) scheduledTask.getTask())
            && scheduledTask.getSchedule() instanceof RunNowSchedule) {
          throw new TaskAlreadyScheduledException(
              scheduledTask, "Found same task in scheduler queue.");
        }
      }
    }

    return true;
  }
  public <T> ScheduledServiceBaseResource getServiceRestModel(ScheduledTask<T> task) {
    ScheduledServiceBaseResource resource = null;

    if (RunNowSchedule.class.isAssignableFrom(task.getSchedule().getClass())
        || ManualRunSchedule.class.isAssignableFrom(task.getSchedule().getClass())) {
      resource = new ScheduledServiceBaseResource();
    } else if (OnceSchedule.class.isAssignableFrom(task.getSchedule().getClass())) {
      resource = new ScheduledServiceOnceResource();

      OnceSchedule taskSchedule = (OnceSchedule) task.getSchedule();
      ScheduledServiceOnceResource res = (ScheduledServiceOnceResource) resource;

      res.setStartDate(formatDate(taskSchedule.getStartDate()));
      res.setStartTime(formatTime(taskSchedule.getStartDate()));
    } else if (HourlySchedule.class.isAssignableFrom(task.getSchedule().getClass())) {
      resource = new ScheduledServiceHourlyResource();

      HourlySchedule taskSchedule = (HourlySchedule) task.getSchedule();
      ScheduledServiceHourlyResource res = (ScheduledServiceHourlyResource) resource;

      res.setStartDate(formatDate(taskSchedule.getStartDate()));
      res.setStartTime(formatTime(taskSchedule.getStartDate()));
    } else if (DailySchedule.class.isAssignableFrom(task.getSchedule().getClass())) {
      resource = new ScheduledServiceDailyResource();

      DailySchedule taskSchedule = (DailySchedule) task.getSchedule();
      ScheduledServiceDailyResource res = (ScheduledServiceDailyResource) resource;

      res.setStartDate(formatDate(taskSchedule.getStartDate()));
      res.setRecurringTime(formatTime(taskSchedule.getStartDate()));
    } else if (WeeklySchedule.class.isAssignableFrom(task.getSchedule().getClass())) {
      resource = new ScheduledServiceWeeklyResource();

      WeeklySchedule taskSchedule = (WeeklySchedule) task.getSchedule();
      ScheduledServiceWeeklyResource res = (ScheduledServiceWeeklyResource) resource;

      res.setStartDate(formatDate(taskSchedule.getStartDate()));
      res.setRecurringTime(formatTime(taskSchedule.getStartDate()));
      res.setRecurringDay(formatRecurringDayOfWeek(taskSchedule.getDaysToRun()));
    } else if (MonthlySchedule.class.isAssignableFrom(task.getSchedule().getClass())) {
      resource = new ScheduledServiceMonthlyResource();

      MonthlySchedule taskSchedule = (MonthlySchedule) task.getSchedule();
      ScheduledServiceMonthlyResource res = (ScheduledServiceMonthlyResource) resource;

      res.setStartDate(formatDate(taskSchedule.getStartDate()));
      res.setRecurringTime(formatTime(taskSchedule.getStartDate()));
      res.setRecurringDay(formatRecurringDayOfMonth(taskSchedule.getDaysToRun()));
    } else if (CronSchedule.class.isAssignableFrom(task.getSchedule().getClass())) {
      resource = new ScheduledServiceAdvancedResource();

      CronSchedule taskSchedule = (CronSchedule) task.getSchedule();
      ScheduledServiceAdvancedResource res = (ScheduledServiceAdvancedResource) resource;

      res.setCronCommand(taskSchedule.getCronString());
    }

    if (resource != null) {
      resource.setId(task.getId());
      resource.setEnabled(task.isEnabled());
      resource.setName(task.getName());
      resource.setSchedule(getScheduleShortName(task.getSchedule()));
      resource.setTypeId(task.getType());
      resource.setProperties(formatServiceProperties(task.getTaskParams()));
      resource.setAlertEmail(TaskUtils.getAlertEmail(task));
    }

    return resource;
  }
  /**
   * Retrieve a list of scheduled tasks currently configured in nexus.
   *
   * @param allTasks If true, will return all tasks, even non-exposed tasks.
   */
  @Override
  @GET
  @ResourceMethodSignature(output = ScheduledServiceListResourceResponse.class)
  public Object get(Context context, Request request, Response response, Variant variant)
      throws ResourceException {
    boolean allTasks = isAllTasks(request);

    Map<String, List<ScheduledTask<?>>> tasksMap = getNexusScheduler().getAllTasks();

    ScheduledServiceListResourceResponse result = new ScheduledServiceListResourceResponse();

    for (String key : tasksMap.keySet()) {
      List<ScheduledTask<?>> tasks = tasksMap.get(key);

      for (ScheduledTask<?> task : tasks) {
        boolean isExposed = true;

        SchedulerTask<?> st = task.getSchedulerTask();

        if (st != null && st instanceof NexusTask<?>) {
          isExposed = ((NexusTask<?>) st).isExposed();
        }

        if (allTasks || isExposed) {
          if (getLogger().isDebugEnabled()) {
            getLogger()
                .debug("Building task '" + task.getName() + "' of type '" + task.getType() + "'.");
          }

          ScheduledServiceListResource item = new ScheduledServiceListResource();
          item.setResourceURI(createChildReference(request, this, task.getId()).toString());
          item.setLastRunResult(getLastRunResult(task));
          item.setId(task.getId());
          item.setName(task.getName());
          item.setStatus(task.getTaskState().toString());
          item.setReadableStatus(getReadableState(task.getTaskState()));
          item.setTypeId(task.getType());
          ScheduledTaskDescriptor descriptor =
              getNexusConfiguration().getScheduledTaskDescriptor(task.getType());
          if (descriptor != null) {
            item.setTypeName(descriptor.getName());
          }
          item.setCreated(task.getScheduledAt() == null ? "n/a" : task.getScheduledAt().toString());
          item.setLastRunTime(task.getLastRun() == null ? "n/a" : task.getLastRun().toString());
          final Date nextRunTime = getNextRunTime(task);
          item.setNextRunTime(nextRunTime == null ? "n/a" : nextRunTime.toString());
          if (task.getScheduledAt() != null) {
            item.setCreatedInMillis(task.getScheduledAt().getTime());
          }
          if (task.getLastRun() != null) {
            item.setLastRunTimeInMillis(task.getLastRun().getTime());
          }
          if (nextRunTime != null) {
            item.setNextRunTimeInMillis(nextRunTime.getTime());
          }
          item.setSchedule(getScheduleShortName(task.getSchedule()));
          item.setEnabled(task.isEnabled());

          result.addData(item);
        }
      }
    }

    return result;
  }