aboutsummaryrefslogtreecommitdiffstats
path: root/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/XlsOutputHandler.java
blob: 91eaed542933d1edbc5c34f8bdf78ecb861d5a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.openecomp.sdc.asdctool.migration.tasks.handlers;

import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class XlsOutputHandler implements OutputHandler {

	private final static Logger LOGGER = LoggerFactory.getLogger(XlsOutputHandler.class);
	
	private Workbook workbook;
	private Sheet activeSheet;
	private Row currentRow;
	int rowCount = 0;
	
	public XlsOutputHandler(Object... title){
		initiate(title);
	}
	
	@Override
	public void initiate(Object... title) {
		LOGGER.info("Starting to initiate xls output handler. ");
		workbook = new HSSFWorkbook();
		activeSheet = workbook.createSheet("Upgrade Migration 1710.0 results");
		addRecord(title);
		LOGGER.info("Xls output handler has been initiated. ");
	}

	@Override
	public void addRecord(Object... record) {
		LOGGER.debug("Going to add record {} to output. ", record);
		currentRow = activeSheet.createRow(rowCount++);
		LOGGER.debug("A new row has been created");
        int columnCount = 0;
        Cell cell;
        for(Object cellValue : record){
            cell = currentRow.createCell(columnCount++);
            if(cellValue != null)
            	cell.setCellValue(cellValue.toString());
        }
	}

	@Override
	public boolean writeOutput() {
        try {
			DateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
        	String fileName = "UpgradeMigration1710Results_" + df.format(System.currentTimeMillis()) + ".xls";
        	LOGGER.info("Going to write xls output file {}. ", fileName);
			workbook.write(new FileOutputStream(fileName));
			return true;
		} catch (Exception e) {
			LOGGER.error("Failed to write an output file upon  Upgrade migration 1710. Exception {} occured. ", e.getMessage());
			e.printStackTrace();
			return false;
		}
	}

}