/* 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);
 }
  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);
  }