KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > actions > base > ScarabTemplateAction


1 package org.tigris.scarab.actions.base;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48  
49 // Java Stuff
50

51  // Turbine Stuff
52
import org.apache.fulcrum.parser.ParameterParser;
53 import org.apache.log4j.Logger;
54 import org.apache.turbine.RunData;
55 import org.apache.turbine.TemplateAction;
56 import org.apache.turbine.TemplateContext;
57 import org.apache.turbine.tool.IntakeTool;
58 import org.tigris.scarab.tools.ScarabLocalizationTool;
59 import org.tigris.scarab.tools.ScarabRequestTool;
60 import org.tigris.scarab.util.ScarabConstants;
61
62 /**
63  * This is a helper class that extends TemplateAction to add
64  * a couple methods useful for Scarab.
65  *
66  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
67  * @version $Id: ScarabTemplateAction.java 9255 2004-11-14 21:07:04Z dep4b $
68  */

69 public abstract class ScarabTemplateAction extends TemplateAction
70 {
71     private static final Logger LOG = Logger.getLogger("org.tigris.scarab");
72
73     protected static final String JavaDoc ERROR_MESSAGE =
74         "MoreInformationWasRequired";
75     protected static final String JavaDoc NO_PERMISSION_MESSAGE =
76         "YouDoNotHavePermissionToAction";
77     protected static final String JavaDoc DEFAULT_MSG = "YourChangesWereSaved";
78     protected static final String JavaDoc EMAIL_ERROR = "CouldNotSendEmail";
79
80     /**
81      * Helper method to retrieve the IntakeTool from the Context
82      */

83     public IntakeTool getIntakeTool(TemplateContext context)
84     {
85         return (IntakeTool) getTool(context,
86                 ScarabConstants.INTAKE_TOOL);
87     }
88
89     /**
90      * Helper method to retrieve the ScarabRequestTool from the Context
91      */

92     public ScarabRequestTool getScarabRequestTool(TemplateContext context)
93     {
94         return (ScarabRequestTool)context
95             .get(ScarabConstants.SCARAB_REQUEST_TOOL);
96     }
97
98     /**
99      * Helper method to retrieve the ScarabLocalizationTool from the Context
100      */

101     protected final ScarabLocalizationTool
102         getLocalizationTool(TemplateContext context)
103     {
104         return (ScarabLocalizationTool)
105             context.get(ScarabConstants.LOCALIZATION_TOOL);
106     }
107
108     /**
109      * Returns the current template that is being executed, otherwisse
110      * it returns null
111      */

112     public String JavaDoc getCurrentTemplate(RunData data)
113     {
114         return data.getParameters()
115                    .getString(ScarabConstants.TEMPLATE, null);
116     }
117
118     /**
119      * Returns the current template that is being executed, otherwisse
120      * it returns defaultValue.
121      */

122     public String JavaDoc getCurrentTemplate(RunData data, String JavaDoc defaultValue)
123     {
124         return data.getParameters()
125                    .getString(ScarabConstants.TEMPLATE, defaultValue);
126     }
127
128     /**
129      * Returns the nextTemplate to be executed. Otherwise returns null.
130      */

131     public String JavaDoc getNextTemplate(RunData data)
132     {
133         return data.getParameters()
134                    .getString(ScarabConstants.NEXT_TEMPLATE, null);
135     }
136
137     /**
138      * Returns the nextTemplate to be executed. Otherwise returns defaultValue.
139      */

140     public String JavaDoc getNextTemplate(RunData data, String JavaDoc defaultValue)
141     {
142         return data.getParameters()
143                    .getString(ScarabConstants.NEXT_TEMPLATE, defaultValue);
144     }
145
146     /**
147      * Returns the last template to be cancelled back to.
148      */

149     public String JavaDoc getLastTemplate(RunData data)
150     {
151         return data.getParameters()
152                    .getString(ScarabConstants.LAST_TEMPLATE, null);
153     }
154
155     /**
156      * Returns the cancelTemplate to be executed. Otherwise returns null.
157      */

158     public String JavaDoc getCancelTemplate(RunData data)
159     {
160         return data.getParameters()
161                    .getString(ScarabConstants.CANCEL_TEMPLATE, null);
162     }
163
164     /**
165      * Returns the cancelTemplate to be executed.
166      * Otherwise returns defaultValue.
167      */

168     public String JavaDoc getCancelTemplate(RunData data, String JavaDoc defaultValue)
169     {
170         return data.getParameters()
171                    .getString(ScarabConstants.CANCEL_TEMPLATE,
172                               defaultValue);
173     }
174
175     /**
176      * Returns the backTemplate to be executed. Otherwise returns null.
177      */

178     public String JavaDoc getBackTemplate(RunData data)
179     {
180         return data.getParameters()
181                    .getString(ScarabConstants.BACK_TEMPLATE, null);
182     }
183
184     /**
185      * Returns the backTemplate to be executed.
186      * Otherwise returns defaultValue.
187      */

188     public String JavaDoc getBackTemplate(RunData data, String JavaDoc defaultValue)
189     {
190         return data.getParameters()
191                    .getString(ScarabConstants.BACK_TEMPLATE, defaultValue);
192     }
193
194     /**
195      * Returns the other template that is being executed, otherwise
196      * it returns null.
197      */

198     public String JavaDoc getOtherTemplate(RunData data)
199     {
200         return data.getParameters()
201                    .getString(ScarabConstants.OTHER_TEMPLATE);
202     }
203
204     public void doSave(RunData data, TemplateContext context)
205         throws Exception JavaDoc
206     {
207     }
208
209     public void doGonext(RunData data, TemplateContext context)
210         throws Exception JavaDoc
211     {
212         setTarget(data, getNextTemplate(data));
213     }
214
215     public void doGotoothertemplate(RunData data,
216                                      TemplateContext context)
217         throws Exception JavaDoc
218     {
219         data.getParameters().setString(ScarabConstants.CANCEL_TEMPLATE,
220                                        getCurrentTemplate(data));
221         setTarget(data, getOtherTemplate(data));
222     }
223
224     public void doRefresh(RunData data, TemplateContext context)
225         throws Exception JavaDoc
226     {
227         setTarget(data, getCurrentTemplate(data));
228     }
229
230     public void doReset(RunData data, TemplateContext context)
231         throws Exception JavaDoc
232     {
233         IntakeTool intake = getIntakeTool(context);
234         intake.removeAll();
235         setTarget(data, getCurrentTemplate(data));
236     }
237         
238     public void doCancel(RunData data, TemplateContext context)
239         throws Exception JavaDoc
240     {
241         setTarget(data, getCancelTemplate(data));
242     }
243
244     public void doDone(RunData data, TemplateContext context)
245         throws Exception JavaDoc
246     {
247         doSave(data, context);
248         doCancel(data, context);
249     }
250
251     protected Logger log()
252     {
253         return LOG;
254     }
255
256     public void doRefreshresultsperpage(RunData data, TemplateContext context)
257         throws Exception JavaDoc
258     {
259         ParameterParser params = data.getParameters();
260         int oldResultsPerPage = params.getInt("oldResultsPerPage");
261         int newResultsPerPage = params.getInt("resultsPerPage");
262         int oldPageNum = params.getInt("pageNum");
263         
264         //
265
// We want to display whichever page contains the first issue
266
// on the old page.
267
//
268
int firstItem = (oldPageNum - 1) * oldResultsPerPage + 1;
269         int newPageNum = (firstItem / newResultsPerPage) + 1;
270         params.remove("oldResultsPerPage");
271         params.remove("pageNum");
272         params.add("pageNum", newPageNum);
273         setTarget(data, getCurrentTemplate(data));
274     }
275 }
276
Popular Tags