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.commons.validator;
20  
21  import javax.faces.application.FacesMessage;
22  import javax.faces.component.UIComponent;
23  import javax.faces.context.FacesContext;
24  import javax.faces.validator.ValidatorException;
25  
26  /**
27   * A custom validator for creditCards, based upon Jakarta Commons. 
28   * 
29   * Unless otherwise specified, all attributes accept static values or EL expressions
30   * 
31   * @JSFValidator
32   *   name = "mcv:validateCreditCard"
33   *   class = "org.apache.myfaces.commons.validator.CreditCardValidator"
34   *   bodyContent = "empty"
35   *   tagClass = "org.apache.myfaces.commons.validator.ValidateCreditCardTag"
36   *   serialuidtag = "3810660506302799072L"
37   * 
38   * @author mwessendorf (latest modification by $Author: lu4242 $)
39   * @version $Revision: 687839 $ $Date: 2008-08-21 14:59:44 -0500 (Thu, 21 Aug 2008) $
40   */
41  public abstract class AbstractCreditCardValidator extends ValidatorBase {
42  
43      /**
44       * <p>The standard converter id for this converter.</p>
45       */
46      public static final String     VALIDATOR_ID        = "org.apache.myfaces.commons.validator.CreditCard";
47  
48      /**
49       * <p>The message identifier of the {@link FacesMessage} to be created if
50       * the creditcard check fails.</p>
51       */
52      public static final String CREDITCARD_MESSAGE_ID = "org.apache.myfaces.commons.validator.Creditcard.INVALID";
53  
54      public AbstractCreditCardValidator(){
55      }
56  
57      //Field, to init the desired Validator
58      private int _initSum = 0;
59  
60      private org.apache.commons.validator.CreditCardValidator creditCardValidator = null;
61  
62      /**
63       *
64       */
65      public void validate(
66          FacesContext facesContext,
67          UIComponent uiComponent,
68          Object value)
69          throws ValidatorException {
70  
71              if (facesContext == null) throw new NullPointerException("facesContext");
72              if (uiComponent == null) throw new NullPointerException("uiComponent");
73  
74              if (value == null)
75              {
76                  return;
77              }
78          initValidator();
79          if (!this.creditCardValidator.isValid(value.toString())){
80              Object[] args = {value.toString()};
81              throw new ValidatorException(getFacesMessage(CREDITCARD_MESSAGE_ID, args));
82          }
83      }
84  
85  
86      // -------------------------------------------------------- Private Methods
87  
88      /**
89       * <p>initializes the desired validator.</p>
90       */
91  
92      private void initValidator() {
93          if(isNone()){
94              //no cardtypes are allowed
95              creditCardValidator = new org.apache.commons.validator.CreditCardValidator(org.apache.commons.validator.CreditCardValidator.NONE);
96          }
97          else{
98              computeValidators();
99              creditCardValidator = new org.apache.commons.validator.CreditCardValidator(_initSum);
100         }
101     }
102 
103     /**
104      * private methode, that counts the desired creditCards
105      */
106     private void computeValidators(){
107         if(isAmex()){
108             this._initSum= org.apache.commons.validator.CreditCardValidator.AMEX + _initSum;
109         }
110         if(isVisa()){
111             this._initSum= org.apache.commons.validator.CreditCardValidator.VISA+ _initSum;
112         }
113         if(isMastercard()){
114             this._initSum= org.apache.commons.validator.CreditCardValidator.MASTERCARD+ _initSum;
115         }
116         if(isDiscover()){
117             this._initSum= org.apache.commons.validator.CreditCardValidator.DISCOVER+ _initSum;
118         }
119     }
120 
121     //GETTER & SETTER
122     
123     /**
124      * american express cards
125      * 
126      * @JSFProperty
127      *   defaultValue = "true"
128      */
129     public abstract boolean isAmex();
130 
131     /**
132      * validation for discover
133      * 
134      * @JSFProperty
135      *   defaultValue = "true"
136      */
137     public abstract boolean isDiscover();
138 
139     /**
140      * validation for mastercard
141      * 
142      * @JSFProperty
143      *   defaultValue = "true"
144      */
145     public abstract boolean isMastercard();
146 
147     /**
148      * none of the given cardtypes is allowed.
149      * 
150      * @JSFProperty
151      *   defaultValue = "false"
152      */
153     public abstract boolean isNone();
154 
155     /**
156      * validation for visa
157      * 
158      * @JSFProperty
159      *   defaultValue = "true"
160      */
161     public abstract boolean isVisa();
162 
163     public abstract void setAmex(boolean b);
164 
165     public abstract void setDiscover(boolean b);
166 
167     public abstract void setMastercard(boolean b);
168 
169     public abstract void setNone(boolean b);
170 
171     public abstract void setVisa(boolean b);
172 
173 }