Beispiel #1
0
  public static boolean importTable(
      JdbcConnection conn,
      String tableName,
      Map<String, String> features,
      File fileIn,
      File tablePath,
      char delimiter,
      boolean header) {
    boolean ok = true;
    try {
      DbChecker dbCh = new DbChecker(conn);
      if (!dbCh.isTableExist(tableName)) {
        logger.debug("The table which has to be imported has not been created");
        logger.debug("Creation of the table");
        Integer ASCIIVal = (int) delimiter;
        String[] options = {ASCIIVal.toString(), tablePath.getCanonicalPath()};
        conn.executeQuery(
            new MySqlBasicStatement().createExternalTable(tableName, features, options));
      } else {
        // Check if it is the same table
        if (!dbCh.areFeaturesTheSame(tableName, features.keySet())) {
          logger.warn("Mismatch between the table to import and the table in the database");
          return false;
        }

        logger.warn("Have to check if the table is external or not, I do not know how to do that");
      }
    } catch (SQLException e) {
      logger.debug("Fail to watch the datastore");
      logger.debug(e.getMessage());
      return false;
    } catch (IOException e) {
      logger.warn("Fail to get the output path from a File object");
      logger.warn(e.getMessage());
      return false;
    }

    // Check if the input file has the right number of field
    FileChecker fChIn = new FileChecker(fileIn);
    FileChecker fChOut = new FileChecker(tablePath);
    String strLine = "";
    try {

      if (fChIn.isDirectory() || !fChIn.canRead()) {
        logger.warn("The file " + fChIn.getFilename() + "is a directory or can not be read");
        return false;
      }
      BufferedReader br = new BufferedReader(new FileReader(fileIn));
      // Read first line
      strLine = br.readLine();
      br.close();
    } catch (IOException e1) {
      logger.debug("Fail to open the file" + fChIn.getFilename());
      return false;
    }

    if (StringUtils.countMatches(strLine, String.valueOf(delimiter)) != features.size() - 1) {
      logger.warn(
          "File given does not match with the delimiter '"
              + delimiter
              + "' given and the number of fields '"
              + features.size()
              + "'");
      return false;
    }

    BufferedWriter bw = null;
    BufferedReader br = null;

    try {
      bw = new BufferedWriter(new FileWriter(tablePath));
      logger.debug("read the file" + fileIn.getAbsolutePath());
      br = new BufferedReader(new FileReader(fileIn));
      String delimiterStr = "" + delimiter;
      // Read File Line By Line
      while ((strLine = br.readLine()) != null) {
        bw.write("\"" + strLine.replace(delimiterStr, "\",\"") + "\"\n");
      }
      br.close();

      bw.close();
    } catch (FileNotFoundException e1) {
      logger.error(e1.getCause() + " " + e1.getMessage());
      logger.error("Fail to read " + fileIn.getAbsolutePath());
      ok = false;
    } catch (IOException e1) {
      logger.error(
          "Error writting, reading on the filesystem from the directory"
              + fChIn.getFilename()
              + " to the file "
              + fChOut.getFilename());
      ok = false;
    }

    return ok;
  }