View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.creditcardvalidator;
20  
21  import org.apache.myfaces.validator.ValidatorBase;
22  
23  import javax.faces.application.FacesMessage;
24  import javax.faces.component.UIComponent;
25  import javax.faces.context.FacesContext;
26  import javax.faces.validator.ValidatorException;
27  
28  /**
29   * A custom validator for creditCards, based upon Jakarta Commons. 
30   * 
31   * Unless otherwise specified, all attributes accept static values or EL expressions
32   * 
33   * @JSFValidator
34   *   name = "t:validateCreditCard"
35   *   class = "org.apache.myfaces.custom.creditcardvalidator.CreditCardValidator"
36   *   bodyContent = "empty"
37   *   tagClass = "org.apache.myfaces.custom.creditcardvalidator.ValidateCreditCardTag"
38   *   serialuidtag = "3810660506302799072L"
39   *   
40   * @since 1.1.7
41   * @deprecated use myfaces commons mcv:validateCreditCard instead
42   * @author mwessendorf (latest modification by $Author: lu4242 $)
43   * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (Wed, 03 Sep 2008) $
44   */
45  public abstract class AbstractCreditCardValidator extends ValidatorBase {
46  
47      /**
48       * <p>The standard converter id for this converter.</p>
49       */
50      public static final String     VALIDATOR_ID        = "org.apache.myfaces.validator.CreditCard";
51  
52      /**
53       * <p>The message identifier of the {@link FacesMessage} to be created if
54       * the creditcard check fails.</p>
55       */
56      public static final String CREDITCARD_MESSAGE_ID = "org.apache.myfaces.Creditcard.INVALID";
57  
58      public AbstractCreditCardValidator(){
59      }
60  
61      //Field, to init the desired Validator
62      private int _initSum = 0;
63  
64      private org.apache.commons.validator.CreditCardValidator creditCardValidator = null;
65  
66      /**
67       *
68       */
69      public void validate(
70          FacesContext facesContext,
71          UIComponent uiComponent,
72          Object value)
73          throws ValidatorException {
74  
75              if (facesContext == null) throw new NullPointerException("facesContext");
76              if (uiComponent == null) throw new NullPointerException("uiComponent");
77  
78              if (value == null)
79              {
80                  return;
81              }
82          initValidator();
83          if (!this.creditCardValidator.isValid(value.toString())){
84              Object[] args = {value.toString()};
85              throw new ValidatorException(getFacesMessage(CREDITCARD_MESSAGE_ID, args));
86          }
87      }
88  
89  
90      // -------------------------------------------------------- Private Methods
91  
92      /**
93       * <p>initializes the desired validator.</p>
94       */
95  
96      private void initValidator() {
97          if(isNone()){
98              //no cardtypes are allowed
99              creditCardValidator = new org.apache.commons.validator.CreditCardValidator(org.apache.commons.validator.CreditCardValidator.NONE);
100         }
101         else{
102             computeValidators();
103             creditCardValidator = new org.apache.commons.validator.CreditCardValidator(_initSum);
104         }
105     }
106 
107     /**
108      * private methode, that counts the desired creditCards
109      */
110     private void computeValidators(){
111         if(isAmex()){
112             this._initSum= org.apache.commons.validator.CreditCardValidator.AMEX + _initSum;
113         }
114         if(isVisa()){
115             this._initSum= org.apache.commons.validator.CreditCardValidator.VISA+ _initSum;
116         }
117         if(isMastercard()){
118             this._initSum= org.apache.commons.validator.CreditCardValidator.MASTERCARD+ _initSum;
119         }
120         if(isDiscover()){
121             this._initSum= org.apache.commons.validator.CreditCardValidator.DISCOVER+ _initSum;
122         }
123     }
124 
125     //GETTER & SETTER
126     
127     /**
128      * american express cards
129      * 
130      * @JSFProperty
131      *   defaultValue = "true"
132      */
133     public abstract boolean isAmex();
134 
135     /**
136      * validation for discover
137      * 
138      * @JSFProperty
139      *   defaultValue = "true"
140      */
141     public abstract boolean isDiscover();
142 
143     /**
144      * validation for mastercard
145      * 
146      * @JSFProperty
147      *   defaultValue = "true"
148      */
149     public abstract boolean isMastercard();
150 
151     /**
152      * none of the given cardtypes is allowed.
153      * 
154      * @JSFProperty
155      *   defaultValue = "false"
156      */
157     public abstract boolean isNone();
158 
159     /**
160      * validation for visa
161      * 
162      * @JSFProperty
163      *   defaultValue = "true"
164      */
165     public abstract boolean isVisa();
166 
167     public abstract void setAmex(boolean b);
168 
169     public abstract void setDiscover(boolean b);
170 
171     public abstract void setMastercard(boolean b);
172 
173     public abstract void setNone(boolean b);
174 
175     public abstract void setVisa(boolean b);
176 
177 }