Exemple #1
0
  public static Result createJob(String username) {
    // TODO needs to come from the session
    User user = getUser(username);

    // Get source profile
    String sourceProfileId = getFormParam("sourceProfileId");
    if (sourceProfileId == null) {
      Logger.warn(MISSING_PARAM + "sourceProfileId");
      return badRequest(MISSING_PARAM + "sourceProfileId");
    }

    DatastoreProfile sourceProfile = null;
    try {
      sourceProfile = DatastoreProfile.find.byId(Long.parseLong(sourceProfileId));
    } catch (Throwable t) {
      Logger.error(t.getMessage());
    }

    if (sourceProfile == null) {
      Logger.warn(PROFILE_NOT_FOUND + sourceProfileId);
      return notFound(PROFILE_NOT_FOUND + sourceProfileId);
    }

    if (sourceProfile.type != Type.DATASOURCE) {
      Logger.warn(NOT_A_SOURCE.replace("@@id@@", sourceProfileId));
      return badRequest(NOT_A_SOURCE.replace("@@id@@", sourceProfileId));
    }

    Logger.info("New job - source " + sourceProfile.pluginClass);

    String listOfActions = getFormParam("requiredActions");
    if (listOfActions == null) listOfActions = "";
    Logger.info("New job - actions " + listOfActions);

    // Get sink profile
    String sinkProfileId = getFormParam("sinkProfileId");
    if (sinkProfileId == null) {
      Logger.warn(MISSING_PARAM + "sinkProfileId");
      return badRequest(MISSING_PARAM + "sinkProfileId");
    }

    DatastoreProfile sinkProfile = null;
    try {
      sinkProfile = DatastoreProfile.find.byId(Long.parseLong(sinkProfileId));
    } catch (Throwable t) {
      Logger.error(t.getMessage());
    }

    if (sinkProfile == null) {
      Logger.warn(PROFILE_NOT_FOUND + sinkProfileId);
      return notFound(PROFILE_NOT_FOUND + sinkProfileId);
    }

    if (sinkProfile.type != Type.DATASINK) {
      Logger.warn(NOT_A_SINK.replace("@@id@@", sinkProfileId));
      return badRequest(NOT_A_SINK.replace("@@id@@", sinkProfileId));
    }

    Logger.info("New job - sink " + sinkProfile.pluginClass);

    // Time expression
    String timeExpression = getFormParam("timeExpression");
    Logger.info("New job - time expression: " + timeExpression);
    if (timeExpression == null) return badRequest(MISSING_PARAM + "timeExpression");

    // TODO - we start immediately for testing purposes ONLY
    Date start = new Date();

    // Job Title
    String jobTitle = getFormParam("jobTitle");
    if (jobTitle == null) return badRequest(MISSING_PARAM + "jobTitle");

    Job job =
        new Job(
            user,
            jobTitle,
            sourceProfile,
            listOfActions.trim(),
            sinkProfile,
            start,
            getDelay(timeExpression));
    job.save();

    SimpleJobManager.getInstance().queueJob(job);

    return ok();
  }