public void enableTools() {
    List<Schedule> scheduleList = schedulesListCtrl.getSelectedSchedules();

    boolean suspendEnabled = false, resumeEnabled = false;

    boolean resolvedSuspended = false, resolvedEnabled = false;

    for (Schedule schedule : scheduleList) {
      if (!resolvedSuspended
          && !schedule.getTriggerState().equalsIgnoreCase(SUSPENDED_SCHEDULER_STATE)) {
        suspendEnabled = true;
        resolvedSuspended = true;
      }

      if (!resolvedEnabled
          && !schedule.getTriggerState().equalsIgnoreCase(NORMAL_SCHEDULER_STATE)) {
        resumeEnabled = true;
        resolvedEnabled = true;
      }
    }

    int numSelectedItems = scheduleList.size();

    enableWidget(schedulerToolbar.getCreateBtn(), true);
    enableWidget(schedulerToolbar.getUpdateBtn(), 1 == numSelectedItems);
    enableWidget(schedulerToolbar.getDeleteBtn(), numSelectedItems > 0);
    enableWidget(schedulerToolbar.getSuspendBtn(), suspendEnabled);
    enableWidget(schedulerToolbar.getResumeBtn(), resumeEnabled);
    enableWidget(schedulerToolbar.getRunNowBtn(), numSelectedItems > 0);
    enableWidget(schedulerToolbar.getRefreshBtn(), true);
  }
 private int getNumSubscribers() {
   List<Schedule> schedList = schedulesListCtrl.getSelectedSchedules();
   int numSubscribers = 0;
   for (Schedule sched : schedList) {
     try {
       numSubscribers += Integer.parseInt(sched.getSubscriberCount());
     } catch (NumberFormatException ex) {
       // Do nothing, we'll return 0.
     }
   }
   return numSubscribers;
 }
  private void handleUpdateSchedule() {
    final SchedulerToolbarController localThis = this;

    scheduleCreatorDialog.setTitle(Messages.getString("scheduleEditor")); // $NON-NLS-1$
    final List<Schedule> scheduleList = schedulesListCtrl.getSelectedSchedules();
    if (!loadingInitialized) {
      scheduleCreatorDialog.setOkBtnEnabled(false);
    }
    scheduleCreatorDialog.setOnOkHandler(
        new ICallback<MessageDialog>() {
          public void onHandle(MessageDialog d) {
            localThis.updateScheduleWithNewScheduleType();
          }
        });
    this.scheduleCreatorDialog.setOnValidateHandler(
        new IResponseCallback<MessageDialog, Boolean>() {
          public Boolean onHandle(MessageDialog schedDlg) {
            return isUpdateScheduleCreatorDialogValid();
          }
        });
    // the update button should be enabled/disabled to guarantee that one and only one schedule is
    // selected
    assert scheduleList.size() == 1
        : "When clicking update, exactly one schedule should be selected."; //$NON-NLS-1$

    Schedule sched = scheduleList.get(0);
    try {
      initScheduleCreatorDialog(sched);
      scheduleCreatorDialog.center();
      scheduleCreatorDialog.getScheduleEditor().setFocus();
    } catch (CronParseException e) {
      final MessageDialog errorDialog =
          new MessageDialog(
              Messages.getString("error"), // $NON-NLS-1$
              Messages.getString(
                  "invalidCronInInitOfRecurrenceDialog",
                  sched.getCronString(),
                  e.getMessage())); // $NON-NLS-1$
      errorDialog.setOnOkHandler(
          new ICallback<MessageDialog>() {
            public void onHandle(MessageDialog messageDialog) {
              errorDialog.hide();
              scheduleCreatorDialog.center();
            }
          });
      errorDialog.center();
    }
  }
  private void handleRunNowSchedules() {
    final List<Schedule> selectedScheduleList = schedulesListCtrl.getSelectedSchedules();

    AsyncCallback<Object> outerCallback =
        new AsyncCallback<Object>() {

          public void onSuccess(Object result) {
            AsyncCallback<Object> innerCallback =
                new AsyncCallback<Object>() {
                  public void onSuccess(Object pResult) {
                    loadJobsTable();
                  }

                  public void onFailure(Throwable caught) {
                    MessageDialogBox messageDialog =
                        new MessageDialogBox(
                            ExceptionParser.getErrorHeader(caught.getMessage()),
                            ExceptionParser.getErrorMessage(
                                caught.getMessage(), caught.getMessage()),
                            false,
                            false,
                            true);
                    messageDialog.center();
                  }
                }; // end inner callback
            final List<Schedule> subscriptionSchedList =
                getSubscriptionSchedules(selectedScheduleList);
            PacServiceFactory.getSubscriptionService()
                .executeJobs(subscriptionSchedList, innerCallback);
          } // end onSuccess

          public void onFailure(Throwable caught) {
            MessageDialogBox messageDialog =
                new MessageDialogBox(
                    ExceptionParser.getErrorHeader(caught.getMessage()),
                    ExceptionParser.getErrorMessage(caught.getMessage(), caught.getMessage()),
                    false,
                    false,
                    true);
            messageDialog.center();
          }
        }; // end outer callback -----------

    List<Schedule> nonSubscriptionSchedList = getSchedules(selectedScheduleList);
    PacServiceFactory.getSchedulerService().executeJobs(nonSubscriptionSchedList, outerCallback);
  }
  @SuppressWarnings("fallthrough")
  private void updateScheduleWithNewScheduleType() {

    final List<Schedule> scheduleList = schedulesListCtrl.getSelectedSchedules();
    Schedule oldSchedule = scheduleList.get(0);

    AsyncCallback<Object> deleteScheduleCallback =
        new AsyncCallback<Object>() {
          public void onSuccess(Object o) {
            createSchedule();
          }

          public void onFailure(Throwable caught) {
            MessageDialogBox messageDialog =
                new MessageDialogBox(
                    ExceptionParser.getErrorHeader(caught.getMessage()),
                    ExceptionParser.getErrorMessage(caught.getMessage(), caught.getMessage()),
                    false,
                    false,
                    true);
            messageDialog.center();
          }
        };

    // TODO sbarkdull scheduleCreatorDialog -> scheduleEditorDialog
    DualModeScheduleEditor scheduleEditor = scheduleCreatorDialog.getScheduleEditor();

    ISchedulerServiceAsync schedSvc = null;
    if (oldSchedule.isSubscriptionSchedule() != scheduleEditor.isSubscriptionSchedule()) {
      // they are changing the schedule type, so delete it, and add a new one
      schedSvc =
          oldSchedule.isSubscriptionSchedule()
              ? PacServiceFactory.getSubscriptionService()
              : PacServiceFactory.getSchedulerService();
      List<Schedule> deleteList = new ArrayList<Schedule>();
      deleteList.add(oldSchedule);
      schedSvc.deleteJobs(deleteList, deleteScheduleCallback);
    } else {
      // they are NOT changing the schedule type, so just update the existing schedule.
      updateSchedule();
    }
  }
  private void loadJobsTable() {

    final ProgressPopupPanel loadingPanel = new ProgressPopupPanel();
    loadingPanel.setLabelText(Messages.getString("loading")); // $NON-NLS-1$
    loadingPanel.center();

    final int currScrollPos = schedulesListCtrl.getScrollPosition();
    final Map<String, Schedule> schedulesMap = new HashMap<String, Schedule>();

    AsyncCallback<Map<String, Schedule>> schedulerServiceCallback =
        new AsyncCallback<Map<String, Schedule>>() {
          public void onSuccess(Map<String, Schedule> pSchedulesMap) {
            schedulesMap.putAll(pSchedulesMap);
            enableTools();

            AsyncCallback<Map<String, Schedule>> subscriptionServiceCallback =
                new AsyncCallback<Map<String, Schedule>>() {
                  public void onSuccess(Map<String, Schedule> subscriptionSchedulesMap) {
                    List<Schedule> schedulesList =
                        mergeSchedules(schedulesMap, subscriptionSchedulesMap);
                    schedulesModel = new SchedulesModel();
                    schedulesModel.add(schedulesList);
                    initFilterList();
                    updateSchedulesTable();
                    if (INVALID_SCROLL_POS != currScrollPos) {
                      schedulesListCtrl.setScrollPosition(currScrollPos);
                    }
                    enableTools();
                    loadingPanel.hide();
                  } // end inner onSuccess

                  public void onFailure(Throwable caught) {
                    loadingPanel.hide();
                    schedulesListCtrl.setTempMessage(
                        Messages.getString("noSchedules")); // $NON-NLS-1$
                    MessageDialog messageDialog =
                        new MessageDialog(
                            Messages.getString("error"), // $NON-NLS-1$
                            caught.getMessage());
                    messageDialog.center();
                    enableTools();
                  } // end inner onFailure
                }; // end subscriptionServiceCallback

            PacServiceFactory.getSubscriptionService().getJobNames(subscriptionServiceCallback);
          } // end outer onSuccess

          public void onFailure(Throwable caught) {
            loadingPanel.hide();
            schedulesListCtrl.setTempMessage(Messages.getString("noSchedules")); // $NON-NLS-1$
            MessageDialogBox messageDialog =
                new MessageDialogBox(
                    ExceptionParser.getErrorHeader(caught.getMessage()),
                    ExceptionParser.getErrorMessage(caught.getMessage(), caught.getMessage()),
                    false,
                    false,
                    true);
            messageDialog.center();
            messageDialog.show();
            enableTools();
          } // end outer onFailure
        }; // end schedulerServiceCallback

    PacServiceFactory.getSchedulerService().getJobNames(schedulerServiceCallback);
  }
  private void updateSchedule() {

    AsyncCallback<Object> updateScheduleResponseCallback =
        new AsyncCallback<Object>() {
          public void onSuccess(Object o) {
            scheduleCreatorDialog.hide();
            loadJobsTable();
          }

          public void onFailure(Throwable caught) {
            MessageDialogBox messageDialog =
                new MessageDialogBox(
                    ExceptionParser.getErrorHeader(caught.getMessage()),
                    ExceptionParser.getErrorMessage(caught.getMessage(), caught.getMessage()),
                    false,
                    false,
                    true);
            messageDialog.center();
          }
        };
    final List<Schedule> scheduleList = schedulesListCtrl.getSelectedSchedules();
    Schedule oldSchedule = scheduleList.get(0);
    DualModeScheduleEditor scheduleEditor = scheduleCreatorDialog.getScheduleEditor();

    ISchedulerServiceAsync schedSvc =
        scheduleEditor.isSubscriptionSchedule()
            ? PacServiceFactory.getSubscriptionService()
            : PacServiceFactory.getSchedulerService();

    String cronStr = scheduleEditor.getCronString();
    Date startDate = scheduleEditor.getStartDate();
    Date endDate = scheduleEditor.getEndDate();

    if (null == cronStr) { // must be a repeating schedule
      String startTime =
          scheduleEditor
              .getStartTime(); // format of string should be: HH:MM:SS AM/PM, e.g. 7:12:28 PM
      startDate = TimeUtil.getDateTime(startTime, startDate);
      endDate = (null != endDate) ? TimeUtil.getDateTime(startTime, endDate) : null;
    }

    ScheduleEditor.ScheduleType rt = scheduleEditor.getScheduleType();
    switch (rt) {
      case RUN_ONCE:
        schedSvc.updateRepeatSchedule(
            oldSchedule.getJobName(),
            oldSchedule.getJobGroup(),
            oldSchedule.getSchedId(),
            scheduleEditor.getName().trim(),
            scheduleEditor.getGroupName().trim(),
            scheduleEditor.getDescription().trim(),
            startDate,
            endDate,
            "0" /*repeat count*/, //$NON-NLS-1$
            "0" /*repeat time*/, //$NON-NLS-1$
            scheduleCreatorDialog
                .getSolutionRepositoryActionSequenceEditor()
                .getActionsAsString()
                .trim(),
            updateScheduleResponseCallback);
        break;
      case SECONDS: // fall through
      case MINUTES: // fall through
      case HOURS: // fall through
      case DAILY: // fall through
      case WEEKLY: // fall through
      case MONTHLY: // fall through
      case YEARLY:
        if (null == cronStr) {
          String repeatInterval =
              Long.toString(TimeUtil.secsToMillisecs(scheduleEditor.getRepeatInSecs()));
          schedSvc.updateRepeatSchedule(
              oldSchedule.getJobName(),
              oldSchedule.getJobGroup(),
              oldSchedule.getSchedId(),
              scheduleEditor.getName().trim(),
              scheduleEditor.getGroupName().trim(),
              scheduleEditor.getDescription().trim(),
              startDate,
              endDate,
              null /*repeat count*/,
              repeatInterval.trim(),
              scheduleCreatorDialog
                  .getSolutionRepositoryActionSequenceEditor()
                  .getActionsAsString()
                  .trim(),
              updateScheduleResponseCallback);
          break;
        } else {
          // fall through to case CRON
        }
      case CRON:
        schedSvc.updateCronSchedule(
            oldSchedule.getJobName(),
            oldSchedule.getJobGroup(),
            oldSchedule.getSchedId(),
            scheduleEditor.getName().trim(),
            scheduleEditor.getGroupName().trim(),
            scheduleEditor.getDescription().trim(),
            startDate,
            endDate,
            cronStr.trim(),
            scheduleCreatorDialog
                .getSolutionRepositoryActionSequenceEditor()
                .getActionsAsString()
                .trim(),
            updateScheduleResponseCallback);
        break;
      default:
        throw new RuntimeException(
            Messages.getString("invalidRunType", rt.toString())); // $NON-NLS-1$
    }
  }