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 */16/*17 * PortletAction.java18 *19 *20 * $Id: PortletAction.java,v 1.8.2.1 2003/02/24 18:45:42 tkuebler Exp $21 *22 * Created on January 29, 2003, 2:52 PM23 */24packageorg.apache.jetspeed.modules.actions.portlets;
2526import org.apache.jetspeed.portal.Portlet;
27import org.apache.jetspeed.portal.portlets.GenericMVCPortlet;
28import org.apache.jetspeed.util.PortletSessionState;
29import org.apache.turbine.util.RunData;
30import org.apache.velocity.context.Context;
3132333435/***36 *37 * Abstract holder for the portlet specific methods required above and38 * beyond the standard turbine action.39 * 40 * Extends the PortletActionEvent, which41 * encapsulates the event handling feature.42 * 43 * @author tkuebler44 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>45 * @version $Id: PortletAction.java,v 1.8.2.1 2003/02/24 18:45:42 tkuebler Exp $46 * @stereotype moment-interval47 * 48 */495051/*52 * Note:53 * assumes a templated portlet, maybe should break this up into two classes54 * the portletaction would only have the build*Context methods55 * the templatedportletaction would have the rest56 * skipping this complexity for now since non-templated portlet actions can just57 * extend ActionEvent and forget about the context probably58 */59publicabstractclassPortletAction60extendsPortletActionEvent61 {
6263/*** Creates a new instance of PortletAction */64publicPortletAction()
65 {
66 }
6768publicvoid doPerform(RunData data)
69 throws Exception
70 {
7172// assumes a context for the portlet...73// should check for null and create one74 doPerform(data, getContext(data));
75 }
7677/***78 * You SHOULD override this method and implement it in your79 * action.80 *81 * @param data Turbine information.82 * @param context Context for web pages.83 * @exception Exception, a generic exception.84 */85publicabstractvoid doPerform(RunData data, Context context)
86 throws Exception;
8788/***89 * Return the Context90 *91 * @param RunData data92 * @return Context, a context for web pages.93 */94protected Context getContext(RunData data)
95 {
9697return (Context) data.getTemplateInfo().getTemplateContext("VelocityPortletContext");
98 }
99100/***101 * This method is used when you want to short circuit an Action102 * and change the template that will be executed next. The TTL103 * for this is a single request.104 *105 * @param data Turbine information.106 * @param template The template that will be executed next.107 */108publicvoid setTemplate(RunData data, String template)
109 {
110 setTemplate(data, template, false);
111 }
112113/***114 * This method is used when you want to short circuit an Action115 * and change the template that will be executed next. If116 * the <code>persistent</code> attribute is set to <i>true</i>117 * the template value is stored in the portlet's session state118 * and will be used until a new value has been set, either within 119 * portlet session or within the context. Regardless of the120 * value of <code>persistent</code>, the context will ALWAYS121 * have the correct "template" attribute set within.122 *123 * @param data Turbine information.124 * @param template The template that will be executed next.125 * @param persistent whether or not to make the template set 126 * persistent for the extent of the portlet session127 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>128 */129publicvoid setTemplate(RunData data, String template, boolean persistent)
130 {
131 Portlet portlet = getPortlet(getContext(data));
132133if (template != null)
134 {
135if (persistent)
136 {
137 PortletSessionState.setAttribute(
138 portlet,
139 data,
140 GenericMVCPortlet.TEMPLATE,
141 template);
142 }
143else144 {
145// Make sure there is no ssession residue ;)146 resetTemplate(data);
147 }
148149// Always make the current template is available within the150// context.151 getContext(data).put("template", template);
152 }
153154 }
155156/***157 * Clears the PortletSessionState of the <code>template</code>158 * attribute.159 */160protectedvoid resetTemplate(RunData data)
161 {
162 Portlet portlet = getPortlet(getContext(data));
163 PortletSessionState.clearAttribute(portlet, data, GenericMVCPortlet.PORTLET);
164 }
165166167168publicPortlet getPortlet(Context context)
169 {
170return (Portlet) context.get(GenericMVCPortlet.PORTLET);
171 }
172/***173 * Retrieves the template for this PortletAction's Portlet.174 * The Portlet <code>init()</code> will have already initialized175 * the template value within the current context in this order:<br/>176 * 1. From the PortletSessionState's "template" attribute <br />177 * 2. From the PortletConfig's "template" parameter.<br /><br />178 * However, the action may have overriden this value using179 * any of the <code>setTemplate()</code> methods.180 * @param Context context the context for this action's portlet.181 * @return String Current view template for this action's portlet.182 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>183 */184public String getTemplate(Context context)
185 {
186187return (String) context.get(GenericMVCPortlet.TEMPLATE);
188 }
189190protectedabstractvoid buildConfigureContext(Portlet portlet, Context context, RunData data)
191 throws Exception;
192193protectedabstractvoid buildMaximizedContext(Portlet portlet, Context context, RunData data)
194 throws Exception;
195196protectedabstractvoid buildNormalContext(Portlet portlet, Context context, RunData data)
197 throws Exception;
198 }