aboutsummaryrefslogtreecommitdiffstats
path: root/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/XlsOutputHandler.java
blob: d51271bd697ae44d2009aa9d9370c7f05df7f571 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

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();
    }

}