blob: b5ea6dd0a9d802412095d236e5b366fcaa4938a0 (
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
package org.openecomp.sdc.asdctool.migration.tasks.handlers;
import org.apache.commons.lang3.StringUtils;
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.openecomp.sdc.common.log.wrappers.Logger;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
public class XlsOutputHandler implements OutputHandler {
private static final Logger log = Logger.getLogger(XlsOutputHandler.class);
private Workbook workbook;
private Sheet activeSheet;
private int rowCount = 0;
private String sheetName;
private String outputPath;
public XlsOutputHandler(String outputPath, String sheetName, Object... title){
this.outputPath = outputPath;
this.sheetName = sheetName;
initiate(sheetName, title);
}
@Override
public void initiate(String sheetName, Object... title) {
log.info("#initiate - Starting to initiate XlsOutputHandler. ");
workbook = new HSSFWorkbook();
activeSheet = workbook.createSheet(sheetName);
addRecord(title);
log.info("#initiate - XlsOutputHandler has been initiated. ");
}
@Override
public void addRecord(Object... record) {
log.info("#addRecord - Going to add record {} to output. ", record);
Row currentRow = activeSheet.createRow(rowCount++);
log.info("#addRecord - 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 writeOutputAndCloseFile() {
if (rowCount <= 1) {
return false;
}
try {
FileOutputStream file = getXlsFile();
workbook.write(file);
file.close();
return true;
} catch (Exception e) {
log.debug("#writeOutputAndCloseFile - Failed to write an output file. ", e);
return false;
}
}
public String getOutputPath() {
return outputPath;
}
FileOutputStream getXlsFile() throws FileNotFoundException {
String fileName = buildFileName();
log.info("#getXlsFile - Going to write the output file {}. ", fileName);
return new FileOutputStream(fileName);
}
private String buildFileName() {
StringBuilder fileName = new StringBuilder();
if(StringUtils.isNotEmpty(outputPath)){
fileName.append(outputPath);
}
return fileName.append(sheetName)
.append("_")
.append(new SimpleDateFormat("yyyyMMdd_HHmmss").format(System.currentTimeMillis()))
.append(".xls")
.toString();
}
}
|