/**
   * Generates chunk definitions for the whole given schema.
   *
   * @param schemaName Name of the schema for which chunk definitions are generated.
   */
  private void generateChunksForSchema(String schemaName) {
    try {
      if (logger.isDebugEnabled()) logger.debug("Getting list of tables from " + schemaName);

      ArrayList<Table> tablesFromSchema = connection.getTables(schemaName, true, true);
      if (logger.isDebugEnabled()) logger.debug("Tables : " + tablesFromSchema);
      if (tablesFromSchema != null && tablesFromSchema.size() > 0) {
        for (Table table : tablesFromSchema) {
          ChunkRequest tableReq = new ChunkRequest(table.getSchema(), table.getName());
          if (!ignoreTablesDefinition.getChunksDefinitions().contains(tableReq)) {
            generateChunksForTable(table, -1, null);
          }
        }
      }
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
  }
  /** TODO: runTask definition. */
  private void runTask() {
    connection = null;
    try {
      connection = DatabaseFactory.createDatabase(url, user, password);
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    try {
      connection.connect();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    if (ignoreTablesFile != null) {
      ignoreTablesDefinition = new ChunkDefinitions(ignoreTablesFile);
      try {
        ignoreTablesDefinition.parseFile();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    // Check whether we have to use a chunk definition file
    if (chunkDefFile != null) {
      logger.info("Using definition from file " + chunkDefFile);
      chunkDefinition = new ChunkDefinitions(chunkDefFile);
      try {
        chunkDefinition.parseFile();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ReplicatorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      LinkedList<ChunkRequest> chunksDefinitions = chunkDefinition.getChunksDefinitions();
      for (ChunkRequest chunkRequest : chunksDefinitions) {
        if (chunkRequest.getTable() != null) {
          try {
            Table table =
                connection.findTable(chunkRequest.getSchema(), chunkRequest.getTable(), true);

            if (table != null)
              generateChunksForTable(table, chunkRequest.getChunkSize(), chunkRequest.getColumns());
            else
              logger.warn(
                  "Failed while processing table "
                      + chunkRequest.getSchema()
                      + "."
                      + chunkRequest.getTable()
                      + " : table not found.");
          } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (ReplicatorException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        } else if (chunkRequest.getSchema() != null) {
          generateChunksForSchema(chunkRequest.getSchema());
        }
      }
    } else {
      try {
        DatabaseMetaData databaseMetaData = connection.getDatabaseMetaData();
        ResultSet schemasRs = databaseMetaData.getSchemas();
        while (schemasRs.next()) {
          String schemaName = schemasRs.getString("TABLE_SCHEM");
          // TODO: System schemas could be needed -> this needs a
          // setting
          if (!connection.isSystemSchema(schemaName)) {
            generateChunksForSchema(schemaName);
          }
        }
        schemasRs.close();
      } catch (SQLException e) {
        logger.error(e);
      } catch (Exception e) {
        logger.error(e);
      }
    }

    // Stop threads
    for (int i = 0; i < extractChannels; i++) {
      logger.info("Posting job complete request " + i);
      try {
        chunks.put(new NumericChunk());
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    if (logger.isDebugEnabled()) logger.debug(this.getName() + " done.");
  }