KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > entities > kernel > ValidatableEntityVO


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.entities.kernel;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.infoglue.cms.util.ConstraintExceptionBuffer;
30 import org.infoglue.cms.util.validators.Constants;
31 import org.infoglue.cms.util.validators.ConstraintRule;
32 import org.infoglue.cms.util.validators.ConstraintRuleList;
33 import org.infoglue.cms.util.validators.EmailValidator;
34 import org.infoglue.cms.util.validators.StringValidator;
35
36 /**
37  * ValitadeableEntity.java
38  * Created on 2002-sep-16
39  * @author Stefan Sik, ss@frovi.com
40  * ss
41  *
42  * Provides entityvaluobjects with a set of validation rules.
43  * The rules can be retrieved and reviewed by action classes
44  * with getConstraintRules.
45  *
46  *
47  */

48
49 // Implement BaseEntityVO to get the interface
50

51 public abstract class ValidatableEntityVO implements BaseEntityVO
52 {
53     // BaseEntityVO
54
/**
55      * @see org.infoglue.cms.entities.kernel.BaseEntityVO#getId()
56      */

57     // public abstract Integer getId();
58
// end BaseEntityVO
59

60     protected ConstraintRuleList rules = new ConstraintRuleList();;
61     
62     public ConstraintRule getRule(String JavaDoc fieldName)
63     {
64         return rules.getRule(fieldName);
65     }
66     
67     
68     /**
69      * getConstraintRules
70      * returns a collection of ConstraintRule objects
71      * this is the collection returned by getConstraintRuleList().getRules()
72      */

73     public Collection JavaDoc getConstraintRules()
74     {
75         return rules.getRules();
76     }
77
78     /**
79      * getConstraintRuleList
80      * returns the ConstraintRuleList object
81      */

82     public ConstraintRuleList getConstraintRuleList()
83     {
84         return rules;
85     }
86     
87     public abstract void PrepareValidation();
88     
89     /**
90      * @return ConstraintExceptionBuffer
91      */

92     public ConstraintExceptionBuffer validate()
93     {
94         return validate(this);
95     }
96     
97     public ConstraintExceptionBuffer validate(ValidatableEntityVO vo)
98     {
99         // This method loops through the rulelist and creates
100
// validators according to the settings in each rule.
101
// The old validators are used to do the actual validation
102
// but I have changed them to use less constructor
103
// parameter passing in favour for setters.
104

105         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
106         
107         // Prepare the object for validation
108
vo.PrepareValidation();
109         
110         // Loop through rules and create validators
111
Iterator JavaDoc iterator = vo.getConstraintRules().iterator();
112         while (iterator.hasNext())
113         {
114             ConstraintRule cr = (ConstraintRule) iterator.next();
115             Integer JavaDoc intId = vo.getId();
116
117             // an ugly switch for now.
118
switch (cr.getConstraintType())
119             {
120                 case Constants.EMAIL:
121                 {
122                     if (cr.getValue() != null)
123                     {
124                         // Create validator
125
EmailValidator v = new EmailValidator(cr.getFieldName());
126                         
127                         // Set properties
128
v.setObjectClass(vo.getConstraintRuleList().getEntityClass());
129                         v.setRange(cr.getValidRange());
130                         v.setIsRequired(cr.required);
131                         v.setMustBeUnique(cr.unique);
132                         v.setExcludeId(intId);
133
134                         // Do the limbo
135
v.validate((String JavaDoc) cr.getValue(), ceb);
136                         
137                         // <todo>
138
// Note: the actual value validated should be extracted
139
// from the vo using the fieldname with reflection.
140
// </todo>
141

142                     }
143                     break;
144                 }
145                 case Constants.STRING:
146                 {
147                     if (cr.getValue() != null)
148                     {
149                         StringValidator v = new StringValidator(cr.getFieldName());
150                         v.setObjectClass(vo.getConstraintRuleList().getEntityClass());
151                         v.setRange(cr.getValidRange());
152                         v.setIsRequired(cr.required);
153                         v.setMustBeUnique(cr.unique);
154                         v.setExcludeId(intId);
155
156                         v.validate((String JavaDoc) cr.getValue(), ceb);
157                     }
158                     break;
159                 }
160                 case Constants.FLOAT:
161                 {
162                     break;
163                 }
164                 case Constants.INTEGER:
165                 {
166                     break;
167                 }
168                 case Constants.PROPERNOUN:
169                 {
170                     break;
171                 }
172                 
173             } // switch
174

175         } // while
176

177         return ceb;
178     }
179         
180     
181 }
182
Popular Tags