diff --git a/pom.xml b/pom.xml index 5266ee6..4b46cd0 100644 --- a/pom.xml +++ b/pom.xml @@ -1,23 +1,40 @@ + + 4.0.0 top.leafage leafage-starter-parent - 0.1.6 + 0.2.0 leafage-common - 0.1.9 + 0.2.0 jar leafage-common common for leafage - 5.2.2 + 5.2.3 diff --git a/src/main/java/top/leafage/common/basic/AbstractBasicService.java b/src/main/java/top/leafage/common/AbstractBasicService.java similarity index 69% rename from src/main/java/top/leafage/common/basic/AbstractBasicService.java rename to src/main/java/top/leafage/common/AbstractBasicService.java index 9c1e262..b214fd7 100644 --- a/src/main/java/top/leafage/common/basic/AbstractBasicService.java +++ b/src/main/java/top/leafage/common/AbstractBasicService.java @@ -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; diff --git a/src/main/java/top/leafage/common/basic/AbstractTreeNodeService.java b/src/main/java/top/leafage/common/AbstractTreeNodeService.java similarity index 64% rename from src/main/java/top/leafage/common/basic/AbstractTreeNodeService.java rename to src/main/java/top/leafage/common/AbstractTreeNodeService.java index 8a3e8f3..0a47a6a 100644 --- a/src/main/java/top/leafage/common/basic/AbstractTreeNodeService.java +++ b/src/main/java/top/leafage/common/AbstractTreeNodeService.java @@ -1,5 +1,21 @@ -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; @@ -7,10 +23,8 @@ 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 @@ -20,7 +34,6 @@ */ public abstract class AbstractTreeNodeService 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"; @@ -33,9 +46,9 @@ public abstract class AbstractTreeNodeService extends AbstractBasicService { * @param t 实例数据 * @param expand 扩展字段 * @return TreeNode 对象 - * @since 0.1.7 + * @since 0.2.0 */ - protected TreeNode construct(T t, Set expand) { + protected TreeNode node(T t, Set expand) { Class childClass = t.getClass(); Object code = this.getCode(t, childClass); Object name = this.getName(t, childClass); @@ -50,6 +63,23 @@ protected TreeNode construct(T t, Set expand) { return treeNode; } + /** + * 转换并设置 TreeNode + * + * @param treeNodes TreeNode 对象 + * @return TreeNode 对象集合 + * @since 0.2.0 + */ + protected List nodes(List treeNodes) { + Map> 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()); + } + /** * 扩展数据 * @@ -75,25 +105,6 @@ private void expand(TreeNode treeNode, Class clazz, T t, Set 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 * @@ -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); } @@ -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); - } } diff --git a/src/main/java/top/leafage/common/ExcelReader.java b/src/main/java/top/leafage/common/ExcelReader.java index c2dd0bf..9994b25 100644 --- a/src/main/java/top/leafage/common/ExcelReader.java +++ b/src/main/java/top/leafage/common/ExcelReader.java @@ -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; @@ -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; @@ -150,7 +167,7 @@ private static T mapping(Row row, Class clazz) { writeData(t, cell, descriptor); } } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | - IntrospectionException e) { + IntrospectionException e) { log.error("对象映射异常!", e); return null; } diff --git a/src/main/java/top/leafage/common/basic/TreeNode.java b/src/main/java/top/leafage/common/TreeNode.java similarity index 75% rename from src/main/java/top/leafage/common/basic/TreeNode.java rename to src/main/java/top/leafage/common/TreeNode.java index 8ba9817..d2206a6 100644 --- a/src/main/java/top/leafage/common/basic/TreeNode.java +++ b/src/main/java/top/leafage/common/TreeNode.java @@ -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; @@ -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 { /** * 代码 diff --git a/src/main/java/top/leafage/common/basic/ValidMessage.java b/src/main/java/top/leafage/common/ValidMessage.java similarity index 58% rename from src/main/java/top/leafage/common/basic/ValidMessage.java rename to src/main/java/top/leafage/common/ValidMessage.java index d840c08..dcce0b1 100644 --- a/src/main/java/top/leafage/common/basic/ValidMessage.java +++ b/src/main/java/top/leafage/common/ValidMessage.java @@ -1,7 +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. + * + */ -import java.io.Serial; -import java.io.Serializable; +package top.leafage.common; /** * 参数校验信息 @@ -9,10 +23,7 @@ * @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 @@ -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; } diff --git a/src/main/java/top/leafage/common/basic/AbstractVO.java b/src/main/java/top/leafage/common/basic/AbstractVO.java deleted file mode 100644 index 5aadb68..0000000 --- a/src/main/java/top/leafage/common/basic/AbstractVO.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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 extends BasicVO { - - /** - * 修改时间 - */ - private LocalDateTime modifyTime; - - /** - * modify time getter - * - * @return modify time - */ - public LocalDateTime getModifyTime() { - return modifyTime; - } - - /** - * modify time setter - * - * @param modifyTime modify time - */ - public void setModifyTime(LocalDateTime modifyTime) { - this.modifyTime = modifyTime; - } - -} diff --git a/src/main/java/top/leafage/common/basic/BasicVO.java b/src/main/java/top/leafage/common/basic/BasicVO.java deleted file mode 100644 index 846a802..0000000 --- a/src/main/java/top/leafage/common/basic/BasicVO.java +++ /dev/null @@ -1,57 +0,0 @@ -package top.leafage.common.basic; - -/** - * 基础VO类 - * - * @author liwenqiang 2022/7/23 10:04 - * @since 0.1.9 - **/ -public class BasicVO { - - - /** - * 代码 - */ - private C code; - - /** - * 名称 - */ - private String name; - - /** - * code getter - * - * @return C - */ - public C getCode() { - return code; - } - - /** - * code setter - * - * @param code code of model - */ - public void setCode(C code) { - this.code = code; - } - - /** - * name getter - * - * @return name - */ - public String getName() { - return name; - } - - /** - * name setter - * - * @param name name of model - */ - public void setName(String name) { - this.name = name; - } -} diff --git a/src/main/java/top/leafage/common/package-info.java b/src/main/java/top/leafage/common/package-info.java deleted file mode 100644 index e53b4aa..0000000 --- a/src/main/java/top/leafage/common/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -/** - * common interfaces or methods with webmvc and webflux - * - * @author liwenqiang 2019/10/23 9:51 - **/ -package top.leafage.common; \ No newline at end of file diff --git a/src/main/java/top/leafage/common/reactive/ReactiveAbstractTreeNodeService.java b/src/main/java/top/leafage/common/reactive/ReactiveAbstractTreeNodeService.java index 610b6e3..b457882 100644 --- a/src/main/java/top/leafage/common/reactive/ReactiveAbstractTreeNodeService.java +++ b/src/main/java/top/leafage/common/reactive/ReactiveAbstractTreeNodeService.java @@ -1,15 +1,29 @@ +/* + * 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.reactive; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import top.leafage.common.basic.AbstractTreeNodeService; -import top.leafage.common.basic.TreeNode; +import top.leafage.common.AbstractTreeNodeService; +import top.leafage.common.TreeNode; import java.util.List; -import java.util.Map; -import java.util.Objects; import java.util.Set; -import java.util.stream.Collectors; /** * For reactive to construct tree @@ -24,9 +38,9 @@ public abstract class ReactiveAbstractTreeNodeService extends AbstractTreeNod * * @param children 子节点 * @return 树节点数据集 - * @since 0.1.9 + * @since 0.2.0 */ - protected Flux convert(Flux children) { + protected Mono> convert(Flux children) { return this.convert(children, null); } @@ -36,18 +50,12 @@ protected Flux convert(Flux children) { * @param children 子节点 * @param expand 扩展属性 * @return 树节点数据集 - * @since 0.1.9 + * @since 0.2.0 */ - protected Flux convert(Flux children, Set expand) { - Flux nodesFlux = children.map(child -> this.construct(child, expand)); - Mono>> mapMono = nodesFlux.filter(node -> Objects.nonNull(node.getSuperior()) && - !"0".equals(node.getSuperior())) - .collect(Collectors.groupingBy(TreeNode::getSuperior)); - - return nodesFlux.zipWith(mapMono, (node, map) -> { - node.setChildren(map.get(node.getCode())); - return node; - }).filter(node -> Objects.isNull(node.getSuperior()) || "0".equals(node.getSuperior())); + protected Mono> convert(Flux children, Set expand) { + Flux nodesFlux = children.map(child -> this.node(child, expand)); + + return nodesFlux.collectList().map(this::nodes); } } diff --git a/src/main/java/top/leafage/common/reactive/ReactiveBasicService.java b/src/main/java/top/leafage/common/reactive/ReactiveBasicService.java index 35cde22..b92113a 100644 --- a/src/main/java/top/leafage/common/reactive/ReactiveBasicService.java +++ b/src/main/java/top/leafage/common/reactive/ReactiveBasicService.java @@ -1,7 +1,25 @@ +/* + * 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.reactive; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; + import java.util.List; /** diff --git a/src/main/java/top/leafage/common/servlet/ServletAbstractTreeNodeService.java b/src/main/java/top/leafage/common/servlet/ServletAbstractTreeNodeService.java index 8f5f5c5..b3b4918 100644 --- a/src/main/java/top/leafage/common/servlet/ServletAbstractTreeNodeService.java +++ b/src/main/java/top/leafage/common/servlet/ServletAbstractTreeNodeService.java @@ -1,14 +1,27 @@ +/* + * 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.servlet; -import top.leafage.common.basic.AbstractTreeNodeService; -import top.leafage.common.basic.TreeNode; +import top.leafage.common.AbstractTreeNodeService; +import top.leafage.common.TreeNode; import java.util.List; -import java.util.Map; -import java.util.Objects; import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; /** * For servlet to construct tree @@ -23,7 +36,7 @@ public abstract class ServletAbstractTreeNodeService extends AbstractTreeNode * * @param children 子节点 * @return 树节点数据集 - * @since 0.1.9 + * @since 0.2.0 */ protected List convert(List children) { return this.convert(children, null); @@ -35,17 +48,11 @@ protected List convert(List children) { * @param children 子节点 * @param expand 扩展属性 * @return 树节点数据集 - * @since 0.1.9 + * @since 0.2.0 */ protected List convert(List children, Set expand) { - Stream stream = children.stream().map(child -> this.construct(child, expand)); - Map> listMap = stream.filter(node -> Objects.nonNull(node.getSuperior()) && - !"0".equals(node.getSuperior())) - .collect(Collectors.groupingBy(TreeNode::getSuperior)); - - stream.forEach(node -> node.setChildren(listMap.get(node.getCode()))); - return stream.filter(node -> Objects.isNull(node.getSuperior()) || "0".equals(node.getSuperior())) - .collect(Collectors.toList()); + List treeNodes = children.stream().map(child -> this.node(child, expand)).toList(); + return this.nodes(treeNodes); } } diff --git a/src/main/java/top/leafage/common/servlet/ServletBasicService.java b/src/main/java/top/leafage/common/servlet/ServletBasicService.java index 8b7d92f..d3c9f5b 100644 --- a/src/main/java/top/leafage/common/servlet/ServletBasicService.java +++ b/src/main/java/top/leafage/common/servlet/ServletBasicService.java @@ -1,6 +1,20 @@ /* - * Copyright (c) 2019. Abeille All Right Reserved. + * 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.servlet; import java.util.Collections; @@ -87,7 +101,7 @@ default V modify(C code, D d) { * 批量保存 * * @param dList 实例集合 - * @return an array containing the elements of this list + * @return an list containing the elements of this list */ default List saveAll(List dList) { return Collections.emptyList();