Skip to content

Commit

Permalink
Merge pull request #29 from little3201/develop
Browse files Browse the repository at this point in the history
ExcelReader重写
  • Loading branch information
little3201 authored Sep 25, 2024
2 parents 154c6bf + 34196c7 commit d864658
Show file tree
Hide file tree
Showing 11 changed files with 460 additions and 395 deletions.
48 changes: 30 additions & 18 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Java CI with Maven

name: SonarCloud
on:
push:
branches: [ master ]
branches:
- main
pull_request:
branches: [ master ]

types: [opened, synchronize, reopened]
jobs:
build:

name: Build and analyze
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Build with Maven
run: mvn -B package --file pom.xml
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'zulu' # Alternative distribution options are available.
- name: Cache SonarCloud packages
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=little3201_leafage-common
22 changes: 16 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>top.leafage</groupId>
<artifactId>leafage-starter-parent</artifactId>
<version>0.3.1</version>
<version>0.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand All @@ -34,20 +34,30 @@
<description>common for leafage</description>

<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<!-- reactor -->
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
Expand Down
122 changes: 57 additions & 65 deletions src/main/java/top/leafage/common/AbstractTreeNodeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,142 +27,134 @@
import java.util.stream.Collectors;

/**
* Constructs a tree node service.
* Abstract service for constructing tree nodes from objects.
*
* @author liwenqiang 2021-07-21 20:08
* @param <T> the type of object representing a node
* @author wq li
* @since 0.1.3
*/
public abstract class AbstractTreeNodeService<T> {

private static final String ID = "id";
// field suffix, like groupName, roleName suffix is Name
private static final String NAME = "name";
private static final String SUPERIOR_ID = "superiorId";

private static final Logger log = StatusLogger.getLogger();

/**
* Constructs a tree node.
* Constructs a tree node from an object, optionally expanding additional properties.
*
* @param t Object
* @param expand Set of properties to expand
* @return TreeNode object
* @param t the object representing the node
* @param expand a set of property names to expand on the node
* @return a constructed TreeNode
* @since 0.2.0
*/
protected TreeNode node(T t, Set<String> expand) {
Class<?> aClass = t.getClass();
// id from superior class
Object id = this.getId(t, aClass.getSuperclass().getSuperclass());
// name form class
Object id = this.getId(t, aClass.getSuperclass());
Object name = this.getName(t, aClass);
// superiorId from class
Object superiorId = this.getSuperiorId(t, aClass);

TreeNode treeNode = new TreeNode(Objects.nonNull(id) ? (Long) id : null,
Objects.nonNull(name) ? String.valueOf(name) : null);
// set superior
treeNode.setSuperior(Objects.nonNull(superiorId) ? (Long) superiorId : null);

// deal expand
this.expand(treeNode, aClass, t, expand);
return treeNode;
}

/**
* Gets and sets children.
* Sets the children for tree nodes based on their superior IDs.
*
* @param treeNodes List of children
* @return List of TreeNode objects
* @param treeNodes the list of tree nodes
* @return a list of root nodes (nodes without a superior)
* @since 0.2.0
*/
protected List<TreeNode> children(List<TreeNode> treeNodes) {
Map<Long, List<TreeNode>> listMap = treeNodes.stream().filter(node -> Objects.nonNull(node.getSuperior()) &&
0 == node.getSuperior())
Map<Long, List<TreeNode>> nodesMap = treeNodes.stream()
.filter(node -> Objects.nonNull(node.getSuperior()) && node.getSuperior() != 0)
.collect(Collectors.groupingBy(TreeNode::getSuperior));
// get children from grouped map
treeNodes.forEach(node -> node.setChildren(listMap.get(node.getId())));
return treeNodes.stream().filter(node -> Objects.isNull(node.getSuperior()) || 0 == node.getSuperior())

treeNodes.forEach(node -> node.setChildren(nodesMap.get(node.getId())));

return treeNodes.stream()
.filter(node -> Objects.isNull(node.getSuperior()) || node.getSuperior() == 0)
.collect(Collectors.toList());
}

/**
* Expand data.
* Expands additional properties for a TreeNode.
*
* @param treeNode TreeNode object
* @param clazz Class type
* @param t Object
* @param expand Set of properties to expand
* @param treeNode the TreeNode to expand
* @param clazz the class of the object
* @param t the object representing the node
* @param expand a set of property names to expand
*/
private void expand(TreeNode treeNode, Class<?> clazz, T t, Set<String> expand) {
if (expand != null && !expand.isEmpty()) {
Map<String, Object> map = new HashMap<>(expand.size());
expand.forEach(field -> {
try {
PropertyDescriptor superIdDescriptor = new PropertyDescriptor(field, clazz);
Object value = superIdDescriptor.getReadMethod().invoke(t);

map.put(field, value);
} catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
log.error("expand data error.", e);
Map<String, Object> expandedData = new HashMap<>(expand.size());
try {
for (String field : expand) {
PropertyDescriptor descriptor = new PropertyDescriptor(field, clazz);
Object value = descriptor.getReadMethod().invoke(t);
expandedData.put(field, value);
}
});
treeNode.setExpand(map);
} catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
log.error("Error expanding data for TreeNode.", e);
}
treeNode.setExpand(expandedData);
}
}

/**
* Gets the ID.
* Retrieves the ID from the object.
*
* @param obj Object instance
* @param clazz Class type
* @return ID value
* @param obj the object instance
* @param clazz the class of the object
* @return the ID value, or null if an error occurs
*/
protected Object getId(Object obj, Class<?> clazz) {
Object id = null;
private Object getId(Object obj, Class<?> clazz) {
try {
PropertyDescriptor idDescriptor = new PropertyDescriptor(ID, clazz);
id = idDescriptor.getReadMethod().invoke(obj);
return idDescriptor.getReadMethod().invoke(obj);
} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
log.error("get id error.", e);
log.error("Error retrieving ID.", e);
return null;
}
return id;
}

/**
* Gets the name.
* Retrieves the name from the object.
*
* @param t Object
* @param clazz Class type
* @return Name value
* @param t the object instance
* @param clazz the class of the object
* @return the name value, or null if an error occurs
*/
protected Object getName(T t, Class<?> clazz) {
Object name = null;
private Object getName(T t, Class<?> clazz) {
try {
// name
PropertyDescriptor nameDescriptor = new PropertyDescriptor(NAME, clazz);
name = nameDescriptor.getReadMethod().invoke(t);
return nameDescriptor.getReadMethod().invoke(t);
} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
log.error("get name error.", e);
log.error("Error retrieving name.", e);
return null;
}
return name;
}

/**
* Gets the superior ID.
* Retrieves the superior ID from the object.
*
* @param t Object
* @param clazz Class type
* @return Superior ID value
* @param t the object instance
* @param clazz the class of the object
* @return the superior ID value, or null if an error occurs
*/
private Object getSuperiorId(T t, Class<?> clazz) {
Object superiorId = null;
try {
PropertyDescriptor superiorIdDescriptor = new PropertyDescriptor(SUPERIOR_ID, clazz);
superiorId = superiorIdDescriptor.getReadMethod().invoke(t);

return superiorIdDescriptor.getReadMethod().invoke(t);
} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
log.error("get superiorId error.", e);
log.error("Error retrieving superior ID.", e);
return null;
}
return superiorId;
}
}

Loading

0 comments on commit d864658

Please sign in to comment.