You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by "fuweng11 (via GitHub)" <gi...@apache.org> on 2023/04/17 11:30:02 UTC

[GitHub] [inlong] fuweng11 commented on a diff in pull request #7869: [INLONG-7867][Manager] Support data validation when importing Excel file

fuweng11 commented on code in PR #7869:
URL: https://github.com/apache/inlong/pull/7869#discussion_r1168537587


##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/util/Preconditions.java:
##########
@@ -153,6 +155,17 @@ public static void expectNotBlank(String obj, ErrorCodeEnum errorCodeEnum, Strin
         }
     }
 
+    public static void expectBlank(String obj, ErrorCodeEnum errorCodeEnum, String errMsg) {
+        if (StringUtils.isNotBlank(obj)) {
+            throw new BusinessException(errorCodeEnum, errMsg);
+        }
+    }
+    public static void expectBlank(List<String> obj, ErrorCodeEnum errorCodeEnum, String errMsg) {

Review Comment:
   expectEmpty.



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -553,17 +599,22 @@ public static <T> ClassMeta<T> of(Class<T> clazz)
             for (Field field : fields) {
                 ExcelField excelField = field.getAnnotation(ExcelField.class);
                 if (excelField != null) {
+                    Class<? extends ExcelCellValidator> validatorClass = excelField.validator();
                     ExcelCellDataTransfer excelCellDataTransfer = excelField.x2oTransfer();
+                    ExcelCellValidator excelCellValidator = null;
+                    if (validatorClass != ExcelCellValidator.class) {
+                        excelCellValidator = validatorClass.newInstance();
+                    }
                     meta.addField(field.getName(), excelField.name(), field, field.getType(),
-                            excelCellDataTransfer);
+                            excelCellDataTransfer, excelCellValidator);

Review Comment:
   `excelCellValidator` maybe is null.



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -372,35 +373,54 @@ public static <E> List<E> read(InputStream is, Class<E> clazz)
 
                 int lastRowNum = sheet.getLastRowNum();
                 List<E> currentResult = new ArrayList<>(lastRowNum);
-
+                List<String> validateResult = new ArrayList<>(lastRowNum);
                 for (int rowNum = 1; rowNum <= lastRowNum; ++rowNum) {
                     XSSFRow row = sheet.getRow(rowNum);
                     if (row == null) {
                         continue;
                     }
+                    // Mark the row only if all cells are not null
+                    Map<Integer, FieldMeta> positionFieldMetaMap = classMeta.positionFieldMetaMap;
+                    boolean rowNotNull = positionFieldMetaMap.keySet()
+                            .stream()
+                            .map(colIndex -> row.getCell(colIndex,
+                                    Row.MissingCellPolicy.RETURN_BLANK_AS_NULL) != null)
+                            .reduce(false, (left, right) -> left || right);
+
+                    // Skip if all columns are null
+                    if (!rowNotNull) {
+                        continue;
+                    }
+
                     E instance = null;
+                    StringBuilder colValidateResult = new StringBuilder();
                     boolean hasValueInRow = false;
-                    for (Map.Entry<Integer, FieldMeta> entry : classMeta.positionFieldMetaMap.entrySet()) {
+                    for (Map.Entry<Integer, FieldMeta> entry : positionFieldMetaMap.entrySet()) {
                         Integer colIndex = entry.getKey();
                         FieldMeta fieldMeta = entry.getValue();
                         XSSFCell cell = row.getCell(colIndex, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
-                        if (cell == null) {
-                            continue;
-                        }
+
                         hasValueInRow = true;
                         ExcelCellDataTransfer cellDataTransfer = fieldMeta.getCellDataTransfer();
                         Object value = parseCellValue(cellDataTransfer, cell);
                         if (instance == null) {
                             instance = clazz.newInstance();
                         }
+                        validateCellValue(fieldMeta, value).ifPresent(info -> colValidateResult.append("Column ")
+                                .append(colIndex + 1).append(":").append(info).append(";"));
                         fieldMeta.getField().setAccessible(true);
                         fieldMeta.getField().set(instance, value);
-
                     }
                     if (hasValueInRow) {
                         currentResult.add(instance);
                     }
+                    if (colValidateResult.length() > 0) {
+                        String lineValidateResult = "Error in Row " + (rowNum + 1) + ", " + colValidateResult;

Review Comment:
   Suggest use `String.format()`.



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -457,6 +477,27 @@ private static String parseCellValue(Cell cell) {
         return cellValue;
     }
 
+    /**
+     * Validate the cell value of a given field in the Excel sheet
+     *
+     * @param fieldMeta the meta information of the field to validate
+     * @param value     the value of the field to validate
+     */
+    private static Optional<String> validateCellValue(
+            FieldMeta fieldMeta,
+            Object value) {
+        ExcelCellValidator cellValidator = fieldMeta.getCellValidator();
+        if (cellValidator != null) {

Review Comment:
   ```
     if (cellValidator != null&&!cellValidator.validate(value)) {
          return Optional.of(cellValidator.getInvalidTip());
     } else {
           return Optional.empty();
     }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org