|
| 1 | +package com.gitblit.models; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import com.gitblit.utils.StringUtils; |
| 9 | + |
| 10 | +public class TreeNodeModel implements Serializable, Comparable<TreeNodeModel> { |
| 11 | + |
| 12 | + private static final long serialVersionUID = 1L; |
| 13 | + final TreeNodeModel parent; |
| 14 | + final String name; |
| 15 | + final List<TreeNodeModel> subFolders = new ArrayList<>(); |
| 16 | + final List<RepositoryModel> repositories = new ArrayList<>(); |
| 17 | + |
| 18 | + /** |
| 19 | + * Create a new tree root |
| 20 | + */ |
| 21 | + public TreeNodeModel() { |
| 22 | + this.name = "/"; |
| 23 | + this.parent = null; |
| 24 | + } |
| 25 | + |
| 26 | + protected TreeNodeModel(String name, TreeNodeModel parent) { |
| 27 | + this.name = name; |
| 28 | + this.parent = parent; |
| 29 | + } |
| 30 | + |
| 31 | + public int getDepth() { |
| 32 | + if(parent == null) { |
| 33 | + return 0; |
| 34 | + }else { |
| 35 | + return parent.getDepth() +1; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Add a new sub folder to the current folder |
| 41 | + * |
| 42 | + * @param subFolder the subFolder to create |
| 43 | + * @return the newly created folder to allow chaining |
| 44 | + */ |
| 45 | + public TreeNodeModel add(String subFolder) { |
| 46 | + TreeNodeModel n = new TreeNodeModel(subFolder, this); |
| 47 | + subFolders.add(n); |
| 48 | + Collections.sort(subFolders); |
| 49 | + return n; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Add the given repo to the current folder |
| 54 | + * |
| 55 | + * @param repo |
| 56 | + */ |
| 57 | + public void add(RepositoryModel repo) { |
| 58 | + repositories.add(repo); |
| 59 | + Collections.sort(repositories); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Adds the given repository model within the given path. Creates parent folders if they do not exist |
| 64 | + * |
| 65 | + * @param path |
| 66 | + * @param model |
| 67 | + */ |
| 68 | + public void add(String path, RepositoryModel model) { |
| 69 | + TreeNodeModel folder = getSubTreeNode(this, path, true); |
| 70 | + folder.add(model); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String toString() { |
| 75 | + String string = name + "\n"; |
| 76 | + for(TreeNodeModel n : subFolders) { |
| 77 | + string += "f"; |
| 78 | + for(int i = 0; i < n.getDepth(); i++) { |
| 79 | + string += "-"; |
| 80 | + } |
| 81 | + string += n.toString(); |
| 82 | + } |
| 83 | + |
| 84 | + for(RepositoryModel n : repositories) { |
| 85 | + string += "r"; |
| 86 | + for(int i = 0; i < getDepth()+1; i++) { |
| 87 | + string += "-"; |
| 88 | + } |
| 89 | + string += n.toString() + "\n"; |
| 90 | + } |
| 91 | + |
| 92 | + return string; |
| 93 | + } |
| 94 | + |
| 95 | + public boolean containsSubFolder(String path) { |
| 96 | + return containsSubFolder(this, path); |
| 97 | + } |
| 98 | + |
| 99 | + public TreeNodeModel getSubFolder(String path) { |
| 100 | + return getSubTreeNode(this, path, false); |
| 101 | + } |
| 102 | + |
| 103 | + public List<Serializable> getTreeAsListForFrontend(){ |
| 104 | + List<Serializable> l = new ArrayList<>(); |
| 105 | + getTreeAsListForFrontend(l, this); |
| 106 | + return l; |
| 107 | + } |
| 108 | + |
| 109 | + private static void getTreeAsListForFrontend(List<Serializable> list, TreeNodeModel node) { |
| 110 | + list.add(node); |
| 111 | + for(TreeNodeModel t : node.subFolders) { |
| 112 | + getTreeAsListForFrontend(list, t); |
| 113 | + } |
| 114 | + for(RepositoryModel r : node.repositories) { |
| 115 | + list.add(r); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static TreeNodeModel getSubTreeNode(TreeNodeModel node, String path, boolean create) { |
| 120 | + if(!StringUtils.isEmpty(path)) { |
| 121 | + boolean isPathInCurrentHierarchyLevel = path.lastIndexOf('/') < 0; |
| 122 | + if(isPathInCurrentHierarchyLevel) { |
| 123 | + for(TreeNodeModel t : node.subFolders) { |
| 124 | + if(t.name.equals(path) ) { |
| 125 | + return t; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if(create) { |
| 130 | + node.add(path); |
| 131 | + return getSubTreeNode(node, path, true); |
| 132 | + } |
| 133 | + }else { |
| 134 | + //traverse into subFolder |
| 135 | + String folderInCurrentHierarchyLevel = StringUtils.getFirstPathElement(path); |
| 136 | + |
| 137 | + for(TreeNodeModel t : node.subFolders) { |
| 138 | + if(t.name.equals(folderInCurrentHierarchyLevel) ) { |
| 139 | + String folderInNextHierarchyLevel = path.substring(path.indexOf('/') + 1, path.length()); |
| 140 | + return getSubTreeNode(t, folderInNextHierarchyLevel, create); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if(create) { |
| 145 | + String folderInNextHierarchyLevel = path.substring(path.indexOf('/') + 1, path.length()); |
| 146 | + return getSubTreeNode(node.add(folderInCurrentHierarchyLevel), folderInNextHierarchyLevel, true); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return null; |
| 152 | + } |
| 153 | + |
| 154 | + private static boolean containsSubFolder(TreeNodeModel node, String path) { |
| 155 | + return getSubTreeNode(node, path, false) != null; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public int compareTo(TreeNodeModel t) { |
| 160 | + return StringUtils.compareRepositoryNames(name, t.name); |
| 161 | + } |
| 162 | + |
| 163 | + public TreeNodeModel getParent() { |
| 164 | + return parent; |
| 165 | + } |
| 166 | + |
| 167 | + public String getName() { |
| 168 | + return name; |
| 169 | + } |
| 170 | + |
| 171 | + public List<TreeNodeModel> getSubFolders() { |
| 172 | + return subFolders; |
| 173 | + } |
| 174 | + |
| 175 | + public List<RepositoryModel> getRepositories() { |
| 176 | + return repositories; |
| 177 | + } |
| 178 | +} |
0 commit comments