protected void createSnapshotTask(String name) throws Exception {
    ScheduledServicePropertyResource repositoryProp = new ScheduledServicePropertyResource();
    repositoryProp.setKey("repositoryId");
    repositoryProp.setValue("nexus-test-harness-snapshot-repo");

    ScheduledServicePropertyResource keepSnapshotsProp = new ScheduledServicePropertyResource();
    keepSnapshotsProp.setKey("minSnapshotsToKeep");
    keepSnapshotsProp.setValue(String.valueOf(0));

    ScheduledServicePropertyResource ageProp = new ScheduledServicePropertyResource();
    ageProp.setKey("removeOlderThanDays");
    ageProp.setValue(String.valueOf(0));

    ScheduledServicePropertyResource removeReleasedProp = new ScheduledServicePropertyResource();
    removeReleasedProp.setKey("removeIfReleaseExists");
    removeReleasedProp.setValue(String.valueOf(true));

    ScheduledServiceBaseResource scheduledTask = new ScheduledServiceBaseResource();
    scheduledTask.setEnabled(true);
    scheduledTask.setId(null);
    scheduledTask.setName(name);
    scheduledTask.setTypeId(SnapshotRemovalTaskDescriptor.ID);
    scheduledTask.setSchedule("manual");
    scheduledTask.addProperty(repositoryProp);
    scheduledTask.addProperty(keepSnapshotsProp);
    scheduledTask.addProperty(ageProp);
    scheduledTask.addProperty(removeReleasedProp);

    Status status = TaskScheduleUtil.create(scheduledTask);

    Assert.assertTrue(status.isSuccess());
  }
  public NexusTask<?> getModelNexusTask(ScheduledServiceBaseResource model, Request request)
      throws IllegalArgumentException, ResourceException {
    String serviceType = model.getTypeId();

    NexusTask<?> task = (NexusTask<?>) getNexusScheduler().createTaskInstance(serviceType);

    for (Iterator iter = model.getProperties().iterator(); iter.hasNext(); ) {
      ScheduledServicePropertyResource prop = (ScheduledServicePropertyResource) iter.next();
      task.addParameter(prop.getKey(), prop.getValue());
    }

    TaskUtils.setAlertEmail(task, model.getAlertEmail());
    TaskUtils.setId(task, model.getId());
    TaskUtils.setName(task, model.getName());

    return task;
  }
  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;
  }
  public Schedule getModelSchedule(ScheduledServiceBaseResource model)
      throws ParseException, InvalidConfigurationException {
    Schedule schedule = null;

    if (ScheduledServiceAdvancedResource.class.isAssignableFrom(model.getClass())) {
      schedule = new CronSchedule(((ScheduledServiceAdvancedResource) model).getCronCommand());
    } else if (ScheduledServiceMonthlyResource.class.isAssignableFrom(model.getClass())) {
      Date date =
          parseDate(
              ((ScheduledServiceMonthlyResource) model).getStartDate(),
              ((ScheduledServiceMonthlyResource) model).getRecurringTime());

      // validateStartDate( ( (ScheduledServiceMonthlyResource) model ).getStartDate() );

      // validateTime( "recurringTime", date );

      schedule =
          new MonthlySchedule(
              date,
              null,
              formatRecurringDayOfMonth(
                  ((ScheduledServiceMonthlyResource) model).getRecurringDay()));
    } else if (ScheduledServiceWeeklyResource.class.isAssignableFrom(model.getClass())) {
      Date date =
          parseDate(
              ((ScheduledServiceWeeklyResource) model).getStartDate(),
              ((ScheduledServiceWeeklyResource) model).getRecurringTime());

      // validateStartDate( ( (ScheduledServiceWeeklyResource) model ).getStartDate() );

      // validateTime( "recurringTime", date );

      schedule =
          new WeeklySchedule(
              date,
              null,
              formatRecurringDayOfWeek(((ScheduledServiceWeeklyResource) model).getRecurringDay()));
    } else if (ScheduledServiceDailyResource.class.isAssignableFrom(model.getClass())) {
      Date date =
          parseDate(
              ((ScheduledServiceDailyResource) model).getStartDate(),
              ((ScheduledServiceDailyResource) model).getRecurringTime());

      // validateStartDate( ( (ScheduledServiceDailyResource) model ).getStartDate() );

      // validateTime( "recurringTime", date );

      schedule = new DailySchedule(date, null);
    } else if (ScheduledServiceHourlyResource.class.isAssignableFrom(model.getClass())) {
      Date date =
          parseDate(
              ((ScheduledServiceHourlyResource) model).getStartDate(),
              ((ScheduledServiceHourlyResource) model).getStartTime());

      // validateStartDate( ( (ScheduledServiceHourlyResource) model ).getStartDate() );

      // validateTime( "startTime", date );

      schedule = new HourlySchedule(date, null);
    } else if (ScheduledServiceOnceResource.class.isAssignableFrom(model.getClass())) {
      Date date =
          parseDate(
              ((ScheduledServiceOnceResource) model).getStartDate(),
              ((ScheduledServiceOnceResource) model).getStartTime());

      validateStartDate(((ScheduledServiceOnceResource) model).getStartDate());

      validateTime("startTime", date);

      schedule =
          new OnceSchedule(
              parseDate(
                  ((ScheduledServiceOnceResource) model).getStartDate(),
                  ((ScheduledServiceOnceResource) model).getStartTime()));
    } else {
      schedule = new ManualRunSchedule();
    }

    return schedule;
  }
 public String getModelName(ScheduledServiceBaseResource model) {
   return model.getName();
 }
  /** 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;
  }