Skip to content

Commit

Permalink
Merge pull request #26 from little3201/develop
Browse files Browse the repository at this point in the history
更新 convert()实现
  • Loading branch information
little3201 authored Jan 7, 2023
2 parents c8b7b97 + 241f9ba commit c2d5500
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 215 deletions.
23 changes: 20 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2018-2022 the original author or authors.
~
~ Licensed 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
~
~ https://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.
~
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.leafage</groupId>
<artifactId>leafage-starter-parent</artifactId>
<version>0.1.6</version>
<version>0.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<artifactId>leafage-common</artifactId>
<version>0.1.9</version>
<version>0.2.0</version>
<packaging>jar</packaging>

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

<properties>
<poi.version>5.2.2</poi.version>
<poi.version>5.2.3</poi.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
package top.leafage.common.basic;
/*
* Copyright 2018-2022 the original author or authors.
*
* Licensed 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
*
* https://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 top.leafage.common;

import java.security.SecureRandom;
import java.time.LocalDate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package top.leafage.common.basic;
/*
* Copyright 2018-2023 the original author or authors.
*
* Licensed 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
*
* https://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 top.leafage.common;

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;
import java.util.*;
import java.util.stream.Collectors;

/**
* Construct tree
Expand All @@ -20,7 +34,6 @@
*/
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";
Expand All @@ -33,9 +46,9 @@ public abstract class AbstractTreeNodeService<T> extends AbstractBasicService {
* @param t 实例数据
* @param expand 扩展字段
* @return TreeNode 对象
* @since 0.1.7
* @since 0.2.0
*/
protected TreeNode construct(T t, Set<String> expand) {
protected TreeNode node(T t, Set<String> expand) {
Class<?> childClass = t.getClass();
Object code = this.getCode(t, childClass);
Object name = this.getName(t, childClass);
Expand All @@ -50,6 +63,23 @@ protected TreeNode construct(T t, Set<String> expand) {
return treeNode;
}

/**
* 转换并设置 TreeNode
*
* @param treeNodes TreeNode 对象
* @return TreeNode 对象集合
* @since 0.2.0
*/
protected List<TreeNode> nodes(List<TreeNode> treeNodes) {
Map<String, List<TreeNode>> listMap = treeNodes.stream().filter(node -> Objects.nonNull(node.getSuperior()) &&
!"0".equals(node.getSuperior()))
.collect(Collectors.groupingBy(TreeNode::getSuperior));
// get children from grouped map
treeNodes.forEach(node -> node.setChildren(listMap.get(node.getCode())));
return treeNodes.stream().filter(node -> Objects.isNull(node.getSuperior()) || "0".equals(node.getSuperior()))
.collect(Collectors.toList());
}

/**
* 扩展数据
*
Expand All @@ -75,25 +105,6 @@ private void expand(TreeNode treeNode, Class<?> clazz, T t, Set<String> expand)
}
}

/**
* 获取ID
*
* @param t 对象
* @param clazz 类型
* @return ID
*/
protected Object getId(T t, Class<?> clazz) {
Object superiorId = null;
try {
// ID是集成基础父类的,所以要通过superClass获取
PropertyDescriptor superIdDescriptor = new PropertyDescriptor(ID, clazz.getSuperclass());
superiorId = superIdDescriptor.getReadMethod().invoke(t);
} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
log.error("get id error.", e);
}
return superiorId;
}

/**
* 获取name
*
Expand All @@ -115,15 +126,15 @@ protected Object getName(T t, Class<?> clazz) {
/**
* 获取code
*
* @param t 对象
* @param obj 实例
* @param clazz 类型
* @return code
*/
protected Object getCode(T t, Class<?> clazz) {
protected Object getCode(Object obj, Class<?> clazz) {
Object code = null;
try {
PropertyDescriptor superIdDescriptor = new PropertyDescriptor(CODE, clazz);
code = superIdDescriptor.getReadMethod().invoke(t);
code = superIdDescriptor.getReadMethod().invoke(obj);
} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
log.error("get code error.", e);
}
Expand All @@ -140,23 +151,16 @@ protected Object getCode(T t, Class<?> clazz) {
private Object getSuperior(T t, Class<?> clazz) {
Object superior = null;
try {
PropertyDescriptor superIdDescriptor = new PropertyDescriptor(SUPERIOR, clazz);
superior = superIdDescriptor.getReadMethod().invoke(t);
PropertyDescriptor superDescriptor = new PropertyDescriptor(SUPERIOR, clazz);
superior = superDescriptor.getReadMethod().invoke(t);
// superior code
if (Objects.nonNull(superior)) {
superior = this.getCode(superior, superior.getClass());
}

} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
log.error("get superior error.", e);
}
return superior;
}

/**
* 检查是否上下级节点
*
* @param superiorId 上级节点ID
* @param child 对象实例
* @return true-是,false-否
*/
protected boolean check(Object superiorId, T child) {
Object superior = this.getSuperior(child, child.getClass());
return superiorId.equals(superior);
}
}
21 changes: 19 additions & 2 deletions src/main/java/top/leafage/common/ExcelReader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package top.leafage.common;
/*
* Copyright 2018-2022 the original author or authors.
*
* Licensed 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
*
* https://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 top.leafage.common;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.status.StatusLogger;
Expand All @@ -9,6 +25,7 @@
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.File;
Expand Down Expand Up @@ -150,7 +167,7 @@ private static <T> T mapping(Row row, Class<T> clazz) {
writeData(t, cell, descriptor);
}
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException |
IntrospectionException e) {
IntrospectionException e) {
log.error("对象映射异常!", e);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package top.leafage.common.basic;
/*
* Copyright 2018-2022 the original author or authors.
*
* Licensed 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
*
* https://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 top.leafage.common;

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

Expand All @@ -10,10 +25,7 @@
*
* @author liwenqiang 2021-07-02 17:18
*/
public class TreeNode implements Serializable {

@Serial
private static final long serialVersionUID = 3977470984616592112L;
public class TreeNode {

/**
* 代码
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package top.leafage.common.basic;
/*
* Copyright 2018-2022 the original author or authors.
*
* Licensed 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
*
* https://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.
*
*/

import java.io.Serial;
import java.io.Serializable;
package top.leafage.common;

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

@Serial
private static final long serialVersionUID = 3385684846039604779L;
public abstract class ValidMessage {

/**
* code
Expand All @@ -33,29 +44,24 @@ public class ValidMessage implements Serializable {
* 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;
/**
* non null
*/
private static final String NON_NULL = " must not be null.";
/**
* code must not be null
*/
public static final String CODE_NOT_NULL = CODE + NON_NULL;
}
Loading

0 comments on commit c2d5500

Please sign in to comment.