Program: 126740
Project: 126740
Buggy source:
public List<Catalog> fillCatalogs(Connection dbConn,DatabaseMetaData dbJDBCMetadata,IMetadataConnection metaConnection,List<String> catalogFilter){
  List<Catalog> catalogList=new ArrayList<Catalog>();
  if (dbJDBCMetadata == null) {
    return null;
  }
  if (ConnectionUtils.isPostgresql(dbJDBCMetadata)) {
    return fillPostgresqlCatalogs(metaConnection,dbConn,dbJDBCMetadata,catalogList);
  }
  if (dbConn != null && dbConn.isContextMode()) {
    if (EDatabaseTypeName.MYSQL.getProduct().equals(((DatabaseConnection)dbConn).getProductId()) || EDatabaseTypeName.MSSQL.getProduct().equals(((DatabaseConnection)dbConn).getProductId()) || EDatabaseTypeName.MSSQL05_08.getProduct().equals(((DatabaseConnection)dbConn).getProductId())) {
      IMetadataConnection iMetadataCon=metaConnection;
      if (iMetadataCon == null) {
        iMetadataCon=ConvertionHelper.convert(dbConn);
      }
      if (iMetadataCon != null) {
        String catalogTemp=iMetadataCon.getDatabase();
        if ("String_Node_Str".equals(catalogTemp)) {
          catalogFilter.clear();
        }
      }
    }
  }
  try {
    if (dbJDBCMetadata.getDatabaseProductName() != null && dbJDBCMetadata.getDatabaseProductName().indexOf(EDatabaseTypeName.ORACLEFORSID.getProduct()) > -1) {
      return catalogList;
    }
    if (ConnectionUtils.isOdbcTeradata(dbJDBCMetadata)) {
      return catalogList;
    }
    ResultSet catalogNames=null;
    if (dbJDBCMetadata instanceof SybaseDatabaseMetaData) {
      catalogNames=((SybaseDatabaseMetaData)dbJDBCMetadata).getCatalogs(((DatabaseConnection)dbConn).getUsername());
    }
 else {
      catalogNames=dbJDBCMetadata.getCatalogs();
    }
    List<String> schemaFilterList=new ArrayList<String>();
    if (catalogNames != null) {
      boolean isHive=MetadataConnectionUtils.isHive(dbJDBCMetadata);
      boolean isSybase=MetadataConnectionUtils.isSybase(dbJDBCMetadata);
      while (catalogNames.next()) {
        String catalogName=null;
        try {
          String temp=null;
          if (isHive) {
            temp=MetaDataConstants.TABLE_CAT.name();
          }
 else {
            temp=MetadataConnectionUtils.isOdbcPostgresql(dbJDBCMetadata) ? DatabaseConstant.ODBC_POSTGRESQL_CATALOG_NAME : MetaDataConstants.TABLE_CAT.name();
          }
          catalogName=catalogNames.getString(temp);
          if (!isHive && !MetadataConnectionUtils.isODBCCatalog(catalogName,dbJDBCMetadata)) {
            continue;
          }
        }
 catch (        Exception e) {
          log.warn(e,e);
          if (dbJDBCMetadata.getDatabaseProductName() != null && dbJDBCMetadata.getDatabaseProductName().toLowerCase().indexOf(DatabaseConstant.POSTGRESQL_PRODUCT_NAME) > -1) {
            catalogName="String_Node_Str";
          }
        }
        if (catalogName != null) {
          if (!isNullSID(dbConn) && dbConn != null && !((DatabaseConnection)dbConn).getDatabaseType().equals(EDatabaseTypeName.AS400.getDisplayName()) && !((DatabaseConnection)dbConn).getDatabaseType().equals(EDatabaseTypeName.HSQLDB_IN_PROGRESS.getDisplayName()) && !((DatabaseConnection)dbConn).getDatabaseType().equals(EDatabaseTypeName.HSQLDB_SERVER.getDisplayName()) && !((DatabaseConnection)dbConn).getDatabaseType().equals(EDatabaseTypeName.HSQLDB_WEBSERVER.getDisplayName())) {
            String databaseOnConnWizard=((DatabaseConnection)dbConn).getSID();
            postFillCatalog(catalogList,catalogFilter,schemaFilterList,TalendCWMService.getReadableName(dbConn,databaseOnConnWizard),dbConn);
            break;
          }
 else           if (isCreateElement(catalogFilter,catalogName)) {
            postFillCatalog(catalogList,catalogFilter,schemaFilterList,catalogName,dbConn);
          }
        }
      }
      catalogNames.close();
      if (!isHive) {
        List<Catalog> removeCatalogList=new ArrayList<Catalog>();
        for (        Catalog catalog : catalogList) {
          List<Schema> schemaList=new ArrayList<Schema>();
          try {
            schemaList=fillSchemaToCatalog(dbConn,dbJDBCMetadata,catalog,schemaFilterList);
            if (!schemaList.isEmpty() && schemaList.size() > 0) {
              CatalogHelper.addSchemas(schemaList,catalog);
            }
          }
 catch (          Throwable e) {
            removeCatalogList.add(catalog);
          }
        }
        if (isSybase && catalogFilter != null && !catalogFilter.isEmpty() && catalogFilter.size() > 0 && catalogList.isEmpty() && catalogList.size() == 0) {
          catalogFilter.clear();
          return fillCatalogs(dbConn,dbJDBCMetadata,catalogFilter);
        }
        catalogList.removeAll(removeCatalogList);
      }
      Set<MetadataTable> tableSet=ConnectionHelper.getTables(dbConn);
      List<Catalog> replaceCatalogs=new ArrayList<Catalog>();
      List<String> catalogName=new ArrayList<String>();
      for (      MetadataTable table : tableSet) {
        EObject eContainer=table.eContainer();
        if (eContainer != null) {
          if (eContainer instanceof Catalog) {
            Catalog c=(Catalog)eContainer;
            String name=c.getName();
            if (!catalogName.contains(name)) {
              replaceCatalogs.add(c);
              catalogName.add(name);
            }
          }
 else           if (eContainer instanceof Schema) {
            EObject parent=eContainer.eContainer();
            if (parent != null && parent instanceof Catalog) {
              Catalog c=(Catalog)parent;
              String name=c.getName();
              if (!catalogName.contains(name)) {
                List<Schema> filterSchemas=new ArrayList<Schema>();
                List<String> schemaName=new ArrayList<String>();
                List<Schema> schemas=CatalogHelper.getSchemas(c);
                for (                Schema schema : schemas) {
                  if (schemaFilterList != null) {
                    if (schemaFilterList.contains(schema.getName())) {
                      filterSchemas.add(schema);
                      schemaName.add(schema.getName());
                    }
 else                     if (schema.getOwnedElement() != null && !schema.getOwnedElement().isEmpty()) {
                      filterSchemas.add(schema);
                      schemaName.add(schema.getName());
                    }
                  }
                }
                for (                Catalog catalog : catalogList) {
                  if (catalog.getName().equals(name)) {
                    boolean added=false;
                    for (                    Schema schema : CatalogHelper.getSchemas(catalog)) {
                      if (!schemaName.contains(schema.getName())) {
                        filterSchemas.add(schema);
                        added=true;
                      }
                    }
                    if (added) {
                      break;
                    }
                  }
                }
                c.getOwnedElement().clear();
                CatalogHelper.addSchemas(filterSchemas,c);
                replaceCatalogs.add(c);
                catalogName.add(name);
              }
            }
          }
        }
      }
      if (this.isLinked() && !catalogList.isEmpty()) {
        ConnectionHelper.addCatalogs(catalogList,dbConn);
      }
      for (      Catalog catalog : replaceCatalogs) {
        List<Catalog> list=new ArrayList<Catalog>();
        String name=catalog.getName();
        Catalog c=(Catalog)ConnectionHelper.getPackage(name,dbConn,Catalog.class);
        if (c != null) {
          list.add(c);
          ConnectionHelper.removeCatalogs(list,dbConn);
          ConnectionHelper.addCatalog(catalog,dbConn);
        }
 else {
          ConnectionHelper.addCatalog(catalog,dbConn);
        }
      }
    }
  }
 catch (  SQLException e) {
    log.warn("String_Node_Str",e);
  }
  return catalogList;
}

