KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > om > registry > base > BasePortletEntry


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
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" BASIS,
12  * 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
17 package org.apache.jetspeed.om.registry.base;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.Vector JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import org.apache.jetspeed.om.registry.*;
26 import org.apache.jetspeed.services.Registry;
27
28 /**
29  * Default bean like implementation of the PortletEntry interface
30  * suitable for serialization with Castor
31  *
32  * @author <a HREF="mailto:raphael@apache.org">Raphaël Luta</a>
33  * @version $Id: BasePortletEntry.java,v 1.5 2004/02/23 03:08:26 jford Exp $
34  */

35 public class BasePortletEntry extends BasePortletInfoEntry
36    implements PortletEntry, java.io.Serializable JavaDoc
37 {
38
39     private String JavaDoc parent;
40
41     private ContentURL url = new BaseContentURL();
42
43     protected Vector JavaDoc categories = new Vector JavaDoc();
44
45     private boolean application;
46
47     private String JavaDoc type = PortletEntry.TYPE_ABSTRACT;
48
49     private boolean isRef = true;
50
51     /**
52      * Implements the equals operation so that 2 elements are equal if
53      * all their member values are equal.
54      */

55     public boolean equals(Object JavaDoc object)
56     {
57         if (object==null)
58         {
59             return false;
60         }
61
62         BasePortletEntry obj = (BasePortletEntry)object;
63
64         if (application!=obj.isApplication())
65         {
66             return false;
67         }
68
69         if (parent!=null)
70         {
71             if (!parent.equals(obj.getParent()))
72             {
73                 return false;
74             }
75         }
76         else
77         {
78             if (obj.getParent()!=null)
79             {
80                 return false;
81             }
82         }
83
84         if (type!=null)
85         {
86             if (!type.equals(obj.getType()))
87             {
88                 return false;
89             }
90         }
91         else
92         {
93             if (obj.getType()!=null)
94             {
95                 return false;
96             }
97         }
98
99         if (url!=null)
100         {
101             if (!url.equals(obj.getContentURL()))
102             {
103                 return false;
104             }
105         }
106         else
107         {
108             if (obj.getContentURL()!=null)
109             {
110                 return false;
111             }
112         }
113
114         Iterator JavaDoc i = categories.iterator();
115         Iterator JavaDoc i2 = obj.getCategories().iterator();
116         while(i.hasNext())
117         {
118             BaseCategory c1 = (BaseCategory)i.next();
119             BaseCategory c2 = null;
120
121             if (i2.hasNext())
122             {
123                 c2 = (BaseCategory)i2.next();
124             }
125             else
126             {
127                 return false;
128             }
129
130             if (!c1.equals(c2))
131             {
132                 return false;
133             }
134         }
135
136         if (i2.hasNext())
137         {
138             return false;
139         }
140
141         return super.equals(object);
142     }
143
144     /** @return the URL associated with this portlet or null */
145     public String JavaDoc getURL()
146     {
147         return this.url.getURL();
148     }
149
150     /**
151      * Sets the URL for this PortletEntry
152      * @param url the new PortletEntry URL
153      */

154     public void setURL( String JavaDoc url )
155     {
156         this.url.setURL(url);
157     }
158
159     public boolean isCachedOnURL()
160     {
161         return url.isCacheKey();
162     }
163
164     public void setCachedOnURL(boolean cache)
165     {
166         url.setCachedOnURL(cache);
167     }
168
169     public ContentURL getURLEntry()
170     {
171         return url;
172     }
173
174     /** @return the entry name from which this one is derived */
175     public String JavaDoc getParent()
176     {
177         return this.parent;
178     }
179
180     /** @return the classname associated to this entry */
181     public String JavaDoc getClassname()
182     {
183         if (isRef && (classname == null) )
184         {
185             return getParentEntry().getClassname();
186         }
187
188         return classname;
189     }
190
191
192     /**
193      * Sets the ancestor for this PortletEntry.
194      * @param parent the new ancestor entry name. This name should
195      * be defined in the system registry
196      */

197     public void setParent( String JavaDoc parent )
198     {
199         this.parent = parent;
200     }
201
202     /** @return true is this entry is only accessible by the
203       * portal administrators.
204       */

205     public boolean isAdmin()
206     {
207         if (getSecurity()!=null)
208         {
209             return "admin".equals(getSecurity().getRole());
210         }
211
212         return false;
213     }
214
215     /** @return true is the PortletEntry is marked as an application */
216     public boolean isApplication()
217     {
218         return this.application;
219     }
220
221     /** Sets the application status of this portlet entry. If an entry
222      * is maked as application, the associated portlet will only be displayed
223      * in Maximized mode and can be retrieved specifically
224      *
225      * @param application the new application status
226      */

227     public void setApplication( boolean application )
228     {
229         this.application = application;
230     }
231
232     /** @return the type of this entry */
233     public String JavaDoc getType()
234     {
235         return this.type;
236     }
237
238     /** Sets the type of this entry. The type specifies whether it is
239      * abstract, instance or ref
240      *
241      * @param type the new type for the PortletEntry
242      */

243     public void setType( String JavaDoc type )
244     {
245         this.isRef = PortletEntry.TYPE_REF.equals(type);
246         this.type = type;
247     }
248
249     /** This method is used by the Castor persistence system to retrieve
250      * the application status
251      *
252      * @see PortletEntry#isApplication
253      * @return the application status of this entry
254      */

255     public boolean getApplication()
256     {
257         return this.application;
258     }
259
260     public String JavaDoc getTitle()
261     {
262         String JavaDoc title = super.getTitle();
263         if (title != null)
264             return title;
265         if (isRef)
266         {
267            return getParentEntry().getTitle();
268         }
269         return null;
270     }
271
272     public String JavaDoc getDescription()
273     {
274         String JavaDoc desc = super.getDescription();
275         if (desc != null)
276             return desc;
277
278         if (isRef)
279         {
280             return getParentEntry().getDescription();
281         }
282         return null;
283     }
284
285     /** Looks up in the Registry the parent entry for this real entry */
286     public PortletEntry getParentEntry()
287     {
288         PortletEntry parent = null;
289         parent = (PortletEntry)Registry.getEntry( Registry.PORTLET, getParent() );
290         if (parent == null)
291         {
292             parent = new BasePortletEntry();
293             parent.setName(getParent());
294             parent.setType(PortletEntry.TYPE_ABSTRACT);
295         }
296         return parent;
297     }
298
299     public MetaInfo getMetaInfo()
300     {
301         MetaInfo meta = super.getMetaInfo();
302         if (meta == null)
303         {
304             return getParentEntry().getMetaInfo();
305         }
306         return meta;
307     }
308
309     /** @return an enumeration of this entry parameter names */
310     public Iterator JavaDoc getParameterNames()
311     {
312         if (isRef)
313         {
314             Hashtable JavaDoc hash = new Hashtable JavaDoc();
315             Iterator JavaDoc i = super.getParameterNames();
316             while(i.hasNext())
317             {
318                 hash.put(i.next(),"1");
319             }
320             i = getParentEntry().getParameterNames();
321             while(i.hasNext())
322             {
323                 hash.put(i.next(),"1");
324             }
325
326             return hash.keySet().iterator();
327         }
328
329         return super.getParameterNames();
330     }
331
332     /** Search for a named parameter and return the associated
333      * parameter object. The search is case sensitive.
334      *
335      * @return the parameter object for a given parameter name
336      * @param name the parameter name to look for
337      */

338     public Parameter getParameter( String JavaDoc name )
339     {
340         Parameter p = super.getParameter(name);
341         if (isRef && p == null)
342         {
343             return getParentEntry().getParameter(name);
344         }
345         return p;
346     }
347
348     public CachedParameter getCachedParameter( String JavaDoc name )
349     {
350         Parameter p = getParameter(name);
351         return (CachedParameter)p;
352     }
353
354     /** Returns a map of parameter values keyed on the parameter names
355      * @return the parameter values map
356      */

357     public Map JavaDoc getParameterMap()
358     {
359         Hashtable JavaDoc params = (Hashtable JavaDoc)super.getParameterMap();
360
361         if (isRef)
362         {
363             Map JavaDoc map = getParentEntry().getParameterMap();
364             map.putAll(params);
365             return map;
366         }
367
368         return params;
369     }
370
371     /**
372      * Returns a list of the supported media type names
373      *
374      * @return an iterator on the supported media type names
375      */

376     public Iterator JavaDoc listMediaTypes()
377     {
378         if (isRef)
379         {
380             Map JavaDoc types = new HashMap JavaDoc();
381
382             Iterator JavaDoc i = super.listMediaTypes();
383             while(i.hasNext())
384             {
385                 types.put(i.next(),"1");
386             }
387
388             i = getParentEntry().listMediaTypes();
389             while(i.hasNext())
390             {
391                 types.put(i.next(),"1");
392             }
393
394             return types.keySet().iterator();
395         }
396
397         return super.listMediaTypes();
398     }
399
400     /**
401      * Test if a given media type is supported by this entry.
402      * The test is done by a case sensitive name comparison
403      *
404      * @param name the media type name to test for.
405      * @return true is the media type is supported false otherwise
406      */

407     public boolean hasMediaType(String JavaDoc name)
408     {
409         if (isRef)
410         {
411             return super.hasMediaType(name) || getParentEntry().hasMediaType(name);
412         }
413
414         return super.hasMediaType(name);
415     }
416
417     /** @return the URL associated with this portlet or null */
418     public BaseContentURL getContentURL()
419     {
420         return (BaseContentURL)this.url;
421     }
422
423     /**
424      * Sets the URL for this PortletEntry
425      * @param url the new PortletEntry URL
426      */

427     public void setContentURL( BaseContentURL url )
428     {
429         this.url = url;
430     }
431
432     /*
433      * Categories
434      */

435     public Vector JavaDoc getCategories()
436     {
437         return this.categories;
438     }
439
440     public void setCategories(Vector JavaDoc v)
441     {
442         this.categories = v;
443     }
444
445     /**
446      * Returns a list of the supported media type names
447      *
448      * @return an iterator on the supported media type names
449      */

450     public Iterator JavaDoc listCategories()
451     {
452         return new PortletIterator(this, "getCategories");
453     }
454
455     /**
456      * Test if a given category exists for this entry
457      *
458      * @param name the category name
459      * @return true is the category exists in the default group
460      */

461     public boolean hasCategory(String JavaDoc name)
462     {
463         return hasCategory(name, PortletEntry.DEFAULT_GROUP);
464     }
465
466     /**
467      * Test if a given category exists for this entry, in the specified group of categories.
468      *
469      * @param name the category name
470      * @param group the category group
471      * @return true is the category exists in the specified group
472      */

473     public boolean hasCategory(String JavaDoc name, String JavaDoc group)
474     {
475         Iterator JavaDoc it = listCategories();
476         while (it.hasNext())
477         {
478             Category cat = (Category)it.next();
479             if (cat.getName().equals(name) && cat.getGroup().equals(group))
480                 return true;
481         }
482         return false;
483     }
484
485
486     /**
487      * Add a new category to this portlet entry in the default group.
488      *
489      * @param name the category name
490      */

491     public void addCategory(String JavaDoc name)
492     {
493         addCategory(name, PortletEntry.DEFAULT_GROUP);
494     }
495
496     /**
497      * Add a new category to this portlet entry.
498      *
499      * @param name the category name
500      * @param group the category group name
501      */

502     public void addCategory(String JavaDoc name, String JavaDoc group)
503     {
504         if (!hasCategory(name, group))
505         {
506             Category cat = new BaseCategory();
507             cat.setName(name);
508             cat.setGroup(group);
509             categories.add(cat);
510         }
511     }
512
513     /**
514      * Remove a category from this portlet entry in the default group.
515      *
516      * @param name the category name
517      */

518     public void removeCategory(String JavaDoc name)
519     {
520         removeCategory(name, PortletEntry.DEFAULT_GROUP);
521     }
522
523     /**
524      * Remove a category from this portlet entry in the specified group.
525      *
526      * @param name the media type name to remove.
527      * @param group the category group name
528      */

529     public void removeCategory(String JavaDoc name, String JavaDoc group)
530     {
531         for (int ix = 0; ix < categories.size(); ix++)
532         {
533             Category cat = (Category)categories.elementAt(ix);
534             if (cat.getName().equals(name) && cat.getGroup().equals(group))
535             {
536                 categories.remove(ix);
537                 return;
538             }
539         }
540     }
541 }
542
543
544
Popular Tags