View Javadoc

1   /*
2    * Copyright 2007 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
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     /* (non-Javadoc)
108      * @see nl.hippo.portal.cms.site.SiteMapItem#getSiteMap()
109      */
110     public SiteMap getSiteMap() {
111         return siteMap;
112     }
113 
114     /* (non-Javadoc)
115      * @see nl.hippo.portal.cms.site.SiteMapItem#getChild(java.lang.String)
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     /* (non-Javadoc)
127      * @see nl.hippo.portal.cms.site.SiteMapItem#getChildren()
128      */
129     public List getChildren() {
130         return children;
131     }
132 
133     /* (non-Javadoc)
134      * @see nl.hippo.portal.cms.site.SiteMapItem#getSrc()
135      */
136     public String getSrc() {
137         return src;
138     }
139 
140     /* (non-Javadoc)
141      * @see nl.hippo.portal.cms.site.SiteMapItem#getLevel()
142      */
143     public int getLevel() {
144         return level;
145     }
146 
147     /* (non-Javadoc)
148      * @see nl.hippo.portal.cms.site.SiteMapItem#getId()
149      */
150     public String getId() {
151         return id;
152     }
153 
154     /* (non-Javadoc)
155      * @see nl.hippo.portal.cms.site.SiteMapItem#getName()
156      */
157     public String getName() {
158         return name;
159     }
160 
161     /* (non-Javadoc)
162      * @see nl.hippo.portal.cms.site.SiteMapItem#getParent()
163      */
164     public SiteMapItem getParent() {
165         return parent;
166     }
167 
168     /* (non-Javadoc)
169      * @see nl.hippo.portal.cms.site.SiteMapItem#getPath()
170      */
171     public String getPath() {
172         return path;
173     }
174 
175     /* (non-Javadoc)
176      * @see nl.hippo.portal.cms.site.SiteMapItem#getPaths()
177      */
178     public String[] getPaths() {
179         return (String[]) paths.clone();
180     }
181 
182     /* (non-Javadoc)
183      * @see nl.hippo.portal.cms.site.SiteMapItem#getTemplate()
184      */
185     public String getTemplate() {
186         return template;
187     }
188 
189     /* (non-Javadoc)
190      * @see nl.hippo.portal.cms.site.SiteMapItem#getTitle()
191      */
192     public String getTitle() {
193         return title;
194     }
195 
196     /* (non-Javadoc)
197      * @see nl.hippo.portal.cms.site.SiteMapItem#isLeaf()
198      */
199     public boolean isLeaf() {
200         return leaf;
201     }
202 
203     /* (non-Javadoc)
204      * @see nl.hippo.portal.cms.site.SiteMapItem#isHidden()
205      */
206     public boolean isHidden() {
207         return hidden;
208     }
209 
210     /* (non-Javadoc)
211      * @see nl.hippo.portal.cms.site.SiteMapItem#isLinkable()
212      */
213     public boolean isLinkable() {
214         return linkable;
215     }
216 
217     /* (non-Javadoc)
218      * @see nl.hippo.portal.cms.site.SiteMapItem#isDocument()
219      */
220     public boolean isDocument() {
221         return false;
222     }
223 
224     /* (non-Javadoc)
225      * @see nl.hippo.portal.cms.site.SiteMapItem#isMenuItem()
226      */
227     public boolean isMenuItem() {
228         return menuItem;
229     }
230 
231     /* (non-Javadoc)
232      * @see java.lang.Comparable#compareTo(java.lang.Object)
233      */
234     public int compareTo(Object obj) {
235         return getName().compareTo(((SiteMapItem) obj).getName());
236     }
237 
238     /* (non-Javadoc)
239      * @see java.lang.Object#equals(java.lang.Object)
240      */
241     public boolean equals(Object obj) {
242         return obj != null && obj instanceof SiteMapItem && getPath().equals(((SiteMapItem) obj).getPath());
243     }
244 
245     /* (non-Javadoc)
246      * @see nl.hippo.portal.cms.site.SiteMapItem#getAttribute(java.lang.String)
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     /* (non-Javadoc)
256      * @see nl.hippo.portal.cms.site.SiteMapItem#setAttributes(java.util.Map)
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 }