2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.asdctool.migration.tasks.handlers;
23 import org.apache.commons.lang3.StringUtils;
24 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
25 import org.apache.poi.ss.usermodel.Cell;
26 import org.apache.poi.ss.usermodel.Row;
27 import org.apache.poi.ss.usermodel.Sheet;
28 import org.apache.poi.ss.usermodel.Workbook;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import java.io.FileNotFoundException;
32 import java.io.FileOutputStream;
33 import java.text.SimpleDateFormat;
35 public class XlsOutputHandler implements OutputHandler {
37 private static final Logger log = Logger.getLogger(XlsOutputHandler.class);
38 private Workbook workbook;
39 private Sheet activeSheet;
40 private int rowCount = 0;
41 private String sheetName;
42 private String outputPath;
44 public XlsOutputHandler(String outputPath, String sheetName, Object... title){
45 this.outputPath = outputPath;
46 this.sheetName = sheetName;
47 initiate(sheetName, title);
51 public void initiate(String sheetName, Object... title) {
52 log.info("#initiate - Starting to initiate XlsOutputHandler. ");
53 workbook = new HSSFWorkbook();
54 activeSheet = workbook.createSheet(sheetName);
56 log.info("#initiate - XlsOutputHandler has been initiated. ");
60 public void addRecord(Object... record) {
61 log.info("#addRecord - Going to add record {} to output. ", record);
62 Row currentRow = activeSheet.createRow(rowCount++);
63 log.info("#addRecord - A new row has been created");
66 for(Object cellValue : record){
67 cell = currentRow.createCell(columnCount++);
68 if (cellValue != null) {
69 cell.setCellValue(cellValue.toString());
75 public boolean writeOutputAndCloseFile() {
80 FileOutputStream file = getXlsFile();
84 } catch (Exception e) {
85 log.debug("#writeOutputAndCloseFile - Failed to write an output file. The {} exception occurred. ", e.getMessage());
90 public String getOutputPath() {
94 FileOutputStream getXlsFile() throws FileNotFoundException {
95 String fileName = buildFileName();
96 log.info("#getXlsFile - Going to write the output file {}. ", fileName);
97 return new FileOutputStream(fileName);
100 private String buildFileName() {
101 StringBuilder fileName = new StringBuilder();
102 if(StringUtils.isNotEmpty(outputPath)){
103 fileName.append(outputPath);
105 return fileName.append(sheetName)
107 .append(new SimpleDateFormat("yyyyMMdd_HHmmss").format(System.currentTimeMillis()))