private ResultSet getQueryResult(
      Connection connection, PivotWidget widget, Map<String, Object> urlQueryParameters)
      throws ReportRunnerException {

    Entity entity = widget.getEntity();
    ro.nextreports.engine.Report report =
        NextUtil.getNextReport(storageService.getSettings(), entity);
    DataSource dataSource = getDataSource(entity);

    String sql = ro.nextreports.engine.util.ReportUtil.getSql(report);
    // retrieves the report parameters
    Map<String, QueryParameter> parameters = new LinkedHashMap<String, QueryParameter>();
    List<QueryParameter> parameterList = report.getParameters();
    if (parameterList != null) {
      for (QueryParameter param : parameterList) {
        parameters.put(param.getName(), param);
      }
    }

    Map<String, Object> parameterValues = new HashMap<String, Object>();

    // pivot is running with settings parameter values
    ChartUtil.initParameterSettings(
        parameterValues,
        widget.getQueryRuntime(),
        dashboardService.getUserWidgetParameters(widget.getId()));

    // parameters from embedded code
    try {
      ReportUtil.addUrlQueryParameters(
          storageService.getSettings(), entity, parameterValues, urlQueryParameters);
    } catch (Exception e1) {
      e1.printStackTrace();
      LOG.error(e1.getMessage(), e1);
    }

    QueryExecutor executor = null;
    try {
      boolean csv = CSVDialect.DRIVER_CLASS.equals(dataSource.getDriver());

      Query query = new Query(sql);
      executor = new QueryExecutor(query, parameters, parameterValues, connection, true, true, csv);
      executor.setMaxRows(0);
      executor.setTimeout(storageService.getSettings().getQueryTimeout());

      QueryResult queryResult = executor.execute();

      return queryResult.getResultSet();
    } catch (Exception e) {
      throw new ReportRunnerException(e);
    } finally {
      if (executor != null) {
        executor.closeCursors();
      }
    }
  }
  public RunHistoryQueryPanel(String id, final RunReportHistory runHistory) {
    super(id);

    int index = runHistory.getPath().lastIndexOf("/runHistory");
    String reportPath = runHistory.getPath().substring(0, index);
    Report report;
    String query = "NA";
    try {
      report = (Report) storageService.getEntity(reportPath);

      if (ReportConstants.NEXT.equals(report.getType())) {
        query =
            ro.nextreports.engine.util.ReportUtil.getSql(
                NextUtil.getNextReport(settings.getSettings(), report),
                runHistory.getParametersValues());
      }
    } catch (NotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    add(new MultiLineLabel("query", new Model<String>(query)));
  }
  public void actionPerformed(ActionEvent e) {

    if (NextReportsUtil.isInnerEdit()) {
      return;
    }

    if (MessageUtil.showReconnect()) {
      return;
    }

    if (queryName == null) {
      QueryBrowserPanel browser = new QueryBrowserPanel();
      dialog = new QueryBrowserDialog(browser);
      dialog.pack();
      dialog.setResizable(false);
      Show.centrateComponent(Globals.getMainFrame(), dialog);
      dialog.setVisible(true);

      // On double click in OpenQuery dialog the dialog is closed and set to null
      // see comment #2
      if (dialog != null) {
        if (dialog.okPressed()) {
          queryName = browser.getSelectedName();
          queryPath = browser.getSelectedFilePath();
        } else {
          executed = false;
          return;
        }
      }
    }

    if (queryName == null) {
      return;
    }

    // comment #2  : for double click in OpenQuery dialog
    if ((dialog != null) && dialog.isVisible()) {
      dialog.dispose();
      dialog = null;
    }

    final QueryBuilderPanel builderPanel = Globals.getMainFrame().getQueryBuilderPanel();

    if (!NextReportsUtil.saveYesNoCancel(I18NSupport.getString("open.query.desc"))) {
      executed = false;
      return;
    }

    // queryPath is null when we use Wizard
    if (queryPath != null) {
      byte status = ReportUtil.isValidReportVersion(queryPath);
      if (ReportUtil.REPORT_INVALID_OLDER == status) {
        Show.error(I18NSupport.getString("query.version.invalid.older"));
        return;
      } else if (ReportUtil.REPORT_INVALID_NEWER == status) {
        Show.error(
            I18NSupport.getString(
                "query.version.invalid.newer", ReleaseInfoAdapter.getVersionNumber()));
        return;
      }
    }

    Thread executorThread =
        new Thread(
            new Runnable() {

              public void run() {
                UIActivator activator =
                    new UIActivator(Globals.getMainFrame(), I18NSupport.getString("load.query"));
                activator.start();

                builderPanel.emptyReportAndChart();
                Globals.setCurrentReportName(null);
                Globals.setCurrentReportAbsolutePath(null);
                Globals.setCurrentChartName(null);
                Globals.setCurrentChartAbsolutePath(null);
                Globals.setReportLoaded(false);
                Globals.setChartLoaded(false);
                Globals.setInitialQuery("");
                // builderPanel.selectDesignerTab();

                ReportPersistence repPersist =
                    ReportPersistenceFactory.createReportPersistence(
                        Globals.getReportPersistenceType());
                try {
                  final Report report;
                  if (createdReport != null) {
                    report = createdReport;
                  } else {
                    report = repPersist.loadReport(queryPath);
                  }

                  final Map<String, List<CheckListItem>> itemMap = TableUtil.getItemMap(report);

                  if (activator != null) {
                    activator.stop();
                    activator = null;
                  }

                  SwingUtilities.invokeAndWait(
                      new Runnable() {
                        public void run() {

                          builderPanel.clear(true);

                          if (report.getSql() != null) {
                            builderPanel.selectSQLViewTab();
                            builderPanel.setUserSql(report.getSql());
                          } else if (report.getQuery() != null) {
                            builderPanel.drawDesigner(report, itemMap);
                            builderPanel.selectDesignerTab();
                            itemMap.clear();
                          }
                          builderPanel.setParameters(report.getParameters());
                          // builderPanel.selectTreeNode(report.getName(), DBObject.QUERIES);
                          if (selection) {
                            builderPanel.selectTreeNode(
                                report.getName(), queryPath, DBObject.QUERIES);
                          }

                          Globals.setCurrentQueryName(queryName);
                          // System.out.println("---- queryPath="+queryPath);
                          Globals.setCurrentReportAbsolutePath(null);
                          Globals.setCurrentQueryAbsolutePath(queryPath);
                          Globals.getMainMenuBar().newQueryActionUpdate();
                          Globals.getMainToolBar().newQueryActionUpdate();
                          Globals.setOriginalSql(builderPanel.getUserSql());
                          executed = true;

                          afterCreation();
                        }
                      });

                } catch (Exception e1) {
                  Show.error(e1);
                } finally {
                  queryName = null;
                  if (activator != null) {
                    activator.stop();
                  }
                }
              }
            },
            "NEXT : " + getClass().getSimpleName());
    executorThread.start();
  }