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