1 package org.apache.maven.index.treeview;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 /**
27 * Base set of functionality for the TreeNode that all implementations will need.
28 */
29 public abstract class AbstractTreeNode
30 implements TreeNode
31 {
32 /**
33 * The type of node.
34 */
35 private String type;
36
37 /**
38 * Flag that determines if the node is a leaf.
39 */
40 private boolean leaf;
41
42 /**
43 * The name of the node.
44 */
45 private String nodeName;
46
47 /**
48 * The path of the node.
49 */
50 private String path;
51
52 /**
53 * The children of this node.
54 */
55 private List<TreeNode> children;
56
57 /**
58 * The group id of this node.
59 */
60 private String groupId;
61
62 /**
63 * The artifact id of this node.
64 */
65 private String artifactId;
66
67 /**
68 * The version of this node.
69 */
70 private String version;
71
72 /**
73 * The repository id that this node is stored in.
74 */
75 private String repositoryId;
76
77 private final transient IndexTreeView treeView;
78
79 private final transient TreeViewRequest request;
80
81 /**
82 * Constructor that takes an IndexTreeView implementation and a TreeNodeFactory implementation;
83 *
84 * @param tview
85 * @param request
86 */
87 public AbstractTreeNode( IndexTreeView tview, TreeViewRequest request )
88 {
89 this.treeView = tview;
90
91 this.request = request;
92 }
93
94 /**
95 * Get the type of node.
96 *
97 * @return Type
98 */
99 public Type getType()
100 {
101 return Type.valueOf( type );
102 }
103
104 /**
105 * Set the type of node.
106 *
107 * @param type
108 */
109 public void setType( Type type )
110 {
111 this.type = type.name();
112 }
113
114 /**
115 * Get flag that determines if the node is a leaf.
116 *
117 * @return boolean
118 */
119 public boolean isLeaf()
120 {
121 return leaf;
122 }
123
124 /**
125 * Set flag that determines if the node is a leaf.
126 *
127 * @param leaf
128 */
129 public void setLeaf( boolean leaf )
130 {
131 this.leaf = leaf;
132 }
133
134 /**
135 * Get the name of the node.
136 *
137 * @return String
138 */
139 public String getNodeName()
140 {
141 return nodeName;
142 }
143
144 /**
145 * Set the name of the node.
146 *
147 * @param nodeName
148 */
149 public void setNodeName( String nodeName )
150 {
151 this.nodeName = nodeName;
152 }
153
154 /**
155 * Get the path of the node.
156 *
157 * @return String
158 */
159 public String getPath()
160 {
161 return path;
162 }
163
164 /**
165 * Set the path of the node.
166 *
167 * @param path
168 */
169 public void setPath( String path )
170 {
171 this.path = path;
172 }
173
174 /**
175 * Get the group id of this node.
176 *
177 * @return String
178 */
179 public String getGroupId()
180 {
181 return groupId;
182 }
183
184 /**
185 * Set the group id of this node.
186 *
187 * @param groupId
188 */
189 public void setGroupId( String groupId )
190 {
191 this.groupId = groupId;
192 }
193
194 /**
195 * Get the artifact id of this node.
196 *
197 * @return String
198 */
199 public String getArtifactId()
200 {
201 return artifactId;
202 }
203
204 /**
205 * Set the artifact id of this node.
206 *
207 * @param artifactId
208 */
209 public void setArtifactId( String artifactId )
210 {
211 this.artifactId = artifactId;
212 }
213
214 /**
215 * Get the version of this node.
216 *
217 * @return String
218 */
219 public String getVersion()
220 {
221 return version;
222 }
223
224 /**
225 * Set the version of this node.
226 *
227 * @param version
228 */
229 public void setVersion( String version )
230 {
231 this.version = version;
232 }
233
234 /**
235 * Get the repository id that this node is stored in.
236 *
237 * @return String
238 */
239 public String getRepositoryId()
240 {
241 return repositoryId;
242 }
243
244 /**
245 * Set the repository id that this node is stored in.
246 *
247 * @param repositoryId
248 */
249 public void setRepositoryId( String repositoryId )
250 {
251 this.repositoryId = repositoryId;
252 }
253
254 /**
255 * Get the children of this node. If this is a leaf node, null will be returned. This will NOT perform any actions
256 * on the index to retrieve the children, will only return children that have already been loaded via the
257 * listChildren method.
258 *
259 * @return List<TreeNode>
260 */
261 public List<TreeNode> getChildren()
262 {
263 if ( children == null && !isLeaf() )
264 {
265 children = new ArrayList<>();
266 }
267
268 return children;
269 }
270
271 /**
272 * Get the children of this node. If this is a leaf node, null will be returned. This will use the index to retrieve
273 * the list of child nodes.
274 *
275 * @return List<TreeNode>
276 */
277 public List<TreeNode> listChildren()
278 throws IOException
279 {
280 if ( !isLeaf() && getChildren().isEmpty() && !isLeaf() )
281 {
282 children =
283 treeView.listNodes(
284 new TreeViewRequest( request.getFactory(), getPath(), request.getFieldHints(),
285 request.getArtifactInfoFilter(), request.getIndexingContext() ) ).getChildren();
286 }
287
288 return children;
289 }
290
291 /**
292 * Find a TreeNode based upon a path and Type check.
293 *
294 * @return TreeNode
295 */
296 public TreeNode findChildByPath( String path, Type type )
297 throws IOException
298 {
299 for ( TreeNode child : getChildren() )
300 {
301 if ( path.equals( child.getPath() ) && type.equals( child.getType() ) )
302 {
303 return child;
304 }
305 }
306
307 return null;
308 }
309 }