View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.jetspeed.om.page.psml;
19  
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Stack;
25  
26  import org.apache.jetspeed.om.folder.Folder;
27  import org.apache.jetspeed.om.folder.MenuDefinition;
28  import org.apache.jetspeed.om.folder.MenuExcludeDefinition;
29  import org.apache.jetspeed.om.folder.MenuIncludeDefinition;
30  import org.apache.jetspeed.om.folder.MenuOptionsDefinition;
31  import org.apache.jetspeed.om.folder.MenuSeparatorDefinition;
32  import org.apache.jetspeed.om.folder.psml.MenuDefinitionImpl;
33  import org.apache.jetspeed.om.folder.psml.MenuExcludeDefinitionImpl;
34  import org.apache.jetspeed.om.folder.psml.MenuIncludeDefinitionImpl;
35  import org.apache.jetspeed.om.folder.psml.MenuOptionsDefinitionImpl;
36  import org.apache.jetspeed.om.folder.psml.MenuSeparatorDefinitionImpl;
37  import org.apache.jetspeed.om.page.Fragment;
38  import org.apache.jetspeed.om.page.Page;
39  import org.apache.jetspeed.page.impl.DatabasePageManagerUtils;
40  
41  /***
42   * @version $Id: PageImpl.java 550655 2007-06-26 01:41:35Z taylor $
43   */
44  public class PageImpl extends DocumentImpl implements Page
45  {
46      private DefaultsImpl defaults = new DefaultsImpl();
47  
48      private Fragment root = null;
49  
50      private int hashCode;
51  
52      /***
53       * menuDefinitions - menu definitions for page
54       */
55      private List menuDefinitions;
56      
57      public PageImpl()
58      {
59          // empty constructor
60          super();
61      }
62  
63      /***
64       * <p>
65       * setId
66       * </p>
67       *
68       * @see org.apache.jetspeed.om.page.psml.AbstractBaseElement#setId(java.lang.String)
69       * @param id
70       */
71      public void setId( String id )
72      {
73          // Cheaper to generate the hash code now then every call to hashCode()
74          hashCode = (Page.class.getName()+":"+id).hashCode();
75          super.setId(id);        
76      }
77      
78      /***
79       * <p>
80       * equals
81       * </p>
82       * 
83       * @see java.lang.Object#equals(java.lang.Object)
84       * @param obj
85       * @return
86       */
87      public boolean equals( Object obj )
88      {
89          if (obj instanceof Page)
90          {
91              Page page = (Page) obj;
92              return page != null && page.getId() != null && 
93                     this.getId() != null && this.getId().equals(page.getId());
94          }
95          else
96          {
97              return false;
98          }
99  
100     }
101 
102     /***
103      * <p>
104      * hashCode
105      * </p>
106      * 
107      * @see java.lang.Object#hashCode()
108      * @return
109      */
110     public int hashCode()
111     {       
112         return hashCode;
113     }
114 
115     public String getSkin()
116     {
117         return defaults.getSkin();
118     }
119 
120     public void setSkin( String skinName )
121     {
122         defaults.setSkin(skinName);
123     }
124 
125     /* (non-Javadoc)
126      * @see org.apache.jetspeed.om.page.Page#getEffectiveDefaultDecorator(java.lang.String)
127      */
128     public String getEffectiveDefaultDecorator(String fragmentType)
129     {
130         // get locally defined decorator
131         String decorator = getDefaultDecorator(fragmentType);
132         if (decorator == null)
133         {
134             // delegate to parent folder
135             Folder parentFolder = (Folder)getParent();
136             if (parentFolder != null)
137             {
138                 return parentFolder.getEffectiveDefaultDecorator(fragmentType);
139             }
140         }
141         return decorator;
142     }
143 
144     public String getDefaultDecorator( String fragmentType )
145     {
146         return defaults.getDecorator(fragmentType);
147     }
148 
149     public void setDefaultDecorator( String decoratorName, String fragmentType )
150     {
151         defaults.setDecorator(fragmentType, decoratorName);
152     }
153 
154     public Fragment getRootFragment()
155     {
156         return this.root;
157     }
158 
159     public void setRootFragment( Fragment root )
160     {
161         this.root = root;
162         if (root instanceof FragmentImpl)
163         {
164             ((FragmentImpl)root).setPage(this);
165         }        
166     }
167 
168     public Fragment getFragmentById( String id )
169     {
170         Stack stack = new Stack();
171         if (getRootFragment() != null)
172         {
173             stack.push(getRootFragment());
174         }
175 
176         Fragment f = (Fragment) stack.pop();
177 
178         while ((f != null) && (!(f.getId().equals(id))))
179         {
180             Iterator i = f.getFragments().iterator();
181 
182             while (i.hasNext())
183             {
184                 stack.push(i.next());
185             }
186 
187             if (stack.size() > 0)
188             {
189                 f = (Fragment) stack.pop();
190             }
191             else
192             {
193                 f = null;
194             }
195         }
196 
197         return f;
198     }
199 
200     public Fragment removeFragmentById( String id )
201     {
202         // find fragment by id, tracking fragment parent
203         Map parents = new HashMap();
204         Stack stack = new Stack();
205         if (getRootFragment() != null)
206         {
207             stack.push(getRootFragment());
208         }
209         Fragment f = (Fragment) stack.pop();
210         while ((f != null) && (!(f.getId().equals(id))))
211         {
212             Iterator i = f.getFragments().iterator();
213 
214             while (i.hasNext())
215             {
216                 Fragment child = (Fragment)i.next();
217                 stack.push(child);
218                 parents.put(child, f);
219             }
220 
221             if (stack.size() > 0)
222             {
223                 f = (Fragment) stack.pop();
224             }
225             else
226             {
227                 f = null;
228             }
229         }
230 
231         // remove fragment from parent/page root
232         if (f != null)
233         {
234             Fragment parent = (Fragment)parents.get(f);
235             if (parent != null)
236             {
237                 if (parent.getFragments().remove(f))
238                 {
239                     return f;
240                 }
241             }
242             else
243             {
244                 if (f == root)
245                 {
246                     root = null;
247                     return f;
248                 }
249             }
250         }
251 
252         // not found or removed
253         return null;
254     }
255 
256     public List getFragmentsByName( String name )
257     {
258         List fragments = DatabasePageManagerUtils.createList();
259 
260         Stack stack = new Stack();
261         if (getRootFragment() != null)
262         {
263             stack.push(getRootFragment());
264         }
265 
266         Fragment f = (Fragment) stack.pop();
267 
268         while (f != null)
269         {
270             if ((f.getName() != null) && f.getName().equals(name))
271             {
272                 fragments.add(f);
273             }
274 
275             Iterator i = f.getFragments().iterator();
276 
277             while (i.hasNext())
278             {
279                 stack.push(i.next());
280             }
281 
282             if (stack.size() > 0)
283             {
284                 f = (Fragment) stack.pop();
285             }
286             else
287             {
288                 f = null;
289             }
290         }
291 
292         return fragments;
293     }
294 
295     public DefaultsImpl getDefaults()
296     {
297         return this.defaults;
298     }
299 
300     public void setDefaults( DefaultsImpl defaults )
301     {
302         this.defaults = defaults;
303     }
304 
305     /***
306      * <p>
307      * getType
308      * </p>
309      *
310      * @see org.apache.jetspeed.om.page.Document#getType()
311      * @return
312      */
313     public String getType()
314     {       
315         return DOCUMENT_TYPE;
316     }
317 
318     /***
319      * getMenuDefinitions - get list of menu definitions
320      *
321      * @return definition list
322      */
323     public List getMenuDefinitions()
324     {
325         return menuDefinitions;
326     }
327 
328     /***
329      * newMenuDefinition - creates a new empty menu definition
330      *
331      * @return a newly created MenuDefinition object for use in Page
332      */
333     public MenuDefinition newMenuDefinition()
334     {
335         return new MenuDefinitionImpl();
336     }
337 
338     /***
339      * newMenuExcludeDefinition - creates a new empty menu exclude definition
340      *
341      * @return a newly created MenuExcludeDefinition object for use in Page
342      */
343     public MenuExcludeDefinition newMenuExcludeDefinition()
344     {
345         return new MenuExcludeDefinitionImpl();
346     }
347 
348     /***
349      * newMenuIncludeDefinition - creates a new empty menu include definition
350      *
351      * @return a newly created MenuIncludeDefinition object for use in Page
352      */
353     public MenuIncludeDefinition newMenuIncludeDefinition()
354     {
355         return new MenuIncludeDefinitionImpl();
356     }
357 
358     /***
359      * newMenuOptionsDefinition - creates a new empty menu options definition
360      *
361      * @return a newly created MenuOptionsDefinition object for use in Page
362      */
363     public MenuOptionsDefinition newMenuOptionsDefinition()
364     {
365         return new MenuOptionsDefinitionImpl();
366     }
367 
368     /***
369      * newMenuSeparatorDefinition - creates a new empty menu separator definition
370      *
371      * @return a newly created MenuSeparatorDefinition object for use in Page
372      */
373     public MenuSeparatorDefinition newMenuSeparatorDefinition()
374     {
375         return new MenuSeparatorDefinitionImpl();
376     }
377 
378     /***
379      * setMenuDefinitions - set list of menu definitions
380      *
381      * @param definitions definition list
382      */
383     public void setMenuDefinitions(List definitions)
384     {
385         menuDefinitions = definitions;
386     }
387 
388     /***
389      * unmarshalled - notification that this instance has been
390      *                loaded from the persistent store
391      */
392     public void unmarshalled()
393     {
394         // notify super class implementation
395         super.unmarshalled();
396 
397         // propagate unmarshalled notification
398         // to all menu definitions
399         if (menuDefinitions != null)
400         {
401             Iterator menuIter = menuDefinitions.iterator();
402             while (menuIter.hasNext())
403             {
404                 ((MenuDefinitionImpl)menuIter.next()).unmarshalled();
405             }
406         }
407 
408         // propagate unmarshalled notification
409         // to root fragment
410         if (root != null)
411         {
412             ((FragmentImpl)root).unmarshalled();
413         }
414 
415         // default title of pages to name
416         if (getTitle() == null)
417         {
418             setTitle(getTitleName());
419         }
420     }
421 
422     /***
423      * marshalling - notification that this instance is to
424      *               be saved to the persistent store
425      */
426     public void marshalling()
427     {
428         // propagate marshalling notification
429         // to root fragment
430         if (root != null)
431         {
432             ((FragmentImpl)root).marshalling();
433         }
434 
435         // propagate marshalling notification
436         // to all menu definitions
437         if (menuDefinitions != null)
438         {
439             Iterator menuIter = menuDefinitions.iterator();
440             while (menuIter.hasNext())
441             {
442                 ((MenuDefinitionImpl)menuIter.next()).marshalling();
443             }
444         }
445 
446         // notify super class implementation
447         super.marshalling();
448     }
449 }
450