public void testMetaData() throws ReportDataFactoryException {
    final KettleDataFactory kettleDataFactory = new KettleDataFactory();
    kettleDataFactory.initialize(new DesignTimeDataFactoryContext());
    kettleDataFactory.setQuery(
        "default",
        new KettleTransFromFileProducer(QUERY, STEP, new String[0], new ParameterMapping[0]));

    final DataFactoryMetaData metaData = kettleDataFactory.getMetaData();
    final Object queryHash =
        metaData.getQueryHash(kettleDataFactory, "default", new StaticDataRow());
    assertNotNull(queryHash);

    final KettleDataFactory kettleDataFactory2 = new KettleDataFactory();
    kettleDataFactory2.initialize(new DesignTimeDataFactoryContext());
    kettleDataFactory2.setQuery(
        "default",
        new KettleTransFromFileProducer(QUERY + "2", STEP, new String[0], new ParameterMapping[0]));
    kettleDataFactory2.setQuery(
        "default2",
        new KettleTransFromFileProducer(QUERY, STEP, new String[0], new ParameterMapping[0]));

    assertNotEquals(
        "Physical Query is not the same",
        queryHash,
        metaData.getQueryHash(kettleDataFactory2, "default", new StaticDataRow()));
    assertEquals(
        "Physical Query is the same",
        queryHash,
        metaData.getQueryHash(kettleDataFactory2, "default2", new StaticDataRow()));
  }
  protected void updateSelection() {
    final ReportSelectionModel selectionModel1 = getSelectionModel();
    if (selectionModel1 == null) {
      setEnabled(false);
      editedDataFactory = null;
      return;
    }

    final Object[] selectedObjects = selectionModel1.getSelectedElements();
    for (int i = 0; i < selectedObjects.length; i++) {
      final Object selectedObject = selectedObjects[i];
      if (selectedObject instanceof ReportQueryNode == false) {
        continue;
      }
      final ReportQueryNode queryNode = (ReportQueryNode) selectedObject;
      final DataFactory dataFactory = queryNode.getDataFactory();

      final DataFactoryMetaData metadata = dataFactory.getMetaData();
      if (metadata.isEditable()) {
        setEnabled(true);
        return;
      }
    }

    setEnabled(false);
  }
  public void testParameterMetadata() throws ReportDataFactoryException {
    final DriverConnectionProvider drc = new DriverConnectionProvider();
    drc.setDriver("org.hsqldb.jdbcDriver");
    drc.setUrl("jdbc:hsqldb:mem:SampleData");
    drc.setProperty("user", "sa");
    drc.setProperty("password", "");
    final SQLReportDataFactory sqlReportDataFactory = new SQLReportDataFactory(drc);
    initializeDataFactory(sqlReportDataFactory);
    final DataFactoryMetaData metaData = sqlReportDataFactory.getMetaData();
    sqlReportDataFactory.setQuery("test", "SELECT * FROM TABLE WHERE p=${x}");
    String[] fields =
        metaData.getReferencedFields(sqlReportDataFactory, "test", new StaticDataRow());
    assertNotNull(fields);
    assertEquals(2, fields.length);
    assertEquals("x", fields[0]);
    assertEquals(DataFactory.QUERY_LIMIT, fields[1]);

    sqlReportDataFactory.setQuery("test2", "SELECT * FROM TABLE WHERE p=${x} OR p=${y} OR p=${x}");
    fields = metaData.getReferencedFields(sqlReportDataFactory, "test2", new StaticDataRow());
    assertNotNull(fields);
    assertEquals(3, fields.length);
    assertEquals("x", fields[0]);
    assertEquals("y", fields[1]);
    assertEquals(DataFactory.QUERY_LIMIT, fields[2]);
  }
  protected String getDialogTitle() {

    if (datasourceId == null) {
      return "";
    }

    DataFactoryMetaData meta = DataFactoryRegistry.getInstance().getMetaData(datasourceId);
    String displayName = meta.getDisplayName(getLocale());
    return Messages.getString("KettleEmbeddedDataSourceDialog.Title", displayName);
  }
  public void testParameter() throws ReportDataFactoryException {
    final KettleDataFactory kettleDataFactory = new KettleDataFactory();
    kettleDataFactory.initialize(new DesignTimeDataFactoryContext());
    final ParameterMapping[] parameterMappings = {
      new ParameterMapping("name", "kettle-name"),
      new ParameterMapping("name2", "k3"),
      new ParameterMapping("name", "k2")
    };
    final String[] argumentNames = {"arg0"};
    kettleDataFactory.setQuery(
        "default", new KettleTransFromFileProducer(QUERY, STEP, argumentNames, parameterMappings));

    final DataFactoryMetaData metaData = kettleDataFactory.getMetaData();
    final String[] fields =
        metaData.getReferencedFields(kettleDataFactory, "default", new StaticDataRow());
    assertNotNull(fields);
    assertEquals(4, fields.length);
    assertEquals("arg0", fields[0]);
    assertEquals("name", fields[1]);
    assertEquals("name2", fields[2]);
    assertEquals(DataFactory.QUERY_LIMIT, fields[3]);
  }
  public void performEdit(final DataFactory dataFactory, final String queryName)
      throws ReportDataFactoryException {
    final DataFactoryMetaData metadata = dataFactory.getMetaData();
    if (metadata.isEditable() == false) {
      return;
    }

    final DataSourcePlugin dataSourcePlugin = metadata.createEditor();
    final DataFactory storedFactory = dataFactory.derive();
    if (dataSourcePlugin.canHandle(dataFactory)) {
      final ReportRenderContext activeContext = getActiveContext();
      final AbstractReportDefinition report = activeContext.getReportDefinition();
      final boolean editingActiveQuery = contains(report.getQuery(), dataFactory.getQueryNames());

      final ReportDesignerDesignTimeContext designTimeContext =
          new ReportDesignerDesignTimeContext(getReportDesignerContext());
      editedDataFactory =
          dataSourcePlugin.performEdit(designTimeContext, dataFactory, queryName, null);
      if (editedDataFactory == null) {
        return;
      }

      final Window parentWindow = designTimeContext.getParentWindow();
      parentWindow.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

      final CompoundDataFactory collection = (CompoundDataFactory) report.getDataFactory();
      final int dataFactoryCount = collection.size();
      for (int j = 0; j < dataFactoryCount; j++) {
        final DataFactory originalDataFactory = collection.getReference(j);
        if (originalDataFactory == dataFactory) {
          collection.remove(j);

          final DataFactory editedClone = editedDataFactory.derive();
          collection.add(j, editedDataFactory);
          activeContext
              .getUndo()
              .addChange(
                  ActionMessages.getString("EditQueryAction.Text"),
                  new DataSourceEditUndoEntry(j, storedFactory, editedClone));

          report.notifyNodeChildRemoved(originalDataFactory);
          report.notifyNodeChildAdded(editedDataFactory);

          parentWindow.setCursor(Cursor.getDefaultCursor());
          if (editingActiveQuery == false) {
            // if we are editing a query that is not the one the current report uses, do not mess
            // around with it.
            return;
          }

          final String[] editedQueries = editedDataFactory.getQueryNames();
          if (contains(report.getQuery(), editedQueries) == false) {
            report.setQuery(null);
          }
          return;
        }
      }

      throw new IllegalStateException();
    }
  }
  public void testMetaDataDrc() {
    final DriverConnectionProvider drc = new DriverConnectionProvider();
    drc.setDriver("org.hsqldb.jdbcDriver");
    drc.setUrl("jdbc:hsqldb:mem:SampleData");
    drc.setProperty("user", "sa");
    drc.setProperty("password", "");
    final SQLReportDataFactory sqlReportDataFactory = new SQLReportDataFactory(drc);
    final DataFactoryMetaData metaData = sqlReportDataFactory.getMetaData();
    assertNull(
        "No name property set, so display-name must be null",
        metaData.getDisplayConnectionName(sqlReportDataFactory));
    drc.setProperty("::pentaho-reporting::name", "test");
    assertEquals(
        "Name property set, so display name must be test",
        "test",
        metaData.getDisplayConnectionName(sqlReportDataFactory));
    sqlReportDataFactory.setQuery("test", "SELECT * FROM TABLE");

    assertNotNull(
        "QueryHash must exist",
        metaData.getQueryHash(sqlReportDataFactory, "test", new StaticDataRow()));

    final SQLReportDataFactory sqlReportDataFactory2 = new SQLReportDataFactory(drc);
    sqlReportDataFactory2.setQuery("test", "SELECT * FROM TABLE2");

    assertNotEquals(
        "Physical Queries do not match, so query hash must be different",
        metaData.getQueryHash(sqlReportDataFactory, "test", new StaticDataRow()),
        (metaData.getQueryHash(sqlReportDataFactory2, "test", new StaticDataRow())));

    sqlReportDataFactory2.setQuery("test2", "SELECT * FROM TABLE");
    final Object qh1 = metaData.getQueryHash(sqlReportDataFactory, "test", new StaticDataRow());
    final Object qh2 = metaData.getQueryHash(sqlReportDataFactory2, "test2", new StaticDataRow());
    assertEquals("Physical Queries match, so queries are considered the same", qh1, qh2);

    final DriverConnectionProvider drc2 = new DriverConnectionProvider();
    drc.setDriver("org.hsqldb.jdbcDriver");
    drc.setUrl("jdbc:hsqldb:mem:SampleData2");
    drc.setProperty("user", "sa");
    drc.setProperty("password", "");
    final SQLReportDataFactory sqlReportDataFactory3 = new SQLReportDataFactory(drc2);
    sqlReportDataFactory3.setQuery("test", "SELECT * FROM TABLE2");
    assertNotEquals(
        "Connections do not match, so query hash must be different",
        metaData.getQueryHash(sqlReportDataFactory, "test", new StaticDataRow()),
        (metaData.getQueryHash(sqlReportDataFactory3, "test", new StaticDataRow())));

    sqlReportDataFactory3.setQuery("test2", "SELECT * FROM TABLE");
    assertNotEquals(
        "Connections do not match, so queries are considered the same",
        metaData.getQueryHash(sqlReportDataFactory, "test", new StaticDataRow()),
        metaData.getQueryHash(sqlReportDataFactory3, "test2", new StaticDataRow()));
  }