/**
   * Creates a table with columns got from metadata. PAY ATTENTION TO THE FACT THAT THE INPUT
   * CONNECTION WON'T BE CLOSED!!!!!
   *
   * @param conn The JDBC connection to be used
   * @param meta The metadata of the dataset to be persisted on the database
   * @param tableName The name of the table to be created
   * @param list The list of the fields of the dataset to be included on table
   * @return A DataSetTableDescriptor that contains the association between table's columns and
   *     dataset's fields.
   */
  public static DataSetTableDescriptor createTemporaryTable(
      Connection conn, IMetaData meta, String tableName, List<String> selectedFields) {
    logger.debug("IN");

    DataSetTableDescriptor dstd = null;
    Statement st = null;
    String sqlQuery = null;

    if (selectedFields == null) {
      selectedFields = new ArrayList<String>();
    }

    try {
      CreateTableCommand createTableCommand =
          new CreateTableCommand(tableName, conn.getMetaData().getDriverName());

      // run through all columns in order to build the SQL columndefinition
      int count = meta.getFieldCount();
      logger.debug("The table tableName has " + count + " columns ");
      for (int i = 0; i < count; i++) {
        IFieldMetaData fieldMeta = meta.getFieldMeta(i);
        String fieldName = fieldMeta.getName();
        if (selectedFields.isEmpty() || selectedFields.contains(fieldName)) {
          createTableCommand.addColumn(fieldMeta);
        }
      }

      // after built columns create SQL Query
      sqlQuery = createTableCommand.createSQLQuery();

      // execute
      logger.debug("Executing the query " + sqlQuery + "...");
      st = conn.createStatement();
      st.execute(sqlQuery);
      logger.debug("Query executed");
      dstd = createTableCommand.getDsTableDescriptor();
      LogMF.debug(logger, "The query descriptor is {0}", dstd);

    } catch (SQLException e) {
      logger.error("Error in excuting statement " + sqlQuery, e);
      throw new SpagoBIRuntimeException("Error creating temporary table", e);
    } finally {
      try {
        if (st != null) {
          st.close();
        }
      } catch (SQLException e) {
        logger.error("could not free resources ", e);
      }
    }
    logger.debug("OUT");
    return dstd;
  }
  private void changeAlias(IDataStore dataStore) {
    logger.debug("IN");
    IMetaData metaData = dataStore.getMetaData();

    for (int i = 0; i < metaData.getFieldCount(); i++) {
      IFieldMetaData meta = metaData.getFieldMeta(i);
      Column col = registryConfig.getColumnConfiguration(meta.getAlias());
      if (col.getTitle() != null) {
        meta.setAlias(col.getTitle());
        // metaData.changeFieldAlias(i, col.getTitle());
        logger.debug("Changed alias of column " + meta.getName() + " to " + col.getTitle());
      }
    }

    logger.debug("OUT");
  }