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
| @Override protected List<Map<String, Object>> parseRowData(ExcelRow row, long batchTime) throws Exception { String sheet = row.getSheetName(); List<String> columns = row.getColumnList();
Map<String,Object> data = new HashMap<>(); data.put("sheet", sheet); data.put("row", row.getRowIndex());
Map<String, Map<String,String>> rowRule = sheetRule.get(sheet); if(row.getRowIndex() == 1){ LinkedHashMap<Integer,String> link = new LinkedHashMap<>(); for(int i = 0;i < columns.size();i++){ link.put(i, columns.get(i).trim()); } isDataLack(sheet, link, rowRule); columnIndexMap.put(sheet, link); return Arrays.asList(data); }
LinkedHashMap<Integer,String> indexMap = columnIndexMap.get(sheet); for(Entry<Integer,String> entry : indexMap.entrySet()){ int index = entry.getKey(); String columnName = entry.getValue(); String columnValue = null; if(columns.size() > index){ columnValue = columns.get(index).trim(); } Map<String,String> columnRule = rowRule.get(columnName); if(columnRule == null){ continue; } String field = columnRule.get("field"); String type = columnRule.get("type"); String pattern = columnRule.get("pattern"); String defaultValue = columnRule.get("default"); String notNull = columnRule.get("notnull"); if(StringUtils.isBlank(columnValue)){ if(!StringUtils.isBlank(defaultValue)){ columnValue = defaultValue; }else if("true".equals(notNull)){ throw new IllegalArgumentException( buildError("columnValue cannot be null", sheet, row.getRowIndex(), columnName)); } }
if(!PatternUtil.match(pattern, columnValue)){ throw new IllegalArgumentException( buildError("invalid columnValue", sheet, row.getRowIndex(), columnName)); } parseValue(columnValue, type, field, data, columnName); }
for(Entry<String, Map<String,String>> entry : rowRule.entrySet()){ if(indexMap.containsValue(entry.getKey())){ continue; } Map<String,String> map = entry.getValue(); String defaultValue = map.get("default"); if(StringUtils.isBlank(defaultValue)){ continue; } String field = map.get("field"); String type = map.get("type"); String pattern = map.get("pattern"); String notNull = map.get("notnull"); if("true".equals(notNull)){ throw new IllegalArgumentException( buildError("columnValue cannot be null", sheet, row.getRowIndex(), entry.getKey())); } if(!PatternUtil.match(pattern, defaultValue)){ throw new IllegalArgumentException( buildError("invalid columnValue", sheet, row.getRowIndex(), entry.getKey())); } parseValue(defaultValue, type, field, data, entry.getKey()); }
return Arrays.asList(data); }
protected void isDataLack(String sheetName, Map<Integer,String> indexMap, Map<String, Map<String,String>> rule){ for(Entry<String, Map<String,String>> entry : rule.entrySet()){ String column = entry.getKey(); String unnecessary = entry.getValue().get("unnecessary"); if("true".equals(unnecessary)){ continue; } if(!indexMap.containsValue(column)){ throw new IllegalArgumentException("column not found:" + column + ", sheet=" + sheetName); } } }
protected void parseValue(String value, String type, String field, Map<String,Object> data, String columnName) throws Exception { if(StringUtils.isBlank(value)){ return; } data.put(field, value); }
|