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