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