1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
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
521 ValidationErrors validationErrors = validate(request,
522 request.getSession());
523
524
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