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