1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.dbforms.event;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.commons.validator.Validator;
27 import org.apache.commons.validator.ValidatorResources;
28
29 import org.dbforms.config.DbFormsConfig;
30 import org.dbforms.config.DbFormsErrors;
31 import org.dbforms.config.FieldValues;
32 import org.dbforms.config.MultipleValidationException;
33
34 import org.dbforms.util.MessageResources;
35
36 import org.dbforms.validation.ValidatorConstants;
37
38 import java.util.Locale;
39 import java.util.Vector;
40
41 import javax.servlet.ServletContext;
42 import javax.servlet.http.HttpServletRequest;
43
44
45
46 /***
47 * abstract base class for all database operations which need validation, e.g.
48 * InsertEvent and UpdateEvent
49 *
50 * @author hkk
51 */
52 public abstract class AbstractValidationEvent extends AbstractDatabaseEvent {
53 private static Log logCat = LogFactory.getLog(AbstractValidationEvent.class.getName());
54
55 /***
56 * Creates a new ValidationEvent object.
57 *
58 * @param tableId DOCUMENT ME!
59 * @param keyId DOCUMENT ME!
60 * @param request DOCUMENT ME!
61 * @param config DOCUMENT ME!
62 */
63 public AbstractValidationEvent(int tableId, String keyId,
64 HttpServletRequest request, DbFormsConfig config) {
65 super(tableId, keyId, request, config);
66 }
67
68 /***
69 * DO the validation of the form with Commons-Validator.
70 *
71 * @param formValidatorName The form name to retreive in validation.xml
72 * @param context The servlet request we are processing
73 *
74 * @exception MultipleValidationException The Vector of errors throwed with
75 * this exception
76 */
77 public void doValidation(String formValidatorName, ServletContext context)
78 throws MultipleValidationException {
79 FieldValues fieldValues = getFieldValues();
80
81
82 if (fieldValues.size() == 0) {
83 return;
84 }
85
86
87 ValidatorResources vr = (ValidatorResources) context.getAttribute(ValidatorConstants.VALIDATOR);
88
89 if (vr == null) {
90 return;
91 }
92
93 Validator validator = new Validator(vr, formValidatorName.trim());
94 Vector errors = new Vector();
95 DbFormsErrors dbFormErrors = (DbFormsErrors) context.getAttribute(DbFormsErrors.ERRORS);
96 Locale locale = MessageResources.getLocale(getRequest());
97
98
99 validator.addResource(Validator.BEAN_KEY, fieldValues);
100
101
102 validator.addResource("java.util.Vector", errors);
103 validator.addResource(Validator.LOCALE_KEY, locale);
104
105
106 validator.addResource("org.dbforms.config.DbFormsErrors", dbFormErrors);
107
108 try {
109 validator.validate();
110 } catch (Exception ex) {
111 logCat.error("\n!!! doValidation error for : " + formValidatorName
112 + " !!!\n" + ex);
113 }
114
115
116 if (errors.size() > 0) {
117 throw new MultipleValidationException(errors);
118 }
119 }
120 }