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