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 * A ValidCharactersConstraint based on AlphaNumericValidationPattern.
026 *
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029@BeanTags({@BeanTag(name = "alphaNumericPatternConstraint", parent="AlphaNumericPatternConstraint"),
030        @BeanTag(name = "alphaNumericWithBasicPunc", parent="AlphaNumericWithBasicPunc")})
031public class AlphaNumericPatternConstraint extends AllowCharacterConstraint {
032    protected boolean lowerCase = false;
033    protected boolean upperCase = false;
034
035    /**
036     * A label key is auto generated for this bean if none is set. This generated message can be
037     * overridden through setMessageKey, but the generated message should cover most cases.
038     *
039     * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getMessageKey()
040     */
041    @Override
042    public String getMessageKey() {
043        if (StringUtils.isEmpty(messageKey)) {
044            StringBuilder key = new StringBuilder("");
045            if (lowerCase) {
046                return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphanumericPatternLowerCase");
047            } else if (upperCase) {
048                return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphanumericPatternUpperCase");
049            } else {
050                return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "alphanumericPattern");
051            }
052        }
053
054        return messageKey;
055    }
056
057    /**
058     * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersPatternConstraint#getRegexString()
059     */
060    @Override
061    protected String getRegexString() {
062        StringBuilder regexString = new StringBuilder("[A-Za-z0-9");
063        /*
064         * This check must be first because we are removing the base 'A-Z' if lowerCase == true
065         */
066        if (lowerCase) {
067            regexString = new StringBuilder("[a-z0-9");
068        } else if (upperCase) {
069            regexString = new StringBuilder("[A-Z0-9");
070        }
071
072        regexString.append(this.getAllowedCharacterRegex());
073
074        regexString.append("]");
075
076        return regexString.toString();
077    }
078
079    /**
080     * @return the lowerCase
081     */
082    @BeanTagAttribute(name = "lowerCase")
083    public boolean isLowerCase() {
084        return this.lowerCase;
085    }
086
087    /**
088     * Only allow lowerCase characters. DO NOT use with upperCase option, no flags set for case
089     * means both upper and lower case are allowed.
090     *
091     * @param lowerCase the lowerCase to set
092     */
093    public void setLowerCase(boolean lowerCase) {
094        this.lowerCase = lowerCase;
095    }
096
097    @BeanTagAttribute(name = "upperCase")
098    public boolean isUpperCase() {
099        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}