Exemplo n.º 1
0
  /** Generate the ORM code for the class. */
  public void generate() throws IOException {
    Map<String, Integer> columnTypes = connManager.getColumnTypes(tableName);

    String[] colNames = options.getColumns();
    if (null == colNames) {
      colNames = connManager.getColumnNames(tableName);
    }

    // Generate the Java code
    StringBuilder sb = generateClassForColumns(columnTypes, colNames);

    // Write this out to a file.
    String codeOutDir = options.getCodeOutputDir();

    // TODO(aaron): Allow package subdirectory (that goes in sourceFilename).
    String sourceFilename = tableName + ".java";
    String filename = codeOutDir + sourceFilename;

    LOG.debug("Writing source file: " + filename);
    LOG.debug("Table name: " + tableName);
    StringBuilder sbColTypes = new StringBuilder();
    for (String col : colNames) {
      Integer colType = columnTypes.get(col);
      sbColTypes.append(col + ":" + colType + ", ");
    }
    String colTypeStr = sbColTypes.toString();
    LOG.debug("Columns: " + colTypeStr);

    compileManager.addSourceFile(sourceFilename);

    // Create any missing parent directories.
    File file = new File(filename);
    String dirname = file.getParent();
    if (null != dirname) {
      boolean mkdirSuccess = new File(dirname).mkdirs();
      if (!mkdirSuccess) {
        LOG.debug("Could not create directory tree for " + dirname);
      }
    }

    OutputStream ostream = null;
    Writer writer = null;
    try {
      ostream = new FileOutputStream(filename);
      writer = new OutputStreamWriter(ostream);
      writer.append(sb.toString());
    } finally {
      if (null != writer) {
        try {
          writer.close();
        } catch (IOException ioe) {
          // ignored because we're closing.
        }
      }

      if (null != ostream) {
        try {
          ostream.close();
        } catch (IOException ioe) {
          // ignored because we're closing.
        }
      }
    }
  }