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/07 03:47:17 UTC

[GitHub] [inlong] fuweng11 commented on a diff in pull request #7793: [INLONG-7792][Manager] Support export Excel template file of StreamSource

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


##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelCellDataTransfer.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.manager.common.tool.excel;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public enum ExcelCellDataTransfer {
+
+    DATE {
+
+        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+        String parse2Text(Object o) {
+            if (o == null) {
+                return null;
+            } else if (o instanceof Date) {
+                Date date = (Date) o;
+                return this.simpleDateFormat.format(date);
+            } else {
+                return String.valueOf(o);
+            }
+        }
+
+        Object parseFromText(String so) {
+            if (so != null && so.length() > 0) {
+                String s = so;
+                Date date = null;
+
+                try {
+                    date = this.simpleDateFormat.parse(s);
+                } catch (ParseException var5) {

Review Comment:
   Why is it named var5?



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -0,0 +1,351 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.manager.common.tool.excel;
+
+import lombok.Data;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.commons.lang3.tuple.Triple;
+import org.apache.inlong.manager.common.tool.excel.annotation.ExcelEntity;
+import org.apache.inlong.manager.common.tool.excel.annotation.ExcelField;
+import org.apache.inlong.manager.common.util.Preconditions;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static org.apache.inlong.manager.common.util.Preconditions.expectTrue;
+
+/**
+ * Utility class for working with Excel files.
+ */
+public class ExcelTool {
+
+    private ExcelTool() {
+
+    }
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelTool.class);
+    private static final int DEFAULT_COLUMN_WIDTH = 10000;
+
+    /**
+     * Extracts the header row from a given class and returns it as a list of strings.
+     */
+    public static <E> List<String> extractHeader(Class<E> e1Class) {
+        List<String> list = new LinkedList<>();
+        Field[] fields = e1Class.getDeclaredFields();
+        if (fields.length > 0) {
+            for (Field field : fields) {
+                field.setAccessible(true);
+                ExcelField excel = field.getAnnotation(ExcelField.class);
+                if (excel != null) {
+                    String excelName = excel.name();
+                    list.add(excelName);
+                }
+            }
+            return !list.isEmpty() ? list : Collections.emptyList();
+        } else {
+            return Collections.emptyList();
+        }
+    }
+
+    /**
+     * Writes the given content to an Excel document.
+     *
+     * @param contents the list of contents to write
+     * @param out      the output stream to write to
+     * @throws IOException if there is an error writing to the output stream
+     */
+    public static <T> void write(List<T> contents, OutputStream out) throws IOException {
+        Preconditions.expectNotEmpty(contents, "Content can not be empty!");
+        Class<?> clazz = contents.get(0).getClass();
+        List<Map<String, String>> maps = write2List(contents);
+        doWrite(maps, clazz, out);
+    }
+
+    public static <T> void doWrite(List<Map<String, String>> maps, Class<T> clazz, OutputStream out)
+            throws IOException {
+        List<String> heads = extractHeader(clazz);
+        if (heads.isEmpty()) {
+            throw new IllegalArgumentException(
+                    "At least one field must be marked as Excel Field by annotation @ExcelField in class " + clazz);
+        }
+        XSSFWorkbook hwb = new XSSFWorkbook();
+        XSSFSheet sheet = hwb.createSheet("Sheet 1");
+
+        for (int index = 0; index < heads.size(); index++) {
+            sheet.setColumnWidth(index, DEFAULT_COLUMN_WIDTH);
+        }
+        fillSheetHeader(sheet.createRow(0), heads);
+        //

Review Comment:
   Remove it.



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelCellDataTransfer.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.manager.common.tool.excel;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public enum ExcelCellDataTransfer {
+
+    DATE {
+
+        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+        String parse2Text(Object o) {
+            if (o == null) {
+                return null;
+            } else if (o instanceof Date) {
+                Date date = (Date) o;
+                return this.simpleDateFormat.format(date);
+            } else {
+                return String.valueOf(o);
+            }
+        }
+
+        Object parseFromText(String so) {
+            if (so != null && so.length() > 0) {
+                String s = so;

Review Comment:
   Why is it named so?



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -0,0 +1,351 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.manager.common.tool.excel;
+
+import lombok.Data;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.commons.lang3.tuple.Triple;
+import org.apache.inlong.manager.common.tool.excel.annotation.ExcelEntity;
+import org.apache.inlong.manager.common.tool.excel.annotation.ExcelField;
+import org.apache.inlong.manager.common.util.Preconditions;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static org.apache.inlong.manager.common.util.Preconditions.expectTrue;
+
+/**
+ * Utility class for working with Excel files.
+ */
+public class ExcelTool {
+
+    private ExcelTool() {
+
+    }
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelTool.class);
+    private static final int DEFAULT_COLUMN_WIDTH = 10000;
+
+    /**
+     * Extracts the header row from a given class and returns it as a list of strings.
+     */
+    public static <E> List<String> extractHeader(Class<E> e1Class) {
+        List<String> list = new LinkedList<>();
+        Field[] fields = e1Class.getDeclaredFields();
+        if (fields.length > 0) {
+            for (Field field : fields) {
+                field.setAccessible(true);
+                ExcelField excel = field.getAnnotation(ExcelField.class);
+                if (excel != null) {
+                    String excelName = excel.name();
+                    list.add(excelName);
+                }
+            }
+            return !list.isEmpty() ? list : Collections.emptyList();
+        } else {
+            return Collections.emptyList();
+        }
+    }
+
+    /**
+     * Writes the given content to an Excel document.
+     *
+     * @param contents the list of contents to write
+     * @param out      the output stream to write to
+     * @throws IOException if there is an error writing to the output stream
+     */
+    public static <T> void write(List<T> contents, OutputStream out) throws IOException {
+        Preconditions.expectNotEmpty(contents, "Content can not be empty!");
+        Class<?> clazz = contents.get(0).getClass();
+        List<Map<String, String>> maps = write2List(contents);
+        doWrite(maps, clazz, out);
+    }
+
+    public static <T> void doWrite(List<Map<String, String>> maps, Class<T> clazz, OutputStream out)
+            throws IOException {
+        List<String> heads = extractHeader(clazz);
+        if (heads.isEmpty()) {
+            throw new IllegalArgumentException(
+                    "At least one field must be marked as Excel Field by annotation @ExcelField in class " + clazz);
+        }
+        XSSFWorkbook hwb = new XSSFWorkbook();
+        XSSFSheet sheet = hwb.createSheet("Sheet 1");
+
+        for (int index = 0; index < heads.size(); index++) {
+            sheet.setColumnWidth(index, DEFAULT_COLUMN_WIDTH);
+        }
+        fillSheetHeader(sheet.createRow(0), heads);
+        //
+        if (CollectionUtils.isNotEmpty(maps)) {
+            fillSheetContent(sheet, heads, maps);
+        }
+
+        hwb.write(out);
+        out.close();
+        LOGGER.info("Database export succeeded");
+    }
+
+    /**
+     * Fills the output stream with the provided class meta.
+     */
+    public static <T> void write(Class<T> clazz, OutputStream out) throws IOException {
+        Preconditions.expectTrue(clazz != null, "Class can not be empty!");
+
+        doWrite(null, clazz, out);
+    }
+
+    /**
+     * Fills the content rows of a given sheet with the provided content maps and headers.
+     */
+    private static void fillSheetContent(XSSFSheet sheet, List<String> heads, List<Map<String, String>> contents) {
+        Optional.ofNullable(contents)
+                .ifPresent(c -> IntStream.range(0, c.size())
+                        .forEach(lineId -> {
+                            Map<String, String> line = contents.get(lineId);
+                            Row row = sheet.createRow(lineId + 1);
+                            IntStream.range(0, heads.size()).forEach(colId -> {
+                                String title = heads.get(colId);
+                                String ov = line.get(title);
+                                String value = ov == null ? "" : ov;

Review Comment:
   StringUtils.isBlank(ov).  Why is it named ov?



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -0,0 +1,351 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.manager.common.tool.excel;
+
+import lombok.Data;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.commons.lang3.tuple.Triple;
+import org.apache.inlong.manager.common.tool.excel.annotation.ExcelEntity;
+import org.apache.inlong.manager.common.tool.excel.annotation.ExcelField;
+import org.apache.inlong.manager.common.util.Preconditions;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static org.apache.inlong.manager.common.util.Preconditions.expectTrue;
+
+/**
+ * Utility class for working with Excel files.
+ */
+public class ExcelTool {
+
+    private ExcelTool() {
+
+    }
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelTool.class);
+    private static final int DEFAULT_COLUMN_WIDTH = 10000;
+
+    /**
+     * Extracts the header row from a given class and returns it as a list of strings.
+     */
+    public static <E> List<String> extractHeader(Class<E> e1Class) {
+        List<String> list = new LinkedList<>();
+        Field[] fields = e1Class.getDeclaredFields();
+        if (fields.length > 0) {
+            for (Field field : fields) {
+                field.setAccessible(true);
+                ExcelField excel = field.getAnnotation(ExcelField.class);
+                if (excel != null) {
+                    String excelName = excel.name();
+                    list.add(excelName);
+                }
+            }
+            return !list.isEmpty() ? list : Collections.emptyList();
+        } else {
+            return Collections.emptyList();
+        }
+    }
+
+    /**
+     * Writes the given content to an Excel document.
+     *
+     * @param contents the list of contents to write
+     * @param out      the output stream to write to
+     * @throws IOException if there is an error writing to the output stream
+     */
+    public static <T> void write(List<T> contents, OutputStream out) throws IOException {
+        Preconditions.expectNotEmpty(contents, "Content can not be empty!");
+        Class<?> clazz = contents.get(0).getClass();
+        List<Map<String, String>> maps = write2List(contents);
+        doWrite(maps, clazz, out);
+    }
+
+    public static <T> void doWrite(List<Map<String, String>> maps, Class<T> clazz, OutputStream out)
+            throws IOException {
+        List<String> heads = extractHeader(clazz);
+        if (heads.isEmpty()) {
+            throw new IllegalArgumentException(
+                    "At least one field must be marked as Excel Field by annotation @ExcelField in class " + clazz);
+        }
+        XSSFWorkbook hwb = new XSSFWorkbook();
+        XSSFSheet sheet = hwb.createSheet("Sheet 1");
+
+        for (int index = 0; index < heads.size(); index++) {
+            sheet.setColumnWidth(index, DEFAULT_COLUMN_WIDTH);
+        }
+        fillSheetHeader(sheet.createRow(0), heads);
+        //
+        if (CollectionUtils.isNotEmpty(maps)) {
+            fillSheetContent(sheet, heads, maps);
+        }
+
+        hwb.write(out);
+        out.close();
+        LOGGER.info("Database export succeeded");
+    }
+
+    /**
+     * Fills the output stream with the provided class meta.
+     */
+    public static <T> void write(Class<T> clazz, OutputStream out) throws IOException {
+        Preconditions.expectTrue(clazz != null, "Class can not be empty!");

Review Comment:
   Preconditions.expectNotNull



-- 
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