Fixed version (ground truth):
public List<Catalog> fillCatalogs(DatabaseConnection dbConn,DatabaseMetaData dbJDBCMetadata,IMetadataConnection metaConnection,List<String> catalogFilter){
  List<Catalog> catalogList=new ArrayList<Catalog>();
  if (dbJDBCMetadata == null) {
    return null;
  }
  if (ConnectionUtils.isPostgresql(dbJDBCMetadata)) {
    return fillPostgresqlCatalogs(metaConnection,dbConn,dbJDBCMetadata,catalogList);
  }
  if (dbConn != null && dbConn.isContextMode()) {
    if (EDatabaseTypeName.MYSQL.getProduct().equals(dbConn.getProductId()) || EDatabaseTypeName.MSSQL.getProduct().equals(dbConn.getProductId()) || EDatabaseTypeName.MSSQL05_08.getProduct().equals(dbConn.getProductId())) {
      IMetadataConnection iMetadataCon=metaConnection;
      if (iMetadataCon == null) {
        iMetadataCon=ConvertionHelper.convert(dbConn);
      }
      if (iMetadataCon != null) {
        String catalogTemp=iMetadataCon.getDatabase();
        if ("String_Node_Str".equals(catalogTemp)) {
          catalogFilter.clear();
        }
      }
    }
  }
  try {
    if (dbJDBCMetadata.getDatabaseProductName() != null && dbJDBCMetadata.getDatabaseProductName().indexOf(EDatabaseTypeName.ORACLEFORSID.getProduct()) > -1) {
      return catalogList;
    }
    if (ConnectionUtils.isOdbcTeradata(dbJDBCMetadata)) {
      return catalogList;
    }
    ResultSet catalogNames=null;
    if (dbJDBCMetadata instanceof SybaseDatabaseMetaData) {
      catalogNames=((SybaseDatabaseMetaData)dbJDBCMetadata).getCatalogs(dbConn.getUsername());
    }
 else {
      catalogNames=dbJDBCMetadata.getCatalogs();
    }
    List<String> schemaFilterList=new ArrayList<String>();
    if (catalogNames != null) {
      boolean isHive=MetadataConnectionUtils.isHive(dbJDBCMetadata);
      boolean isSybase=MetadataConnectionUtils.isSybase(dbJDBCMetadata);
      while (catalogNames.next()) {
        String catalogName=null;
        try {
          String temp=null;
          if (isHive) {
            temp=MetaDataConstants.TABLE_CAT.name();
          }
 else {
            temp=MetadataConnectionUtils.isOdbcPostgresql(dbJDBCMetadata) ? DatabaseConstant.ODBC_POSTGRESQL_CATALOG_NAME : MetaDataConstants.TABLE_CAT.name();
          }
          catalogName=catalogNames.getString(temp);
          if (!isHive && !MetadataConnectionUtils.isODBCCatalog(catalogName,dbJDBCMetadata)) {
            continue;
          }
        }
 catch (        Exception e) {
          log.warn(e,e);
          if (dbJDBCMetadata.getDatabaseProductName() != null && dbJDBCMetadata.getDatabaseProductName().toLowerCase().indexOf(DatabaseConstant.POSTGRESQL_PRODUCT_NAME) > -1) {
            catalogName="String_Node_Str";
          }
        }
        if (catalogName != null) {
          if (!isNullSID(dbConn) && dbConn != null && !dbConn.getDatabaseType().equals(EDatabaseTypeName.AS400.getDisplayName()) && !dbConn.getDatabaseType().equals(EDatabaseTypeName.HSQLDB_IN_PROGRESS.getDisplayName()) && !dbConn.getDatabaseType().equals(EDatabaseTypeName.HSQLDB_SERVER.getDisplayName()) && !dbConn.getDatabaseType().equals(EDatabaseTypeName.HSQLDB_WEBSERVER.getDisplayName())) {
            String databaseOnConnWizard=dbConn.getSID();
            postFillCatalog(catalogList,catalogFilter,schemaFilterList,TalendCWMService.getReadableName(dbConn,databaseOnConnWizard),dbConn);
            break;
          }
 else           if (isCreateElement(catalogFilter,catalogName)) {
            postFillCatalog(catalogList,catalogFilter,schemaFilterList,catalogName,dbConn);
          }
        }
      }
      catalogNames.close();
      if (!isHive) {
        List<Catalog> removeCatalogList=new ArrayList<Catalog>();
        for (        Catalog catalog : catalogList) {
          List<Schema> schemaList=new ArrayList<Schema>();
          try {
            schemaList=fillSchemaToCatalog(dbConn,dbJDBCMetadata,catalog,schemaFilterList);
            if (!schemaList.isEmpty() && schemaList.size() > 0) {
              CatalogHelper.addSchemas(schemaList,catalog);
            }
          }
 catch (          Throwable e) {
            removeCatalogList.add(catalog);
          }
        }
        if (isSybase && catalogFilter != null && !catalogFilter.isEmpty() && catalogFilter.size() > 0 && catalogList.isEmpty() && catalogList.size() == 0) {
          catalogFilter.clear();
          return fillCatalogs(dbConn,dbJDBCMetadata,catalogFilter);
        }
        catalogList.removeAll(removeCatalogList);
      }
      Set<MetadataTable> tableSet=ConnectionHelper.getTables(dbConn);
      List<Catalog> replaceCatalogs=new ArrayList<Catalog>();
      List<String> catalogName=new ArrayList<String>();
      for (      MetadataTable table : tableSet) {
        EObject eContainer=table.eContainer();
        if (eContainer != null) {
          if (eContainer instanceof Catalog) {
            Catalog c=(Catalog)eContainer;
            String name=c.getName();
            if (!catalogName.contains(name)) {
              replaceCatalogs.add(c);
              catalogName.add(name);
            }
          }
 else           if (eContainer instanceof Schema) {
            EObject parent=eContainer.eContainer();
            if (parent != null && parent instanceof Catalog) {
              Catalog c=(Catalog)parent;
              String name=c.getName();
              if (!catalogName.contains(name)) {
                List<Schema> filterSchemas=new ArrayList<Schema>();
                List<String> schemaName=new ArrayList<String>();
                List<Schema> schemas=CatalogHelper.getSchemas(c);
                for (                Schema schema : schemas) {
                  if (schemaFilterList != null) {
                    if (schemaFilterList.contains(schema.getName())) {
                      filterSchemas.add(schema);
                      schemaName.add(schema.getName());
                    }
 else                     if (schema.getOwnedElement() != null && !schema.getOwnedElement().isEmpty()) {
                      filterSchemas.add(schema);
                      schemaName.add(schema.getName());
                    }
                  }
                }
                for (                Catalog catalog : catalogList) {
                  if (catalog.getName().equals(name)) {
                    boolean added=false;
                    for (                    Schema schema : CatalogHelper.getSchemas(catalog)) {
                      if (!schemaName.contains(schema.getName())) {
                        filterSchemas.add(schema);
                        added=true;
                      }
                    }
                    if (added) {
                      break;
                    }
                  }
                }
                c.getOwnedElement().clear();
                CatalogHelper.addSchemas(filterSchemas,c);
                replaceCatalogs.add(c);
                catalogName.add(name);
              }
            }
          }
        }
      }
      if (this.isLinked() && !catalogList.isEmpty()) {
        ConnectionHelper.addCatalogs(catalogList,dbConn);
      }
      for (      Catalog catalog : replaceCatalogs) {
        List<Catalog> list=new ArrayList<Catalog>();
        String name=catalog.getName();
        Catalog c=(Catalog)ConnectionHelper.getPackage(name,dbConn,Catalog.class);
        if (c != null) {
          list.add(c);
          ConnectionHelper.removeCatalogs(list,dbConn);
          ConnectionHelper.addCatalog(catalog,dbConn);
        }
 else {
          ConnectionHelper.addCatalog(catalog,dbConn);
        }
      }
    }
  }
 catch (  SQLException e) {
    log.warn("String_Node_Str",e);
  }
  return catalogList;
}