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.util.template;
18  
19  import org.apache.turbine.util.RunData;
20  import org.apache.turbine.util.DynamicURI;
21  import org.apache.turbine.util.template.TemplateLink;
22  import org.apache.turbine.services.pull.ApplicationTool;
23  
24  import org.apache.jetspeed.portal.Portlet;
25  import org.apache.jetspeed.portal.PortletSet;
26  import org.apache.jetspeed.portal.PortletController;
27  import org.apache.jetspeed.portal.PanedPortletController;
28  import org.apache.jetspeed.om.profile.Entry;
29  import org.apache.jetspeed.om.profile.Portlets;
30  
31  import org.apache.jetspeed.services.rundata.JetspeedRunData;
32  import org.apache.jetspeed.services.resources.JetspeedResources;
33  
34  /***
35   * <p>A customized version of the TemplateLink which can handle portlet
36   * references.</p>
37   *
38   * <p>It is inserted into the template context by Turbine, via request tools.</p>
39   *
40   * <p>Each portlet must call setPortlet(this) on it before entering the template
41   * rendering code. This is done currently in VelocityPortlet.</p>
42   *
43   * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
44   * @author <a href="mailto:sgala@apache.org">Santiago Gala</a>
45   * @version $Id: JetspeedTemplateLink.java,v 1.14 2004/02/23 03:20:46 jford Exp $
46   *
47   * @deprecated Use JetspeedBaseLink
48   */
49  public class JetspeedTemplateLink
50      extends TemplateLink implements ApplicationTool
51  {
52  
53      // parameter names for the Jetspeed framework elements
54      public static final String PORTLET_KEY = "portlet";
55      public static final String ACTION_KEY = "action";
56      public static final String SCREEN_KEY = "screen";
57      public static final String TEMPLATE_KEY = "template";
58      public static final String PANEL_KEY = "select-panel";
59  
60      /***
61       *<p>The name of the portlet for which a URL will be generated.</p>
62       */
63      private String portletName = null;
64  
65      /***
66       *<p>Request to which we refer.</p>
67       */
68      private JetspeedRunData data = null;
69  
70      /***
71       *<p>The portlet that will be used to build the reference.</p>
72       */
73      protected Portlet activePortlet = null;
74  
75  
76  
77      /***
78       * Empty constructor.for introspection
79       */
80      public JetspeedTemplateLink()
81      {
82      }
83  
84      /***
85       * Constructor.
86       *
87       * @param data A Jetspeed RunData object.
88       */
89      public JetspeedTemplateLink(RunData data)
90      {
91          super(data);
92          this.data = (JetspeedRunData)data;
93  
94          String tmpl = this.data.getRequestedTemplate();
95          if (tmpl!=null)
96          {
97              setPage(tmpl);
98          }
99      }
100 
101     /***
102      * This will initialise a JetspeedTool object that was
103      * constructed with the default constructor (ApplicationTool
104      * method).
105      *
106      * @param data assumed to be a RunData object
107      */
108     public void init(Object data)
109     {
110         super.init(data);
111         this.data = (JetspeedRunData)data;
112 
113         String tmpl = this.data.getRequestedTemplate();
114         if (tmpl!=null)
115         {
116             setPage(tmpl);
117         }
118     }
119 
120     /***
121      * <p> Set the portlet giving context to this Link object.</p>
122      *
123      */
124     public void setPortlet(Portlet portlet)
125     {
126         this.activePortlet=portlet;
127     }
128 
129 
130 
131     /***
132      * Refresh method - does nothing
133      */
134     public void refresh()
135     {
136         super.refresh();
137 
138         String tmpl = this.data.getRequestedTemplate();
139         if (tmpl!=null)
140         {
141             setPage(tmpl);
142         }
143     }
144 
145     /***
146      * Return a URI that refers to the named portlet.
147      *
148      * @param portlet the name of the portlet to link to
149      * @return a DynamicURI referencing the named portlet for easy link construction in template
150      */
151     public DynamicURI forPortlet(String portlet)
152     {
153         this.portletName = portlet;
154         removePathInfo(getPortletKey());
155         removeQueryData(getPortletKey());
156         return addPathInfo(getPortletKey(), portlet);
157     }
158 
159     /***
160      * Add a portlet reference in the link.
161      *
162      * @param portlet the name of the portlet to link to
163      * @deprecated the name is confusing. Use @see(#forPortlet()) instead.
164      * @return a DynamicURI referencing the named portlet for easy link construction in template
165      */
166     public DynamicURI setPortlet(String portlet)
167     {
168         return this.forPortlet( portlet );
169     }
170 
171     /***
172      * @return the portlet parameter value
173      */
174     public String getPortlet()
175     {
176         return this.portletName;
177     }
178 
179     /***
180      * @return the portlet parameter name
181      */
182     public String getPortletKey()
183     {
184         return PORTLET_KEY;
185     }
186 
187     /***
188      * @return the action parameter name
189      */
190     public String getActionKey()
191     {
192         return ACTION_KEY;
193     }
194 
195     /***
196      * @return the screen parameter name
197      */
198     public String getScreenKey()
199     {
200         return SCREEN_KEY;
201     }
202 
203     /***
204      * @return the template parameter name
205      */
206     public String getPageKey()
207     {
208         return TEMPLATE_KEY;
209     }
210 
211     /***
212      * Add a select-panel reference in the link
213      *
214      * @param portlet the name of the portlet to link to
215      * @return a self reference for easy link construction in template
216      */
217     public DynamicURI setPanel(String panel)
218     {
219         removePathInfo(JetspeedResources.PATH_PANEID_KEY);
220         removeQueryData(JetspeedResources.PATH_PANEID_KEY);
221         PortletController controller = activePortlet.getPortletConfig()
222                                                         .getPortletSet()
223                                                         .getController();
224 
225        String id = null;
226 
227         if (controller instanceof PanedPortletController)
228         {
229             PortletSet set = controller.getPortlets();
230             Portlet portlet = null;
231             if (null != set)
232             {
233                 portlet = set.getPortletByName(panel);
234             }
235             if (portlet != null)
236                 id = portlet.getID();
237         }
238         else
239         {
240            Portlets entry = data.getProfile().getDocument().getPortlets(panel);
241            if (entry != null)
242                id = entry.getId();
243          }
244         return addPathInfo(JetspeedResources.PATH_PANEID_KEY, id);
245     }
246 
247     /***
248      * <p>Use the activePortlet to get the current Panel name (key).</p>
249      *
250      * @return the panel parameter name
251      */
252     public String getPanelKey()
253     {
254         String panelName = PANEL_KEY;
255         try
256         {
257             PortletController controller = activePortlet.getPortletConfig()
258                                                         .getPortletSet()
259                                                         .getController();
260 
261             if (controller instanceof PanedPortletController)
262             {
263                 panelName=((PanedPortletController)controller).getParameterName();
264             }
265 
266         }
267         catch (Exception e)
268         {
269             panelName = PANEL_KEY;
270         }
271 
272         return panelName;
273     }
274 
275     /***
276      *
277      *
278      */
279     public String toString()
280     {
281         String tmpl = this.data.getRequestedTemplate();
282         if (tmpl!=null)
283         {
284             setPage(tmpl);
285         }
286 
287         String buf = super.toString();
288         return buf;
289     }
290 
291     public DynamicURI getPortletByName(String portletName)
292     {
293         String id = null;
294         Entry entry = data.getProfile().getDocument().getEntry(portletName);
295         //Portlets pEntry =  data.getProfile().getDocument().getPortletsById(entry.getId());
296         if (entry != null)
297         {
298             id = entry.getId();
299         }
300         System.out.println("js_peid:"+id);
301         return addPathInfo("js_peid", id);
302 
303     }
304 
305     public void clear()
306     {
307         removePathInfo();
308         removeQueryData();
309     }
310 }