1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.om.registry.base;
18
19 import java.util.Iterator;
20 import java.util.Vector;
21 import java.util.Map;
22 import java.util.Hashtable;
23 import java.util.HashMap;
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
37 {
38
39 private String parent;
40
41 private ContentURL url = new BaseContentURL();
42
43 protected Vector categories = new Vector();
44
45 private boolean application;
46
47 private String 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 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 i = categories.iterator();
115 Iterator 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 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 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 getParent()
176 {
177 return this.parent;
178 }
179
180 /*** @return the classname associated to this entry */
181 public String 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 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 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 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 getTitle()
261 {
262 String 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 getDescription()
273 {
274 String 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 getParameterNames()
311 {
312 if (isRef)
313 {
314 Hashtable hash = new Hashtable();
315 Iterator 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 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 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 getParameterMap()
358 {
359 Hashtable params = (Hashtable)super.getParameterMap();
360
361 if (isRef)
362 {
363 Map 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 listMediaTypes()
377 {
378 if (isRef)
379 {
380 Map types = new HashMap();
381
382 Iterator 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 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
434
435 public Vector getCategories()
436 {
437 return this.categories;
438 }
439
440 public void setCategories(Vector 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 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 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 name, String group)
474 {
475 Iterator 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 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 name, String 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 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 name, String 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