KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > event > ValidationEvent


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/ValidationEvent.java,v 1.14 2004/10/20 10:51:29 hkollmann Exp $
3  * $Revision: 1.14 $
4  * $Date: 2004/10/20 10:51:29 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 JavaDoc;
39 import java.util.Vector JavaDoc;
40
41 import javax.servlet.ServletContext JavaDoc;
42 import javax.servlet.http.HttpServletRequest JavaDoc;
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 ValidationEvent extends DatabaseEvent {
53    private static Log logCat = LogFactory.getLog(ValidationEvent.class.getName()); // logging category for this class
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 ValidationEvent(int tableId, String JavaDoc keyId,
64       HttpServletRequest JavaDoc 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 JavaDoc formValidatorName, ServletContext JavaDoc context)
78       throws MultipleValidationException {
79       FieldValues fieldValues = getFieldValues();
80
81       // If no data to validate, return
82
if (fieldValues.size() == 0) {
83          return;
84       }
85
86       // Retreive ValidatorResources from Application context (loaded with ConfigServlet)
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 JavaDoc errors = new Vector JavaDoc();
95       DbFormsErrors dbFormErrors = (DbFormsErrors) context.getAttribute(DbFormsErrors.ERRORS);
96       Locale JavaDoc locale = MessageResources.getLocale(getRequest());
97
98       // Add these resources to perform validation
99
validator.addResource(Validator.BEAN_KEY, fieldValues);
100
101       // The values
102
validator.addResource("java.util.Vector", errors);
103       validator.addResource(Validator.LOCALE_KEY, locale);
104
105       // Vector of errors to populate
106
validator.addResource("org.dbforms.config.DbFormsErrors", dbFormErrors);
107
108       try {
109          validator.validate();
110       } catch (Exception JavaDoc ex) {
111          logCat.error("\n!!! doValidation error for : " + formValidatorName
112             + " !!!\n" + ex);
113       }
114
115       // If error(s) found, throw Exception
116
if (errors.size() > 0) {
117          throw new MultipleValidationException(errors);
118       }
119    }
120 }
121
Popular Tags