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();
    }
  }
 /**
  * this method is used to create a DBCatalogRepNode. And Sybase is a specal case because we don't
  * create schema level when the version of stdio is lower than 5.0.0.So we will create
  * DBSybaseCatalogRepNode to deal with this special case
  *
  * @param viewObject
  * @param parent parent of repositoryNode
  * @param type
  * @return
  */
 public static RepositoryNode createDBCatalogRepNode(
     IRepositoryViewObject viewObject,
     RepositoryNode parent,
     ENodeType type,
     org.talend.core.model.general.Project inWhichProject) {
   Item databaseItem = viewObject.getProperty().getItem();
   DatabaseConnection dbConnection =
       (DatabaseConnection) ((DatabaseConnectionItem) databaseItem).getConnection();
   SupportDBUrlType dbTypeByKey = SupportDBUrlType.getDBTypeByKey(dbConnection.getDatabaseType());
   switch (dbTypeByKey) {
     case SYBASEDEFAULTURL:
       return new DBSybaseCatalogRepNode(viewObject, parent, type, inWhichProject);
     default:
       return new DBCatalogRepNode(viewObject, parent, type, inWhichProject);
   }
 }
  public static void addTableForSpecifiedDataPackage(
      DatabaseConnection dbconn, MetadataTable dbtable)
      throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
    // if the database connection is contextmodel, need to get the original value of every parameter
    IMetadataConnection imetadataConnection = ConvertionHelper.convert(dbconn);
    DatabaseConnection conn = (DatabaseConnection) imetadataConnection.getCurrentConnection();
    Collection<orgomg.cwm.objectmodel.core.Package> newDataPackage =
        EcoreUtil.copyAll(dbconn.getDataPackage());
    ConnectionHelper.addPackages(newDataPackage, conn);

    // String catalog = "";
    // fixed bug TDI-19395
    String catalog = imetadataConnection.getDatabase();
    String schema = "";
    EObject container = dbtable.eContainer();
    if (container != null) {
      if (container instanceof Schema) {
        schema = ((Schema) container).getName();
        EObject c = container.eContainer();
        if (c != null && c instanceof Catalog) {
          catalog = ((Catalog) c).getName();
        }
      } else if (container instanceof Catalog) {
        catalog = ((Catalog) container).getName();
      }
    }
    boolean isAccess =
        EDatabaseTypeName.ACCESS.getDisplayName().equals(imetadataConnection.getDbType());
    if (!isAccess) {
      schema = ExtractMetaDataUtils.getInstance().getMeataConnectionSchema(imetadataConnection);
    }
    addTableForTemCatalogOrSchema(catalog, schema, dbconn, dbtable, imetadataConnection);
  }
  public static Set<org.talend.core.model.metadata.builder.connection.MetadataTable>
      getTablesFromCurrentCatalogOrSchema(String dbsid, String schema, DatabaseConnection dbconn) {

    Set<org.talend.core.model.metadata.builder.connection.MetadataTable> allTables =
        new HashSet<org.talend.core.model.metadata.builder.connection.MetadataTable>();
    /* context model show all tables */
    if (dbconn.isContextMode()) {
      allTables = ConnectionHelper.getTables(dbconn);
    } else {
      boolean hasSchemaInCatalog = false;
      Catalog c = (Catalog) ConnectionHelper.getPackage(dbsid, dbconn, Catalog.class);
      Schema s = (Schema) ConnectionHelper.getPackage(schema, dbconn, Schema.class);
      List<Schema> subschemas = new ArrayList<Schema>();
      if (c != null) {
        subschemas = CatalogHelper.getSchemas(c);
        hasSchemaInCatalog = subschemas.size() > 0;
      }
      if (c != null && s == null && !hasSchemaInCatalog) { // only catalog
        PackageHelper.getAllTables(c, allTables);
        // PackageHelper.addMetadataTable(dbtable, c);

      } else if (s != null && !hasSchemaInCatalog && c == null) { // only schema
        PackageHelper.getAllTables(s, allTables);
        // PackageHelper.addMetadataTable(dbtable, s);
      } else if (c != null && hasSchemaInCatalog) { // both schema and catalog
        subschemas = CatalogHelper.getSchemas(c);
        hasSchemaInCatalog = subschemas.size() > 0;
        if (subschemas.size() > 0) {
          for (Schema current : subschemas) {
            if (current.getName() == null) {
              /* if the current schema no name should set an empty string for name, bug 17244 */
              current.setName("");
            }
            if (current.getName().equals(schema)) {
              s = current;
              break;
            }
          }
          /**
           * if dont specifc a schema because of getUiSchema() is null,show all cataogs table by
           * default,or it will cause bug 0016578
           */
          if (s == null || "".equals(s)) {
            // allTables = ConnectionHelper.getTables(dbconn);
            PackageHelper.getAllTables(c, allTables);
          } else {
            PackageHelper.getAllTables(s, allTables);
          }
          // PackageHelper.addMetadataTable(dbtable, s);
        }
      } else {
        // return nothing
      }
    }
    return allTables;
  }
  public static RepositoryComponentSetting getSetting(Item item, ERepositoryObjectType type) {
    if (item == null) {
      return null;
    }
    boolean subItem = (type == ERepositoryObjectType.METADATA_CON_TABLE);

    for (RepositoryComponentSetting setting : getSettings()) {
      Class<Item>[] classes = setting.getClasses();
      if (classes != null) {
        for (Class<Item> clazz : classes) {
          if (clazz.isAssignableFrom(item.getClass())) {
            if (clazz.isAssignableFrom(DatabaseConnectionItem.class)) { // for db
              EDatabaseTypeName[] dbTypes = setting.getDbTypes();
              if (dbTypes != null) {
                for (EDatabaseTypeName dbType : dbTypes) {
                  DatabaseConnectionItem dbItem = (DatabaseConnectionItem) item;
                  DatabaseConnection dbConnection = (DatabaseConnection) dbItem.getConnection();
                  // use the display name as the database type
                  if (dbType.getDisplayName().equals(dbConnection.getDatabaseType())) {
                    if (!subItem) {
                      return setting;
                    }
                    if (subItem && setting.withSchema()) {
                      return setting;
                    }
                  }
                }
              }
            } else {
              return setting;
            }
          }
        }
      }
    }
    return null;
  }
 /* return all tables from current datapackage with List,so that the result is order-sorted */
 public static List<org.talend.core.model.metadata.builder.connection.MetadataTable>
     getTablesFromSpecifiedDataPackageWithOders(DatabaseConnection dbconn) {
   // if the database connection is contextmodel, need to get the original value of every parameter
   IMetadataConnection iMetadataConnection = ConvertionHelper.convert(dbconn);
   String schema = dbconn.getUiSchema();
   String catalog = dbconn.getSID();
   String databaseType = dbconn.getDatabaseType();
   EDatabaseTypeName currentType = EDatabaseTypeName.getTypeFromDbType(databaseType);
   EDatabaseSchemaOrCatalogMapping curCatalog = currentType.getCatalogMappingField();
   EDatabaseSchemaOrCatalogMapping curSchema = currentType.getSchemaMappingField();
   if (curCatalog != null && curSchema != null) {
     switch (curCatalog) {
       case Login:
         catalog = dbconn.getUsername();
         break;
       case None:
         catalog = "";
         break;
     }
     switch (curSchema) {
       case Login:
         schema = dbconn.getUsername();
         break;
       case Schema:
         schema = dbconn.getUiSchema();
         break;
       case None:
         schema = "";
         break;
       case Default_Name:
         schema = dbconn.getName(); // label for default name for
         // access or such kind of
         // non-catalogs databases
         break;
     }
   }
   boolean isAccess =
       EDatabaseTypeName.ACCESS.getDisplayName().equals(iMetadataConnection.getDbType());
   if (!isAccess) {
     schema = ExtractMetaDataUtils.getInstance().getDBConnectionSchema(dbconn);
   }
   return getTablesFromCurrentCatalogOrSchemaWithOrders(catalog, schema, dbconn);
 }
 /* method is used to remove table from database */
 public static void removeTables(
     String tableLabel,
     orgomg.cwm.objectmodel.core.Package subpack,
     DatabaseConnection connection) {
   if (subpack == null) {
     for (orgomg.cwm.objectmodel.core.Package pk : connection.getDataPackage()) {
       Iterator<ModelElement> iterator = pk.getOwnedElement().iterator();
       while (iterator.hasNext()) {
         Object o = iterator.next();
         if (o instanceof MetadataTable) {
           MetadataTable table = (MetadataTable) o;
           if (table.getLabel() != null && table.getLabel().equals(tableLabel)) {
             iterator.remove();
             break;
           }
         }
         if (o instanceof orgomg.cwm.objectmodel.core.Package) {
           subpack = (orgomg.cwm.objectmodel.core.Package) o;
           removeTables(tableLabel, subpack, connection);
         }
       }
     }
   } else {
     Iterator<ModelElement> iterator = subpack.getOwnedElement().iterator();
     while (iterator.hasNext()) {
       Object o = iterator.next();
       if (o instanceof MetadataTable) {
         MetadataTable table = (MetadataTable) o;
         if (table.getLabel() != null && table.getLabel().equals(tableLabel)) {
           iterator.remove();
           break;
         }
       }
     }
   }
 }
 private boolean setJDBCMysqlConnection(DatabaseConnection dbProvider) {
   // General JDBC case
   dbProvider.setComment(""); // $NON-NLS-1$
   dbProvider.setSID(""); // $NON-NLS-1$
   dbProvider.setDatasourceName(""); // $NON-NLS-1$
   dbProvider.setDatabaseType("General JDBC"); // $NON-NLS-1$
   dbProvider.setDriverJarPath(
       "E:\\tools\\jdbc\\mysql-connector-java-5.1.12-bin.jar"); //$NON-NLS-1$
   dbProvider.setDbVersionString(""); // $NON-NLS-1$
   dbProvider.setDriverClass("org.gjt.mm.mysql.Driver"); // $NON-NLS-1$
   dbProvider.setFileFieldName(""); // $NON-NLS-1$
   dbProvider.setId("_9bw28cccEeGQNaw_qcyMFw"); // $NON-NLS-1$
   dbProvider.setLabel("jdbcmysql1"); // $NON-NLS-1$
   dbProvider.setNullChar(""); // $NON-NLS-1$
   dbProvider.setRawPassword("shenze"); // $NON-NLS-1$
   dbProvider.setPort("3306"); // $NON-NLS-1$
   dbProvider.setServerName(""); // $NON-NLS-1$
   dbProvider.setSqlSynthax("SQL Syntax"); // $NON-NLS-1$
   dbProvider.setUiSchema(""); // $NON-NLS-1$
   dbProvider.setStringQuote(""); // $NON-NLS-1$
   dbProvider.setURL("jdbc:mysql://192.168.30.151:3306/?noDatetimeStringSync=true"); // $NON-NLS-1$
   dbProvider.setAdditionalParams(""); // $NON-NLS-1$
   dbProvider.setUsername("shenze"); // $NON-NLS-1$
   dbProvider.setDbmsId("mysql_id"); // $NON-NLS-1$
   dbProvider.setProductId("JDBC"); // $NON-NLS-1$
   dbProvider.setDBRootPath(""); // $NON-NLS-1$
   dbProvider.setSQLMode(false);
   dbProvider.setContextMode(false);
   dbProvider.setContextId(""); // $NON-NLS-1$
   dbProvider.setContextName(""); // $NON-NLS-1$
   ConnectionHelper.setPurpose("my test purpose", dbProvider); // $NON-NLS-1$
   ConnectionHelper.setDescription("my test description", dbProvider); // $NON-NLS-1$
   ConnectionHelper.setAuthor("*****@*****.**", dbProvider); // $NON-NLS-1$
   ConnectionHelper.setDevStatus("DEV", dbProvider); // $NON-NLS-1$
   ConnectionHelper.setVersion("1.1", dbProvider); // $NON-NLS-1$
   ConnectionHelper.setUniverse("", dbProvider); // $NON-NLS-1$
   return true;
 }
  private static void fillValuesForSchemaOrCatalog(
      EDatabaseSchemaOrCatalogMapping catalog,
      EDatabaseSchemaOrCatalogMapping schema,
      IMetadataConnection metadataConnection,
      DatabaseConnection dbconn) {
    Schema s = null;
    Catalog c = null;
    List<Schema> schemas = new ArrayList<Schema>();
    String user = metadataConnection.getUsername();
    String defaultname = dbconn.getName();
    String dbsid = metadataConnection.getDatabase();
    String dbuischema = metadataConnection.getSchema();
    if (schema != null && catalog != null) {
      if (schema.equals(EDatabaseSchemaOrCatalogMapping.None)
          && !catalog.equals(EDatabaseSchemaOrCatalogMapping.None)) { // only
        // catalog
        if (catalog.equals(EDatabaseSchemaOrCatalogMapping.Sid)) {
          c = CatalogHelper.createCatalog(dbsid);
          c.getDataManager().add(dbconn);
          ConnectionHelper.addCatalog(c, dbconn);
        }

      } else if (!schema.equals(EDatabaseSchemaOrCatalogMapping.None) // only schema
          && catalog.equals(EDatabaseSchemaOrCatalogMapping.None)) {
        if (schema.equals(EDatabaseSchemaOrCatalogMapping.Schema)) {
          s = SchemaHelper.createSchema(dbuischema);
          s.getDataManager().add(dbconn);
          ConnectionHelper.addSchema(s, dbconn);
        }
        if (schema.equals(EDatabaseSchemaOrCatalogMapping.Login)) {
          s = SchemaHelper.createSchema(user);
          s.getDataManager().add(dbconn);
          ConnectionHelper.addSchema(s, dbconn);
        }
        if (schema.equals(
            EDatabaseSchemaOrCatalogMapping.Default_Name)) { // for databases like access
          s = SchemaHelper.createSchema(defaultname);
          s.getDataManager().add(dbconn);
          ConnectionHelper.addSchema(s, dbconn);
        }
      } else { // both schema and catalog
        String cvalue = dbsid;
        String svalue = null;
        cvalue = dbsid;
        switch (schema) {
          case Sid:
            svalue = dbsid;
            break;
          case Schema:
            svalue = dbuischema;
            break;
          case Login:
            svalue = user;
            break;
        }
        c = CatalogHelper.createCatalog(cvalue);
        s = SchemaHelper.createSchema(svalue);
        schemas.add(s);
        CatalogHelper.addSchemas(schemas, c);
        c.getDataManager().add(dbconn);
        ConnectionHelper.addCatalog(c, dbconn);
      }
    }
  }
  private static void addTableForTemCatalogOrSchema(
      String dbsid,
      String schema,
      DatabaseConnection connection,
      MetadataTable dbtable,
      IMetadataConnection iMetadataConnection,
      int stackCount)
      throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
    if (3 < stackCount) {
      // if loop count is more than 3 times, seems it will never end this loop, and will never get
      // the dbsid and
      // schema
      return;
    }
    boolean hasSchemaInCatalog = false;
    boolean isAccess =
        EDatabaseTypeName.ACCESS.getDisplayName().equals(iMetadataConnection.getDbType());
    Catalog c = (Catalog) ConnectionHelper.getPackage(dbsid, connection, Catalog.class);
    Schema s = (Schema) ConnectionHelper.getPackage(schema, connection, Schema.class);
    List<Schema> subschemas = new ArrayList<Schema>();
    if (c != null) {
      subschemas = CatalogHelper.getSchemas(c);
      hasSchemaInCatalog = subschemas.size() > 0;
    }
    if (c != null && s == null && !hasSchemaInCatalog) { // only catalog
      PackageHelper.addMetadataTable(dbtable, c);

    } else if (s != null && !hasSchemaInCatalog && c == null) { // only schema
      PackageHelper.addMetadataTable(dbtable, s);
    } else if (c != null && hasSchemaInCatalog) { // both schema and catalog
      subschemas = CatalogHelper.getSchemas(c);
      hasSchemaInCatalog = subschemas.size() > 0;
      if (subschemas.size() > 0) {
        for (Schema current : subschemas) {
          if (current.getName().equals(schema)) {
            s = current;
            break;
          }
        }

        if (s != null) {
          // for bug 16794
          if (s instanceof SchemaImpl) {
            SchemaImpl schemaElement = (SchemaImpl) s;
            EList<ModelElement> ownedElement = schemaElement.getOwnedElement();
            ownedElement.add(dbtable);
          }
        } else if (subschemas.size() > 0) {
          // added for bug 17467
          // set db connection's schema as null, and retrieve schema again, to add some tables
          for (int i = 0; i < subschemas.size(); i++) {
            SchemaImpl schemaElement = (SchemaImpl) subschemas.get(i);
            EList<ModelElement> ownedElement = schemaElement.getOwnedElement();
            ownedElement.add(dbtable);
          }
        }
        // PackageHelper.addMetadataTable(dbtable, s);
      }
    } else if (s == null
        && c == null
        && !isAccess
        && stackCount == 1) { // TDI-20584:after migration from 4.0 to
      // 4.2,lost all catalogs
      // and schemas for database
      // in case after migration connetion from the version before 4.0,there is no any db structure
      // on
      // temConnection,it will casue pbs,so sychronize with imetadataConnection
      fillCatalogAndSchemas(iMetadataConnection, connection);
      addTableForTemCatalogOrSchema(
          dbsid, schema, connection, dbtable, iMetadataConnection, stackCount + 1);
    } else {
      /*
       * if there is no catalog or schema,create the structure correctly rather than always create a catalog,found
       * this issue when fixing bug 16636
       */
      ProjectNodeHelper.addCatalogOrSchema(iMetadataConnection, connection);
      if (isAccess) {
        addTableForTemCatalogOrSchema(
            dbsid, connection.getName(), connection, dbtable, iMetadataConnection, stackCount + 1);
      } else {
        addTableForTemCatalogOrSchema(
            dbsid, schema, connection, dbtable, iMetadataConnection, stackCount + 1);
      }
    }
  }
  /**
   * wzhang Comment method "addDefaultTableForSpecifiedDataPackage". this function only for add
   * metadataTable.
   *
   * @param dbconn
   * @param dbtable
   * @throws SQLException
   * @throws IllegalAccessException
   * @throws InstantiationException
   * @throws ClassNotFoundException
   */
  public static void addDefaultTableForSpecifiedDataPackage(
      DatabaseConnection dbconn, MetadataTable dbtable)
      throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
    // if the database connection is contextmodel, need to get the original value of every parameter
    ExtractMetaDataUtils extractMeta = ExtractMetaDataUtils.getInstance();
    IMetadataConnection imetadataConnection = ConvertionHelper.convert(dbconn);
    String schema = imetadataConnection.getSchema();
    String catalog = imetadataConnection.getDatabase();
    String databaseType = imetadataConnection.getDbType();
    EDatabaseTypeName currentType = EDatabaseTypeName.getTypeFromDbType(databaseType);

    // IDBMetadataProvider extractor =
    // ExtractMetaDataFromDataBase.getProviderByDbType(databaseType);
    // if (extractor != null && currentType.isUseProvider()) {
    // catalog = extractor.getDefaultCatalogName();
    // }

    EDatabaseSchemaOrCatalogMapping curCatalog = currentType.getCatalogMappingField();
    EDatabaseSchemaOrCatalogMapping curSchema = currentType.getSchemaMappingField();
    if (curCatalog != null && curSchema != null) {
      switch (curCatalog) {
        case Login:
          catalog = imetadataConnection.getUsername();
          break;
        case None:
          catalog = "";
          break;
      }
      switch (curSchema) {
        case Login:
          schema = imetadataConnection.getUsername();
          break;
        case Schema:
          schema = imetadataConnection.getSchema();
          break;
        case None:
          schema = "";
          break;
        case Default_Name:
          schema = dbconn.getName(); // label for default name for
          // access or such kind of
          // non-catalogs databases
          break;
      }
    }
    boolean isAccess =
        EDatabaseTypeName.ACCESS.getDisplayName().equals(imetadataConnection.getDbType());
    if (!isAccess) {
      schema = extractMeta.getMeataConnectionSchema(imetadataConnection);
    }
    // for olap connection
    boolean isOlap = extractMeta.isOLAPConnection(dbconn);
    if (isOlap) {
      List<Catalog> catalogs = ConnectionHelper.getCatalogs(dbconn);
      if (!catalogs.isEmpty()) {
        Catalog c = catalogs.get(0);
        catalog = c.getName();
        if (!CatalogHelper.getSchemas(c).isEmpty()) {
          Schema s = CatalogHelper.getSchemas(c).get(0);
          schema = s.getName();
        }
      }
    }

    addTableForTemCatalogOrSchema(catalog, schema, dbconn, dbtable, imetadataConnection);
  }