View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.krad.datadictionary.validation.constraint;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.uif.UifConstants;
20  
21  /**
22   * A ValidCharactersConstraint based on AlphaNumericValidationPattern.
23   * 
24   * @author Kuali Rice Team (rice.collab@kuali.org)
25   */
26  public class AlphaNumericPatternConstraint extends AllowCharacterConstraint {
27      protected boolean lowerCase = false;
28      protected boolean upperCase = false;
29  
30      /**
31       * A label key is auto generated for this bean if none is set. This generated message can be
32       * overridden through setLabelKey, but the generated message should cover most cases.
33       * 
34       * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getLabelKey()
35       */
36      @Override
37      public String getLabelKey() {
38          if (StringUtils.isEmpty(labelKey)) {
39              StringBuilder key = new StringBuilder("");
40              if (lowerCase) {
41                 return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphanumericPatternLowerCase");
42              }
43              else if(upperCase){
44                 return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphanumericPatternUpperCase");
45              } else {
46                  return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphanumericPattern");
47              }
48          }
49          return labelKey;
50      }
51  
52      /**
53       * The labelKey should only be set if the auto generated message by this class needs to be
54       * overridden
55       * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#setLabelKey(java.lang.String)
56       */
57      @Override
58      public void setLabelKey(String labelKey) {
59          super.setLabelKey(labelKey);
60      }
61  
62      /**
63       * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersPatternConstraint#getRegexString()
64       */
65      @Override
66      protected String getRegexString() {
67          StringBuilder regexString = new StringBuilder("[A-Za-z0-9");
68          /*
69           * This check must be first because we are removing the base 'A-Z' if lowerCase == true
70           */
71          if (lowerCase) {
72              regexString = new StringBuilder("[a-z0-9");
73          }
74          else if(upperCase){
75              regexString = new StringBuilder("[A-Z0-9");
76          }
77  
78          regexString.append(this.getAllowedCharacterRegex());
79  
80          regexString.append("]");
81  
82          return regexString.toString();
83      }
84  
85      /**
86       * @return the lowerCase
87       */
88      public boolean isLowerCase() {
89          return this.lowerCase;
90      }
91  
92      /**
93       * Only allow lowerCase characters. DO NOT use with upperCase option, no flags set for case
94       * means both upper and lower case are allowed.
95       * @param lowerCase the lowerCase to set
96       */
97      public void setLowerCase(boolean lowerCase) {
98          this.lowerCase = lowerCase;
99      }
100 
101     public boolean isUpperCase() {
102         return upperCase;
103     }
104 
105     /**
106      * Only allow upperCase characters.  DO NOT use with lowerCase option, no flags set for case
107      * means both upper and lower case are allowed.
108      * @param lowerCase the lowerCase to set
109      */
110     public void setUpperCase(boolean upperCase) {
111         this.upperCase = upperCase;
112     }
113 }