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

[GitHub] [inlong] featzhang opened a new pull request, #7793: [INLONG-7792][Manager] Support export Excel template file of StreamSource

featzhang opened a new pull request, #7793:
URL: https://github.com/apache/inlong/pull/7793

   ## [INLONG-7792][Manager] Support parsing and exporting Excel files
   
   Excel is one of the common office software formats for most enterprises. For InLong, it is very convenient to import and export batch information, such as batch operations of table fields, by supporting direct parsing of files.
   
   
   Considering that there will be scenarios of importing and exporting Excel in the future, here we have only made a thin encapsulation on Apache POI to make it as universal as possible.
   
   - Fixes #7792 
   
   
   ### Validation Results
   
   <img width="998" alt="image" src="https://user-images.githubusercontent.com/5709212/230706958-696eee7c-232a-455c-ae82-00d27f4c036a.png">
   


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


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

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7793:
URL: https://github.com/apache/inlong/pull/7793#discussion_r1160513705


##########
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);
+        }
+        try (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);
+            // Fill content if data exist.
+            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.expectNotNull(clazz, "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 originValue = line.get(title);
+                                String value = originValue == null ? "" : originValue;

Review Comment:
   Fixed



##########
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);
+        }
+        try (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);
+            // Fill content if data exist.
+            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.expectNotNull(clazz, "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 originValue = line.get(title);
+                                String value = originValue == null ? "" : originValue;
+                                Cell cell = row.createCell(colId);
+                                cell.setCellValue(value);
+                            });
+                        }));
+    }
+
+    private static void fillSheetHeader(XSSFRow row, List<String> heads) {
+        IntStream.range(0, heads.size()).forEach(index -> {
+            XSSFCell cell = row.createCell(index);
+            cell.setCellValue(new XSSFRichTextString(heads.get(index)));
+        });
+    }
+
+    /**
+     * Convert a list of objects to a list of maps, where each map represents an object's fields and values.
+     *
+     * @param objects The list of objects to be converted.
+     * @param <E>     The type of the objects to be converted.
+     * @return A list of maps, where each map represents an object's fields and values.
+     */
+    public static <E> List<Map<String, String>> write2List(List<E> objects) {
+        E e1 = objects.get(0);

Review Comment:
   Fixed



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


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

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7793:
URL: https://github.com/apache/inlong/pull/7793#discussion_r1160445366


##########
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:
   Fixed



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


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

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7793:
URL: https://github.com/apache/inlong/pull/7793#discussion_r1161069764


##########
inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/InlongStreamController.java:
##########
@@ -183,4 +191,22 @@ public Response<List<StreamField>> parseFields(@RequestBody ParseFieldRequest pa
         return Response.success(streamService.parseFields(parseFieldRequest));
     }
 
+    @RequestMapping(value = "/stream/fieldsImportTemplate", method = RequestMethod.GET)
+    @ApiOperation(value = "Download fields import template")
+    public void downloadFieldsImportTemplate(HttpServletResponse response) {

Review Comment:
   When using a response to download files, HttpServletResponse is used to output the response's content, status code, and headers. I understand that the return value of this method has no effect. Instead, the file cannot be processed when the spring converters is called to output the content:
   
   No converter for [class org.apache.inlong.manager.pojo.common.Response] with preset Content-Type 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'



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


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

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7793:
URL: https://github.com/apache/inlong/pull/7793#discussion_r1161069764


##########
inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/InlongStreamController.java:
##########
@@ -183,4 +191,22 @@ public Response<List<StreamField>> parseFields(@RequestBody ParseFieldRequest pa
         return Response.success(streamService.parseFields(parseFieldRequest));
     }
 
+    @RequestMapping(value = "/stream/fieldsImportTemplate", method = RequestMethod.GET)
+    @ApiOperation(value = "Download fields import template")
+    public void downloadFieldsImportTemplate(HttpServletResponse response) {

Review Comment:
   Fixed



##########
inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/InlongStreamController.java:
##########
@@ -183,4 +191,22 @@ public Response<List<StreamField>> parseFields(@RequestBody ParseFieldRequest pa
         return Response.success(streamService.parseFields(parseFieldRequest));
     }
 
+    @RequestMapping(value = "/stream/fieldsImportTemplate", method = RequestMethod.GET)
+    @ApiOperation(value = "Download fields import template")
+    public void downloadFieldsImportTemplate(HttpServletResponse response) {
+        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss"));
+        String fileName = String.format("InLong-stream-fields-template-%s.xlsx", date);
+        response.setHeader("Content-Disposition",
+                "attachment;filename=" + fileName);
+        response.setContentType("multipart/form-data");
+
+        try {
+            ServletOutputStream outputStream = response.getOutputStream();
+            ExcelTool.write(StreamField.class, outputStream);
+
+        } catch (IOException e) {
+            log.error("Can not properly download Excel file", e);

Review Comment:
   Fixed



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


[GitHub] [inlong] dockerzhang merged pull request #7793: [INLONG-7792][Manager] Support export Excel template file of StreamSource

Posted by "dockerzhang (via GitHub)" <gi...@apache.org>.
dockerzhang merged PR #7793:
URL: https://github.com/apache/inlong/pull/7793


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


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

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7793:
URL: https://github.com/apache/inlong/pull/7793#discussion_r1160511841


##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelCellDataTransfer.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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 org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+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 cellValue) {
+            if (cellValue == null) {
+                return null;
+            } else if (cellValue instanceof Date) {
+                Date date = (Date) cellValue;
+                return this.simpleDateFormat.format(date);
+            } else {
+                return String.valueOf(cellValue);
+            }
+        }
+
+        Object parseFromText(String cellValue) {
+            if (cellValue != null && cellValue.length() > 0) {

Review Comment:
   Fixed



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


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

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7793:
URL: https://github.com/apache/inlong/pull/7793#discussion_r1161069764


##########
inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/InlongStreamController.java:
##########
@@ -183,4 +191,22 @@ public Response<List<StreamField>> parseFields(@RequestBody ParseFieldRequest pa
         return Response.success(streamService.parseFields(parseFieldRequest));
     }
 
+    @RequestMapping(value = "/stream/fieldsImportTemplate", method = RequestMethod.GET)
+    @ApiOperation(value = "Download fields import template")
+    public void downloadFieldsImportTemplate(HttpServletResponse response) {

Review Comment:
   When using a response to download files, HttpServletResponse is used to output the response's content, status code, and headers. I understand that the return value of this method has no effect. Instead, the file cannot be processed when the spring converters is called to output the content:
   
   No converter for [class org.apache.inlong.manager.pojo.common.Response] with preset Content-Type 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'



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


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

Posted by "fuweng11 (via GitHub)" <gi...@apache.org>.
fuweng11 commented on code in PR #7793:
URL: https://github.com/apache/inlong/pull/7793#discussion_r1160508780


##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelCellDataTransfer.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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 org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+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 cellValue) {
+            if (cellValue == null) {
+                return null;
+            } else if (cellValue instanceof Date) {
+                Date date = (Date) cellValue;
+                return this.simpleDateFormat.format(date);
+            } else {
+                return String.valueOf(cellValue);
+            }
+        }
+
+        Object parseFromText(String cellValue) {
+            if (cellValue != null && cellValue.length() > 0) {

Review Comment:
   Use StringUtils.isNotBlank



##########
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);
+        }
+        try (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);
+            // Fill content if data exist.
+            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.expectNotNull(clazz, "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 originValue = line.get(title);
+                                String value = originValue == null ? "" : originValue;
+                                Cell cell = row.createCell(colId);
+                                cell.setCellValue(value);
+                            });
+                        }));
+    }
+
+    private static void fillSheetHeader(XSSFRow row, List<String> heads) {
+        IntStream.range(0, heads.size()).forEach(index -> {
+            XSSFCell cell = row.createCell(index);
+            cell.setCellValue(new XSSFRichTextString(heads.get(index)));
+        });
+    }
+
+    /**
+     * Convert a list of objects to a list of maps, where each map represents an object's fields and values.
+     *
+     * @param objects The list of objects to be converted.
+     * @param <E>     The type of the objects to be converted.
+     * @return A list of maps, where each map represents an object's fields and values.
+     */
+    public static <E> List<Map<String, String>> write2List(List<E> objects) {
+        E e1 = objects.get(0);

Review Comment:
   Why is it named e1



##########
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);
+        }
+        try (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);
+            // Fill content if data exist.
+            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.expectNotNull(clazz, "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 originValue = line.get(title);
+                                String value = originValue == null ? "" : originValue;

Review Comment:
   Use StringUtils.isNotBlank.



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


[GitHub] [inlong] featzhang closed pull request #7793: [INLONG-7792][Manager] Support export Excel template file of StreamSource

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang closed pull request #7793: [INLONG-7792][Manager] Support export Excel template file of StreamSource
URL: https://github.com/apache/inlong/pull/7793


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


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

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7793:
URL: https://github.com/apache/inlong/pull/7793#discussion_r1160445158


##########
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:
   Fixed



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


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

Posted by "fuweng11 (via GitHub)" <gi...@apache.org>.
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


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

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7793:
URL: https://github.com/apache/inlong/pull/7793#discussion_r1160445273


##########
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:
   Fixed



##########
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:
   Fixed



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


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

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7793:
URL: https://github.com/apache/inlong/pull/7793#discussion_r1160445471


##########
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:
   Fixed



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


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

Posted by "healchow (via GitHub)" <gi...@apache.org>.
healchow commented on code in PR #7793:
URL: https://github.com/apache/inlong/pull/7793#discussion_r1160579045


##########
inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/InlongStreamController.java:
##########
@@ -183,4 +191,22 @@ public Response<List<StreamField>> parseFields(@RequestBody ParseFieldRequest pa
         return Response.success(streamService.parseFields(parseFieldRequest));
     }
 
+    @RequestMapping(value = "/stream/fieldsImportTemplate", method = RequestMethod.GET)
+    @ApiOperation(value = "Download fields import template")
+    public void downloadFieldsImportTemplate(HttpServletResponse response) {

Review Comment:
   Suggest return true of false for the API method, you can wrap it with `Response`.



##########
inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/InlongStreamController.java:
##########
@@ -183,4 +191,22 @@ public Response<List<StreamField>> parseFields(@RequestBody ParseFieldRequest pa
         return Response.success(streamService.parseFields(parseFieldRequest));
     }
 
+    @RequestMapping(value = "/stream/fieldsImportTemplate", method = RequestMethod.GET)
+    @ApiOperation(value = "Download fields import template")
+    public void downloadFieldsImportTemplate(HttpServletResponse response) {
+        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss"));
+        String fileName = String.format("InLong-stream-fields-template-%s.xlsx", date);
+        response.setHeader("Content-Disposition",
+                "attachment;filename=" + fileName);
+        response.setContentType("multipart/form-data");
+
+        try {
+            ServletOutputStream outputStream = response.getOutputStream();
+            ExcelTool.write(StreamField.class, outputStream);
+
+        } catch (IOException e) {
+            log.error("Can not properly download Excel file", e);

Review Comment:
   If error, please throw an `BusinessException`.



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