View Javadoc

1   /*
2    * Copyright 2000-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.controls;
18  
19  // Turbine stuff
20  import org.apache.turbine.util.RunData;
21  
22  // Jetspeed stuff
23  import org.apache.jetspeed.om.security.JetspeedUser;
24  import org.apache.jetspeed.portal.Portlet;
25  import org.apache.jetspeed.portal.PortletSet;
26  import org.apache.jetspeed.portal.PortletState;
27  import org.apache.jetspeed.portal.PanedPortletController;
28  import org.apache.jetspeed.services.JetspeedSecurity;
29  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30  import org.apache.jetspeed.services.logging.JetspeedLogger;
31  import org.apache.jetspeed.services.rundata.JetspeedRunData;
32  import org.apache.jetspeed.services.security.PortalResource;
33  import org.apache.jetspeed.util.template.JetspeedLink;
34  import org.apache.jetspeed.util.template.JetspeedLinkFactory;
35  import org.apache.jetspeed.services.persistence.PersistenceManager;
36  import org.apache.jetspeed.portal.PortletInstance;
37  
38  // Velocity Stuff
39  import org.apache.velocity.context.Context;
40  
41  // Java stuff
42  import java.util.Collection;
43  import java.util.Comparator;
44  import java.util.List;
45  import java.util.TreeSet;
46  import java.util.Vector;
47  import java.util.Enumeration;
48  
49  /***
50   * A Velocity based portlet control designed for handling a PortletSet
51   * child
52   *
53   * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
54   *
55   * @version $Id: VelocityPortletSetControl.java,v 1.15 2004/02/23 03:25:35 jford Exp $
56   */
57  public class VelocityPortletSetControl extends VelocityPortletControl
58  {
59  
60      /***
61       * Static initialization of the logger for this class
62       */    
63      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(VelocityPortletSetControl.class.getName());    
64      
65      /***
66       * This method adds the control specific objects to the context
67       *
68       * @param rundata the RunData object for this request
69       * @param context the Context used by the template
70       */
71      public void buildContext(RunData rundata, Context context)
72      {
73          if (getPortlet() instanceof PortletSet)
74          {
75              context.put("tabs", getTabs((PortletSet) getPortlet(), rundata, context));
76          }
77      }    
78  
79      /***
80       * Populate a list of tabs that should be displayed by this control.
81       * Each tab represents a child portlet.
82       *
83       * This method works best if the child of this control is a PortletSet
84       * whose controller implements the PanedPortletController interface.
85       * 
86       * @param portlet the base portlet to explore for children
87       * @
88       */
89      private Collection getTabs(PortletSet portlets, RunData rundata, Context context)
90      {       
91          TreeSet tabs = new TreeSet(new PortletTabComparator());
92          PanedPortletController controller = null;
93  
94          // if portlet is a PortletSet, try to retrieve the Controller
95          // we need a PanedPortletController to work properly.
96          if (portlets.getController() instanceof PanedPortletController)
97          {    
98              controller = (PanedPortletController) portlets.getController();
99          }
100 
101         int count = 0;
102         for (Enumeration en = portlets.getPortlets(); en.hasMoreElements(); count++)
103         {
104             Portlet p = (Portlet) en.nextElement();
105             PortalResource portalResource = new PortalResource(p);
106 
107             // Secure the tabs
108             try
109             {
110                 JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
111                 portalResource.setOwner(jsLink.getUserName());
112                 JetspeedLinkFactory.putInstance(jsLink);
113             }
114             catch (Exception e)
115             {
116                 logger.warn(e.toString(), e);
117                 portalResource.setOwner(null);
118             }
119             JetspeedRunData jdata = (JetspeedRunData) rundata;
120             boolean hasView = JetspeedSecurity.checkPermission((JetspeedUser) jdata.getUser(),
121                                                                 portalResource, 
122                                                                 JetspeedSecurity.PERMISSION_VIEW);
123             if (!hasView)
124             {
125                 continue;
126             }
127             // skip any closed portlet
128             if ((p instanceof PortletState) && (((PortletState) p).isClosed(rundata)))
129             {
130                 continue;
131             }            
132 
133             String mstate = p.getAttribute("_menustate", "open", rundata);
134             if (mstate.equals("closed"))
135             {
136                 continue;
137             }
138 
139             PortletTab tab = new PortletTab();
140             
141             // Handle the portlet title
142             String title = null;            
143             PortletInstance pi = PersistenceManager.getInstance(p, rundata);
144             if (pi != null)
145             {
146                 title = pi.getTitle();
147                 if (title == null)
148                 {
149                     title = (p.getTitle() != null) ? p.getTitle() : p.getName();
150                 }
151             }
152             tab.setTitle(title);
153 
154             tab.setPosition(p.getPortletConfig().getPosition());
155             if (tabs.contains(tab))
156             {
157                 PortletTab lastTab = (PortletTab) tabs.last();
158                 int nextPos = lastTab.getPosition() + 1;
159                 tab.setPosition(nextPos);            
160             }        
161                 
162             if (controller != null)
163             {
164                 tab.setSelected(controller.isSelected(p, rundata));
165                 tab.setLink(controller.getPortletURI(p, rundata).toString());                
166             }
167                 
168             tab.setActions(buildActionList(rundata, p));
169             tabs.add(tab);
170         }
171 
172         return tabs;
173     }
174     
175     /*** Utilty class describing a Tab elemnt in the template Velocity Context
176      */
177     public class PortletTab
178     {
179         private String title = null;
180         private boolean selected = false;
181         private String link = null;
182         private List actions = null;
183         private int position = -1;
184         
185         public String getTitle()
186         {
187             return this.title;
188         }
189         
190         public void setTitle(String title)
191         {
192             this.title = title;
193         }
194         
195         public boolean isSelected()
196         {
197             return this.selected;
198         }
199         
200         public void setSelected(boolean selected)
201         {
202             this.selected = selected;
203         }
204         
205         public String getLink()
206         {
207             return this.link;
208         }
209         
210         public void setLink(String link)
211         {
212             this.link = link;
213         }
214         
215         public List getActions()
216         {
217             return (this.actions == null) ? new Vector() : this.actions;
218         }
219         
220         public void setActions(List actions)
221         {
222             this.actions = actions;
223         }
224 
225         public int getPosition()
226         {
227             return position;
228         }
229          
230         public void setPosition(int pos)
231         {
232             position = pos;
233         }   
234     }
235 
236     /***
237      * Used to correctly order tabs based on the position value
238      * that is found each PortletTab's parent Portlet's PortletConfig object.
239      */
240     public class PortletTabComparator implements  Comparator
241     {
242 
243         /***
244          * @see Comparator#compare(Object, Object)
245          */
246         public int compare(Object o1, Object o2)
247         {
248             try
249             {
250                 PortletTab pt1 = (PortletTab) o1;
251                 PortletTab pt2 = (PortletTab) o2;
252                 int pos1 = pt1.getPosition();
253                 int pos2 = pt2.getPosition();
254           
255                 if (pos1 < pos2)
256                 {
257                   return -1;
258                 }
259                 else if (pos1 > pos2)
260                 {
261                   return 1;
262                 }
263                 else
264                 {
265                   return 0;
266                 }                
267             }
268             catch (ClassCastException e)
269             {
270                 logger.error( "Exception in compare", e );
271                 return 0;
272             }
273         }
274     }
275 
276 }