protected boolean addBlockoutPeriod(
      final JSONObject schedule, final JsJobTrigger trigger, String urlSuffix) {
    String url = GWT.getHostPageBaseURL() + "api/scheduler/blockout/" + urlSuffix; // $NON-NLS-1$

    RequestBuilder addBlockoutPeriodRequest = new RequestBuilder(RequestBuilder.POST, url);
    addBlockoutPeriodRequest.setHeader("accept", "text/plain"); // $NON-NLS-1$ //$NON-NLS-2$
    addBlockoutPeriodRequest.setHeader(
        "Content-Type", "application/json"); // $NON-NLS-1$ //$NON-NLS-2$
    addBlockoutPeriodRequest.setHeader("If-Modified-Since", "01 Jan 1970 00:00:00 GMT");

    // Create a unique blockout period name
    final Long duration = trigger.getBlockDuration();
    final String blockoutPeriodName =
        trigger.getScheduleType()
            + Random.nextInt()
            + ":"
            + //$NON-NLS-1$
            /* PentahoSessionHolder.getSession().getName() */ "admin"
            + ":"
            + duration; //$NON-NLS-1$ //$NON-NLS-2$

    // Add blockout specific parameters
    JSONObject addBlockoutParams = schedule;
    addBlockoutParams.put("jobName", new JSONString(blockoutPeriodName)); // $NON-NLS-1$
    addBlockoutParams.put("duration", new JSONNumber(duration)); // $NON-NLS-1$
    addBlockoutParams.put("timeZone", new JSONString(scheduleEditorWizardPanel.getTimeZone()));

    try {
      addBlockoutPeriodRequest.sendRequest(
          addBlockoutParams.toString(),
          new RequestCallback() {
            public void onError(Request request, Throwable exception) {
              MessageDialogBox dialogBox =
                  new MessageDialogBox(
                      Messages.getString("error"),
                      exception.toString(),
                      false,
                      false,
                      true); //$NON-NLS-1$
              dialogBox.center();
              setDone(false);
            }

            public void onResponseReceived(Request request, Response response) {
              if (response.getStatusCode() == Response.SC_OK) {
                if (null != callback) {
                  callback.okPressed();
                }
              }
            }
          });
    } catch (RequestException e) {
      // ignored
    }

    return true;
  }
  @SuppressWarnings("deprecation")
  public JsJobTrigger getJsJobTrigger() {
    JsJobTrigger jsJobTrigger = JsJobTrigger.instance();

    ScheduleType scheduleType = scheduleEditorWizardPanel.getScheduleType();
    Date startDate = scheduleEditorWizardPanel.getStartDate();
    String startTime = scheduleEditorWizardPanel.getStartTime();

    int startHour = getStartHour(startTime);
    int startMin = getStartMin(startTime);
    int startYear = startDate.getYear();
    int startMonth = startDate.getMonth();
    int startDay = startDate.getDate();
    Date startDateTime = new Date(startYear, startMonth, startDay, startHour, startMin);

    Date endDate = scheduleEditorWizardPanel.getEndDate();
    MonthOfYear monthOfYear = scheduleEditor.getRecurrenceEditor().getSelectedMonth();
    List<DayOfWeek> daysOfWeek = scheduleEditor.getRecurrenceEditor().getSelectedDaysOfWeek();
    Integer dayOfMonth = scheduleEditor.getRecurrenceEditor().getSelectedDayOfMonth();
    WeekOfMonth weekOfMonth = scheduleEditor.getRecurrenceEditor().getSelectedWeekOfMonth();

    if (isBlockoutDialog) {
      jsJobTrigger.setBlockDuration(calculateBlockoutDuration());
    } else {
      // blockDuration is only valid for blockouts
      jsJobTrigger.setBlockDuration(new Long(-1));
    }

    if (scheduleType == ScheduleType.RUN_ONCE) { // Run once types
      jsJobTrigger.setType("simpleJobTrigger"); // $NON-NLS-1$
      jsJobTrigger.setRepeatInterval(0);
      jsJobTrigger.setRepeatCount(0);
      jsJobTrigger.setNativeStartTime(
          DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(startDateTime));
    } else if ((scheduleType == ScheduleType.SECONDS)
        || (scheduleType == ScheduleType.MINUTES)
        || (scheduleType == ScheduleType.HOURS)) {
      int repeatInterval = 0;
      try { // Simple Trigger Types
        repeatInterval = Integer.parseInt(scheduleEditorWizardPanel.getRepeatInterval());
      } catch (Exception e) {
        // ignored
      }
      jsJobTrigger.setType("simpleJobTrigger"); // $NON-NLS-1$
      jsJobTrigger.setRepeatInterval(repeatInterval);
      jsJobTrigger.setRepeatCount(-1);
      jsJobTrigger.setNativeStartTime(
          DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(startDateTime));
      if (endDate != null) {
        jsJobTrigger.setNativeEndTime(
            DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(endDate));
      }
    } else if (scheduleType == ScheduleType.DAILY) {
      if (scheduleEditor.getRecurrenceEditor().isEveryNDays()) {
        int repeatInterval = 0;
        try { // Simple Trigger Types
          repeatInterval = Integer.parseInt(scheduleEditorWizardPanel.getRepeatInterval());
        } catch (Exception e) {
          // ignored
        }
        jsJobTrigger.setType("simpleJobTrigger"); // $NON-NLS-1$
        jsJobTrigger.setRepeatInterval(repeatInterval);
        jsJobTrigger.setRepeatCount(-1);
        jsJobTrigger.setNativeStartTime(
            DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(startDateTime));
        if (endDate != null) {
          jsJobTrigger.setNativeEndTime(
              DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(endDate));
        }
      } else {
        JsArrayInteger jsDaysOfWeek = (JsArrayInteger) JavaScriptObject.createArray();
        int i = 0;
        for (DayOfWeek dayOfWeek : daysOfWeek) {
          jsDaysOfWeek.set(i++, dayOfWeek.ordinal() + 1);
        }
        JsArrayInteger hours = (JsArrayInteger) JavaScriptObject.createArray();
        hours.set(0, startHour);
        JsArrayInteger minutes = (JsArrayInteger) JavaScriptObject.createArray();
        hours.set(0, startMin);
        JsArrayInteger seconds = (JsArrayInteger) JavaScriptObject.createArray();
        hours.set(0, 0);

        jsJobTrigger.setType("complexJobTrigger"); // $NON-NLS-1$
        jsJobTrigger.setDayOfWeekRecurrences(jsDaysOfWeek);
        jsJobTrigger.setHourRecurrences(hours);
        jsJobTrigger.setMinuteRecurrences(minutes);
        jsJobTrigger.setSecondRecurrences(seconds);
        jsJobTrigger.setNativeStartTime(
            DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(startDateTime));
        if (endDate != null) {
          jsJobTrigger.setNativeEndTime(
              DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(endDate));
        }
      }
    } else if (scheduleType == ScheduleType.CRON) { // Cron jobs
      jsJobTrigger.setType("cronJobTrigger"); // $NON-NLS-1$
    } else if ((scheduleType == ScheduleType.WEEKLY) && (daysOfWeek.size() > 0)) {
      JsArrayInteger jsDaysOfWeek = (JsArrayInteger) JavaScriptObject.createArray();
      int i = 0;
      for (DayOfWeek dayOfWeek : daysOfWeek) {
        jsDaysOfWeek.set(i++, dayOfWeek.ordinal() + 1);
      }
      JsArrayInteger hours = (JsArrayInteger) JavaScriptObject.createArray();
      hours.set(0, startHour);
      JsArrayInteger minutes = (JsArrayInteger) JavaScriptObject.createArray();
      hours.set(0, startMin);
      JsArrayInteger seconds = (JsArrayInteger) JavaScriptObject.createArray();
      hours.set(0, 0);

      jsJobTrigger.setType("complexJobTrigger"); // $NON-NLS-1$
      jsJobTrigger.setDayOfWeekRecurrences(jsDaysOfWeek);
      jsJobTrigger.setHourRecurrences(hours);
      jsJobTrigger.setMinuteRecurrences(minutes);
      jsJobTrigger.setSecondRecurrences(seconds);
      jsJobTrigger.setNativeStartTime(
          DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(startDateTime));
      if (endDate != null) {
        jsJobTrigger.setNativeEndTime(
            DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(endDate));
      }
    } else if ((scheduleType == ScheduleType.MONTHLY)
        || ((scheduleType == ScheduleType.YEARLY) && (monthOfYear != null))) {
      jsJobTrigger.setType("complexJobTrigger"); // $NON-NLS-1$

      if (dayOfMonth != null) {
        JsArrayInteger jsDaysOfMonth = (JsArrayInteger) JavaScriptObject.createArray();
        jsDaysOfMonth.set(0, dayOfMonth);

        JsArrayInteger hours = (JsArrayInteger) JavaScriptObject.createArray();
        hours.set(0, startHour);
        JsArrayInteger minutes = (JsArrayInteger) JavaScriptObject.createArray();
        hours.set(0, startMin);
        JsArrayInteger seconds = (JsArrayInteger) JavaScriptObject.createArray();
        hours.set(0, 0);

        jsJobTrigger.setType("complexJobTrigger"); // $NON-NLS-1$
        if (monthOfYear != null) {
          JsArrayInteger jsMonthsOfYear = (JsArrayInteger) JavaScriptObject.createArray();
          jsMonthsOfYear.set(0, monthOfYear.ordinal() + 1);
          jsJobTrigger.setMonthlyRecurrences(jsMonthsOfYear);
        }
        jsJobTrigger.setDayOfMonthRecurrences(jsDaysOfMonth);
        jsJobTrigger.setHourRecurrences(hours);
        jsJobTrigger.setMinuteRecurrences(minutes);
        jsJobTrigger.setSecondRecurrences(seconds);
        jsJobTrigger.setNativeStartTime(
            DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(startDateTime));
        if (endDate != null) {
          jsJobTrigger.setNativeEndTime(
              DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(endDate));
        }
      } else if ((daysOfWeek.size() > 0) && (weekOfMonth != null)) {
        JsArrayInteger jsDaysOfWeek = (JsArrayInteger) JavaScriptObject.createArray();
        int i = 0;
        for (DayOfWeek dayOfWeek : daysOfWeek) {
          jsDaysOfWeek.set(i++, dayOfWeek.ordinal() + 1);
        }

        JsArrayInteger hours = (JsArrayInteger) JavaScriptObject.createArray();
        hours.set(0, startHour);
        JsArrayInteger minutes = (JsArrayInteger) JavaScriptObject.createArray();
        hours.set(0, startMin);
        JsArrayInteger seconds = (JsArrayInteger) JavaScriptObject.createArray();
        hours.set(0, 0);

        jsJobTrigger.setType("complexJobTrigger"); // $NON-NLS-1$
        if (monthOfYear != null) {
          JsArrayInteger jsMonthsOfYear = (JsArrayInteger) JavaScriptObject.createArray();
          jsMonthsOfYear.set(0, monthOfYear.ordinal() + 1);
          jsJobTrigger.setMonthlyRecurrences(jsMonthsOfYear);
        }
        jsJobTrigger.setHourRecurrences(hours);
        jsJobTrigger.setMinuteRecurrences(minutes);
        jsJobTrigger.setSecondRecurrences(seconds);
        jsJobTrigger.setQualifiedDayOfWeek(daysOfWeek.get(0).name());
        jsJobTrigger.setDayOfWeekQualifier(weekOfMonth.name());
        jsJobTrigger.setNativeStartTime(
            DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(startDateTime));
        if (endDate != null) {
          jsJobTrigger.setNativeEndTime(
              DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(endDate));
        }
      }
    }
    return jsJobTrigger;
  }
  private void setupExisting(JsJob jsJob) {
    if (jsJob != null && !jsJob.equals("")) { // $NON-NLS-1$
      JsJobTrigger jsJobTrigger = jsJob.getJobTrigger();
      ScheduleType scheduleType = ScheduleType.valueOf(jsJobTrigger.getScheduleType());
      // scheduleEditor.setScheduleName(jsJob.getJobName());
      scheduleEditor.setScheduleType(scheduleType);
      if (scheduleType == ScheduleType.CRON
          || jsJobTrigger.getType().equals("cronJobTrigger")) { // $NON-NLS-1$
        scheduleEditor.getCronEditor().setCronString(jsJobTrigger.getCronString());
      } else if (jsJobTrigger.getType().equals("simpleJobTrigger")) { // $NON-NLS-1$
        if (jsJobTrigger.getRepeatCount() == -1) {
          // Recurring simple Trigger
          int interval = jsJobTrigger.getRepeatInterval();

          scheduleEditor.setRepeatInSecs(interval);
          if (scheduleType == ScheduleType.DAILY) {
            DailyRecurrenceEditor dailyRecurrenceEditor =
                scheduleEditor.getRecurrenceEditor().getDailyEditor();
            dailyRecurrenceEditor.setEveryNDays();
          }
        }
      } else if (jsJobTrigger.getType().equals("complexJobTrigger")) { // $NON-NLS-1$
        if (scheduleType == ScheduleType.DAILY) {
          // Daily
          DailyRecurrenceEditor dailyRecurrenceEditor =
              scheduleEditor.getRecurrenceEditor().getDailyEditor();
          if (jsJobTrigger.isWorkDaysInWeek()) {
            dailyRecurrenceEditor.setEveryWeekday();
          } else {
            dailyRecurrenceEditor.setEveryNDays();
          }
        } else if (scheduleType == ScheduleType.WEEKLY) {
          int[] daysOfWeek = jsJobTrigger.getDayOfWeekRecurrences();
          WeeklyRecurrenceEditor weeklyRecurrenceEditor =
              scheduleEditor.getRecurrenceEditor().getWeeklyEditor();
          String strDays = ""; // $NON-NLS-1$
          for (int i = 0; i < daysOfWeek.length; i++) {
            strDays += Integer.toString(daysOfWeek[i]) + ","; // $NON-NLS-1$
          }
          weeklyRecurrenceEditor.setCheckedDaysAsString(strDays, 1);
        } else if (scheduleType == ScheduleType.MONTHLY) {
          MonthlyRecurrenceEditor monthlyRecurrenceEditor =
              scheduleEditor.getRecurrenceEditor().getMonthlyEditor();
          if (jsJobTrigger.isQualifiedDayOfWeekRecurrence()) {
            // Run Every on ___day of Nth week every month
            monthlyRecurrenceEditor.setDayOfWeek(
                TimeUtil.DayOfWeek.valueOf(jsJobTrigger.getQualifiedDayOfWeek()));
            monthlyRecurrenceEditor.setWeekOfMonth(
                TimeUtil.WeekOfMonth.valueOf(jsJobTrigger.getDayOfWeekQualifier()));
            monthlyRecurrenceEditor.setNthDayNameOfMonth();
          } else {
            // Run on Nth day of the month
            monthlyRecurrenceEditor.setDayOfMonth(
                Integer.toString(jsJobTrigger.getDayOfMonthRecurrences()[0]));
          }
        } else if (scheduleType == ScheduleType.YEARLY) {
          YearlyRecurrenceEditor yearlyRecurrenceEditor =
              scheduleEditor.getRecurrenceEditor().getYearlyEditor();
          if (jsJobTrigger.isQualifiedDayOfWeekRecurrence()) {
            // Run Every on ___day of Nth week of the month M yearly
            yearlyRecurrenceEditor.setDayOfWeek(
                TimeUtil.DayOfWeek.valueOf(jsJobTrigger.getQualifiedDayOfWeek()));
            yearlyRecurrenceEditor.setWeekOfMonth(
                TimeUtil.WeekOfMonth.valueOf(jsJobTrigger.getDayOfWeekQualifier()));
            yearlyRecurrenceEditor.setMonthOfYear1(
                TimeUtil.MonthOfYear.get(jsJobTrigger.getMonthlyRecurrences()[0] - 1));
            yearlyRecurrenceEditor.setNthDayNameOfMonthName();
          } else {
            // Run on Nth day of the month M yearly
            yearlyRecurrenceEditor.setDayOfMonth(
                Integer.toString(jsJobTrigger.getDayOfMonthRecurrences()[0]));
            yearlyRecurrenceEditor.setMonthOfYear0(
                TimeUtil.MonthOfYear.get(jsJobTrigger.getMonthlyRecurrences()[0] - 1));
            yearlyRecurrenceEditor.setEveryMonthOnNthDay();
          }
        }
      }
      scheduleEditor.setStartDate(jsJobTrigger.getStartTime());
      scheduleEditor.setStartTime(
          DateTimeFormat.getFormat(PredefinedFormat.HOUR_MINUTE_SECOND)
              .format(jsJobTrigger.getStartTime()));
      if (jsJobTrigger.getEndTime() == null) {
        scheduleEditor.setNoEndDate();
      } else {
        scheduleEditor.setEndDate(jsJobTrigger.getEndTime());
        scheduleEditor.setEndBy();
      }

      if (isBlockoutDialog) {
        this.scheduleEditor.setDurationFields(jsJobTrigger.getBlockDuration());
      }
    }
  }