1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.modules.actions.controllers;
18
19
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
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
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
55 {
56
57
58 Context context = getContext(rundata);
59 if (context != null)
60 {
61
62
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 e)
74 {
75
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 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
111
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 name = controller.getPortlets().getName();
130 String template = (String)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
148
149 JetspeedRunData jdata = (JetspeedRunData) rundata;
150
151
152 SessionState customizationState = jdata.getPageSessionState();
153
154 String saveLabel = null;
155 if (((String) 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
181 }
182 }