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   * PortletAction.java
18   *
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 PM
23   */
24  package org.apache.jetspeed.modules.actions.portlets;
25  
26  import org.apache.jetspeed.portal.Portlet;
27  import org.apache.jetspeed.portal.portlets.GenericMVCPortlet;
28  import org.apache.jetspeed.util.PortletSessionState;
29  import org.apache.turbine.util.RunData;
30  import org.apache.velocity.context.Context;
31  
32  
33  
34  
35  /***
36   *
37   * Abstract holder for the portlet specific methods required above and
38   * beyond the standard turbine action.
39   * 
40   * Extends the PortletActionEvent, which
41   * encapsulates the event handling feature.
42   * 
43   * @author  tkuebler
44   * @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-interval
47   * 
48   */
49  
50  
51  /*
52   * Note:
53   * assumes a templated portlet, maybe should break this up into two classes
54   * the portletaction would only have the build*Context methods
55   * the templatedportletaction would have the rest
56   * skipping this complexity for now since non-templated portlet actions can just
57   * extend ActionEvent and forget about the context probably
58   */
59  public abstract class PortletAction
60      extends PortletActionEvent
61    {
62  
63      /*** Creates a new instance of PortletAction */
64      public PortletAction()
65        {
66        }
67  
68      public void doPerform(RunData data)
69                     throws Exception
70        {
71  
72          // assumes a context for the portlet...
73          // should check for null and create one
74          doPerform(data, getContext(data));
75        }
76  
77      /***
78       * You SHOULD override this method and implement it in your
79       * action.
80       *
81       * @param data Turbine information.
82       * @param context Context for web pages.
83       * @exception Exception, a generic exception.
84       */
85      public abstract void doPerform(RunData data, Context context)
86                              throws Exception;
87  
88      /***
89       * Return the Context
90       *
91       * @param RunData data
92       * @return Context, a context for web pages.
93       */
94      protected Context getContext(RunData data)
95        {
96  
97          return (Context) data.getTemplateInfo().getTemplateContext("VelocityPortletContext");
98        }
99  
100     /***
101      * This method is used when you want to short circuit an Action
102      * and change the template that will be executed next.  The TTL
103      * for this is a single request.
104      *
105      * @param data Turbine information.
106      * @param template The template that will be executed next.
107      */
108     public void setTemplate(RunData data, String template)
109       {
110         setTemplate(data, template, false);
111       }
112       
113     /***
114      * This method is used when you want to short circuit an Action
115      * and change the template that will be executed next. If
116      * the <code>persistent</code> attribute is set to <i>true</i>
117      * the template value is stored in the portlet's session state
118      * and will be used until a new value has been set, either within 
119      * portlet session or within the context.  Regardless of the
120      * value of <code>persistent</code>, the context will ALWAYS
121      * 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 session
127      * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
128      */
129 	public void setTemplate(RunData data, String template, boolean persistent)
130 	{
131 		Portlet portlet = getPortlet(getContext(data));
132 
133 		if (template != null)
134 		{
135 			if (persistent)
136 			{
137 				PortletSessionState.setAttribute(
138 					portlet,
139 					data,
140 					GenericMVCPortlet.TEMPLATE,
141 					template);
142 			}
143 			else
144 			{
145 				// Make sure there is no ssession residue ;)
146 				resetTemplate(data);
147 			}
148 
149 			// Always make the current template is available within the
150 			// context.
151 			getContext(data).put("template", template);
152 		}
153 
154 	}
155 	
156 	/***
157 	 * Clears the PortletSessionState of the <code>template</code>
158 	 * attribute.
159 	 */
160 	protected void resetTemplate(RunData data)
161 	{
162 		Portlet portlet = getPortlet(getContext(data));
163 		PortletSessionState.clearAttribute(portlet, data, GenericMVCPortlet.PORTLET);
164 	}
165       
166   
167       
168     public Portlet getPortlet(Context context)
169     {
170     	return (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 initialized
175 	 * 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 using
179 	 * 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      */
184 	public String getTemplate(Context context)
185 	{		
186 
187 		return (String) context.get(GenericMVCPortlet.TEMPLATE);
188 	}
189 
190     protected abstract void buildConfigureContext(Portlet portlet, Context context, RunData data)
191                                            throws Exception;
192 
193     protected abstract void buildMaximizedContext(Portlet portlet, Context context, RunData data)
194                                            throws Exception;
195 
196     protected abstract void buildNormalContext(Portlet portlet, Context context, RunData data)
197                                         throws Exception;
198   }