001 /** 002 * Copyright 2005-2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.datadictionary.validation.constraint; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.rice.krad.uif.UifConstants; 020 021 022 /** 023 * Pattern for matching alpha characters 024 * 025 * @author Kuali Rice Team (rice.collab@kuali.org) 026 */ 027 public class AlphaPatternConstraint extends AllowCharacterConstraint { 028 protected boolean lowerCase = false; 029 protected boolean upperCase = false; 030 031 /** 032 * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString() 033 */ 034 protected String getRegexString() { 035 StringBuilder regexString = new StringBuilder("[A-Za-z"); 036 /* 037 * This check must be first because we are removing the base 'A-Z' if lowerCase == true 038 */ 039 if (lowerCase) { 040 regexString = new StringBuilder("[a-z"); 041 } 042 else if(upperCase){ 043 regexString = new StringBuilder("[A-Z"); 044 } 045 regexString.append(this.getAllowedCharacterRegex()); 046 regexString.append("]"); 047 048 return regexString.toString(); 049 } 050 051 /** 052 * A label key is auto generated for this bean if none is set. This generated message can be 053 * overridden through setLabelKey, but the generated message should cover most cases. 054 * 055 * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getLabelKey() 056 */ 057 @Override 058 public String getLabelKey() { 059 if (StringUtils.isEmpty(labelKey)) { 060 StringBuilder key = new StringBuilder(""); 061 if (lowerCase) { 062 return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphaPatternLowerCase"); 063 } else if(upperCase){ 064 return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphaPatternUpperCase"); 065 } 066 else{ 067 return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphaPattern"); 068 } 069 } 070 return labelKey; 071 } 072 073 /** 074 * @return the lowerCase 075 */ 076 public boolean isLowerCase() { 077 return this.lowerCase; 078 } 079 080 /** 081 * Only allow lowerCase characters. DO NOT use with upperCase option, no flags set for case 082 * means both upper and lower case are allowed. 083 * @param lowerCase the lowerCase to set 084 */ 085 public void setLowerCase(boolean lowerCase) { 086 this.lowerCase = lowerCase; 087 } 088 089 public boolean isUpperCase() { 090 return upperCase; 091 } 092 093 /** 094 * Only allow upperCase characters. DO NOT use with lowerCase option, no flags set for case 095 * means both upper and lower case are allowed. 096 * @param lowerCase the lowerCase to set 097 */ 098 public void setUpperCase(boolean upperCase) { 099 this.upperCase = upperCase; 100 } 101 102 }