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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.portal.controllers;
1819// Turbine stuff20import org.apache.turbine.util.DynamicURI;
21import org.apache.turbine.util.RunData;
2223// Jetspeed stuff24import org.apache.jetspeed.portal.Portlet;
25import org.apache.jetspeed.portal.PanedPortletController;
26import org.apache.jetspeed.util.template.JetspeedLink;
27import org.apache.jetspeed.util.template.JetspeedLinkFactory;
28import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29import org.apache.jetspeed.services.logging.JetspeedLogger;
30import org.apache.jetspeed.services.statemanager.SessionState;
31import org.apache.jetspeed.services.rundata.JetspeedRunData;
32import org.apache.jetspeed.services.resources.JetspeedResources;
3334/***35 * A Velocity based portlet controller implementation that can be used36 * to manage paned content (ie, where a only a subset of all portlets37 * is visible at any given time)38 * 39 * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>40 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>41 *42 * @version $Id: VelocityPanedPortletController.java,v 1.13 2004/02/23 03:25:06 jford Exp $43 */4445publicclassVelocityPanedPortletControllerextendsVelocityPortletController46 implements PanedPortletController47 {
4849/***50 * Static initialization of the logger for this class51 */52privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(VelocityPanedPortletController.class.getName());
5354publicstaticfinal String DEFAULT_PARAMETER = "pane";
5556/*57 * @return the pane parameter name58 *59 */60public String getPaneParameter()
61 {
62return JetspeedResources.PATH_PANEID_KEY;
63 }
646566/***67 * Test whether the selected portlet is considered selected for the current68 * request.69 *70 * @param p the Portlet to check71 * @param rundata the RunData for the request72 * @return true if the portlet is selected, false otherwise73 */74publicboolean isSelected( Portlet p, RunData rundata )
75 {
76 String peid = rundata.getParameters().getString( getPaneParameter() );
77 String pname = rundata.getParameters().getString(JetspeedResources.PATH_PANENAME_KEY);
78 String last = retrievePaneIDFromSession(rundata);
7980//match by portlet name if appropriate.81if (pname != null && pname.equals(p.getName()))
82 {
83returntrue;
84 }
8586if (peid == null)
87 {
88if (last == null)
89 {
90return (getPortlets().getPortletAt( 0 ) == p);
91 }
92else93 {
94if (pname == null)
95 {
96return (p.getID().equals(last));
97 }
98else99 {
100//If the current portlet set has a portlet with the same name as the one we're trying to select101//we don't want to select anything else b/c we'll select the one we want AND the last-used portlet.102//If the portlet set doesn't have a portlet by this name, we WANT to select the last103//used, otherwise nothing will be selected in this set.104return (getPortlets().getPortletByName(pname) == null && p.getID().equals(last));
105 }
106 }
107 }
108else109 {
110 String subPane = null;
111int index = peid.indexOf(JetspeedResources.PATH_SUBPANE_SEPARATOR);
112if (index > -1)
113 {
114 subPane = peid.substring(index + 1);
115 peid = peid.substring(0, index);
116 }
117118if ( p.getID().equals(peid) ) // && subPane == null )119 {
120returntrue;
121 }
122123// is this the sub pane?124if (subPane!=null && p.getID().equals(subPane))
125 {
126// change the currently selected pane in the user session127// If this is not done, the tab selection works, but the128// content is picked up from the pane in the session!!129if(!p.getAttribute("_menustate", "open", rundata).equals("closed")) {
130SessionState state = ((JetspeedRunData)rundata).getPortletSessionState(getPortlets().getID());
131 state.setAttribute(JetspeedResources.PATH_PANEID_KEY, subPane);
132 }
133134returntrue;
135 }
136137// is the peid for this tab set?138if (getPortlets().getPortletByID(peid) != null)
139 {
140// its for another tab in this set141return false;
142 }
143if (subPane == null)
144 {
145if (last == null)
146 {
147return (getPortlets().getPortletAt( 0 ) == p);
148 }
149else150 {
151return (p.getID().equals(last));
152 }
153 }
154else155 {
156if (p.getID().equals( subPane ))
157 {
158// change the currently selected pane in the user session159// If this is not done, the tab selection works, but the160// content is picked up from the pane in the session!!161if(!p.getAttribute("_menustate", "open", rundata).equals("closed"))
162 {
163SessionState state =
164 ((JetspeedRunData)rundata).getPortletSessionState(getPortlets().getID());
165 state.setAttribute(JetspeedResources.PATH_PANEID_KEY, subPane);
166 }
167returntrue;
168 }
169 }
170 }
171return false;
172 }
173174/***175 * Builds the link to access to a given pane.176 *177 * @param rundata The request data.178 * @param portlet The portlet to build the link for by id.179 * @return DynamicURI A new Dynamic URI with the query parameter180 */181public DynamicURI getPortletURI( Portlet portlet, RunData rundata )
182 {
183JetspeedLink jsLink = null;
184try185 {
186 jsLink = JetspeedLinkFactory.getInstance(rundata);
187 }
188catch( Exception e)
189 {
190 logger.error("Exception", e);
191 }
192 DynamicURI uri = jsLink.getPaneById(portlet.getID());
193 JetspeedLinkFactory.putInstance(jsLink);
194195return uri;
196 }
197198/***199 * Returns the pane id of the parameter used for pane selection200 *201 * @param rundata The request data.202 * @param byParameter Set to true to look by query parameter first.203 * @return String The pane id for the selected pane.204 *205 */206public String retrievePaneID(RunData rundata, boolean byParameter)
207 {
208if (false == byParameter)
209return retrievePaneIDFromSession(rundata);
210211 String pane = rundata.getParameters().getString( getPaneParameter() );
212213if (pane == null)
214 {
215// the parameter is undefined, search for sticky value in session216 String id = getPortlets().getID();
217 pane = retrievePaneIDFromSession(rundata);
218 }
219220if(pane != null)
221 {
222int index = pane.indexOf(JetspeedResources.PATH_SUBPANE_SEPARATOR);
223if (index > -1)
224 {
225//return pane.substring(index + 1);226return pane.substring(0, index);
227 }
228 }
229230return pane;
231 }
232233/***234 * Returns the pane id from the session for pane selection of this portlet set / portal page235 *236 * @param rundata The request data.237 * @return String The pane id for the selected pane.238 *239 */240protected String retrievePaneIDFromSession(RunData rundata)
241 {
242// get the state for this portlet (portlet set) in this page in this session243SessionState state = ((JetspeedRunData)rundata).getPortletSessionState(getPortlets().getID());
244245// get the PANE_PARAMETER attribute246 String pane = (String) state.getAttribute(JetspeedResources.PATH_PANEID_KEY);
247248// if not yet defined, select the first portlet set249if (pane == null)
250 {
251// use default252if(getPortlets().size() > 0)
253 {
254 pane = getPortlets().getPortletAt(0).getID();
255 }
256 }
257258return pane;
259 }
260261/***262 * Saves the pane id to the session to remember selection state of menu or tab for this portlet set / portal page.263 *264 * @param rundata The request data.265 * @param id The tab id to save for this controller266 */267publicvoid savePaneID( RunData data, String id )
268 {
269// get the state for this portlet (portlet set) in this page in this session270SessionState state = ((JetspeedRunData)data).getPortletSessionState(getPortlets().getID());
271272// set the PANE_PARAMETER attribute273 state.setAttribute(JetspeedResources.PATH_PANEID_KEY, id);
274 }
275276/***277 * Sets the name of the parameter that will define which pane should278 * be displayed279 *280 * @deprecated281 *282 * @param name the selection parameter name283 */284publicvoid setParameterName( String name )
285 {
286 getConfig().setInitParameter( "parameter", name );
287 }
288289/***290 * Returns the name of the parameter used for pane selection291 *292 * @deprecated293 */294public String getParameterName()
295 {
296return getConfig().getInitParameter( "parameter", DEFAULT_PARAMETER )
297 + getPortlets().getName();
298 }
299300/***301 * Returns the name of the parameter used for pane selection302 *303 * @deprecated304 *305 */306public String retrievePaneName(RunData rundata)
307 {
308 String pane = rundata.getParameters().getString( getParameterName() );
309310if (pane == null)
311 {
312// the parameter is undefined, search for sticky value in session313 pane = (String)rundata.getUser().getTemp( "pane-"+getParameterName() );
314315if (pane == null)
316 {
317// use default318 pane = getConfig().getInitParameter( "defaultpane", "0" );
319 }
320 }
321322return pane;
323 }
324325/***326 * Sets the name of the parameter that will define which pane should327 * be displayed328 *329 * @deprecated330 *331 * @param name the selection parameter name332 */333publicvoid savePaneName( RunData data, String name )
334 {
335 data.getUser().setTemp( "pane-"+getParameterName(), name );
336 }
337338 }
339