refactor: 重新确定ESB数据导入方式

重新确定ESB数据导入方式
This commit is contained in:
song-tianyang 2021-05-06 14:18:56 +08:00 committed by fit2-zhao
parent 51a331c757
commit 7220882d7d
4 changed files with 182 additions and 42 deletions

View File

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.automation.EsbDataStruct;
import io.metersphere.api.dto.definition.parse.esb.ESBExcelSheetInfo;
import io.metersphere.api.dto.definition.parse.esb.EsbExcelDataStruct;
import io.metersphere.api.dto.definition.parse.esb.EsbSheetDataStruct;
import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor;
@ -40,10 +41,14 @@ import java.util.regex.Pattern;
*/
public class ESBParser extends EsbAbstractParser {
private final int REQUEST_MESSAGE_ROW = 5;
private Map<String, Model> definitions = null;
/**
* 导出模板
*
* @param response
* @param fileName
*/
public static void export(HttpServletResponse response, String fileName) {
XSSFWorkbook wb = null;
@ -94,7 +99,6 @@ public class ESBParser extends EsbAbstractParser {
short[] colorIndexArr = {IndexedColors.LIGHT_GREEN.getIndex(), IndexedColors.ORCHID.getIndex(), IndexedColors.YELLOW.getIndex()};
for (short colorIndex : colorIndexArr) {
XSSFCellStyle style = workbook.createCellStyle();
// style.setFillForegroundColor(IndexedColors.VIOLET.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
@ -123,7 +127,6 @@ public class ESBParser extends EsbAbstractParser {
if (sheet == null) {
return;
}
sheet.setColumnWidth(0, 4000);
sheet.setColumnWidth(1, 4000);
sheet.setColumnWidth(2, 4000);
@ -349,7 +352,6 @@ public class ESBParser extends EsbAbstractParser {
}
private static void setCellValue(String textValue, Cell cell, XSSFFont font, XSSFCellStyle cellStyle) {
// HSSFRichTextString richString = new HSSFRichTextString(textValue);
XSSFRichTextString richString = new XSSFRichTextString(textValue);
richString.applyFont(font);
if (cellStyle != null) {
@ -393,35 +395,21 @@ public class ESBParser extends EsbAbstractParser {
if (headSheet == null) {
return importDataStruct;
}
int simpleCodeIndex = 0;
int simpleNameIndex = 1;
int apiNameIndex = 6;
int apiDescIndex = 7;
int chineNameIndex = 8;
int descIndex = 9;
int apiPositionIndex = 10;
int cellCount = 11;
// if (isHeadSheet) {
apiNameIndex = 5;
apiDescIndex = 6;
chineNameIndex = 7;
descIndex = 8;
apiPositionIndex = 9;
cellCount = 10;
// }
ESBExcelSheetInfo sheetInfo = this.getEsbExcelSheetInfo(headSheet);
int rowCount = headSheet.getLastRowNum();
//根据模版样式如果不是报文头则要取接口信息
if (!isHeadSheet) {
Row interfaceInfoRow = headSheet.getRow(1);
if (interfaceInfoRow != null) {
List<String> rowDataArr = this.getRowDataByStartIndexAndEndIndex(interfaceInfoRow, 0, cellCount - 1);
if (rowDataArr.size() >= cellCount) {
String interfaceCode = rowDataArr.get(simpleCodeIndex);
String interfaceName = rowDataArr.get(apiNameIndex);
String interfaceDesc = rowDataArr.get(apiDescIndex);
List<String> rowDataArr = this.getRowDataByStartIndexAndEndIndex(interfaceInfoRow, 0, sheetInfo.getCellCount() - 1);
if (rowDataArr.size() >= sheetInfo.getCellCount()) {
String interfaceCode = rowDataArr.get(sheetInfo.getSimpleCodeIndex());
String interfaceName = rowDataArr.get(sheetInfo.getServiceNameIndex());
String interfaceDesc = rowDataArr.get(sheetInfo.getServiceScenarioIndex());
if (StringUtils.isEmpty(interfaceName)) {
interfaceName = rowDataArr.get(simpleNameIndex);
interfaceName = rowDataArr.get(sheetInfo.getSimpleNameIndex());
}
importDataStruct.setInterfaceInfo(interfaceCode, interfaceName, interfaceDesc);
}
@ -431,10 +419,11 @@ public class ESBParser extends EsbAbstractParser {
//部分office/wpf生成的文件会出现几万多空行容易造成内存溢出这里进行判断连续五行为空白时认为读取结束
int blankRowCount = 0;
boolean isRequest = true;
for (int startRow = REQUEST_MESSAGE_ROW; startRow < rowCount; startRow++) {
for (int startRow = sheetInfo.getRequestMessageRow(); startRow < rowCount; startRow++) {
Row row = headSheet.getRow(startRow);
List<String> rowDataArr = this.getRowDataByStartIndexAndEndIndex(row, 0, cellCount - 1);
boolean isBlankRow = this.checkBlankRow(rowDataArr, cellCount);
List<String> rowDataArr = this.getRowDataByStartIndexAndEndIndex(row, 0, sheetInfo.getCellCount() - 1);
boolean isBlankRow = this.checkBlankRow(rowDataArr, sheetInfo.getCellCount());
if (!isBlankRow) {
String cellFlag = rowDataArr.get(0);
if (StringUtils.equals(cellFlag, "输出")) {
@ -452,13 +441,14 @@ public class ESBParser extends EsbAbstractParser {
} else {
blankRowCount = 0;
EsbDataStruct dataStruct = new EsbDataStruct();
boolean initDataSuccess = dataStruct.initDefaultData(rowDataArr.get(apiNameIndex), rowDataArr.get(apiDescIndex), rowDataArr.get(chineNameIndex), rowDataArr.get(descIndex));
boolean initDataSuccess = dataStruct.initDefaultData(
rowDataArr.get(sheetInfo.getApiNameIndex()), rowDataArr.get(sheetInfo.getDataTypeIndex()), rowDataArr.get(sheetInfo.getChineNameIndex()), rowDataArr.get(sheetInfo.getDescIndex()));
if (!initDataSuccess) {
continue;
}
boolean isHead = isHeadSheet;
if (rowDataArr.size() > cellCount) {
if (StringUtils.equals(rowDataArr.get(apiPositionIndex), "SYS_HEAD")) {
if (rowDataArr.size() > sheetInfo.getCellCount()) {
if (StringUtils.equals(rowDataArr.get(sheetInfo.getApiPositionIndex()), "SYS_HEAD")) {
isHead = true;
}
@ -769,5 +759,96 @@ public class ESBParser extends EsbAbstractParser {
return StringUtils.isBlank(val) ? "" : val;
}
private ESBExcelSheetInfo getEsbExcelSheetInfo(Sheet sheet) {
String apiCodeCellName = "交易码";
String apiNameCellName = "交易名称";
String serviceNameCellName = "服务名称";
String serviceScenarioCellName = "服务场景";
String englishCellName = "英文名称";
String chineseCellName = "中文名称";
String dataTypeCellName = "数据类型";
String descCellName = "备注";
int maxContinuityEmptyCellCount = 10;
ESBExcelSheetInfo sheetInfo = new ESBExcelSheetInfo();
int lastRowNum = sheet.getLastRowNum();
rowForeach:
for (int rowIndex = 0; rowIndex <= lastRowNum; rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row != null) {
if (rowIndex == 0) {
int lastCellNumber = row.getLastCellNum();
//连续空白数据的统计超过连续maxContinuityEmptyCellCount个空白数据时停止读取当前行
// 某些Excel进行读取时会有缺陷单元格没有数据但是因为格式进行过设置会一直读取容易造成内存溢出
int continuityEmptyCount = 0;
cellForeach:
for (int i = 0; i <= lastCellNumber; i++) {
Cell cell = row.getCell(i);
if (cell != null) {
String cellValue = this.getCellValue(cell).trim();
if (StringUtils.isEmpty(cellValue)) {
continuityEmptyCount++;
if (continuityEmptyCount > maxContinuityEmptyCellCount) {
break cellForeach;
}
} else {
continuityEmptyCount = 0;
if (StringUtils.equals(cellValue, apiCodeCellName)) {
sheetInfo.setSimpleCodeIndex(i);
} else if (StringUtils.equals(cellValue, apiNameCellName)) {
sheetInfo.setSimpleNameIndex(i);
} else if (StringUtils.equals(cellValue, serviceNameCellName)) {
sheetInfo.setServiceNameIndex(i);
} else if (StringUtils.equals(cellValue, serviceScenarioCellName)) {
sheetInfo.setServiceScenarioIndex(i);
}
}
}
}
} else {
//根据第一行服务名称的起始列下标开始读取 进行匹配英文名称数据类型/长度中文名称备注
int startReadIndex = sheetInfo.getApiNameIndex();
int lastCellNumber = row.getLastCellNum();
int continuityEmptyCount = 0;
cellForeach:
for (int i = startReadIndex; i < lastCellNumber; i++) {
Cell cell = row.getCell(i);
if (cell != null) {
String cellValue = this.getCellValue(cell).trim();
if (StringUtils.isEmpty(cellValue)) {
continuityEmptyCount++;
if (continuityEmptyCount > maxContinuityEmptyCellCount) {
break cellForeach;
}
} else {
continuityEmptyCount = 0;
if (StringUtils.equals(cellValue, englishCellName)) {
sheetInfo.setApiNameIndex(i);
} else if (StringUtils.equals(cellValue, chineseCellName)) {
sheetInfo.setChineNameIndex(i);
} else if (StringUtils.contains(cellValue, dataTypeCellName)) {
sheetInfo.setDataTypeIndex(i);
} else if (StringUtils.equals(cellValue, descCellName)) {
sheetInfo.setDescIndex(i);
}
}
}
}
if (sheetInfo.installedApiInfoIndex()) {
sheetInfo.countApiPosisionIndex();
sheetInfo.setRequestMessageRow(rowIndex + 1);
break rowForeach;
}
}
}
}
return sheetInfo;
}
}

View File

@ -0,0 +1,52 @@
package io.metersphere.api.dto.definition.parse.esb;
import lombok.Getter;
import lombok.Setter;
/**
* @author song.tianyang
* @Date 2021/5/6 3:25 下午
* @Description
*/
@Getter
@Setter
public class ESBExcelSheetInfo {
//交易码下标
private int simpleCodeIndex = 0;
//交易名称下标
private int simpleNameIndex = 0;
//服务名下标
private int serviceNameIndex = 0;
//服务场景下标
private int serviceScenarioIndex = 0;
//接口名字下标
private int apiNameIndex = 0;
//数据类型下标
private int dataTypeIndex = 0;
//中文名称
private int chineNameIndex = 0;
//描述
private int descIndex = 0;
//api位置
private int apiPositionIndex = 0;
//每一行的单元格最大长度
private int cellCount = 0;
//请求信息起始行
private int requestMessageRow = 0;
//允许的最大空白行数连续超过这个数字的空白行则认为sheet读取结束
private int maxEmptyRowCount = 20;
public boolean installedApiInfoIndex() {
return (apiNameIndex != 0 && dataTypeIndex != 0);
}
public void countApiPosisionIndex() {
apiPositionIndex = apiNameIndex > apiPositionIndex ? apiNameIndex : apiPositionIndex;
apiPositionIndex = dataTypeIndex > apiPositionIndex ? dataTypeIndex : apiPositionIndex;
apiPositionIndex = chineNameIndex > apiPositionIndex ? chineNameIndex : apiPositionIndex;
apiPositionIndex = descIndex > apiPositionIndex ? descIndex : apiPositionIndex;
apiPositionIndex++;
cellCount = apiPositionIndex;
}
}

View File

@ -1,9 +0,0 @@
package io.metersphere.excel.domain;
/**
* @author song.tianyang
* @Date 2021/3/22 7:04 下午
* @Description
*/
public class EsbExcelData {
}

View File

@ -142,6 +142,22 @@ export default {
if (this.basisData.tags instanceof Array) {
this.basisData.tags = JSON.stringify(this.basisData.tags);
}
if (this.basisData.method == 'ESB') {
let validataResult = this.$refs.esbDefinition.validateEsbDataStruct(this.request.esbDataStruct);
if (!validataResult) {
return;
}
if (this.request.esbDataStruct != null) {
this.esbDataStruct = JSON.stringify(this.request.esbDataStruct);
this.basisData.esbDataStruct = this.esbDataStruct;
}
if (this.request.backEsbDataStruct != null) {
this.basisData.backEsbDataStruct = JSON.stringify(this.request.backEsbDataStruct);
}
if (this.request.backScript != null) {
this.basisData.backScript = JSON.stringify(this.request.backScript);
}
}
this.$emit('runTest', this.basisData);
}
},