Catalog alignment
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / tasks / handlers / XlsOutputHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.migration.tasks.handlers;
22
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;
30
31 import java.io.FileNotFoundException;
32 import java.io.FileOutputStream;
33 import java.text.SimpleDateFormat;
34
35 public class XlsOutputHandler implements OutputHandler {
36
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;
43         
44         public XlsOutputHandler(String outputPath, String sheetName, Object... title){
45                 this.outputPath = outputPath;
46             this.sheetName = sheetName;
47                 initiate(sheetName, title);
48         }
49         
50         @Override
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);
55                 addRecord(title);
56                 log.info("#initiate - XlsOutputHandler has been initiated. ");
57         }
58
59         @Override
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");
64         int columnCount = 0;
65         Cell cell;
66         for(Object cellValue : record){
67             cell = currentRow.createCell(columnCount++);
68             if (cellValue != null) {
69                 cell.setCellValue(cellValue.toString());
70             }
71         }
72         }
73
74         @Override
75         public boolean writeOutputAndCloseFile() {
76                 if (rowCount <= 1) {
77                         return false;
78                 }
79         try {
80                         FileOutputStream file = getXlsFile();
81                         workbook.write(file);
82                         file.close();
83                         return true;
84                 } catch (Exception e) {
85                         log.debug("#writeOutputAndCloseFile - Failed to write an output file. The {} exception occurred. ", e.getMessage());
86                         return false;
87                 }
88         }
89
90         public String getOutputPath() {
91                 return outputPath;
92         }
93
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);
98     }
99
100     private String buildFileName() {
101         StringBuilder fileName = new StringBuilder();
102         if(StringUtils.isNotEmpty(outputPath)){
103             fileName.append(outputPath);
104         }
105         return fileName.append(sheetName)
106                 .append("_")
107                 .append(new SimpleDateFormat("yyyyMMdd_HHmmss").format(System.currentTimeMillis()))
108                 .append(".xls")
109                                 .toString();
110     }
111
112 }