View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/AbstractValidationEvent.java,v 1.1 2005/11/30 20:31:17 hkollmann Exp $
3    * $Revision: 1.1 $
4    * $Date: 2005/11/30 20:31:17 $
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;
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()); // 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 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        // 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        errors       = new Vector();
95        DbFormsErrors dbFormErrors = (DbFormsErrors) context.getAttribute(DbFormsErrors.ERRORS);
96        Locale        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 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 }