KickJava   Java API By Example, From Geeks To Geeks.

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


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
import java.util.List JavaDoc;
51
52 import org.apache.fulcrum.intake.Retrievable;
53 import org.apache.fulcrum.intake.model.Field;
54 import org.apache.fulcrum.intake.model.Group;
55 import org.apache.fulcrum.parser.ParameterParser;
56 import org.apache.log4j.Logger;
57 import org.apache.turbine.RunData;
58 import org.apache.turbine.TemplateContext;
59 import org.apache.turbine.TemplateSecureAction;
60 import org.apache.turbine.tool.IntakeTool;
61 import org.tigris.scarab.om.Module;
62 import org.tigris.scarab.om.ScarabUser;
63 import org.tigris.scarab.screens.Default;
64 import org.tigris.scarab.services.security.ScarabSecurity;
65 import org.tigris.scarab.tools.ScarabLocalizationTool;
66 import org.tigris.scarab.tools.ScarabRequestTool;
67 import org.tigris.scarab.tools.localization.L10NKeySet;
68 import org.tigris.scarab.tools.localization.LocalizationKey;
69 import org.tigris.scarab.util.ScarabConstants;
70
71 /**
72  * This is a badly named class which is essentially equivalent to the
73  * Default.java Screen except that it has a few helper methods.
74  *
75  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
76  * @version $Id: RequireLoginFirstAction.java 9280 2004-11-27 01:11:13Z jorgeuriarte $
77  */

