KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > actions > controllers > VelocityControllerAction


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.modules.actions.controllers;
18
19 // Jetspeed stuff
20
import org.apache.jetspeed.portal.PortletController;
21 import org.apache.jetspeed.services.rundata.JetspeedRunData;
22 import org.apache.jetspeed.services.statemanager.SessionState;
23
24 // Turbine stuff
25
import org.apache.turbine.util.RunData;
26 import org.apache.turbine.services.velocity.TurbineVelocity;
27 import org.apache.turbine.modules.actions.VelocityAction;
28 import org.apache.turbine.services.localization.Localization;
29
30 // Velocity Stuff
31
import org.apache.velocity.context.Context;
32
33 /**
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 automatically
37  * associated through the registry PortletName
38  *
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  */

42 public abstract class VelocityControllerAction extends VelocityAction
43 {
44
45     /**
46      * This overrides the default Action.perform() to execute the
47      * doEvent() method. If that fails, then it will execute the
48      * doPerform() method instead.
49      *
50      * @param data A Turbine RunData object.
51      * @exception Exception, a generic exception.
52      */

53     protected void perform( RunData rundata )
54         throws Exception JavaDoc
55     {
56         // first try to see if there are some events registered for this
57
// action...
58
Context context = getContext(rundata);
59         if (context != null)
60         {
61             // if context is already defined, events have already been
62
// processed, call doPerform
63
doPerform(rundata);
64         }
65         else
66         {
67             context = TurbineVelocity.getContext();
68             rundata.getTemplateInfo().setTemplateContext("VelocityActionContext",context);
69             try
70             {
71                 executeEvents(rundata, context );
72             }
73             catch (NoSuchMethodException JavaDoc e)
74             {
75                 // no event selected
76
doPerform(rundata);
77             }
78         }
79     }
80
81     /**
82      * This method is used when you want to short circuit an Action
83      * 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      */

88     public void setTemplate(RunData data,
89                             String JavaDoc template)
90     {
91         getContext(data).put( "template" , template );
92     }
93
94     /**
95      * Return the Context needed by Velocity.
96      *
97      * @param RunData data
98      * @return Context, a context for web pages.
99      */

100     protected Context getContext(RunData data)
101     {
102         return (Context)data.getTemplateInfo()
103                             .getTemplateContext( "VelocityControllerContext" );
104     }
105
106     public void doPerform( RunData rundata, Context context )
107     {
108         PortletController controller = (PortletController)context.get( "controller" );
109
110         // if we're in customization mode for the given set, handle
111
// customization
112
if (((JetspeedRunData)rundata).getMode()==JetspeedRunData.CUSTOMIZE)
113         {
114             buildCustomizeContext( controller, context, rundata);
115             return;
116         }
117
118         buildNormalContext( controller, context, rundata);
119     }
120
121     /**
122      * Subclasses must override this method to provide default behavior
123      * for the portlet action
124      */

125     protected void buildCustomizeContext( PortletController controller,
126                                           Context context,
127                                           RunData rundata )
128     {
129         String JavaDoc name = controller.getPortlets().getName();
130         String JavaDoc template = (String JavaDoc)context.get("template");
131
132         int dotIdx = template.lastIndexOf('.');
133         if (dotIdx > -1)
134         {
135             template = template.substring(0,dotIdx)
136                        + "-customize.vm";
137         }
138         else
139         {
140             template = template+"-customize";
141         }
142         
143         setTemplate(rundata, template);
144         
145         context.put( "action", controller.getConfig().getInitParameter("action"));
146
147         // We want the save button to say different things based on whether we're about to save to persistent storage
148
// (Save and Apply) or just go the next screen (Apply).
149
JetspeedRunData jdata = (JetspeedRunData) rundata;
150
151         // get the customization state for this page
152
SessionState customizationState = jdata.getPageSessionState();
153
154         String JavaDoc saveLabel = null;
155         if (((String JavaDoc) customizationState.getAttribute("customize-paneName")).equalsIgnoreCase("*"))
156         {
157             saveLabel = Localization.getString(rundata, "CUSTOMIZER_SAVEAPPLY");
158         }
159         else
160         {
161             saveLabel = Localization.getString(rundata, "CUSTOMIZER_APPLY");
162         }
163         context.put("saveLabel", saveLabel);
164
165     }
166
167     /**
168      * Subclasses must override this method to provide default behavior
169      * for the portlet action
170      */

171     protected abstract void buildNormalContext( PortletController controller,
172                                                 Context context,
173                                                 RunData rundata );
174
175
176     /** Switch out of customize mode
177      */

178     public void doCancel(RunData data, Context context)
179     {
180         // nothing to do
181
}
182 }
183
Popular Tags