Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.dinky.service.catalogue.context;

import cn.hutool.core.collection.CollectionUtil;
import org.dinky.data.model.Catalogue;
import org.dinky.data.model.Task;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CatalogueTreeBuildContext {
private final Map<Integer, List<Catalogue>> childMap;
private final Map<Integer, Task> taskMap;

public CatalogueTreeBuildContext(Map<Integer, List<Catalogue>> childMap, Map<Integer, Task> taskMap) {
this.childMap = childMap;
this.taskMap = taskMap;
}

public List<Catalogue> getChildren(Integer parentId) {
List<Catalogue> children = childMap.get(parentId);
if (CollectionUtil.isEmpty(children)) {
return new ArrayList<>();
}
return new ArrayList<>(children);
}

public boolean hasChild(Integer parentId) {
List<Catalogue> children = childMap.get(parentId);
return CollectionUtil.isNotEmpty(children);
}

public Task getTask(Integer taskId) {
if (taskId == null) {
return null;
}
return taskMap.get(taskId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.dinky.service.MonitorService;
import org.dinky.service.TaskService;
import org.dinky.service.catalogue.CatalogueService;
import org.dinky.service.catalogue.context.CatalogueTreeBuildContext;
import org.dinky.service.catalogue.factory.CatalogueFactory;
import org.dinky.service.catalogue.factory.CatalogueTreeSortFactory;
import org.dinky.service.catalogue.strategy.CatalogueTreeSortStrategy;
Expand All @@ -70,6 +71,8 @@
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.LinkedHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -141,11 +144,21 @@ public List<Catalogue> buildCatalogueTree(List<Catalogue> catalogueList) {
.collect(Collectors.toList());
}
List<Task> taskList = taskService.list();

Map<Integer, List<Catalogue>> childMap = catalogueList.stream()
.collect(Collectors.groupingBy(
Catalogue::getParentId, LinkedHashMap::new, Collectors.toList()));
Map<Integer, Task> taskMap = taskList.stream()
.filter(task -> task.getId() != null)
.collect(Collectors.toMap(Task::getId, Function.identity(), (existing, replacement) -> existing));
CatalogueTreeBuildContext context = new CatalogueTreeBuildContext(childMap, taskMap);


List<Catalogue> returnList = new ArrayList<>();
for (Catalogue catalogue : catalogueList) {
// get all child catalogue of parent catalogue id , the 0 is root catalogue
if (catalogue.getParentId() == 0) {
recursionBuildCatalogueAndChildren(catalogueList, catalogue, taskList);
recursionBuildCatalogueAndChildren(catalogue, context);
returnList.add(catalogue);
}
}
Expand All @@ -161,22 +174,18 @@ public List<Catalogue> buildCatalogueTree(List<Catalogue> catalogueList) {
* @param list
* @param catalogues
*/
private void recursionBuildCatalogueAndChildren(List<Catalogue> list, Catalogue catalogues, List<Task> taskList) {
// 得到子节点列表
List<Catalogue> childList = getChildList(list, catalogues);
private void recursionBuildCatalogueAndChildren(Catalogue catalogues, CatalogueTreeBuildContext context) {
List<Catalogue> childList = context.getChildren(catalogues.getId());
catalogues.setChildren(childList);
for (Catalogue tChild : childList) {
if (hasChild(list, tChild)) {
// Determine whether there are child nodes
for (Catalogue children : childList) {
recursionBuildCatalogueAndChildren(list, children, taskList);
}
if (context.hasChild(tChild.getId())) {
recursionBuildCatalogueAndChildren(tChild, context);
} else {
if (tChild.getIsLeaf() || null != tChild.getTaskId()) {
taskList.stream()
.filter(t -> t.getId().equals(tChild.getTaskId()))
.findFirst()
.ifPresent(tChild::setTaskAndNote);
Task task = context.getTask(tChild.getTaskId());
if (task != null) {
tChild.setTaskAndNote(task);
}
}
}
}
Expand Down