View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: DialogForm.java,v $
31   * Revision 1.7  2005/04/29 16:32:00  colinmacleod
32   * Added clearReset as implementation for both
33   * clear and reset methods.
34   *
35   * Revision 1.6  2005/04/09 18:04:18  colinmacleod
36   * Changed copyright text to GPL v2 explicitly.
37   *
38   * Revision 1.5  2005/03/10 10:35:09  colinmacleod
39   * Added default forwards.
40   *
41   * Revision 1.4  2005/01/19 12:56:36  colinmacleod
42   * Added OperationNotSupportedException to clear.
43   *
44   * Revision 1.3  2005/01/07 08:08:24  colinmacleod
45   * Moved up a version number.
46   * Changed copyright notices to 2005.
47   * Updated the documentation:
48   *   - started working on multiproject:site docu.
49   *   - changed the logo.
50   * Added checkstyle and fixed LOADS of style issues.
51   * Added separate thirdparty subproject.
52   * Added struts (in web), util and webgui (in webtheme) from ivata op.
53   *
54   * Revision 1.2  2004/12/23 21:28:32  colinmacleod
55   * Modifications to add ivata op compatibility.
56   *
57   * Revision 1.1.1.1  2004/05/16 20:40:32  colinmacleod
58   * Ready for 0.1 release
59   *
60   * Revision 1.3  2004/03/21 21:16:08  colinmacleod
61   * Shortened name to ivata op.
62   *
63   * Revision 1.2  2004/02/01 22:00:33  colinmacleod
64   * Added full names to author tags
65   *
66   * Revision 1.1.1.1  2004/01/27 20:57:55  colinmacleod
67   * Moved ivata op to SourceForge.
68   *
69   * Revision 1.1.1.1  2003/10/13 20:49:59  colin
70   * Restructured portal into subprojects
71   *
72   * Revision 1.6  2003/04/14 07:12:33  peter
73   * added helpKey field
74   *
75   * Revision 1.5  2003/02/24 19:08:17  colin
76   * *** empty log message ***
77   *
78   * Revision 1.4  2003/02/04 17:37:37  colin
79   * new interface
80   * added onConfirm, onDelete and clear
81   *
82   * Revision 1.3  2003/01/31 16:16:39  colin
83   * Fixed bugs on DeleteWarn & DeleteConfirm.
84   *
85   * Revision 1.1  2003/01/31 13:57:40  colin
86   * first version
87   * -----------------------------------------------------------------------------
88   */
89  package com.ivata.mask.web.struts;
90  import java.util.Locale;
91  
92  import javax.naming.OperationNotSupportedException;
93  import javax.servlet.ServletException;
94  import javax.servlet.http.HttpServletRequest;
95  import javax.servlet.http.HttpSession;
96  
97  import org.apache.log4j.Logger;
98  import org.apache.struts.Globals;
99  import org.apache.struts.action.ActionErrors;
100 import org.apache.struts.action.ActionForm;
101 import org.apache.struts.action.ActionMapping;
102 
103 import com.ivata.mask.util.StringHandling;
104 import com.ivata.mask.util.SystemException;
105 import com.ivata.mask.validation.ValidationErrors;
106 /***
107  * <p>
108  * This form contains buttons and attributes commonly required in <i>ivata masks
109  * </i> forms. You should override it and call <code>reset</code> on this form
110  * from your form.
111  * </p>
112  *
113  * <p>
114  * This class originally appeared as part of <a
115  * href="http://www.ivata.org">ivata op </a> and <a
116  * href="http://www.ivata.com/portal">ivata team portal </a>.
117  * </p>.
118  *
119  * @since ivata masks 0.4 (2003-01-31)
120  * @author Colin MacLeod
121  * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
122  * @version $Revision: 1.7 $
123  */
124 public abstract class DialogForm extends ActionForm {
125     /***
126      * Refer to {@link Logger}.
127      */
128     private static Logger log = Logger.getLogger(DialogForm.class);
129     /***
130      * <p>
131      * If non- <code>null</code> or not empty, specifies that the form
132      * contents should be amended/added and the window left open.
133      * </p>
134      */
135     private String apply = null;
136     /***
137      * <p>
138      * Localization messages resource bundle to use for <code>deleteKey</code>.
139      * </p>
140      */
141     private String bundle = null;
142     /***
143      * <p>
144      * Specifies whether or not we are adding a new element or clearing the form
145      * fields.
146      * </p>
147      */
148     private String clear = null;
149     /***
150      * Refer to {@link #getDefaultForwardApply}.
151      */
152     private String defaultForwardApply = "apply";
153     /***
154      * Refer to {@link #getDefaultForwardOk}.
155      */
156     private String defaultForwardDelete = "delete";
157     /***
158      * Refer to {@link #getDefaultForwardOk}.
159      */
160     private String defaultForwardOk = "ok";
161     /***
162      * <p>
163      * If non- <code>null</code> or not empty, really delete the entry.
164      * </p>
165      */
166     private String deleteConfirm = null;
167     /***
168      * <p>
169      * If non- <code>null</code>, indicates a key to be displayed to warn
170      * before deletion of the items in this form.
171      * </p>
172      *
173      * <p>
174      * The <code>FormTag</code> will check this value (or it's attribute
175      * equivalent) and display JavaScript with the localized string this key
176      * represents.
177      * </p>
178      */
179     private String deleteKey = null;
180     /***
181      * <p>
182      * If non- <code>null</code> or not empty, show a message before deleting.
183      * </p>
184      */
185     private String deleteWarn = null;
186     /***
187      * <p>
188      * Stores the correct help key.
189      * </p>
190      */
191     private String helpKey = null;
192     /***
193      * <p>
194      * If non- <code>null</code> or not empty, specifies that the form
195      * contents should be amended/added.
196      * </p>
197      */
198     private String ok = null;
199     /***
200      * <p>
201      * Clear all bean properties to their default state.The difference between
202      * this and <code>reset</code> is that all properties are changed,
203      * regardless of current request state.
204      * </p>
205      * @throws OperationNotSupported if this method must be overridden to
206      * clear all form properties.
207      */
208     protected void clear()
209         throws OperationNotSupportedException {
210         clearReset();
211     }
212     /***
213      * Private implementation of <code>clear</code> and <code>reset</code> to
214      * avoid repetition.
215      */
216     private void clearReset() {
217         setOk(null);
218         setApply(null);
219         setClear(null);
220         setDeleteWarn(null);
221         setDeleteConfirm(null);
222     }
223     /***
224      * <p>
225      * If non- <code>null</code> or not empty, specifies that the form
226      * contents should be amended/added and the window left open.
227      * </p>
228      *
229      * @return the current value of apply.
230      */
231     public final String getApply() {
232         return apply;
233     }
234     /***
235      * <p>
236      * Localization messages resource bundle to use for <code>deleteKey</code>.
237      * </p>
238      *
239      * @return the current value of bundle.
240      */
241     public final String getBundle() {
242         return bundle;
243     }
244     /***
245      * <p>
246      * Get whether or not we are adding a new element or clearing the form
247      * fields.
248      * </p>
249      *
250      * @return a non- <code>null</code> and not empty value if the form is to
251      *         be cleared.
252      */
253     public final String getClear() {
254         return clear;
255     }
256     /***
257      * Name of the <strong>Struts</strong> forward to go to, if the Apply button
258      * is pressed.
259      * @return Returns the name of the default <strong>Struts</strong> forward
260      * for the Apply button.
261      */
262     public String getDefaultForwardApply() {
263         return defaultForwardApply;
264     }
265     /***
266      * Name of the <strong>Struts</strong> forward to go to, if the Delete
267      * button is pressed.
268      * @return Returns the name of the default <strong>Struts</strong> forward
269      * for the Delete button.
270      */
271     public String getDefaultForwardDelete() {
272         return defaultForwardDelete;
273     }
274     /***
275      * Name of the <strong>Struts</strong> forward to go to, if the Ok button
276      * is pressed.
277      * @return Returns the name of the default <strong>Struts</strong> forward
278      * for the Ok button.
279      */
280     public String getDefaultForwardOk() {
281         return defaultForwardOk;
282     }
283     /***
284      * <p>
285      * If non- <code>null</code> or not empty, delete this element.
286      * </p>
287      *
288      * @return the current value of delete confirm text.
289      */
290     public final String getDeleteConfirm() {
291         return deleteConfirm;
292     }
293     /***
294      * <p>
295      * If non- <code>null</code>, indicates a key to be displayed to warn
296      * before deletion of the items in this form.
297      * </p>
298      *
299      * <p>
300      * The <code>FormTag</code> will check this value (or it's attribute
301      * equivalent) and display JavaScript with the localized string this key
302      * represents.
303      * </p>
304      *
305      * @return the current value of deleteKey.
306      */
307     public final String getDeleteKey() {
308         return deleteKey;
309     }
310     /***
311      * <p>
312      * If non- <code>null</code> or not empty, display a message before
313      * deleting this entry.
314      * </p>
315      *
316      * @return the current value of delete.
317      */
318     public final String getDeleteWarn() {
319         return deleteWarn;
320     }
321     /***
322      * <p>
323      * Stores the correct help key..
324      * </p>
325      *
326      * @return the current value of helpKey.
327      */
328     public final String getHelpKey() {
329         return helpKey;
330     }
331     /***
332      * <p>
333      * If non- <code>null</code> or not empty, specifies that the form
334      * contents should be amended/added.
335      * </p>
336      *
337      * @return the current value last returned by the OK button.
338      */
339     public final String getOk() {
340         return ok;
341     }
342     /***
343      * <p>
344      * Reset all bean properties to their default state. This method is called
345      * before the properties are re-populated by the controller servlet.
346      * </p>
347      *
348      * @param mapping
349      *            The mapping used to select this instance
350      * @param request
351      *            The servlet request we are processing
352      */
353     public void reset(final ActionMapping mapping,
354             final HttpServletRequest request) {
355         clearReset();
356     }
357     /***
358      * <p>
359      * If non- <code>null</code> or not empty, specifies that the form
360      * contents should be amended/added and the window left open.
361      * </p>
362      *
363      * @param applyParam
364      *            the new value of apply.
365      */
366     public final void setApply(final String applyParam) {
367         this.apply = applyParam;
368     }
369     /***
370      * <p>
371      * Localization messages resource bundle to use for <code>deleteKey</code>.
372      * </p>
373      *
374      * @param bundleParam
375      *            the new value of bundle.
376      */
377     public final void setBundle(final String bundleParam) {
378         this.bundle = bundleParam;
379     }
380     /***
381      * <p>
382      * Set whether or not we are adding a new element or clearing the form
383      * fields.
384      * </p>
385      *
386      * @param clearParam
387      *            a non- <code>null</code> and not empty value if the form is
388      *            to be cleared.
389      */
390     public final void setClear(final String clearParam) {
391         this.clear = clearParam;
392     }
393     /***
394      * Refer to {@link #getDefaultForwardApply}.
395      * @param defaultForwardApplyParam Refer to {@link #getDefaultForwardApply}.
396      */
397     public final void setDefaultForwardApply(String defaultForwardApplyParam) {
398         if (log.isDebugEnabled()) {
399             log.debug("setDefaultForwardApply before: '" + defaultForwardApply
400                     + "', after: '" + defaultForwardApplyParam + "'");
401         }
402 
403         defaultForwardApply = defaultForwardApplyParam;
404     }
405     /***
406      * Refer to {@link #getDefaultForwardDelete}.
407      * @param defaultForwardDeleteParam Refer to {@link #getDefaultForwardDelete}.
408      */
409     public void setDefaultForwardDelete(String defaultForwardDeleteParam) {
410         if (log.isDebugEnabled()) {
411             log.debug("setDefaultForwardDelete before: '"
412                     + defaultForwardDelete + "', after: '"
413                     + defaultForwardDeleteParam + "'");
414         }
415 
416         defaultForwardDelete = defaultForwardDeleteParam;
417     }
418     /***
419      * Refer to {@link #getDefaultForwardOk}.
420      * @param defaultForwardOkParam Refer to {@link #getDefaultForwardOk}.
421      */
422     public final void setDefaultForwardOk(String defaultForwardOkParam) {
423         if (log.isDebugEnabled()) {
424             log.debug("setDefaultForwardOk before: '" + defaultForwardOk
425                     + "', after: '" + defaultForwardOkParam + "'");
426         }
427 
428         defaultForwardOk = defaultForwardOkParam;
429     }
430     /***
431      * <p>
432      * If non- <code>null</code> or not empty, delete this entry.
433      * </p>
434      *
435      * @param deleteConfirmParam
436      *            the new value of deleteConfirm.
437      */
438     public final void setDeleteConfirm(final String deleteConfirmParam) {
439         this.deleteConfirm = deleteConfirmParam;
440     }
441     /***
442      * <p>
443      * If non- <code>null</code>, indicates a key to be displayed to warn
444      * before deletion of the items in this form.
445      * </p>
446      *
447      * <p>
448      * The <code>FormTag</code> will check this value (or it's attribute
449      * equivalent) and display JavaScript with the localized string this key
450      * represents.
451      * </p>
452      *
453      * @param deleteKeyParam
454      *            the new value of deleteKey.
455      */
456     public final void setDeleteKey(final String deleteKeyParam) {
457         this.deleteKey = deleteKeyParam;
458     }
459     /***
460      * <p>
461      * If non- <code>null</code> or not empty, display a message before
462      * deleting this entry.
463      * </p>
464      *
465      * @param deleteWarnParam
466      *            the new value of deleteWarn.
467      */
468     public final void setDeleteWarn(final String deleteWarnParam) {
469         this.deleteWarn = deleteWarnParam;
470     }
471     /***
472      * <p>
473      * Stores the correct help key.
474      * </p>
475      *
476      * @param helpKeyParam
477      *            the new value of helpKey.
478      */
479     public final void setHelpKey(final String helpKeyParam) {
480         this.helpKey = helpKeyParam;
481     }
482     /***
483      * <p>
484      * If non- <code>null</code> or not empty, specifies that the form
485      * contents should be amended/added.
486      * </p>
487      *
488      * @param okParam
489      *            the new value returned by the OK button.
490      */
491     public final void setOk(final String okParam) {
492         this.ok = okParam;
493     }
494     /***
495      * <p>
496      * Overridden to check if there were any errors from the mask request
497      * processor.
498      * </p>
499      *
500      * @param mapping
501      *            <strong>Struts </strong> mapping we are currently processing.
502      * @param request
503      *            The non-HTTP request we are processing
504      * @throws ServletException
505      * @return any errors which occurred in validating the object, otherwise an
506      *         empty <code>ActionErrors</code> instance.
507      * @see org.apache.struts.action.ActionForm
508      */
509     public ActionErrors validate(final ActionMapping mapping,
510             final HttpServletRequest request) {
511         // only interested in errors when we confirm the dialog
512         if (StringHandling.isNullOrEmpty(apply)
513                 && StringHandling.isNullOrEmpty(ok)) {
514             return null;
515         }
516         ActionErrors actionErrors = super.validate(mapping, request);
517         if (actionErrors == null) {
518             actionErrors = new ActionErrors();
519         }
520         // get the validation errors from the override
521         ValidationErrors validationErrors = validate(request,
522                 request.getSession());
523 
524         // now convert them to Struts action errors
525         Locale locale = (Locale) request.getSession().getAttribute(
526                 Globals.LOCALE_KEY);
527         try {
528             if (validationErrors != null) {
529                 actionErrors.add(ValidationErrorsConvertor.toActionErrors(
530                             validationErrors, locale));
531             }
532         } catch (SystemException e) {
533             throw new RuntimeException(e);
534         }
535         return actionErrors;
536     }
537 
538     /***
539      * <p>Validates the form contents.</p>
540      *  @return this instance always returns an empty instance of
541      * <code>ValidationErrors</code>.
542      */
543     public ValidationErrors validate(final HttpServletRequest request,
544             final HttpSession session) {
545         return new ValidationErrors();
546     }
547 }
548