bengbengbalabalabeng commented on issue #854:
URL: https://github.com/apache/fesod/issues/854#issuecomment-3883508824
Hi, I have a design proposal for export by grouping support.
### Proposal
This proposal aims to introduce a Java Type-based grouping feature for data
export.
**Use Case:**
This addresses the issue of proliferating redundant VO classes caused by
varying field filtering logic across different export scenarios. By
implementing a grouping mechanism, a single DTO model can flexibly adapt to
multiple business export templates.
**Advantages:**
- Avoids defining groups with "Magic Strings". It leverages the Java
compiler to prevent typos, similar to the `groups` attribute in Bean Validation
annotations.
- Utilizes Interface Inheritance to establish nested and inclusive
relationships between groups.
- Reduce maintenance of redundant VO classes.
### API Usage Example
#### 1. Basic Grouping
Define simple **Marker Interfaces** to filter fields for different scenarios.
- Define Group Interfaces and Entity
```java
public interface SchoolGroup {}
public interface StudentGroup {}
@Data
public class SchoolWholeStatData {
@ExcelProperty(value = "School Name", groups = SchoolGroup.class)
private String schoolName;
@ExcelProperty(value = "Student Name", groups = StudentGroup.class)
private String studentName;
// Fields without 'groups' are treated as common fields and always
exported.
@ExcelProperty(value = "Update Time")
private Date updateTime;
}
```
- Demo
```java
// Export only School-related fields + Common fields
FesodSheet.write(fileName, SchoolWholeStatData.class)
.group(SchoolGroup.class)
.sheet()
.doWrite(dataList);
```
#### 2. Hierarchical Grouping
Leverage interface inheritance to implement a "Base Group + Extended Group"
export pattern.
- Define Group Interfaces with Inheritance and Entity
```java
public interface BaseGroup {}
// DetailGroup includes all fields from BaseGroup
public interface DetailGroup extends BaseGroup {}
@Data
public class OrderData {
@ExcelProperty(value = "Order No", groups = BaseGroup.class)
private String orderNo;
@ExcelProperty(value = "Payment Transaction No", groups =
DetailGroup.class)
private String paymentNo;
}
```
- Demo
```java
// By passing DetailGroup.class, both orderNo (inherited) and paymentNo are
exported.
FesodSheet.write(fileName, OrderData.class)
.group(DetailGroup.class)
.sheet()
.doWrite(dataList);
```
#### 3. Multiple Groups Activation
Supports activating multiple grouping logics within a single export session.
- Demo
```java
// Export both School and Student fields simultaneously
FesodSheet.write(fileName, SchoolWholeStatData.class)
.group(SchoolGroup.class, StudentGroup.class)
.sheet()
.doWrite(dataList);
```
### More
1. Fields not annotated with `groups` are considered "Common Fields" and are
exported by default in all contexts.
2. This feature is fully compatible with Fesod's existing `@ExcelIgnore`,
`@ExcelIgnoreUnannotated`, and `includeColumn*`/`excludeColumn*` logic.
Filtering Priority:
`@ExcelIgnore, @ExcelIgnoreUnannotated` >
`@ExcelProperty(groups = Class<?>[])` >
`includeColumn* / excludeColumn*`
---
Feel free to let me know if you have any suggestions. :)
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]