public static void fillCatalogAndSchemas(
      IMetadataConnection iMetadataConnection, DatabaseConnection temConnection)
      throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
    java.sql.Connection sqlConn = null;
    try {
      MetadataFillFactory dbInstance = MetadataFillFactory.getDBInstance(iMetadataConnection);
      temConnection =
          (DatabaseConnection) dbInstance.fillUIConnParams(iMetadataConnection, temConnection);
      sqlConn = MetadataConnectionUtils.createConnection(iMetadataConnection).getObject();
      // because there is no any structure after import into 423 from 402,just sychronized the two
      // connection's
      // UISchema for fill catalogs and scheams
      if (((DatabaseConnection) iMetadataConnection.getCurrentConnection()).getUiSchema() != null) {
        temConnection.setUiSchema(
            ((DatabaseConnection) iMetadataConnection.getCurrentConnection()).getUiSchema());
      }

      if (((DatabaseConnection) iMetadataConnection.getCurrentConnection()).getSID() != null) {
        temConnection.setSID(
            ((DatabaseConnection) iMetadataConnection.getCurrentConnection()).getSID());
      }

      String dbType = iMetadataConnection.getDbType();
      if (sqlConn != null) {
        DatabaseMetaData dbMetaData = null;
        // Added by Marvin Wang on Mar. 13, 2013 for loading hive jars dynamically, refer to
        // TDI-25072.
        if (EDatabaseTypeName.HIVE.getXmlName().equalsIgnoreCase(dbType)) {
          dbMetaData =
              HiveConnectionManager.getInstance().extractDatabaseMetaData(iMetadataConnection);
        } else {
          dbMetaData =
              ExtractMetaDataUtils.getInstance()
                  .getDatabaseMetaData(sqlConn, dbType, false, iMetadataConnection.getDatabase());
        }
        dbInstance.fillCatalogs(
            temConnection,
            dbMetaData,
            iMetadataConnection,
            MetadataConnectionUtils.getPackageFilter(temConnection, dbMetaData, true));

        dbInstance.fillSchemas(
            temConnection,
            dbMetaData,
            iMetadataConnection,
            MetadataConnectionUtils.getPackageFilter(temConnection, dbMetaData, false));
      }
    } catch (SQLException e) {
      throw e;
    } finally {
      if (sqlConn != null) {
        ConnectionUtils.closeConnection(sqlConn);
      }
      MetadataConnectionUtils.closeDerbyDriver();
    }
  }
  /**
   * DataProviderBuilder constructor.
   *
   * @param conn the connection
   * @param driver the JDBC driver
   * @param databaseUrl the database connection string (must not be null)
   * @param driverProperties the properties passed to the driver (could be null)
   * @throws SQLException
   */
  public DataProviderBuilder(
      DatabaseConnection databaseConn, Connection conn, Driver driver, String databaseUrl)
      throws SQLException {
    super(databaseConn);
    dataProvider = databaseConn;

    // MOD xqliu 2009-07-13 bug 7888
    String identifierQuote =
        org.talend.utils.sql.ConnectionUtils.getConnectionMetadata(conn).getIdentifierQuoteString();
    // ~
    // MOD xqliu 2009-11-24 bug 7888
    ConnectionHelper.setIdentifierQuoteString(
        identifierQuote == null ? "" : identifierQuote, dataProvider);
    // ~
  }
  public static TypedReturnCode<Connection> createConnection(DBConnectionParameter parameter) {
    TypedReturnCode<Connection> tReturnCode = new TypedReturnCode<Connection>(false);
    MetadataFillFactory instance = null;
    instance = MetadataFillFactory.getDBInstance();

    IMetadataConnection metaConnection = instance.fillUIParams(ParameterUtil.toMap(parameter));
    ReturnCode rc = instance.createConnection(metaConnection);
    if (rc.isOk()) {
      Connection dbConn = instance.fillUIConnParams(metaConnection, null);
      DatabaseMetaData dbMetadata = null;
      List<String> packageFilter = ConnectionUtils.getPackageFilter(parameter);
      java.sql.Connection sqlConn = null;
      try {
        if (rc instanceof TypedReturnCode) {
          @SuppressWarnings("rawtypes")
          Object sqlConnObject = ((TypedReturnCode) rc).getObject();
          if (sqlConnObject instanceof java.sql.Connection) {
            sqlConn = (java.sql.Connection) sqlConnObject;
            dbMetadata = org.talend.utils.sql.ConnectionUtils.getConnectionMetadata(sqlConn);
          }
        }

        instance.fillCatalogs(dbConn, dbMetadata, packageFilter);
        instance.fillSchemas(dbConn, dbMetadata, packageFilter);

        tReturnCode.setObject(dbConn);
      } catch (SQLException e) {
        log.error(e, e);
        // Need to add a dialog for report the reson of error
      } finally {
        if (sqlConn != null) {
          ConnectionUtils.closeConnection(sqlConn);
        }
      }
    } else {
      tReturnCode.setMessage(rc.getMessage());
      tReturnCode.setOk(false);
    }
    return tReturnCode;
  }
  /**
   * DOC scorreia Comment method "main".
   *
   * @param args
   */
  public static void main(String[] args) {
    TypedProperties connectionParams =
        PropertiesLoader.getProperties(IndicatorEvaluator.class, "db.properties");
    String driverClassName = connectionParams.getProperty("driver");
    String dbUrl = connectionParams.getProperty("url");
    try {
      // create connection
      java.sql.Connection connection =
          ConnectionUtils.createConnection(dbUrl, driverClassName, connectionParams);

      Connection dataProvider = new AnalysisCreationTest().getDataManager();

      // --- test connection evaluator
      String catalog = "test";

      ConnectionEvaluator evaluator = new ConnectionEvaluator();
      evaluator.setConnection(connection);

      // --- create indicators
      ConnectionIndicator connectionIndicator = SchemaFactory.eINSTANCE.createConnectionIndicator();
      evaluator.storeIndicator(dataProvider, connectionIndicator);

      // SchemaIndicator schemaIndic = SchemaFactory.eINSTANCE.createSchemaIndicator();
      // evaluator.storeIndicator(new CatalogSchema(catalog, null), schemaIndic);
      String sql = createSql(catalog);
      ReturnCode rc = evaluator.evaluateIndicators(sql, false);
      if (!rc.isOk()) {
        System.err.println("Failed to evaluate indicator: " + rc.getMessage());
      }

      // store in file
      File file = new File("out/myi." + IndicatorsPackage.eNAME);
      EMFUtil util = new EMFUtil();
      util.setUsePlatformRelativePath(false);
      if (!util.addPoolToResourceSet(file.toURI().toString(), connectionIndicator)) {
        System.err.println(util.getLastErrorMessage());
      }

      File dp = new File("out/dp.prv");
      // util.addPoolToResourceSet(new File("out/dp.prv"), dataProvider);
      util.addPoolToResourceSet(dp, dataProvider);
      List<Catalog> tdCatalogs = ConnectionHelper.getCatalogs(dataProvider);
      for (Catalog tdCatalog : tdCatalogs) {
        util.addPoolToResourceSet(
            new File("out/" + tdCatalog.getName() + "." + FactoriesUtil.CAT), tdCatalog);
      }
      util.save();
      ConnectionUtils.closeConnection(connection);
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      log.error(e, e);
    } catch (InstantiationException e) {
      // TODO Auto-generated catch block
      log.error(e, e);
    } catch (IllegalAccessException e) {
      // TODO Auto-generated catch block
      log.error(e, e);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      log.error(e, e);
    }
  }