/*
   * (non-Javadoc)
   *
   * @see
   * com.toolsverse.etl.connector.DataSetConnector#prePersist(com.toolsverse
   * .etl.connector.DataSetConnectorParams, com.toolsverse.etl.common.DataSet,
   * com.toolsverse.etl.driver.Driver)
   */
  @SuppressWarnings("resource")
  public void prePersist(ExcelConnectorParams params, DataSet dataSet, Driver driver)
      throws Exception {
    String fileName = null;

    OutputStream out = null;

    if (params.getOutputStream() == null) {
      fileName =
          SystemConfig.instance()
              .getPathUsingAppFolders(
                  params.getFileName(
                      dataSet.getOwnerName() != null ? dataSet.getOwnerName() : dataSet.getName(),
                      ".xls",
                      true));

      params.setRealFileName(fileName);

      out = new FileOutputStream(fileName);

      if (params.getTransactionMonitor() != null) params.getTransactionMonitor().addFile(fileName);
    } else out = params.getOutputStream();

    params.setOut(out);

    Workbook workbook = new HSSFWorkbook();

    params.setWorkbook(workbook);

    Sheet sheet =
        workbook.createSheet(
            Utils.isNothing(params.getSheetName()) ? dataSet.getName() : params.getSheetName());

    params.setSheet(sheet);

    Font labelFont = workbook.createFont();
    labelFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    CellStyle labelCellStyle = workbook.createCellStyle();
    labelCellStyle.setFont(labelFont);

    DataFormat dateTimeFormat = workbook.createDataFormat();
    CellStyle dateTimeCellStyle = workbook.createCellStyle();
    dateTimeCellStyle.setDataFormat(dateTimeFormat.getFormat(params.getDateTimeFormat()));

    params.setDateTimeCellStyle(dateTimeCellStyle);

    DataFormat dateFormat = workbook.createDataFormat();
    CellStyle dateCellStyle = workbook.createCellStyle();
    dateCellStyle.setDataFormat(dateFormat.getFormat(params.getDateFormat()));

    params.setDateCellStyle(dateCellStyle);

    DataFormat timeFormat = workbook.createDataFormat();
    CellStyle timeCellStyle = workbook.createCellStyle();
    timeCellStyle.setDataFormat(timeFormat.getFormat(params.getTimeFormat()));

    params.setTimeCellStyle(timeCellStyle);

    // column names
    Row excelRow = sheet.createRow(0);

    // metadata
    int col = 0;
    for (FieldDef fieldDef : dataSet.getFields().getList()) {
      if (!fieldDef.isVisible()) continue;

      Cell labelCell = excelRow.createCell(col++, Cell.CELL_TYPE_STRING);
      labelCell.setCellStyle(labelCellStyle);
      labelCell.setCellValue(fieldDef.getName());
    }

    params.setPrePersistOccured(true);
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * com.toolsverse.etl.connector.DataSetConnector#inlinePersist(com.toolsverse
   * .etl.connector.DataSetConnectorParams, com.toolsverse.etl.common.DataSet,
   * com.toolsverse.etl.driver.Driver,
   * com.toolsverse.etl.common.DataSetRecord, int, int)
   */
  public void inlinePersist(
      ExcelConnectorParams params,
      DataSet dataSet,
      Driver driver,
      DataSetRecord record,
      int row,
      int records)
      throws Exception {
    if (record == null) return;

    int currentRow = params.getCurrentRow();

    Row excelRow = params.getSheet().createRow(currentRow);

    params.setCurrentRow(++currentRow);

    int colCount = dataSet.getFieldCount();

    for (int col = 0; col < colCount; col++) {
      FieldDef fieldDef = dataSet.getFields().get(col);

      if (!fieldDef.isVisible()) continue;

      Object fieldValue = record.get(col);
      int fType = fieldDef.getSqlDataType();
      String value = null;
      Cell dataCell;

      if (fieldValue != null) {
        value = dataSet.encode(fieldDef, fieldValue, driver, params.getParams(), false);
      }

      if (SqlUtils.isNumber(fType)) {
        dataCell = excelRow.createCell(col, Cell.CELL_TYPE_NUMERIC);

        dataCell.setCellValue(value);
      } else if (SqlUtils.isDateOnly(fType)) {
        dataCell = excelRow.createCell(col, Cell.CELL_TYPE_NUMERIC);

        dataCell.setCellStyle(params.getDateCellStyle());

        if (fieldValue instanceof java.util.Date)
          dataCell.setCellValue((java.util.Date) fieldValue);
        else dataCell.setCellValue(value);

      } else if (SqlUtils.isTime(fType)) {
        dataCell = excelRow.createCell(col, Cell.CELL_TYPE_NUMERIC);

        dataCell.setCellStyle(params.getTimeCellStyle());

        if (fieldValue instanceof java.util.Date)
          dataCell.setCellValue((java.util.Date) fieldValue);
        else dataCell.setCellValue(value);

      } else if (SqlUtils.isTimestamp(fType)) {
        dataCell = excelRow.createCell(col, Cell.CELL_TYPE_NUMERIC);

        dataCell.setCellStyle(params.getDateTimeCellStyle());

        if (fieldValue instanceof java.util.Date)
          dataCell.setCellValue((java.util.Date) fieldValue);
        else dataCell.setCellValue(value);

      } else if (SqlUtils.isBoolean(fType)) {
        dataCell = excelRow.createCell(col, Cell.CELL_TYPE_BOOLEAN);

        if (fieldValue instanceof Boolean) dataCell.setCellValue((Boolean) fieldValue);
        else dataCell.setCellValue(value);

      } else {
        dataCell = excelRow.createCell(col, Cell.CELL_TYPE_STRING);
        dataCell.setCellValue(value);
      }
    }

    if (row >= 0
        && records >= 0
        && !params.isSilent()
        && params.getLogStep() > 0
        && (row % params.getLogStep()) == 0)
      Logger.log(
          Logger.INFO,
          EtlLogger.class,
          dataSet.getName()
              + ": "
              + EtlResource.PERSITING_RECORD.getValue()
              + row
              + " out of "
              + records);
  }