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