1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package nl.hippo.portal.cms.site;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24
25 public class SiteMapItemImpl implements SiteMapItem {
26
27 private final Map childrenMap = new HashMap();
28 private final List childrenList = new ArrayList();
29
30 private final List children = Collections.unmodifiableList(childrenList);
31
32 private final String id;
33
34 private final String name;
35
36 private final SiteMapItem parent;
37
38 private final int level;
39
40 private final String path;
41 private final String[] paths;
42
43 private String title;
44
45 private String src;
46
47 private String template;
48
49 private boolean leaf;
50
51 private boolean hidden;
52
53 private boolean linkable;
54
55 private boolean menuItem;
56
57 private Map attributes;
58
59 private final SiteMap siteMap;
60
61 public SiteMapItemImpl(SiteMap siteMap) {
62 this.id = null;
63 this.name = null;
64 this.title = null;
65 this.src = null;
66 this.template = null;
67 this.menuItem = false;
68 this.parent = null;
69 this.level = 0;
70 this.path = "";
71 this.paths = new String[0];
72 this.siteMap = siteMap;
73 }
74
75 public SiteMapItemImpl(SiteMapItemImpl parent, String id, String name, boolean hidden, SiteMap siteMap) {
76 this.id = id;
77 this.name = name;
78 this.parent = parent;
79 this.hidden = hidden;
80 this.level = parent.getLevel() + 1;
81 this.path = parent.getPath() + "/" + name;
82 this.paths = new String[level];
83 String[] parentPaths = parent.getPaths();
84 for (int i = 0; i < parentPaths.length; i++) {
85 paths[i] = parentPaths[i];
86 }
87 paths[level - 1] = name;
88 parent.addChild(name, this);
89 this.siteMap = siteMap;
90 }
91
92 void makeLeaf(String title, String src, String template, boolean menuItem) {
93 this.title = title;
94 this.src = src != null && src.length() != 0 ? src : null;
95 this.template = template;
96 this.menuItem = menuItem;
97 this.leaf = true;
98 this.linkable = true;
99 }
100
101 void makeNotLinkableLeaf(String title, boolean menuItem) {
102 this.title = title;
103 this.menuItem = menuItem;
104 this.leaf = true;
105 }
106
107
108
109
110 public SiteMap getSiteMap() {
111 return siteMap;
112 }
113
114
115
116
117 public SiteMapItem getChild(String name) {
118 return (SiteMapItem) childrenMap.get(name);
119 }
120
121 private void addChild(String name, SiteMapItemImpl mi) {
122 childrenMap.put(name, mi);
123 childrenList.add(mi);
124 }
125
126
127
128
129 public List getChildren() {
130 return children;
131 }
132
133
134
135
136 public String getSrc() {
137 return src;
138 }
139
140
141
142
143 public int getLevel() {
144 return level;
145 }
146
147
148
149
150 public String getId() {
151 return id;
152 }
153
154
155
156
157 public String getName() {
158 return name;
159 }
160
161
162
163
164 public SiteMapItem getParent() {
165 return parent;
166 }
167
168
169
170
171 public String getPath() {
172 return path;
173 }
174
175
176
177
178 public String[] getPaths() {
179 return (String[]) paths.clone();
180 }
181
182
183
184
185 public String getTemplate() {
186 return template;
187 }
188
189
190
191
192 public String getTitle() {
193 return title;
194 }
195
196
197
198
199 public boolean isLeaf() {
200 return leaf;
201 }
202
203
204
205
206 public boolean isHidden() {
207 return hidden;
208 }
209
210
211
212
213 public boolean isLinkable() {
214 return linkable;
215 }
216
217
218
219
220 public boolean isDocument() {
221 return false;
222 }
223
224
225
226
227 public boolean isMenuItem() {
228 return menuItem;
229 }
230
231
232
233
234 public int compareTo(Object obj) {
235 return getName().compareTo(((SiteMapItem) obj).getName());
236 }
237
238
239
240
241 public boolean equals(Object obj) {
242 return obj != null && obj instanceof SiteMapItem && getPath().equals(((SiteMapItem) obj).getPath());
243 }
244
245
246
247
248 public String getAttribute(String attributeName) {
249 if (attributes != null)
250 return (String) attributes.get(attributeName);
251 else
252 return null;
253 }
254
255
256
257
258 public void setAttributes(Map attributes) {
259 this.attributes = new HashMap(attributes);
260 }
261
262 /*** getSiteMapItemsByField
263 * @param smi
264 * @param field
265 * @param value
266 * @return
267 */
268 public List getSiteMapItemsByField(String field, String value) {
269 List result = new ArrayList();
270 String thisValue = getAttribute(field);
271 if (thisValue != null && thisValue.equals(value)) {
272 result.add(this);
273 }
274 for (Iterator it = getChildren().iterator(); it.hasNext();) {
275 List childResult = ((SiteMapItem) it.next()).getSiteMapItemsByField(field, value);
276 if (childResult.size() > 0)
277 result.addAll(childResult);
278 }
279 return result;
280 }
281
282 public SiteMapItem getFirstMatchingSiteMapItem(String field, String value) {
283 String thisValue = getAttribute(field);
284 if (thisValue != null && thisValue.equals(value)) {
285 return this;
286 }
287 for (Iterator it = getChildren().iterator(); it.hasNext();) {
288 SiteMapItem childResult = ((SiteMapItem) it.next()).getFirstMatchingSiteMapItem(field, value);
289 if (childResult != null)
290 return childResult;
291 }
292 return null;
293 }
294
295 }