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);
  }
 protected void calcuAddedColumns(
     FOXTreeNode node, MetadataColumn targetColumn, EList columnList) {
   if (targetColumn == null) {
     addColumn(node.getLabel(), node, columnList);
   } else {
     node.setColumn(ConvertionHelper.convertToIMetaDataColumn(targetColumn));
   }
 }
 private void addColumn(String label, FOXTreeNode node, EList columnList) {
   if (StringUtils.isEmpty(label)) {
     return;
   }
   MetadataEmfTableEditor editor = new MetadataEmfTableEditor();
   String columnName = label;
   if (columnName.contains(":")) { // $NON-NLS-1$
     columnName = columnName.split(":")[1]; // $NON-NLS-1$
   }
   columnName = columnName.replaceAll("[^a-zA-Z0-9]", "_"); // $NON-NLS-1$
   String dataType = node.getDataType();
   MetadataColumn metadataColumn = ConnectionFactory.eINSTANCE.createMetadataColumn();
   metadataColumn.setLabel(editor.getNextGeneratedColumnName(columnName, columnList));
   metadataColumn.setOriginalField(label);
   metadataColumn.setTalendType(dataType);
   columnList.add(metadataColumn);
   node.setColumn(ConvertionHelper.convertToIMetaDataColumn(metadataColumn));
 }
 /* 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);
 }
  /**
   * 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);
  }