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.core.api.CoreApiServiceLocator;
020import org.kuali.rice.krad.datadictionary.parse.BeanTag;
021import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
022import org.kuali.rice.krad.datadictionary.parse.BeanTags;
023import org.kuali.rice.krad.uif.UifConstants;
024
025/**
026 * ConfigurationBasedRegexPatternConstraint uses a patternTypeKey to get the regex used for validation by key from
027 * the KualiConfigurationService
028 *
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031@BeanTags({@BeanTag(name = "configurationBasedRegexPatternConstraint-bean"),
032        @BeanTag(name = "phoneNumberPatternConstraint-bean", parent = "PhoneNumberPatternConstraint"),
033        @BeanTag(name = "timePatternConstraint-bean", parent = "TimePatternConstraint"),
034        @BeanTag(name = "time24HPatternConstraint-bean", parent = "Time24HPatternConstraint"),
035        @BeanTag(name = "urlPatternConstraint-bean", parent = "UrlPatternConstraint"),
036        @BeanTag(name = "noWhitespacePatternConstraint-bean", parent = "NoWhitespacePatternConstraint"),
037        @BeanTag(name = "javaClassPatternConstraint-bean", parent = "JavaClassPatternConstraint"),
038        @BeanTag(name = "emailAddressPatternConstraint-bean", parent = "EmailAddressPatternConstraint"),
039        @BeanTag(name = "timestampPatternConstraint-bean", parent = "TimestampPatternConstraint"),
040        @BeanTag(name = "yearPatternConstraint-bean", parent = "YearPatternConstraint"),
041        @BeanTag(name = "monthPatternConstraint-bean", parent = "MonthPatternConstraint"),
042        @BeanTag(name = "zipcodePatternConstraint-bean", parent = "ZipcodePatternConstraint")})
043public class ConfigurationBasedRegexPatternConstraint extends ValidDataPatternConstraint {
044    protected String patternTypeKey;
045
046    /**
047     * Message key used to identify the validation pattern
048     *
049     * @return the patternTypeKey
050     */
051    @BeanTagAttribute(name = "patternTypeKey")
052    public String getPatternTypeKey() {
053        return this.patternTypeKey;
054    }
055
056    /**
057     * Setter for the pattern message key
058     *
059     * @param patternTypeKey the patternTypeKey to set
060     */
061    public void setPatternTypeKey(String patternTypeKey) {
062        this.patternTypeKey = patternTypeKey;
063    }
064
065    /**
066     * MessageKey in used in this class have the patternTypeKey appended to the VALIDATION_MSG_KEY_PREFIX by default,
067     * if it is not explicitly set to something else
068     *
069     * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getMessageKey()
070     */
071    @Override
072    public String getMessageKey() {
073        if (StringUtils.isNotEmpty(messageKey)) {
074            return messageKey;
075        }
076
077        StringBuilder buf = new StringBuilder();
078        buf.append(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX).append(getPatternTypeKey());
079        return buf.toString();
080    }
081
082    /**
083     * Uses the key returned by {@link #getPatternTypeKey()} to fetch the
084     * validationPattern's regex string from the ConfigurationService which should not include
085     * the start(^) and end($) symbols
086     *
087     * @return String regex validation string
088     */
089    protected String getRegexString() {
090        return (String) CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(getPatternTypeKey());
091    }
092
093}