package wenrgise.report.excel;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import javax.sql.DataSource;
import jxl.WorkbookSettings;
import jxl.format.CellFormat;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormats;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import wenrgise.common.utility.ServiceLocator;

class ExcelReport {
  WorkbookSettings ws = null;
  
  WritableSheet s = null;
  
  public ExcelReport() {
    this.ws = new WorkbookSettings();
    this.ws.setLocale(new Locale("en", "EN"));
  }
  
  public WorkbookSettings getWorkBookSetting() {
    return this.ws;
  }
  
  public void writeDataSheet(WritableSheet s, ArrayList heading, String query) {
    DataSource oDataSource = null;
    Statement stmt = null;
    Connection conn = null;
    ResultSet results = null;
    DecimalFormat Currency = new DecimalFormat("0.00");
    try {
      oDataSource = (DataSource)ServiceLocator.getLocator().getService("jdbc/conDS");
      conn = oDataSource.getConnection();
      stmt = conn.createStatement();
      results = stmt.executeQuery(query);
      int rowPos = 0;
      WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
      WritableCellFormat cf = new WritableCellFormat(wf);
      cf.setWrap(false);
      int colPos = -1;
      Iterator itrHeading = heading.iterator();
      while (itrHeading.hasNext()) {
        colPos++;
        Heading oHeading = itrHeading.next();
        String sHeading = oHeading.getName();
        s.addCell((WritableCell)new Label(colPos, rowPos, sHeading, (CellFormat)cf));
      } 
      wf = new WritableFont(WritableFont.ARIAL, 8, WritableFont.NO_BOLD);
      while (results.next()) {
        rowPos++;
        colPos = -1;
        Iterator itrHeading1 = heading.iterator();
        while (itrHeading1.hasNext()) {
          colPos++;
          Heading oHeading1 = itrHeading1.next();
          String sHeading1 = oHeading1.getName();
          int sType = oHeading1.getType();
          if (sType == 1) {
            cf = new WritableCellFormat(wf);
            cf.setWrap(false);
            s.addCell((WritableCell)new Label(colPos, rowPos, results.getString(sHeading1), (CellFormat)cf));
            continue;
          } 
          if (sType == 2) {
            cf = new WritableCellFormat(wf, NumberFormats.FLOAT);
            cf.setWrap(false);
            s.addCell((WritableCell)new Number(colPos, rowPos, results.getBigDecimal(sHeading1).floatValue(), (CellFormat)cf));
          } 
        } 
      } 
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      try {
        if (stmt != null)
          stmt.close(); 
        if (results != null)
          results.close(); 
        if (conn != null)
          conn.close(); 
      } catch (Exception e) {
        e.printStackTrace();
      } 
    } 
  }
}
