public static void removeTablesFromCurrentCatalogOrSchema(
      String dbsid,
      String schema,
      DatabaseConnection dbconn,
      Collection<? extends MetadataTable> tablesToDelete) {
    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
      c.getOwnedElement().removeAll(tablesToDelete);

    } else if (s != null && !hasSchemaInCatalog && c == null) { // only schema
      s.getOwnedElement().removeAll(tablesToDelete);
      // 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 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);

          // added for bug 17467
          // set db connection's schema as null, and retrieve schema again, to remove some tables
          EList<ModelElement> ownedElement = c.getOwnedElement();
          if (ownedElement instanceof EObjectContainmentWithInverseEList) {
            EObjectContainmentWithInverseEList elist =
                (EObjectContainmentWithInverseEList) ownedElement;
            if (!elist.isEmpty() && elist.size() > 0) {
              for (int i = 0; i < elist.size(); i++) {
                Object object = elist.get(i);
                if (object instanceof SchemaImpl) {
                  SchemaImpl schemaImpl = (SchemaImpl) object;
                  EList<ModelElement> ownedElement2 = schemaImpl.getOwnedElement();
                  ownedElement2.removeAll(tablesToDelete);
                }
              }
            }
          }

          ownedElement.removeAll(tablesToDelete);

        } else {
          s.getOwnedElement().removeAll(tablesToDelete);
        }
        // PackageHelper.addMetadataTable(dbtable, s);
      }
    } else {
      // return nothing
    }
  }
  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);
      }
    }
  }