View Javadoc

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.portal.portlets;
18  
19  import org.apache.jetspeed.portal.Portlet;
20  import org.apache.jetspeed.portal.PortletException;
21  import org.apache.jetspeed.portal.PortletConfig;
22  import org.apache.jetspeed.om.profile.ProfileLocator;
23  import org.apache.jetspeed.services.Profiler;
24  import org.apache.jetspeed.om.profile.Profile;
25  import org.apache.jetspeed.om.profile.PSMLDocument;
26  import org.apache.jetspeed.om.profile.Portlets;
27  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
28  import org.apache.jetspeed.services.logging.JetspeedLogger;
29  import org.apache.jetspeed.services.rundata.JetspeedRunData;
30  import org.apache.jetspeed.services.persistence.PersistenceManager;
31  import org.apache.jetspeed.services.persistence.PortalPersistenceException;
32  import org.apache.jetspeed.portal.PortletInstance;
33  
34  import org.apache.jetspeed.util.MimeType;
35  
36  import org.apache.jetspeed.util.JetspeedClearElement;
37  import org.apache.ecs.ConcreteElement;
38  
39  import org.apache.turbine.util.RunData;
40  
41  /***
42      Aggregate Portlet aggregates the content of other portlets.
43  
44      This portlet is a test for an alternate aggregation algorithm
45  
46      UNDER CONSTRUCTION
47  
48      @author <A HREF="mailto:taylor@apache.org">David Sean Taylor</A>
49      @version $Id: ContainerTestPortlet.java,v 1.9 2004/03/29 21:38:42 taylor Exp $
50  */
51  
52  public class ContainerTestPortlet implements Portlet /* , PortletState, Cacheable, Refreshable */
53  {
54  
55      /***
56       * Static initialization of the logger for this class
57       */    
58      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(ContainerTestPortlet.class.getName());    
59      
60      private String image = null;
61  
62      private String name = "not set";
63  
64      private String title = "la title";
65  
66      private String description = "description";
67  
68      private String id = null;
69  
70      private String handle = "";
71  
72      private PortletConfig   pc = null;
73  
74  
75     /***
76      Holds instances of ConcreteElements (Portlet output/content)
77      based on its current CapabilityMap.
78      */
79      // protected Hashtable content = new Hashtable();
80  
81      /***
82      The time this portlet was created.
83      */
84      private long creationTime;
85  
86      /***
87      Returns a name for this portlet.  This is used by PSML to identify a Portlet
88      within the PortletRegistry
89      */
90      public String getName()
91      {
92          return name;
93      }
94  
95      /***
96      Sets the name on this Portlet.
97  
98      @see #getName()
99      */
100     public void setName(String name)
101     {
102         System.out.println("setting name = " + name);
103         this.name = name;
104     }
105 
106     /***
107     <p>
108     Allows a Portlet to define its title.  This can be used by a PortletControl
109     for rendering its content.
110     </p>
111 
112     <p>
113     In order to define a default title you should not override this but should
114     call setTitle() within your init() method
115     </p>
116 
117     <p>
118     This should return null if not specified.
119     </p>
120     */
121     public String getTitle()
122     {
123         return this.title;
124     }
125 
126     /***
127      * t a title for this instance of the portlet.  This method is called
128      * from the context variable portlet_instance and from PortletInstance
129      *
130      * If you wish to append to the title, then you code should look like
131      *    getTitle( String instanceTitle)
132      *    {
133      *      return super.getTitle( instanceTitle) + " - Appened title text";
134      *    }
135      *
136      * @param instanceTitle Title from PSML
137      */
138     public String getTitle(String instanceTitle)
139     {
140         if (instanceTitle != null)
141             return instanceTitle;
142         return getTitle();
143     }
144 
145 
146     /***
147     Set the title for this Portlet
148     */
149     public void setTitle( String title )
150     {
151         this.title = title;
152     }
153 
154     /***
155     <p>
156     Returns a description of this portlet.  This should describe what the
157     capabilities of the portlet and how it can help the user.
158     </p>
159 
160     <p>
161     In order to define a default title you should not override (in the
162     AbstractPortlet implementation) this but should call setDescription()
163     within your init() method
164     </p>
165 
166     <p>
167     This should return null if not specified.
168     </p>
169     */
170     public String getDescription()
171     {
172         return description;
173     }
174 
175     /***
176      * Provide a Description within PML if the user has specified one.
177      *
178      * @return a null entry if the user hasn't defined anything
179      */
180     public String getDescription(String instanceDescription)
181     {
182       if (instanceDescription != null)
183           return instanceDescription;
184       return getDescription();
185     }
186 
187     /***
188     Set the description for this Portlet
189     */
190     public void setDescription( String description )
191     {
192         this.description = description;
193     }
194 
195     /***
196      * Getter for property image.
197      * @return Name of portlet image, icon.  The name is expected to be in the form of a URL.
198      */
199     public String getImage()
200     {
201         return image;
202     }
203 
204     /***
205      * Getter for property image.
206      * @return Name of portlet image, icon.  The name is expected to be in the form of a URL.
207      */
208     public String getImage(String instanceImage)
209     {
210       if (instanceImage != null)
211           return instanceImage;
212       return getImage();
213     }
214 
215     public void setImage( String image )
216     {
217         this.image = image;
218     }
219 
220     /***
221     Returns an HTML representation of this portlet.  Usually a Portlet would
222     initialized itself within init() and then when getContent is called it
223     would return its presentation.
224     */
225     public ConcreteElement getContent(RunData rundata)
226     {
227         String key = ((JetspeedRunData)rundata).getProfile().getId()
228                     + "." + this.getID();
229 
230         String path = (String)rundata.getUser().getTemp(key);
231         if (path == null)
232         {
233             path = this.getPortletConfig().getInitParameter("path");
234         }
235 
236         if (null == path)
237         {
238             return new JetspeedClearElement("Path parameter not set");
239         }
240 
241         ProfileLocator locator = Profiler.createLocator();
242         locator.createFromPath(path);
243         String id = locator.getId();
244 
245         try
246         {
247             Profile profile = Profiler.getProfile(locator);
248             PSMLDocument doc = profile.getDocument();
249             if (doc == null)
250             {
251                 return null;
252             }
253             Portlets portlets = doc.getPortlets();
254             //PortletContainer.aggregate(portlets);
255             return new JetspeedClearElement("XXX Under Construction :)");
256         }
257         catch (Exception e)
258         {
259             logger.error("Exception",  e);
260             return new JetspeedClearElement("Error in aggregation portlet: " + e.toString());
261         }
262     }
263 
264     /***
265     All initialization should be performed here.  If your Portlet wants to
266     do any work it should be done here.  You are not guaranteed that any
267     particular order of method call will happen just that init() will happen
268     first. Therefore if you have to calculate things like a title, a
269     description, etc it should happen here.
270     */
271     public void init() throws PortletException
272     {
273         String path = this.pc.getInitParameter("path");
274     }
275 
276 
277     /***
278     Set's the configuration of this servlet.
279     */
280     public void setPortletConfig(PortletConfig pc)
281     {
282         this.pc = pc;
283     }
284 
285 
286     /***
287     Get the config of this servlet.
288     */
289     public PortletConfig getPortletConfig()
290     {
291         return pc;
292     }
293 
294     /***
295     <p>Return true if this portlet is allowed to be edited in the rundata's context .</p>
296 
297     <p>Note:  PortletControl implementations should pay attention to this so
298     that they don't allow this option if it returns false.</p>
299     */
300     public boolean getAllowEdit( RunData rundata )
301     {
302         return false;
303     }
304 
305     /***
306     <p>Return true if this portlets is allowed to be maximized.</p>
307 
308     <p>Note:  PortletControl implementations should pay attention to this so
309     that they don't allow this option if it returns false.</p>
310     */
311     public boolean getAllowMaximize( RunData rundata )
312     {
313         return true;
314     }
315 
316     /***
317     Get the creation time for this Portlet
318     */
319     public long getCreationTime()
320     {
321 
322         return this.creationTime;
323     }
324 
325     /***
326     Set the creation time for this Portlet
327     */
328     public void setCreationTime( long creationTime )
329     {
330         System.out.println("setting creating time");
331         this.creationTime = creationTime;
332     }
333 
334     /***
335     Returns true portlet is able to output content for given mimetype
336     */
337     public boolean supportsType( MimeType mimeType )
338     {
339         return true;
340     }
341 
342    /***
343      * Retrieve a portlet attribute from persistent storage
344      *
345      * @param attrName The attribute to retrieve
346      * @param attrDefValue The value if the attr doesn't exists
347      * @param rundata The RunData object for the current request
348      * @return The attribute value
349      */
350     public String getAttribute( String attrName, String attrDefValue, RunData rundata )
351     {
352         String attrValue = null ;
353 
354         PortletInstance instance = PersistenceManager.getInstance(this, rundata);
355         attrValue = instance.getAttribute(attrName, attrDefValue);
356 
357         return attrValue;
358     }
359 
360     /***
361      * Stores a portlet attribute in persistent storage
362      *
363      * @param attrName The attribute to retrieve
364      * @paarm attrValue The value to store
365      * @param rundata The RunData object for the current request
366      */
367     public void setAttribute( String attrName, String attrValue, RunData rundata )
368     {
369         try
370         {
371             PortletInstance instance = PersistenceManager.getInstance(this, rundata);
372             instance.setAttribute(attrName, attrValue);
373             PersistenceManager.store(instance);
374         }
375         catch (PortalPersistenceException e)
376         {
377             logger.error("Exception while setting attribute "+attrName+" for portlet "+getName(),e);
378         }
379     }
380 
381     /***
382      * Gets the portlet instance associated with this portlet.
383      *
384      * @return PortletInstance
385      */
386     public PortletInstance getInstance(RunData rundata)
387     {
388        return PersistenceManager.getInstance(this, rundata);
389     }
390 
391 
392 
393 
394     /***
395     Retrieve a unique portlet id
396     */
397     public String getID()
398     {
399         return "9";
400     }
401 
402     public void setID(String id)
403     {
404         this.id = id;
405     }
406 
407     /***
408     * @return true if the portlet does its own customization
409     */
410     public boolean providesCustomization()
411     {
412         return false;
413     }
414 
415     /*** Returns TRUE if the title bar in should be displayed. The title bar includes
416      * the portlet title and action buttons.  This
417      *
418      * @param rundata The RunData object for the current request
419      */
420     public boolean isShowTitleBar(RunData rundata)
421     {
422         return true;
423     }
424 
425     /***
426      * Is the portled viewable.
427      * @param rundata The RunData object for the current request
428      * @return <CODE>true</CODE> Viewing is allow
429      * <CODE>false</CODE> Viewing is NOT alowed
430      * 
431      * Override this method to control your own View behavior
432      */
433     public boolean getAllowView( RunData rundata )
434     {
435         return true;
436     }
437     
438 
439 }