Skip to content

Commit

Permalink
Merge pull request #24 from little3201/develop
Browse files Browse the repository at this point in the history
优化TreeNode;Excel类型转换
  • Loading branch information
little3201 authored Apr 14, 2022
2 parents b8f7d99 + 5355caf commit 1383c58
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 91 deletions.
7 changes: 3 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
<parent>
<groupId>top.leafage</groupId>
<artifactId>leafage-starter-parent</artifactId>
<version>0.1.1</version>
<version>0.1.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<artifactId>leafage-common</artifactId>
<version>0.1.5</version>
<version>0.1.8</version>
<packaging>jar</packaging>

<name>leafage-common</name>
<description>common for leafage</description>

<properties>
<java.version>11</java.version>
<poi.version>5.0.0</poi.version>
<poi.version>5.2.2</poi.version>
</properties>

<dependencies>
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/top/leafage/common/ExcelReader.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package top.leafage.common;


import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.File;
Expand All @@ -26,17 +26,18 @@
* 读取excel文件,并解析为指定类型的对象
*
* @author liwenqiang 2021/8/26 9:37
* @since 0.1.4
*/
public class ExcelReader {

private ExcelReader() {
}

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

private static final String XLS = ".xls";
private static final String XLSX = ".xlsx";

private ExcelReader() {
}

/**
* 根据文件后缀名类型获取对应的工作簿对象
*
Expand Down Expand Up @@ -167,6 +168,9 @@ private static <T> T mapping(Row row, Class<T> clazz) {
private static <T> void writeData(T t, Cell cell, PropertyDescriptor descriptor) {
try {
switch (cell.getCellType()) {
case BLANK: // 空字符串
case _NONE: // null
break;
case NUMERIC: // 数字
descriptor.getWriteMethod().invoke(t, cell.getNumericCellValue());
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
import java.util.Collections;
import java.util.List;

/**
* Product random code
*
* @author liwenqiang 2020-10-06 22:09
* @since 0.1.0
*/
public abstract class AbstractBasicService {

private static final List<String> SEED_LIST = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,53 @@
package top.leafage.common.basic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.status.StatusLogger;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* Construct tree
*
* @author liwenqiang 2021-07-21 20:08
* @since 0.1.3
*/
public abstract class AbstractTreeNodeService<T> extends AbstractBasicService {

private static final String ID = "id";
private static final String NAME = "name";
private static final String CODE = "code";
private static final String SUPERIOR = "superior";

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

/**
* 构造 TreeNode 对象
*
* @param superiorCode superior code
* @param t 实例数据
* @param expand 扩展字段
* @return TreeNode 对象
* @since 0.1.7
*/
protected TreeNode construct(Object superiorCode, T t, Set<String> expand) {
Class<?> childClass = t.getClass();
Object code = this.getCode(t, childClass);
Object name = this.getName(t, childClass);

TreeNode treeNode = new TreeNode(Objects.nonNull(code) ? String.valueOf(code) : null,
Objects.nonNull(name) ? String.valueOf(name) : null);
treeNode.setSuperior(Objects.nonNull(superiorCode) ? String.valueOf(superiorCode) : null);

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

/**
* 扩展数据
Expand All @@ -26,15 +57,15 @@ public abstract class AbstractTreeNodeService<T> extends AbstractBasicService {
* @param t 数据实例
* @param expand 扩展字段
*/
protected void expand(TreeNode treeNode, Class<?> clazz, T t, Set<String> expand) {
private void expand(TreeNode treeNode, Class<?> clazz, T t, Set<String> expand) {
if (expand != null && !expand.isEmpty()) {
Map<String, String> map = new HashMap<>(expand.size());
Map<String, Object> map = new HashMap<>(expand.size());
expand.forEach(filed -> {
try {
PropertyDescriptor superIdDescriptor = new PropertyDescriptor(filed, clazz);
Object value = superIdDescriptor.getReadMethod().invoke(t);

map.put(filed, value != null ? value.toString() : null);
map.put(filed, value);
} catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
log.error("expand data error.", e);
}
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/top/leafage/common/basic/AbstractVO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2021. Leafage All Right Reserved.
*/
package top.leafage.common.basic;

import java.time.LocalDateTime;

/**
* Abstract vo
*
* @author liwenqiang 2020-10-06 22:09
* @since 0.1.6
*/
public abstract class AbstractVO<C> {

/**
* 代码
*/
private C code;

/**
* 修改时间
*/
private LocalDateTime modifyTime;

public C getCode() {
return code;
}

public void setCode(C code) {
this.code = code;
}

public LocalDateTime getModifyTime() {
return modifyTime;
}

public void setModifyTime(LocalDateTime modifyTime) {
this.modifyTime = modifyTime;
}
}
16 changes: 12 additions & 4 deletions src/main/java/top/leafage/common/basic/TreeNode.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package top.leafage.common.basic;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class TreeNode {
/**
* Tree node
*
* @author liwenqiang 2021-07-02 17:18
*/
public class TreeNode implements Serializable {

private static final long serialVersionUID = 3977470984616592112L;

/**
* 代码
Expand All @@ -20,7 +28,7 @@ public class TreeNode {
/**
* 扩展属性
*/
private Map<String, String> expand;
private Map<String, Object> expand;

private List<TreeNode> children;

Expand All @@ -41,7 +49,7 @@ public String getSuperior() {
return superior;
}

public Map<String, String> getExpand() {
public Map<String, Object> getExpand() {
return expand;
}

Expand All @@ -61,7 +69,7 @@ public void setSuperior(String superior) {
this.superior = superior;
}

public void setExpand(Map<String, String> expand) {
public void setExpand(Map<String, Object> expand) {
this.expand = expand;
}

Expand Down
59 changes: 59 additions & 0 deletions src/main/java/top/leafage/common/basic/ValidMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package top.leafage.common.basic;

import java.io.Serializable;

/**
* 参数校验信息
*
* @author liwenqiang 2022/4/13 17:13
* @since 0.1.8
**/
public class ValidMessage implements Serializable {

private static final long serialVersionUID = 3385684846039604779L;

/**
* code
*/
private static final String CODE = "code";

/**
* name
*/
private static final String NAME = "name";

/**
* username
*/
private static final String USERNAME = "username";

/**
* not blank
*/
private static final String NOT_BLANK = " must not be blank.";

/**
* non null
*/
private static final String NON_NULL = " must not be null.";

/**
* code must not be blank
*/
public static final String CODE_NOT_BLANK = CODE + NOT_BLANK;

/**
* code must not be null
*/
public static final String CODE_NOT_NULL = CODE + NON_NULL;

/**
* name must not be blank
*/
public static final String NAME_NOT_BLANK = NAME + NOT_BLANK;

/**
* username must not be blank
*/
public static final String USERNAME_NOT_BLANK = USERNAME + NOT_BLANK;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
import top.leafage.common.basic.TreeNode;
import java.util.Set;

/**
* For reactive to construct tree
*
* @author liwenqiang 2021-07-21 20:08
* @since 0.1.3
*/
public abstract class ReactiveAbstractTreeNodeService<T> extends AbstractTreeNodeService<T> {

/**
Expand All @@ -30,19 +36,10 @@ protected Flux<TreeNode> children(T superior, Flux<T> children, Set<String> expa
Class<?> aClass = superior.getClass();
// ID是集成基础父类的,所以要通过superClass获取
Object superiorId = this.getId(superior, aClass);
Object superiorName = this.getName(superior, aClass);
Object superiorCode = this.getCode(superior, aClass);

return children.filter(child -> this.check(superiorId, child)).flatMap(child -> {
Class<?> childClass = child.getClass();
Object code = this.getCode(child, childClass);
Object name = this.getName(child, childClass);

TreeNode treeNode = new TreeNode(code != null ? code.toString() : null,
name != null ? name.toString() : null);
treeNode.setSuperior(superiorName != null ? superiorName.toString() : null);

// deal expand
this.expand(treeNode, childClass, child, expand);
TreeNode treeNode = this.construct(superiorCode, child, expand);

return this.children(child, children, expand).collectList().map(treeNodes -> {
treeNode.setChildren(treeNodes);
Expand Down
Loading

0 comments on commit 1383c58

Please sign in to comment.