
prerna.reactor.export.ToExcelReactor Maven / Gradle / Ivy
The newest version!
package prerna.reactor.export;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import prerna.algorithm.api.SemossDataType;
import prerna.auth.User;
import prerna.auth.utils.AbstractSecurityUtils;
import prerna.auth.utils.SecurityQueryUtils;
import prerna.date.SemossDate;
import prerna.engine.api.IHeadersDataRow;
import prerna.om.InsightFile;
import prerna.om.InsightPanel;
import prerna.om.InsightSheet;
import prerna.poi.main.helper.excel.ExcelUtility;
import prerna.reactor.AbstractReactor;
import prerna.reactor.task.TaskBuilderReactor;
import prerna.sablecc2.om.GenRowStruct;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.PixelOperationType;
import prerna.sablecc2.om.ReactorKeysEnum;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.util.Constants;
import prerna.util.DIHelper;
import prerna.util.Utility;
public class ToExcelReactor extends TaskBuilderReactor {
private static final Logger classLogger = LogManager.getLogger(ToExcelReactor.class);
private static final String CLASS_NAME = ToExcelReactor.class.getName();
protected String fileLocation = null;
protected Logger logger;
protected boolean includeLogo = true;
public ToExcelReactor() {
this.keysToGet = new String[]{ReactorKeysEnum.TASK.getKey(), ReactorKeysEnum.FILE_NAME.getKey(),
ReactorKeysEnum.FILE_PATH.getKey(), ReactorKeysEnum.PASSWORD.getKey(), ReactorKeysEnum.PANEL.getKey()};
}
@Override
public NounMetadata execute() {
organizeKeys();
User user = this.insight.getUser();
// throw error is user doesn't have rights to export data
if(AbstractSecurityUtils.adminSetExporter() && !SecurityQueryUtils.userIsExporter(user)) {
AbstractReactor.throwUserNotExporterError();
}
this.logger = getLogger(CLASS_NAME);
this.task = getTask();
String downloadKey = UUID.randomUUID().toString();
InsightFile insightFile = new InsightFile();
insightFile.setFileKey(downloadKey);
// get a random file name
String prefixName = Utility.normalizePath(this.keyValue.get(ReactorKeysEnum.FILE_NAME.getKey()));
String exportName = AbstractExportTxtReactor.getExportFileName(user, prefixName, "xlsx");
// grab file path to write the file
this.fileLocation = this.keyValue.get(ReactorKeysEnum.FILE_PATH.getKey());
// if the file location is not defined generate a random path and set
// location so that the front end will download
if (this.fileLocation == null) {
String insightFolder = this.insight.getInsightFolder();
File f = new File(insightFolder);
if(!f.exists()) {
f.mkdirs();
}
this.fileLocation = insightFolder + DIR_SEPARATOR + exportName;
insightFile.setDeleteOnInsightClose(true);
} else {
this.fileLocation += DIR_SEPARATOR + exportName;
insightFile.setDeleteOnInsightClose(false);
}
insightFile.setFilePath(this.fileLocation);
try {
buildTask();
} finally {
if(this.task != null) {
try {
this.task.close();
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
}
}
}
// store the insight file
// in the insight so the FE can download it
// only from the given insight
this.insight.addExportFile(downloadKey, insightFile);
NounMetadata retNoun = new NounMetadata(downloadKey, PixelDataType.CONST_STRING, PixelOperationType.FILE_DOWNLOAD);
retNoun.addAdditionalReturn(NounMetadata.getSuccessNounMessage("Successfully generated the excel file"));
return retNoun;
}
@Override
protected void buildTask() {
SXSSFWorkbook workbook = new SXSSFWorkbook(1000);
CreationHelper createHelper = workbook.getCreationHelper();
String sheetName = "Result";
// get the panel
InsightPanel panel = getInsightPanel();
Map> panelFormatting = new HashMap<>();
// if panel is passed
// use that for panel level formatting
// and for the sheet name
if(panel != null) {
// panel level formatting
panelFormatting = panel.getPanelFormatValues();
// sheet name
String sheetId = panel.getSheetId();
InsightSheet sheet = this.insight.getInsightSheet(sheetId);
sheetName = sheet.getSheetLabel();
if (sheetName == null) {
// since we are 0 based, add 1
try {
sheetName = "Sheet" + (Integer.parseInt(sheetId) + 1);
} catch (Exception ignore) {
sheetName = "Sheet " + sheetId;
}
}
}
SXSSFSheet sheet = workbook.createSheet(sheetName);
sheet.setRandomAccessWindowSize(100);
// freeze the first row
sheet.createFreezePane(0, 1);
int i = 0;
int size = 0;
// create typesArr as an array for faster searching
String[] headers = null;
SemossDataType[] typesArr = null;
String[] additionalDataTypeArr = null;
CellStyle[] stylingArr = null;
// style dates
CellStyle dateCellStyle = workbook.createCellStyle();
dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd-MM-yyyy"));
// style timestamps
CellStyle timeStampCellStyle = workbook.createCellStyle();
timeStampCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd-MM-yyyy HH:mm:ss"));
// the excel data row
Row excelRow = null;
int excelRowCounter = 0;
// we need to iterate and write the headers during the first time
if(this.task.hasNext()) {
IHeadersDataRow row = this.task.next();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy