package com.commerzinfo.util;

import com.commerzinfo.DataRow;
import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

public final class ExcelUtil {
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(ExcelUtil.class);

    private ExcelUtil() {
    }

    public static void writeWorkbookToFile(File inFile, Workbook wb) throws IOException {
        File excelFile = createExcelFile(inFile);
        FileOutputStream fileOut = null;

        try {
            fileOut = new FileOutputStream(excelFile);
            wb.write(fileOut);
            if (logger.isInfoEnabled()) {
                logger.info("WRITING OF FILE " + excelFile.getName() + " was succesful");
            }
        } finally {
            IOUtils.closeQuietly(fileOut);
        }
    }

    public static File createExcelFile(File inFile) throws IOException {
        File parentFile = inFile.getParentFile();
        String newFilename = inFile.getName() + ".xls";

        File excelFile = new File(parentFile, newFilename);
        excelFile.createNewFile();

        return excelFile;
    }

    public static void createEmptyRows(Sheet sheet, AtomicInteger rowCounter, int count) {
        for (int i = 0; i < count; i++) {
            createRow(sheet, rowCounter);
        }
    }

    public static Row createRow(Sheet sheet, AtomicInteger rowCounter) {
        Row row = sheet.getRow(rowCounter.get());

        if (row == null) {
            row = sheet.createRow(rowCounter.get());
        }

        rowCounter.incrementAndGet();
        return row;
    }

    public static Cell createCell(Row row, AtomicInteger cellCounter) {
        Cell cell = row.createCell(cellCounter.get());
        cellCounter.incrementAndGet();
        return cell;
    }

    public static void createDataRow(Sheet sheet, CellStyle dateCellStyle, AtomicInteger rowCounter, int startColIdx, String categoryName, DataRow dataRow) {
        Row row = ExcelUtil.createRow(sheet, rowCounter);
        AtomicInteger cellCounter = new AtomicInteger(startColIdx);
        Cell cell;

        cell = ExcelUtil.createCell(row, cellCounter);
        cell.setCellValue(categoryName);

        cell = ExcelUtil.createCell(row, cellCounter);
        cell.setCellStyle(dateCellStyle);
        cell.setCellValue(dataRow.getBookingDate());

        cell = ExcelUtil.createCell(row, cellCounter);
        cell.setCellValue(dataRow.getValue().doubleValue());

        cell = ExcelUtil.createCell(row, cellCounter);
        cell.setCellValue(dataRow.getBookingText());
    }
}

@@ -341,16 +341,53 @@
 ctory;%0A%0A
+import java.io.BufferedOutputStream;%0A
 import j
@@ -460,16 +460,45 @@
 eption;%0A
+import java.io.OutputStream;%0A
 import j
@@ -840,20 +840,16 @@
         
-File
 OutputSt
@@ -853,23 +853,28 @@
 tStream 
-fileOut
+outputStream
  = null;
@@ -905,18 +905,48 @@
     
-fileOut = 
+outputStream = new BufferedOutputStream(
 new 
@@ -968,24 +968,25 @@
 m(excelFile)
+)
 ;%0A          
@@ -996,23 +996,28 @@
 b.write(
-fileOut
+outputStream
 );%0A     
@@ -1214,15 +1214,20 @@
 tly(
-fileOut
+outputStream
 );%0A 

use bufferedoutstream