package io.miti.beetle.exporters;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

public abstract class DBFileWriter
{
  protected StringBuilder sb = null;
  protected String filename = null;
  protected File file = null;
  protected ResultSetMetaData rsmd = null;


  @SuppressWarnings("unused")
  private DBFileWriter() {
    super();
  }


  /**
   * Constructor taking the output filename and result set.
   * 
   * @param sFilename the output filename
   * @param pRSMD the database result set metadata (column names, etc.)
   */
  public DBFileWriter(final String sFilename, final ResultSetMetaData pRSMD) {
    sb = new StringBuilder(100);
    file = new File(sFilename);
    filename = sFilename;
    rsmd = pRSMD;
  }


  /**
   * Abstract method for writing the file's header.
   */
  public abstract void writeHeader();


  /**
   * Abstract method for writing the file's footer.
   */
  public abstract void writeFooter();


  /**
   * Abstract method for writing a database result set row.
   * 
   * @param rsj the result set
   */
  public abstract void writeObject(final ResultSet rsj);
  
  
  /**
   * Write a string to the file.  Default to not forcing a write.
   */
  public final void writeString() {
    writeString(false);
  }
  
  
  /**
   * If the string buffer is big enough, flush it to disk.
   */
  public final void writeString(final boolean forceWrite) {
    // Check the length of the string
    if (forceWrite || sb.length() > 10000) {

      // The input buffer is long enough that we need to
      // write it to file and then clear out the buffer
      PrintWriter filePw = null;
      try {
        // Open the file for appending
        filePw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
            filename, true), StandardCharsets.UTF_8));

        filePw.print(sb.toString());

        filePw.close();
        filePw = null;
      } catch (FileNotFoundException e) {
      } finally {
        if (filePw != null) {
          // Close the writer
          filePw.close();
        }
      }

      sb.setLength(0);
    }
  }
}

@@ -28,16 +28,52 @@
 rters;%0A%0A
+import io.miti.beetle.util.Logger;%0A%0A
 import j
@@ -85,16 +85,16 @@
 o.File;%0A
-
 import j
@@ -507,16 +507,65 @@
 = null;%0A
+  %0A  protected static final String EOL = %22%5Cr%5Cn%22;%0A
 %0A%0A  @Sup
@@ -996,16 +996,16 @@
 lename;%0A
-
     rsmd
@@ -1014,16 +1014,276 @@
  pRSMD;%0A
+    %0A    // Check the output file - if it exists as a file, empty it%0A    if (file.exists() && file.isFile()) %7B%0A      try %7B%0A        file.delete();%0A      %7D catch (Exception ex) %7B%0A        Logger.error(%22Exception deleting file: %22 + ex.getMessage());%0A      %7D%0A    %7D%0A
   %7D%0A%0A%0A  

Delete the output string before saving data to it.