1 package org.openecomp.sdc.asdctool.migration.tasks.handlers;
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
5 import org.apache.poi.ss.usermodel.Cell;
6 import org.apache.poi.ss.usermodel.Row;
7 import org.apache.poi.ss.usermodel.Sheet;
8 import org.apache.poi.ss.usermodel.Workbook;
9 import org.openecomp.sdc.common.log.wrappers.Logger;
11 import java.io.FileNotFoundException;
12 import java.io.FileOutputStream;
13 import java.text.SimpleDateFormat;
15 public class XlsOutputHandler implements OutputHandler {
17 private static final Logger log = Logger.getLogger(XlsOutputHandler.class);
18 private Workbook workbook;
19 private Sheet activeSheet;
20 private int rowCount = 0;
21 private String sheetName;
22 private String outputPath;
24 public XlsOutputHandler(String outputPath, String sheetName, Object... title){
25 this.outputPath = outputPath;
26 this.sheetName = sheetName;
27 initiate(sheetName, title);
31 public void initiate(String sheetName, Object... title) {
32 log.info("#initiate - Starting to initiate XlsOutputHandler. ");
33 workbook = new HSSFWorkbook();
34 activeSheet = workbook.createSheet(sheetName);
36 log.info("#initiate - XlsOutputHandler has been initiated. ");
40 public void addRecord(Object... record) {
41 log.info("#addRecord - Going to add record {} to output. ", record);
42 Row currentRow = activeSheet.createRow(rowCount++);
43 log.info("#addRecord - A new row has been created");
46 for(Object cellValue : record){
47 cell = currentRow.createCell(columnCount++);
48 if (cellValue != null) {
49 cell.setCellValue(cellValue.toString());
55 public boolean writeOutputAndCloseFile() {
60 FileOutputStream file = getXlsFile();
64 } catch (Exception e) {
65 log.debug("#writeOutputAndCloseFile - Failed to write an output file. ", e);
70 public String getOutputPath() {
74 FileOutputStream getXlsFile() throws FileNotFoundException {
75 String fileName = buildFileName();
76 log.info("#getXlsFile - Going to write the output file {}. ", fileName);
77 return new FileOutputStream(fileName);
80 private String buildFileName() {
81 StringBuilder fileName = new StringBuilder();
82 if(StringUtils.isNotEmpty(outputPath)){
83 fileName.append(outputPath);
85 return fileName.append(sheetName)
87 .append(new SimpleDateFormat("yyyyMMdd_HHmmss").format(System.currentTimeMillis()))