Coverage Report - org.kuali.rice.krad.datadictionary.validation.constraint.AlphaNumericPatternConstraint
 
Classes in this File Line Coverage Branch Coverage Complexity
AlphaNumericPatternConstraint
47%
35/73
40%
13/32
2
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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 java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.apache.commons.lang.StringUtils;
 22  
 import org.kuali.rice.core.api.config.property.ConfigurationService;
 23  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 24  
 import org.kuali.rice.krad.uif.UifConstants;
 25  
 
 26  
 /**
 27  
  * A ValidCharactersConstraint based on AlphaNumericValidationPattern.
 28  
  * 
 29  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 30  
  */
 31  18
 public class AlphaNumericPatternConstraint extends ValidCharactersPatternConstraint {
 32  18
     protected boolean allowWhitespace = false;
 33  18
     protected boolean allowUnderscore = false;
 34  18
     protected boolean allowPeriod = false;
 35  18
     protected boolean allowParenthesis = false;
 36  18
     protected boolean allowDollar = false;
 37  18
     protected boolean allowForwardSlash = false;
 38  18
     protected boolean lowerCase = false;
 39  
 
 40  
     /**
 41  
      * A label key is auto generated for this bean if none is set. This generated message can be
 42  
      * overridden through setLabelKey, but the generated message should cover most cases.
 43  
      * 
 44  
      * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getLabelKey()
 45  
      */
 46  
     @Override
 47  
     public String getLabelKey() {
 48  6
         if (StringUtils.isEmpty(labelKey)) {
 49  6
             StringBuilder key = new StringBuilder("");
 50  6
             if (lowerCase) {
 51  0
                 return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphanumericPatternLowerCase");
 52  
             } else {
 53  6
                 return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphanumericPattern");
 54  
             }
 55  
         }
 56  0
         return labelKey;
 57  
     }
 58  
 
 59  
     /**
 60  
      * The labelKey should only be set if the auto generated message by this class needs to be
 61  
      * overridden
 62  
      * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#setLabelKey(java.lang.String)
 63  
      */
 64  
     @Override
 65  
     public void setLabelKey(String labelKey) {
 66  0
         super.setLabelKey(labelKey);
 67  0
     }
 68  
 
 69  
     /**
 70  
      * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersPatternConstraint#getRegexString()
 71  
      */
 72  
     @Override
 73  
     protected String getRegexString() {
 74  
         //Exact same logic is used here as old KS AlphaNumericValidationPattern for server side value
 75  6
         StringBuilder regexString = new StringBuilder("[A-Za-z0-9");
 76  
         /*
 77  
          * This check must be first because we are removing the base 'A-Z' if lowerCase == true
 78  
          */
 79  6
         if (lowerCase) {
 80  0
             regexString = new StringBuilder("[a-z0-9");
 81  
         }
 82  
 
 83  6
         if (allowWhitespace) {
 84  4
             regexString.append("\\s");
 85  
         }
 86  6
         if (allowUnderscore) {
 87  2
             regexString.append("_");
 88  
         }
 89  6
         if (allowPeriod) {
 90  2
             regexString.append(".");
 91  
         }
 92  6
         if (allowParenthesis) {
 93  2
             regexString.append("(");
 94  2
             regexString.append(")");
 95  
         }
 96  6
         if (allowDollar) {
 97  0
             regexString.append("$");
 98  
         }
 99  6
         if (allowForwardSlash) {
 100  0
             regexString.append("/");
 101  
         }
 102  6
         regexString.append("]");
 103  
 
 104  6
         return regexString.toString();
 105  
     }
 106  
 
 107  
     /**
 108  
      * @return the allowWhitespace
 109  
      */
 110  
     public boolean isAllowWhitespace() {
 111  0
         return this.allowWhitespace;
 112  
     }
 113  
 
 114  
     /**
 115  
      * @param allowWhitespace the allowWhitespace to set
 116  
      */
 117  
     public void setAllowWhitespace(boolean allowWhitespace) {
 118  12
         this.allowWhitespace = allowWhitespace;
 119  12
     }
 120  
 
 121  
     /**
 122  
      * @return the allowUnderscore
 123  
      */
 124  
     public boolean isAllowUnderscore() {
 125  0
         return this.allowUnderscore;
 126  
     }
 127  
 
 128  
     /**
 129  
      * @param allowUnderscore the allowUnderscore to set
 130  
      */
 131  
     public void setAllowUnderscore(boolean allowUnderscore) {
 132  6
         this.allowUnderscore = allowUnderscore;
 133  6
     }
 134  
 
 135  
     /**
 136  
      * @return the allowPeriod
 137  
      */
 138  
     public boolean isAllowPeriod() {
 139  0
         return this.allowPeriod;
 140  
     }
 141  
 
 142  
     /**
 143  
      * @param allowPeriod the allowPeriod to set
 144  
      */
 145  
     public void setAllowPeriod(boolean allowPeriod) {
 146  6
         this.allowPeriod = allowPeriod;
 147  6
     }
 148  
 
 149  
     /**
 150  
      * @return the allowParenthesis
 151  
      */
 152  
     public boolean isAllowParenthesis() {
 153  0
         return this.allowParenthesis;
 154  
     }
 155  
 
 156  
     /**
 157  
      * @param allowParenthesis the allowParenthesis to set
 158  
      */
 159  
     public void setAllowParenthesis(boolean allowParenthesis) {
 160  6
         this.allowParenthesis = allowParenthesis;
 161  6
     }
 162  
 
 163  
     /**
 164  
      * @return the allowDollar
 165  
      */
 166  
     public boolean isAllowDollar() {
 167  0
         return this.allowDollar;
 168  
     }
 169  
 
 170  
     /**
 171  
      * @param allowDollar the allowDollar to set
 172  
      */
 173  
     public void setAllowDollar(boolean allowDollar) {
 174  0
         this.allowDollar = allowDollar;
 175  0
     }
 176  
 
 177  
     /**
 178  
      * @return the allowForwardSlash
 179  
      */
 180  
     public boolean isAllowForwardSlash() {
 181  0
         return this.allowForwardSlash;
 182  
     }
 183  
 
 184  
     /**
 185  
      * @param allowForwardSlash the allowForwardSlash to set
 186  
      */
 187  
     public void setAllowForwardSlash(boolean allowForwardSlash) {
 188  0
         this.allowForwardSlash = allowForwardSlash;
 189  0
     }
 190  
 
 191  
     /**
 192  
      * @return the lowerCase
 193  
      */
 194  
     public boolean isLowerCase() {
 195  0
         return this.lowerCase;
 196  
     }
 197  
 
 198  
     /**
 199  
      * @param lowerCase the lowerCase to set
 200  
      */
 201  
     public void setLowerCase(boolean lowerCase) {
 202  0
         this.lowerCase = lowerCase;
 203  0
     }
 204  
 
 205  
     /**
 206  
      * Parameters to be used in the string retrieved by this constraint's labelKey
 207  
      * @return the validationMessageParams
 208  
      */
 209  
     public List<String> getValidationMessageParams() {
 210  0
         if (validationMessageParams == null) {
 211  0
             validationMessageParams = new ArrayList<String>();
 212  0
             ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
 213  0
             StringBuilder paramString = new StringBuilder("");
 214  
 
 215  0
             if (allowWhitespace) {
 216  0
                 paramString.append(", " + configService
 217  
                         .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "whitespace"));
 218  
             }
 219  0
             if (allowUnderscore) {
 220  0
                 paramString.append(", " + configService
 221  
                         .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "underscore"));
 222  
             }
 223  0
             if (allowPeriod) {
 224  0
                 paramString.append(", " + configService
 225  
                         .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "period"));
 226  
             }
 227  0
             if (allowParenthesis) {
 228  0
                 paramString.append(", " + configService
 229  
                         .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "parenthesis"));
 230  
             }
 231  0
             if (allowDollar) {
 232  0
                 paramString.append(", " + configService
 233  
                         .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "dollar"));
 234  
             }
 235  0
             if (allowForwardSlash) {
 236  0
                 paramString.append(", " + configService
 237  
                         .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "forwardSlash"));
 238  
             }
 239  
 
 240  0
             validationMessageParams.add(paramString.toString());
 241  
         }
 242  0
         return this.validationMessageParams;
 243  
     }
 244  
 
 245  
 }