Destiny.Lin
5 months ago
4 changed files with 207 additions and 1 deletions
@ -0,0 +1,151 @@ |
|||||||
|
package com.fr.file; |
||||||
|
|
||||||
|
import com.fr.decision.webservice.bean.entry.FileNodeBean; |
||||||
|
import com.fr.file.filetree.FileNode; |
||||||
|
import com.fr.general.ComparatorUtils; |
||||||
|
import com.fr.log.FineLoggerFactory; |
||||||
|
import com.fr.stable.CoreConstants; |
||||||
|
import com.fr.stable.StableUtils; |
||||||
|
import com.fr.stable.StringUtils; |
||||||
|
import com.fr.workspace.server.repository.tabledata.TableDataRepository; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* CA证书文件相关 |
||||||
|
* |
||||||
|
* @author Destiny.Lin |
||||||
|
* @since 11.0 |
||||||
|
* Created on 2024/9/8 |
||||||
|
*/ |
||||||
|
public class FileCAFILE extends FileNodeFILE{ |
||||||
|
private List<FileCAFILE> children = new ArrayList<>(); |
||||||
|
private boolean builded = false; |
||||||
|
private String id = StringUtils.EMPTY; |
||||||
|
|
||||||
|
public FileCAFILE(FileNode node) { |
||||||
|
super(node); |
||||||
|
} |
||||||
|
|
||||||
|
public FileCAFILE(FileNode node, boolean builded, String id) { |
||||||
|
super(node); |
||||||
|
this.builded = builded; |
||||||
|
this.id = id; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 添加子 |
||||||
|
*/ |
||||||
|
private void addChild(FileCAFILE file) { |
||||||
|
children.add(file); |
||||||
|
} |
||||||
|
|
||||||
|
public List<FileCAFILE> getChildren() { |
||||||
|
return children; |
||||||
|
} |
||||||
|
|
||||||
|
public void setChildren(List<FileCAFILE> children) { |
||||||
|
this.children = children; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isBuilded() { |
||||||
|
return builded; |
||||||
|
} |
||||||
|
|
||||||
|
public void setBuilded(boolean builded) { |
||||||
|
this.builded = builded; |
||||||
|
} |
||||||
|
|
||||||
|
public String getId() { |
||||||
|
return id; |
||||||
|
} |
||||||
|
|
||||||
|
public void setId(String id) { |
||||||
|
this.id = id; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public FILE[] listFiles() { |
||||||
|
|
||||||
|
if (ComparatorUtils.equals(node, null)) { |
||||||
|
node = new FileNode(CoreConstants.SEPARATOR, true); |
||||||
|
} |
||||||
|
if (!node.isDirectory()) { |
||||||
|
return new FILE[]{this}; |
||||||
|
} |
||||||
|
try { |
||||||
|
FileCAFILE root = null; |
||||||
|
if (!this.builded) { |
||||||
|
root = buildTree(); |
||||||
|
FileCAFILE dataFILE = root.getFileCAFILE(this.id); |
||||||
|
this.setChildren(dataFILE.getChildren()); |
||||||
|
this.builded = true; |
||||||
|
} |
||||||
|
return this.getChildren().toArray(new FILE[0]); |
||||||
|
} catch (Exception e) { |
||||||
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||||
|
return new FILE[0]; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private List<FileNodeBean> getBeans() { |
||||||
|
try { |
||||||
|
return TableDataRepository.getInstance().getCertificatesFile(StringUtils.EMPTY); |
||||||
|
} catch (Exception e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 递归获取指定id的文件 |
||||||
|
*/ |
||||||
|
public FileCAFILE getFileCAFILE(String id) { |
||||||
|
if (StringUtils.equals(this.id, id) || StringUtils.equals(StableUtils.pathJoin(this.id, StringUtils.EMPTY), id)) { |
||||||
|
return this; |
||||||
|
} |
||||||
|
for (FileCAFILE file : getChildren()) { |
||||||
|
return file.getFileCAFILE(id); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
private FileCAFILE buildTree() { |
||||||
|
List<FileNodeBean> beans = getBeans(); |
||||||
|
Map<String, FileCAFILE> tree = new HashMap<>(); |
||||||
|
FileCAFILE root = null; |
||||||
|
// id , 父
|
||||||
|
for (FileNodeBean bean : beans) { |
||||||
|
FileNode fileNode = new FileNode(); |
||||||
|
fileNode.setEnvPath(bean.getPath()); |
||||||
|
fileNode.setDirectory(bean.getIsParent()); |
||||||
|
tree.putIfAbsent(bean.getId(), new FileCAFILE(fileNode, true, bean.getId())); |
||||||
|
if (StringUtils.isEmpty(bean.getpId())) { |
||||||
|
root = tree.get(bean.getId()); |
||||||
|
} else { |
||||||
|
tree.putIfAbsent(bean.getpId(), createParent(bean.getpId(), beans)); |
||||||
|
FileCAFILE file = tree.get(bean.getpId()); |
||||||
|
if (file != null) { |
||||||
|
file.addChild(tree.get(bean.getId())); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return root; |
||||||
|
} |
||||||
|
|
||||||
|
private FileCAFILE createParent(String s, List<FileNodeBean> beans) { |
||||||
|
for (FileNodeBean bean : beans) { |
||||||
|
if (StringUtils.equals(bean.getId(), s)) { |
||||||
|
FileNode fileNode = new FileNode(); |
||||||
|
fileNode.setEnvPath(bean.getPath()); |
||||||
|
fileNode.setDirectory(bean.getIsParent()); |
||||||
|
return new FileCAFILE(fileNode, true, bean.getId()); |
||||||
|
} |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue