/* * Copyright (C), 2018-2020 * Project: starter * FileName: ParamColumn * Author: xx * Date: 2020/12/8 11:27 */ package com.fr.plugin.isgd.bean; import com.fanruan.api.i18n.I18nKit; import com.fanruan.api.util.GeneralKit; import com.fanruan.api.util.StringKit; import com.fr.base.Formula; import com.fr.base.Style; import com.fr.chart.chartattr.ChartCollection; import com.fr.chart.chartattr.ChartPainter; import com.fr.general.DateUtils; import com.fr.general.ImageWithSuffix; import com.fr.json.JSONArray; import com.fr.main.impl.LinkWorkBookTemplate; import com.fr.plugin.isgd.entity.ReportIndexInfoEntity; import com.fr.plugin.isgd.menu.ParamConfig; import com.fr.report.cell.ResultCellElement; import com.fr.report.cell.cellattr.core.ResultSubReport; import com.fr.report.cell.cellattr.core.RichText; import com.fr.report.cell.cellattr.core.SubReport; import com.fr.report.cell.cellattr.core.group.DSColumn; import com.fr.report.cell.painter.BiasTextPainter; import com.fr.report.report.ECReport; import com.fr.stable.StableUtils; import org.jetbrains.annotations.NotNull; import java.util.*; /** *
* <自定义列参数bean> * * @author xx * @since 1.0.0 */ public class ParamColumn { public static final String REGEX_HTML1 = "\\&[a-zA-Z]{1,10};"; public static final String REGEX_HTML2 = "<[^>]*>"; public static final String REGEX_HTML3 = "[(/>)<]"; private String reportName; private int[] headerRows; private JSONArray columnArray; public ParamColumn() { } /** * 单元格结果值转为字符串 * * @param resultCellElement * @return */ @NotNull private String getCellText(ResultCellElement resultCellElement) { String cellText; Object value = this.getCellValue(resultCellElement); Style style = resultCellElement.getStyle(); if (style != null) { cellText = Style.valueToText(value, style.getFormat()); } else { cellText = GeneralKit.objectToString(value); } cellText = cellText.replaceAll(REGEX_HTML1, StringKit.EMPTY) .replaceAll(REGEX_HTML2, StringKit.EMPTY).replaceAll(REGEX_HTML3, StringKit.EMPTY); return cellText; } /** * 获得展示列值 * * @param ecReport * @return */ public List getReportIndexInfoEntities(ECReport ecReport, String reportCode, String currentUserName) { List result = new ArrayList<>(); ReportIndexInfoEntity entity; ResultCellElement resultCellElement; for (int headerRow : headerRows) { Iterator iterator = ecReport.getRow(headerRow - 1); String cellText; while (iterator.hasNext()) { resultCellElement = (ResultCellElement) iterator.next(); cellText = getCellText(resultCellElement); entity = new ReportIndexInfoEntity(); entity.setId(UUID.randomUUID().toString()); entity.setReportCode(reportCode); entity.setTableCode(headerRow); entity.setIndexCode(StableUtils.convertIntToABC(resultCellElement.getColumn() + 1)); entity.setIndexName(cellText); entity.setCreateDate(DateUtils.getDate2LStr(new Date())); entity.setUpdateDate(DateUtils.getDate2LStr(new Date())); entity.setOpUsername(currentUserName); result.add(entity); } } return result; } /** * 读取cell值 * * @param cell * @return */ private Object getCellValue(ResultCellElement cell) { if (cell == null) { return StringKit.EMPTY; } Object value = cell.getShowValue(); if (value instanceof Formula) { value = ((Formula) value).getPureContent(); } else if (value instanceof BiasTextPainter) { value = ((BiasTextPainter) value).getText(); } else if (value instanceof DSColumn) { value = ((DSColumn) value).getDSName(); } else if (value instanceof SubReport) { value = ((LinkWorkBookTemplate) ((SubReport) value).getPackee()).getTemplatePath(); } else if (value instanceof ResultSubReport) { value = I18nKit.getLocText("Plugin-isgd_SubReport_Column"); } else if (value instanceof ChartCollection) { value = ((ChartCollection) value).getChartName(0); } else if (value instanceof ChartPainter) { value = ((ChartPainter) value).getGlyphName(0); } else if (value instanceof RichText) { value = ((RichText) value).getContent(); } else if (value instanceof ImageWithSuffix) { value = I18nKit.getLocText("Plugin-isgd_Image_Column"); } return value == null ? StringKit.EMPTY : value; } public String getReportName() { return reportName; } public void setReportName(String reportName) { this.reportName = reportName; } public JSONArray getColumnArray() { return columnArray; } public void setColumnArray(JSONArray columnArray) { this.columnArray = columnArray; } public int[] getHeaderRows() { return headerRows; } /** * 设置表头列值所在行码 * * @param paramConfig * @return */ public void setHeaderRows(ParamConfig paramConfig) { int[] headerRows = paramConfig != null ? paramConfig.getHeaderRowInts() : new int[]{0}; this.setHeaderRows(headerRows); } public void setHeaderRows(int[] headerRow) { this.headerRows = headerRow; } }