78 public abstract class RequireLoginFirstAction extends TemplateSecureAction
79 {
80     private static final Logger LOG = Logger.getLogger("org.tigris.scarab");
81
82     protected static final LocalizationKey ERROR_MESSAGE = L10NKeySet.MoreInformationWasRequired;
83     protected static final LocalizationKey NO_PERMISSION_MESSAGE = L10NKeySet.YouDoNotHavePermissionToAction;
84     protected static final LocalizationKey DEFAULT_MSG = L10NKeySet.YourChangesWereSaved;
85     protected static final LocalizationKey EMAIL_ERROR = L10NKeySet.CouldNotSendEmail;
86     protected static final LocalizationKey EMAIL_ERROR2 = L10NKeySet.CouldNotSendEmail2;
87     protected static final LocalizationKey NO_CHANGES_MADE = L10NKeySet.NoChangesMade;
88
89
90     /**
91      * sets the template to template.login if the user hasn't logged in yet
92      */

93     protected boolean isAuthorized(RunData data) throws Exception JavaDoc
94     {
95         boolean auth = false;
96         String JavaDoc perm = getRequiredPermission(data);
97         TemplateContext context = getTemplateContext(data);
98         ScarabRequestTool scarabR = getScarabRequestTool(context);
99         ScarabLocalizationTool l10n = getLocalizationTool(context);
100         Module currentModule = scarabR.getCurrentModule();
101         ScarabUser user = (ScarabUser)data.getUser();
102
103         if (ScarabSecurity.NONE.equals(perm))
104         {
105             if (!user.hasLoggedIn())
106             {
107                 scarabR.setInfoMessage(L10NKeySet.LoginToAccountWithPermissions);
108                 Default.setTargetLogin(data);
109                 scarabR.setCurrentModule(null);
110             }
111             else
112             {
113                 auth = true;
114             }
115         }
116         else if (perm == null)
117         {
118             scarabR.setAlertMessage(L10NKeySet.ActionNotAssignedPermission);
119         }
120         else
121         {
122             if (currentModule == null)
123             {
124                 scarabR.setInfoMessage(L10NKeySet.SelectModuleToWorkIn);
125                 Default.setTargetSelectModule(data);
126             }
127             else if (! user.hasLoggedIn()
128                 || !user.hasPermission(perm, currentModule))
129             {
130                 scarabR.setInfoMessage(L10NKeySet.LoginToAccountWithPermissions);
131
132                 Default.setTargetLogin(data);
133                 scarabR.setCurrentModule(null);
134             }
135             else
136             {
137                 auth = true;
138             }
139         }
140         return auth;
141     }
142
143     /**
144      * Helper method to retrieve the IntakeTool from the Context
145      */

146     public IntakeTool getIntakeTool(TemplateContext context)
147     {
148         return (IntakeTool)context.get(ScarabConstants.INTAKE_TOOL);
149     }
150
151     /**
152      * Helper method to retrieve the ScarabLocalizationTool from the Context
153      */

154     protected final ScarabLocalizationTool
155         getLocalizationTool(TemplateContext context)
156     {
157         return (ScarabLocalizationTool)
158             context.get(ScarabConstants.LOCALIZATION_TOOL);
159     }
160
161     /**
162      * Helper method to retrieve the ScarabRequestTool from the Context
163      */

164     public ScarabRequestTool getScarabRequestTool(TemplateContext context)
165     {
166         return (ScarabRequestTool)context
167             .get(ScarabConstants.SCARAB_REQUEST_TOOL);
168     }
169
170     /**
171      * Returns the current template that is being executed, otherwisse
172      * it returns null
173      */

174     public String JavaDoc getCurrentTemplate(RunData data)
175     {
176         return data.getParameters()
177                    .getString(ScarabConstants.TEMPLATE, null);
178     }
179
180     /**
181      * Returns the current template that is being executed, otherwisse
182      * it returns defaultValue.
183      */

184     public String JavaDoc getCurrentTemplate(RunData data, String JavaDoc defaultValue)
185     {
186         return data.getParameters()
187                    .getString(ScarabConstants.TEMPLATE, defaultValue);
188     }
189
190     /**
191      * Returns the nextTemplate to be executed. Otherwise returns null.
192      */

193     public String JavaDoc getNextTemplate(RunData data)
194     {
195         return data.getParameters()
196                    .getString(ScarabConstants.NEXT_TEMPLATE, null);
197     }
198
199     /**
200      * Returns the nextTemplate to be executed. Otherwise returns defaultValue.
201      */

202     public String JavaDoc getNextTemplate(RunData data, String JavaDoc defaultValue)
203     {
204         return data.getParameters()
205                    .getString(ScarabConstants.NEXT_TEMPLATE, defaultValue);
206     }
207
208     /**
209      * Returns the last template to be cancelled back to.
210      */

211     public String JavaDoc getLastTemplate(RunData data)
212     {
213         return data.getParameters()
214                    .getString(ScarabConstants.LAST_TEMPLATE, null);
215     }
216
217     /**
218      * Returns the cancelTemplate to be executed. Otherwise returns null.
219      */

220     public String JavaDoc getCancelTemplate(RunData data)
221     {
222         return data.getParameters()
223                    .getString(ScarabConstants.CANCEL_TEMPLATE, null);
224     }
225
226     /**
227      * Returns the cancelTemplate to be executed.
228      * Otherwise returns defaultValue.
229      */

230     public String JavaDoc getCancelTemplate(RunData data, String JavaDoc defaultValue)
231     {
232         return data.getParameters()
233                    .getString(ScarabConstants.CANCEL_TEMPLATE,
234                               defaultValue);
235     }
236
237     /**
238      * Returns the backTemplate to be executed. Otherwise returns null.
239      */

240     public String JavaDoc getBackTemplate(RunData data)
241     {
242         return data.getParameters()
243                    .getString(ScarabConstants.BACK_TEMPLATE, null);
244     }
245
246     /**
247      * Returns the backTemplate to be executed.
248      * Otherwise returns defaultValue.
249      */

250     public String JavaDoc getBackTemplate(RunData data, String JavaDoc defaultValue)
251     {
252         return data.getParameters()
253                    .getString(ScarabConstants.BACK_TEMPLATE, defaultValue);
254     }
255
256     /**
257      * Returns the other template that is being executed, otherwise
258      * it returns null.
259      */

260     public String JavaDoc getOtherTemplate(RunData data)
261     {
262         return data.getParameters()
263                    .getString(ScarabConstants.OTHER_TEMPLATE);
264     }
265
266     public void doSave(RunData data, TemplateContext context)
267         throws Exception JavaDoc
268     {
269     }
270
271     public void doGonext(RunData data, TemplateContext context)
272         throws Exception JavaDoc
273     {
274         setTarget(data, getNextTemplate(data));
275     }
276
277     public void doGotoothertemplate(RunData data,
278                                      TemplateContext context)
279         throws Exception JavaDoc
280     {
281         data.getParameters().setString(ScarabConstants.CANCEL_TEMPLATE,
282                                        getCurrentTemplate(data));
283         setTarget(data, getOtherTemplate(data));
284     }
285
286     public void doRefresh(RunData data, TemplateContext context)
287         throws Exception JavaDoc
288     {
289         setTarget(data, getCurrentTemplate(data));
290     }
291
292     public void doRefreshresultsperpage(RunData data, TemplateContext context)
293         throws Exception JavaDoc
294     {
295         ParameterParser params = data.getParameters();
296         int oldResultsPerPage = params.getInt("oldResultsPerPage");
297         int newResultsPerPage = params.getInt("resultsPerPage");
298         int oldPageNum = params.getInt("pageNum");
299         
300         //
301
// We want to display whichever page contains the first issue
302
// on the old page.
303
//
304
int firstItem = (oldPageNum - 1) * oldResultsPerPage + 1;
305         int newPageNum = (firstItem / newResultsPerPage) + 1;
306         params.remove("oldResultsPerPage");
307         params.remove("pageNum");
308         params.add("pageNum", newPageNum);
309         setTarget(data, getCurrentTemplate(data));
310     }
311
312
313     public void doReset(RunData data, TemplateContext context)
314         throws Exception JavaDoc
315     {
316         IntakeTool intake = getIntakeTool(context);
317         intake.removeAll();
318         setTarget(data, getCurrentTemplate(data));
319     }
320         
321     public void doCancel(RunData data, TemplateContext context)
322         throws Exception JavaDoc
323     {
324         setTarget(data, getCancelTemplate(data));
325     }
326
327     public void doDone(RunData data, TemplateContext context)
328         throws Exception JavaDoc
329     {
330         doSave(data, context);
331         doCancel(data, context);
332     }
333
334     protected Logger log()
335     {
336         return LOG;
337     }
338
339     /**
340      * Flag that marks the action as requiring a permission mapping in
341      * Scarab.properties. The default is true, so actions that only
342      * require login (or only require a permission given some critieria
343      * available in the arguments), should override this method.
344      *
345      * @param data a <code>RunData</code> value
346      * @return a <code>boolean</code> value
347      */

348     protected String JavaDoc getRequiredPermission(RunData data)
349     {
350         return ScarabSecurity.getActionPermission(data.getAction());
351     }
352
353     /**
354      * Check if the objects have duplicate sequence numbers set
355      * @param list List of 'om' objects that need to be checked for duplicate
356      * sequence.
357      * @param intake IntakeTool
358      * @param groupName Intake group name
359      * @param fieldName Intake field name
360      * @param dedupeSeq sequence number set for Duplicate Check element in the
361      * case of attribute groups
362      * @return boolean TRUE if there are duplicate sequence numbers.
363      */

364     public boolean areThereDupeSequences(List JavaDoc list, IntakeTool intake,
365                String JavaDoc groupName, String JavaDoc fieldName, int dedupeSeq)
366         throws Exception JavaDoc
367     {
368         boolean dupeSequenceFound = false;
369         int listSize = (list==null)?0:list.size();
370         Field order1 = null;
371         Field order2 = null;
372         for (int i = 0; i < listSize && !dupeSequenceFound; i++)
373         {
374             Retrievable obj1 = (Retrievable)list.get(i);
375             Group group1 = intake.get(groupName,obj1.getQueryKey(),false);
376             order1 = group1.get(fieldName);
377
378             if (dedupeSeq > 0 && order1.toString().equals(
379                                           Integer.toString(dedupeSeq)))
380             {
381                 dupeSequenceFound = true;
382             }
383
384             for (int j = i - 1; j >= 0 && !dupeSequenceFound; j--)
385             {
386                 Retrievable obj2 = (Retrievable)list.get(j);
387                 Group group2 = intake.get(groupName,obj2.getQueryKey(),
388                                              false);
389                 order2 = group2.get(fieldName);
390                 if (order1.toString().equals(order2.toString()))
391                 {
392                     dupeSequenceFound = true;
393                 }
394             }
395         }
396         return dupeSequenceFound;
397     }
398 }
399
Popular Tags