001    /**
002     * Copyright 2005-2014 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.datadictionary.parse.BeanTag;
020    import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
021    import org.kuali.rice.krad.datadictionary.parse.BeanTags;
022    import org.kuali.rice.krad.uif.UifConstants;
023    
024    /**
025     * Pattern for matching alpha characters
026     *
027     * @author Kuali Rice Team (rice.collab@kuali.org)
028     */
029    @BeanTags({@BeanTag(name = "alphaPatternConstraint-bean", parent = "AlphaPatternConstraint"),
030            @BeanTag(name = "alphaWithBasicPunc-bean", parent = "AlphaWithBasicPunc")})
031    public class AlphaPatternConstraint extends AllowCharacterConstraint {
032        protected boolean lowerCase = false;
033        protected boolean upperCase = false;
034    
035        /**
036         * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
037         */
038        protected String getRegexString() {
039            StringBuilder regexString = new StringBuilder("[A-Za-z");
040            /*
041             * This check must be first because we are removing the base 'A-Z' if lowerCase == true
042             */
043            if (lowerCase) {
044                regexString = new StringBuilder("[a-z");
045            } else if (upperCase) {
046                regexString = new StringBuilder("[A-Z");
047            }
048            regexString.append(this.getAllowedCharacterRegex());
049            regexString.append("]");
050    
051            return regexString.toString();
052        }
053    
054        /**
055         * A message key is auto generated for this bean if none is set. This generated message can be
056         * overridden through setMessageKey, but the generated message should cover most cases.
057         *
058         * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getMessageKey()
059         */
060        @Override
061        public String getMessageKey() {
062            if (StringUtils.isEmpty(messageKey)) {
063                StringBuilder key = new StringBuilder("");
064                if (lowerCase) {
065                    return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphaPatternLowerCase");
066                } else if (upperCase) {
067                    return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphaPatternUpperCase");
068                } else {
069                    return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphaPattern");
070                }
071            }
072    
073            return messageKey;
074        }
075    
076        /**
077         * @return the lowerCase
078         */
079        @BeanTagAttribute(name = "lowerCase")
080        public boolean isLowerCase() {
081            return this.lowerCase;
082        }
083    
084        /**
085         * Only allow lowerCase characters. DO NOT use with upperCase option, no flags set for case
086         * means both upper and lower case are allowed.
087         *
088         * @param lowerCase the lowerCase to set
089         */
090        public void setLowerCase(boolean lowerCase) {
091            this.lowerCase = lowerCase;
092        }
093    
094        @BeanTagAttribute(name = "upperCase")
095        public boolean isUpperCase() {
096            return upperCase;
097        }
098    
099        /**
100         * Only allow upperCase characters.  DO NOT use with lowerCase option, no flags set for case
101         * means both upper and lower case are allowed.
102         *
103         * @param upperCase the lowerCase to set
104         */
105        public void setUpperCase(boolean upperCase) {
106            this.upperCase = upperCase;
107        }
108    
109    }