View Javadoc

1   /**
2    * Copyright 2005-2011 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  /**
23   * Pattern for matching alpha characters
24   * 
25   * @author Kuali Rice Team (rice.collab@kuali.org)
26   */
27  public class AlphaPatternConstraint extends AllowCharacterConstraint {
28      protected boolean lowerCase = false;
29      protected boolean upperCase = false;
30  
31      /**
32       * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
33       */
34      protected String getRegexString() {
35          StringBuilder regexString = new StringBuilder("[A-Za-z");
36          /*
37           * This check must be first because we are removing the base 'A-Z' if lowerCase == true
38           */
39          if (lowerCase) {
40              regexString = new StringBuilder("[a-z");
41          }
42          else if(upperCase){
43              regexString = new StringBuilder("[A-Z");
44          }
45          regexString.append(this.getAllowedCharacterRegex());
46          regexString.append("]");
47  
48          return regexString.toString();
49      }
50  
51      /**
52       * A label key is auto generated for this bean if none is set. This generated message can be
53       * overridden through setLabelKey, but the generated message should cover most cases.
54       * 
55       * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getLabelKey()
56       */
57      @Override
58      public String getLabelKey() {
59          if (StringUtils.isEmpty(labelKey)) {
60              StringBuilder key = new StringBuilder("");
61              if (lowerCase) {
62                  return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphaPatternLowerCase");
63              } else if(upperCase){
64                  return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphaPatternUpperCase");
65              }
66              else{
67                  return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphaPattern");
68              }
69          }
70          return labelKey;
71      }
72  	
73      /**
74       * @return the lowerCase
75       */
76      public boolean isLowerCase() {
77          return this.lowerCase;
78      }
79  
80      /**
81       * Only allow lowerCase characters. DO NOT use with upperCase option, no flags set for case
82       * means both upper and lower case are allowed.
83       * @param lowerCase the lowerCase to set
84       */
85      public void setLowerCase(boolean lowerCase) {
86          this.lowerCase = lowerCase;
87      }
88  
89      public boolean isUpperCase() {
90          return upperCase;
91      }
92  
93      /**
94       * Only allow upperCase characters.  DO NOT use with lowerCase option, no flags set for case
95       * means both upper and lower case are allowed.
96       * @param lowerCase the lowerCase to set
97       */
98      public void setUpperCase(boolean upperCase) {
99          this.upperCase = upperCase;
100     }
101 
102 }