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.modules.actions.controllers;
1819// Jetspeed stuff20import org.apache.jetspeed.portal.PortletController;
21import org.apache.jetspeed.services.rundata.JetspeedRunData;
22import org.apache.jetspeed.services.statemanager.SessionState;
2324// Turbine stuff25import org.apache.turbine.util.RunData;
26import org.apache.turbine.services.velocity.TurbineVelocity;
27import org.apache.turbine.modules.actions.VelocityAction;
28import org.apache.turbine.services.localization.Localization;
2930// Velocity Stuff31import org.apache.velocity.context.Context;
3233/***34 * An abstract action class to build VelocityPortlet actions.35 * 36 * <p>Don't call it from the URL, the Portlet and the Action are automatically37 * associated through the registry PortletName38 * 39 * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>40 * @author <a href="mailto:re_carrasco@bco011.sonda.cl">Roberto Carrasco</a>41 */42publicabstractclassVelocityControllerActionextends VelocityAction
43 {
4445/***46 * This overrides the default Action.perform() to execute the47 * doEvent() method. If that fails, then it will execute the48 * doPerform() method instead.49 *50 * @param data A Turbine RunData object.51 * @exception Exception, a generic exception.52 */53protectedvoid perform( RunData rundata )
54 throws Exception
55 {
56// first try to see if there are some events registered for this57// action...58 Context context = getContext(rundata);
59if (context != null)
60 {
61// if context is already defined, events have already been 62// processed, call doPerform63 doPerform(rundata);
64 }
65else66 {
67 context = TurbineVelocity.getContext();
68 rundata.getTemplateInfo().setTemplateContext("VelocityActionContext",context);
69try70 {
71 executeEvents(rundata, context );
72 }
73catch (NoSuchMethodException e)
74 {
75// no event selected76 doPerform(rundata);
77 }
78 }
79 }
8081/***82 * This method is used when you want to short circuit an Action83 * and change the template that will be executed next.84 *85 * @param data Turbine information.86 * @param template The template that will be executed next.87 */88publicvoid setTemplate(RunData data,
89 String template)
90 {
91 getContext(data).put( "template" , template );
92 }
9394/***95 * Return the Context needed by Velocity.96 *97 * @param RunData data98 * @return Context, a context for web pages.99 */100protected Context getContext(RunData data)
101 {
102return (Context)data.getTemplateInfo()
103 .getTemplateContext( "VelocityControllerContext" );
104 }
105106publicvoid doPerform( RunData rundata, Context context )
107 {
108PortletController controller = (PortletController)context.get( "controller" );
109110// if we're in customization mode for the given set, handle 111// customization112if (((JetspeedRunData)rundata).getMode()==JetspeedRunData.CUSTOMIZE)
113 {
114 buildCustomizeContext( controller, context, rundata);
115return;
116 }
117118 buildNormalContext( controller, context, rundata);
119 }
120121/*** 122 * Subclasses must override this method to provide default behavior 123 * for the portlet action124 */125protectedvoid buildCustomizeContext( PortletController controller,
126 Context context,
127 RunData rundata )
128 {
129 String name = controller.getPortlets().getName();
130 String template = (String)context.get("template");
131132int dotIdx = template.lastIndexOf('.');
133if (dotIdx > -1)
134 {
135 template = template.substring(0,dotIdx)
136 + "-customize.vm";
137 }
138else139 {
140 template = template+"-customize";
141 }
142143 setTemplate(rundata, template);
144145 context.put( "action", controller.getConfig().getInitParameter("action"));
146147// We want the save button to say different things based on whether we're about to save to persistent storage148// (Save and Apply) or just go the next screen (Apply).149JetspeedRunData jdata = (JetspeedRunData) rundata;
150151// get the customization state for this page152SessionState customizationState = jdata.getPageSessionState();
153154 String saveLabel = null;
155if (((String) customizationState.getAttribute("customize-paneName")).equalsIgnoreCase("*"))
156 {
157 saveLabel = Localization.getString(rundata, "CUSTOMIZER_SAVEAPPLY");
158 }
159else160 {
161 saveLabel = Localization.getString(rundata, "CUSTOMIZER_APPLY");
162 }
163 context.put("saveLabel", saveLabel);
164165 }
166167/*** 168 * Subclasses must override this method to provide default behavior 169 * for the portlet action170 */171protectedabstractvoid buildNormalContext( PortletController controller,
172 Context context,
173 RunData rundata );
174175176/*** Switch out of customize mode177 */178publicvoid doCancel(RunData data, Context context)
179 {
180// nothing to do181 }
182 }