KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > validators > AbstractValidator


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.util.validators;
25
26 import java.util.regex.Pattern JavaDoc;
27
28 import org.infoglue.cms.controllers.kernel.impl.simple.ValidationController;
29 import org.infoglue.cms.exception.ConstraintException;
30 import org.infoglue.cms.exception.SystemException;
31 import org.infoglue.cms.util.ConstraintExceptionBuffer;
32
33
34 /**
35  * <todo>
36  * Time is running out, the illusion fades away...
37  * This package will be refactored/extended after iteration 1.
38  * - move to com.holigrow.yoda.util.validators ?
39  * - interfaces + factory
40  * - constructor madness (setXXX instead of N constructors?)
41  * - more validators
42  * - more fun
43  * </todo>
44  *
45  * @@author Mattias Bogeblad
46  */

47
48 public abstract class AbstractValidator
49 {
50
51   private static final String JavaDoc REQUIRED_FIELD_ERROR_CODE = "300";
52   private static final String JavaDoc NOTUNIQUE_FIELD_ERROR_CODE = "302";
53
54
55   // All constraint exceptions found during the validation.
56
private ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
57   // The name of the field being validated.
58
private String JavaDoc fieldName;
59   // Indicates if this field is required.
60
private boolean isRequired;
61   
62   private boolean mustBeUnique;
63   
64   private Class JavaDoc objectClass = null;
65   
66   private Integer JavaDoc excludeId = null;
67   private Object JavaDoc excludeObject = null;
68
69   protected AbstractValidator(String JavaDoc fieldName)
70   {
71     this(fieldName, true);
72   }
73
74
75   /**
76    *
77    */

78   protected AbstractValidator(String JavaDoc fieldName, boolean isRequired)
79   {
80     this.fieldName = fieldName;
81     this.isRequired = isRequired;
82   }
83
84   protected AbstractValidator(String JavaDoc fieldName, boolean isRequired, boolean mustBeUnique, Class JavaDoc objectClass, Integer JavaDoc excludeId, Object JavaDoc excludeObject)
85   {
86     this.fieldName = fieldName;
87     this.isRequired = isRequired;
88     this.mustBeUnique = mustBeUnique;
89     this.objectClass = objectClass;
90     this.excludeId = excludeId;
91     this.excludeObject = excludeObject;
92   }
93
94
95   /**
96    *
97    */

98   private final ConstraintException createConstraintException(String JavaDoc errorCode)
99   {
100     return new ConstraintException(this.fieldName, errorCode);
101   }
102
103   
104   /**
105    *
106    */

107   protected void validateIsRequired(Object JavaDoc value) throws ConstraintException
108   {
109     if(this.isRequired && value == null)
110     {
111       throw createConstraintException(REQUIRED_FIELD_ERROR_CODE);
112     }
113   }
114
115   protected void validateUniqueness(String JavaDoc value) throws ConstraintException, SystemException
116   {
117     if (this.mustBeUnique)
118     {
119         Pattern JavaDoc p = Pattern.compile("[.\\s]+");
120         String JavaDoc[] arrString = p.split(fieldName);
121         String JavaDoc cleanField = arrString[arrString.length-1];
122         
123         if(ValidationController.fieldValueExists(objectClass, cleanField, value, excludeId, excludeObject))
124         {
125           throw createConstraintException(NOTUNIQUE_FIELD_ERROR_CODE);
126         }
127     }
128   }
129
130   /**
131    *
132    */

133   protected final void addConstraintException(String JavaDoc errorCode)
134   {
135     this.ceb.add(createConstraintException(errorCode));
136   }
137
138   /**
139    *
140    */

141   protected final void failIfAnyExceptionsFound() throws ConstraintException
142   {
143     this.ceb.throwIfNotEmpty();
144   }
145
146     /**
147      * Returns the fieldName.
148      * @return String
149      */

150     public String JavaDoc getFieldName()
151     {
152         return fieldName;
153     }
154     
155     /**
156      * Sets the fieldName.
157      * @param fieldName The fieldName to set
158      */

159     public void setFieldName(String JavaDoc fieldName)
160     {
161         this.fieldName = fieldName;
162     }
163     
164     /**
165      * Returns the isRequired.
166      * @return boolean
167      */

168     public boolean isRequired()
169     {
170         return isRequired;
171     }
172     
173     /**
174      * Sets the isRequired.
175      * @param isRequired The isRequired to set
176      */

177     public void setIsRequired(boolean isRequired)
178     {
179         this.isRequired = isRequired;
180     }
181     
182     /**
183      * Returns the mustBeUnique.
184      * @return boolean
185      */

186     public boolean isMustBeUnique()
187     {
188         return mustBeUnique;
189     }
190     
191     /**
192      * Sets the mustBeUnique.
193      * @param mustBeUnique The mustBeUnique to set
194      */

195     public void setMustBeUnique(boolean mustBeUnique)
196     {
197         this.mustBeUnique = mustBeUnique;
198     }
199     
200     /**
201      * Returns the objectClass.
202      * @return Class
203      */

204     public Class JavaDoc getObjectClass()
205     {
206         return objectClass;
207     }
208     
209     /**
210      * Sets the objectClass.
211      * @param objectClass The objectClass to set
212      */

213     public void setObjectClass(Class JavaDoc objectClass)
214     {
215         this.objectClass = objectClass;
216     }
217     
218     /**
219      * Returns the excludeId.
220      * @return int
221      */

222     public Integer JavaDoc getExcludeId()
223     {
224         return excludeId;
225     }
226     
227     /**
228      * Sets the excludeId.
229      * @param excludeId The excludeId to set
230      */

231     public void setExcludeId(Integer JavaDoc excludeId)
232     {
233         this.excludeId = excludeId;
234     }
235
236     /**
237      * Returns the excludeName.
238      * @return int
239      */

240     public Object JavaDoc getExcludeObject()
241     {
242         return excludeObject;
243     }
244     
245     /**
246      * Sets the excludeId.
247      * @param excludeId The excludeId to set
248      */

249     public void setExcludeObject(Object JavaDoc excludeObject)
250     {
251         this.excludeObject = excludeObject;
252     }
253
254 }
255
Popular Tags