/** Add a new scheduled service to nexus. */
  @Override
  @POST
  @ResourceMethodSignature(
      input = ScheduledServiceResourceResponse.class,
      output = ScheduledServiceResourceStatusResponse.class)
  public Object post(Context context, Request request, Response response, Object payload)
      throws ResourceException {
    ScheduledServiceResourceResponse serviceRequest = (ScheduledServiceResourceResponse) payload;
    ScheduledServiceResourceStatusResponse result = null;

    if (serviceRequest != null) {
      ScheduledServiceBaseResource serviceResource = serviceRequest.getData();
      try {
        Schedule schedule = getModelSchedule(serviceRequest.getData());
        ScheduledTask<?> task = null;

        if (schedule != null) {
          task =
              getNexusScheduler()
                  .schedule(
                      getModelName(serviceResource),
                      getModelNexusTask(serviceResource, request),
                      schedule);
        } else {
          task =
              getNexusScheduler()
                  .schedule(
                      getModelName(serviceResource),
                      getModelNexusTask(serviceResource, request),
                      new ManualRunSchedule());
        }

        task.setEnabled(serviceResource.isEnabled());

        // Need to store the enabled flag update
        getNexusScheduler().updateSchedule(task);

        ScheduledServiceResourceStatus resourceStatus = new ScheduledServiceResourceStatus();
        resourceStatus.setResource(serviceResource);
        // Just need to update the id, as the incoming data is a POST w/ no id
        resourceStatus.getResource().setId(task.getId());
        resourceStatus.setResourceURI(createChildReference(request, this, task.getId()).toString());
        resourceStatus.setStatus(task.getTaskState().toString());
        resourceStatus.setReadableStatus(getReadableState(task.getTaskState()));
        resourceStatus.setCreated(
            task.getScheduledAt() == null ? "n/a" : task.getScheduledAt().toString());
        resourceStatus.setLastRunResult(
            TaskState.BROKEN.equals(task.getTaskState()) ? "Error" : "Ok");
        resourceStatus.setLastRunTime(
            task.getLastRun() == null ? "n/a" : task.getLastRun().toString());
        resourceStatus.setNextRunTime(
            task.getNextRun() == null ? "n/a" : task.getNextRun().toString());
        if (task.getScheduledAt() != null) {
          resourceStatus.setCreatedInMillis(task.getScheduledAt().getTime());
        }
        if (task.getLastRun() != null) {
          resourceStatus.setLastRunTimeInMillis(task.getLastRun().getTime());
        }
        if (task.getNextRun() != null) {
          resourceStatus.setNextRunTimeInMillis(task.getNextRun().getTime());
        }

        result = new ScheduledServiceResourceStatusResponse();
        result.setData(resourceStatus);
      } catch (RejectedExecutionException e) {
        getLogger().warn("Execution of task " + getModelName(serviceResource) + " rejected.");

        throw new ResourceException(Status.CLIENT_ERROR_CONFLICT, e.getMessage());
      } catch (ParseException e) {
        getLogger().warn("Unable to parse data for task " + getModelName(serviceResource));

        throw new PlexusResourceException(
            Status.CLIENT_ERROR_BAD_REQUEST,
            e.getMessage(),
            getNexusErrorResponse("cronCommand", e.getMessage()));
      } catch (InvalidConfigurationException e) {
        handleConfigurationException(e);
      }
    }
    return result;
  }
  /**
   * 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;
  }