/**
   * We will compute column-level statistics (min-value, max-value, # of null values, # of distinct
   * values) for all columns in tables in the model IFF the model is physical relational with a Jdbc
   * source. We must first prompt the user for the password, as it is not stored with the Jdbc
   * import settings.
   *
   * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
   * @since 4.3
   */
  @Override
  public void run() {
    if (isEnabled()) {
      final Shell shell = UiPlugin.getDefault().getCurrentWorkbenchWindow().getShell();
      try {
        ModelResource modelResource = ModelUtil.getModelResource(this.selectedModel, false);
        if (modelResource != null) {

          boolean cancelled = openEditorIfNeeded(modelResource);

          if (cancelled) {
            return;
          }
          final Resource resource = modelResource.getEmfResource();

          executeInTransaction(resource, shell);
        }
      } catch (Exception e) {
        InternalModelerJdbcUiPluginConstants.Util.log(e);
        final String title =
            InternalModelerJdbcUiPluginConstants.Util.getString(
                "CostAnalysisAction.errorTitle"); //$NON-NLS-1$
        final String message =
            InternalModelerJdbcUiPluginConstants.Util.getString(
                "CostAnalysisAction.errorMessage"); //$NON-NLS-1$
        MessageDialog.openError(shell, title, message);
      }
    }
  }
  private void internalExecute(final Resource resource, final Shell shell) {
    if (resource != null) {
      final JdbcSource source = JdbcUtil.findJdbcSource(resource);
      if (source != null) {
        final List emfTables = RelationalUtil.findTables(resource);
        final Map tblStats = createTableInfos(emfTables);
        if (tblStats != null && tblStats.size() > 0) {
          CostAnalysisDialog dialog =
              new CostAnalysisDialog(
                  shell,
                  InternalModelerJdbcUiPluginConstants.Util.getString(
                      "CostAnalysisAction.taskDescription"), //$NON-NLS-1$
                  InternalModelerJdbcUiPluginConstants.Util.getString(
                      "CostAnalysisAction.passwordPrompt",
                      new Object[] {source.getUrl(), source.getUsername()}),
                  null,
                  null); //$NON-NLS-1$
          dialog.open();

          final String password = dialog.getValue();
          if (password != null) {
            final Job job =
                new Job(
                    InternalModelerJdbcUiPluginConstants.Util.getString(
                        "CostAnalysisAction.jobDescription")) { //$NON-NLS-1$
                  @Override
                  protected IStatus run(IProgressMonitor monitor) {
                    try {
                      monitor.beginTask(
                          InternalModelerJdbcUiPluginConstants.Util.getString(
                              "CostAnalysisAction.taskDescription"),
                          calculateNumberOfWorkIncrements(tblStats.values())); // $NON-NLS-1$

                      CostAnalyzer costAnalyzer =
                          CostAnalyzerFactory.getCostAnalyzerFactory()
                              .getCostAnalyzer(source, password);
                      // log output to standard out
                      // costAnalyzer.setOutputStream(System.out);
                      costAnalyzer.collectStatistics(tblStats, monitor);

                      if (!monitor.isCanceled()) {
                        populateEmfColumnStatistics(emfTables, tblStats);
                      }

                      monitor.done();

                      if (monitor.isCanceled()) {
                        return Status.CANCEL_STATUS;
                      }

                      return new Status(
                          IStatus.OK,
                          ModelerJdbcUiConstants.PLUGIN_ID,
                          IStatus.OK,
                          InternalModelerJdbcUiPluginConstants.Util.getString(
                              "CostAnalysisAction.statusFinished", emfTables.size()),
                          null); //$NON-NLS-1$
                    } catch (Exception e) {
                      InternalModelerJdbcUiPluginConstants.Util.log(e);
                      return new Status(
                          IStatus.ERROR,
                          ModelerJdbcUiConstants.PLUGIN_ID,
                          IStatus.ERROR,
                          InternalModelerJdbcUiPluginConstants.Util.getString(
                              "CostAnalysisAction.errorMessage"),
                          e); //$NON-NLS-1$
                    } finally {
                    }
                  }
                };

            job.setSystem(false);
            job.setUser(true);
            job.setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
            // start as soon as possible
            job.schedule();
          }
        } else {
          MessageDialog.openInformation(
              shell,
              InternalModelerJdbcUiPluginConstants.Util.getString(
                  "CostAnalysisAction.taskDescription"),
              InternalModelerJdbcUiPluginConstants.Util.getString(
                  "CostAnalysisAction.noValidTablesMessage")); //$NON-NLS-1$ //$NON-NLS-2$
        }
      }
    }
  }