re base code
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / tasks / handlers / XlsOutputHandler.java
1 package org.openecomp.sdc.asdctool.migration.tasks.handlers;
2
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;
10
11 import java.io.FileNotFoundException;
12 import java.io.FileOutputStream;
13 import java.text.SimpleDateFormat;
14
15 public class XlsOutputHandler implements OutputHandler {
16
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;
23         
24         public XlsOutputHandler(String outputPath, String sheetName, Object... title){
25                 this.outputPath = outputPath;
26             this.sheetName = sheetName;
27                 initiate(sheetName, title);
28         }
29         
30         @Override
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);
35                 addRecord(title);
36                 log.info("#initiate - XlsOutputHandler has been initiated. ");
37         }
38
39         @Override
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");
44         int columnCount = 0;
45         Cell cell;
46         for(Object cellValue : record){
47             cell = currentRow.createCell(columnCount++);
48             if (cellValue != null) {
49                 cell.setCellValue(cellValue.toString());
50             }
51         }
52         }
53
54         @Override
55         public boolean writeOutputAndCloseFile() {
56                 if (rowCount <= 1) {
57                         return false;
58                 }
59         try {
60                         FileOutputStream file = getXlsFile();
61                         workbook.write(file);
62                         file.close();
63                         return true;
64                 } catch (Exception e) {
65                         log.debug("#writeOutputAndCloseFile - Failed to write an output file. ", e);
66                         return false;
67                 }
68         }
69
70         public String getOutputPath() {
71                 return outputPath;
72         }
73
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);
78     }
79
80     private String buildFileName() {
81         StringBuilder fileName = new StringBuilder();
82         if(StringUtils.isNotEmpty(outputPath)){
83             fileName.append(outputPath);
84         }
85         return fileName.append(sheetName)
86                 .append("_")
87                 .append(new SimpleDateFormat("yyyyMMdd_HHmmss").format(System.currentTimeMillis()))
88                 .append(".xls")
89                                 .toString();
90     }
91
92 }