KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > creditcardvalidator > CreditCardValidator


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.creditcardvalidator;
17
18 import org.apache.myfaces.util.MessageUtils;
19
20 import javax.faces.application.FacesMessage;
21 import javax.faces.component.StateHolder;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.validator.Validator;
25 import javax.faces.validator.ValidatorException;
26
27 /**
28  * @author mwessendorf (latest modification by $Author: matzew $)
29  * @version $Revision: 1.6 $ $Date: 2004/11/30 09:37:43 $
30  * $Log: CreditCardValidator.java,v $
31  * Revision 1.6 2004/11/30 09:37:43 matzew
32  * changes i18n-messages for validation
33  *
34  * Revision 1.5 2004/10/13 11:50:57 matze
35  * renamed packages to org.apache
36  *
37  * Revision 1.4 2004/07/01 21:53:08 mwessendorf
38  * ASF switch
39  *
40  * Revision 1.3 2004/06/28 22:12:13 o_rossmueller
41  * fix #978654: do not coerce null
42  *
43  * Revision 1.2 2004/06/05 09:37:43 mwessendorf
44  * new validator for regExpr.
45  * and began with Friendly validator messages
46  *
47  * Revision 1.1 2004/05/27 14:09:00 manolito
48  * creditcard and email validator refactored
49  *
50  */

51 public class CreditCardValidator implements Validator,StateHolder {
52     
53     /**
54      * <p>The standard converter id for this converter.</p>
55      */

56     public static final String JavaDoc VALIDATOR_ID = "org.apache.myfaces.validator.CreditCard";
57
58     /**
59      * <p>The message identifier of the {@link FacesMessage} to be created if
60      * the creditcard check fails.</p>
61      */

62     public static final String JavaDoc CREDITCARD_MESSAGE_ID = "org.apache.myfaces.Creditcard.INVALID";
63     
64     //private DEFAULT_VALUES
65
private static final boolean DEFAULT_AMEX = true;
66     private static final boolean DEFAULT_DISCOVER = true;
67     private static final boolean DEFAULT_MASTERCARD = true;
68     private static final boolean DEFAULT_VISA = true;
69     private static final boolean DEFAULT_NONE = false;
70     
71     
72     public CreditCardValidator(){
73     }
74     
75     //Cardtypes, that are supported by Commons-Validator.
76
private Boolean JavaDoc _amex = null;
77     private Boolean JavaDoc _discover = null;
78     private Boolean JavaDoc _mastercard = null;
79     private Boolean JavaDoc _visa = null;
80     private Boolean JavaDoc _none = null;
81     
82     //JSF-Field for StateHolder-IF
83
private boolean _transient = false;
84     
85     //Field, to init the desired Validator
86
private int _initSum = 0;
87
88     private org.apache.commons.validator.CreditCardValidator creditCardValidator = null;
89
90     /**
91      *
92      */

93     public void validate(
94         FacesContext facesContext,
95         UIComponent uiComponent,
96         Object JavaDoc value)
97         throws ValidatorException {
98
99             if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
100             if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
101
102             if (value == null)
103             {
104                 return;
105             }
106         initValidator();
107         if (!this.creditCardValidator.isValid(value.toString())){
108             Object JavaDoc[] args = {value.toString()};
109             throw new ValidatorException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,CREDITCARD_MESSAGE_ID, args));
110         }
111     }
112
113
114     // -------------------------------------------------------- Private Methods
115

116     /**
117      * <p>initializes the desired validator.</p>
118      */

119
120     private void initValidator() {
121         if(isNone()){
122             //no cardtypes are allowed
123
creditCardValidator = new org.apache.commons.validator.CreditCardValidator(org.apache.commons.validator.CreditCardValidator.NONE);
124         }
125         else{
126             computeValidators();
127             creditCardValidator = new org.apache.commons.validator.CreditCardValidator(_initSum);
128         }
129     }
130     
131     /**
132      * private methode, that counts the desired creditCards
133      */

134     private void computeValidators(){
135         if(isAmex()){
136             this._initSum= org.apache.commons.validator.CreditCardValidator.AMEX + _initSum;
137         }
138         if(isVisa()){
139             this._initSum= org.apache.commons.validator.CreditCardValidator.VISA+ _initSum;
140         }
141         if(isMastercard()){
142             this._initSum= org.apache.commons.validator.CreditCardValidator.MASTERCARD+ _initSum;
143         }
144         if(isDiscover()){
145             this._initSum= org.apache.commons.validator.CreditCardValidator.DISCOVER+ _initSum;
146         }
147     }
148
149     //GETTER & SETTER
150
public boolean isAmex() {
151         if (_amex!= null) return _amex.booleanValue();
152         return _amex != null ? _amex.booleanValue() : DEFAULT_AMEX;
153     }
154
155     public boolean isDiscover() {
156         if (_discover!= null) return _discover.booleanValue();
157         return _discover != null ? _discover.booleanValue() : DEFAULT_DISCOVER;
158     }
159
160     public boolean isMastercard() {
161         if (_mastercard!= null) return _mastercard.booleanValue();
162         return _mastercard != null ? _mastercard.booleanValue() : DEFAULT_MASTERCARD;
163     }
164
165     public boolean isNone() {
166         if (_none!= null) return _none.booleanValue();
167         return _none != null ? _none.booleanValue() : DEFAULT_NONE;
168     }
169
170     public boolean isVisa() {
171         if (_visa!= null) return _visa.booleanValue();
172         return _visa != null ? _visa.booleanValue() : DEFAULT_VISA;
173     }
174
175     public void setAmex(boolean b) {
176         _amex = Boolean.valueOf(b);
177     }
178
179     public void setDiscover(boolean b) {
180         _discover = Boolean.valueOf(b);
181     }
182
183     public void setMastercard(boolean b) {
184         _mastercard = Boolean.valueOf(b);
185     }
186
187     public void setNone(boolean b) {
188         _none = Boolean.valueOf(b);
189     }
190
191     public void setVisa(boolean b) {
192         _visa = Boolean.valueOf(b);
193     }
194
195
196     // -------------------------------------------------------- StateholderIF
197

198     /* (non-Javadoc)
199      * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
200      */

201     public Object JavaDoc saveState(FacesContext context) {
202         Object JavaDoc values[] = new Object JavaDoc[6];
203         values[0] = _amex;
204         values[1] = _discover;
205         values[2] = _mastercard;
206         values[3] = _visa;
207         values[4] = _none;
208         return values;
209     }
210
211     /* (non-Javadoc)
212      * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
213      */

214     public void restoreState(FacesContext context, Object JavaDoc state) {
215         Object JavaDoc values[] = (Object JavaDoc[])state;
216         _amex = ((Boolean JavaDoc) values[0]);
217         _discover = ((Boolean JavaDoc) values[1]);
218         _mastercard = ((Boolean JavaDoc) values[2]);
219         _visa = ((Boolean JavaDoc) values[3]);
220         _none = ((Boolean JavaDoc) values[4]);
221     }
222
223     /* (non-Javadoc)
224      * @see javax.faces.component.StateHolder#isTransient()
225      */

226     public boolean isTransient() {
227         return _transient;
228     }
229
230     /* (non-Javadoc)
231      * @see javax.faces.component.StateHolder#setTransient(boolean)
232      */

233     public void setTransient(boolean newTransientValue) {
234         this._transient = newTransientValue;
235     }
236 }
237
Popular Tags