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