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.kns.datadictionary.validation.charlevel;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
021import org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern;
022
023/**
024 * This is a description of what this class does - ctdang don't forget to fill this in. 
025 * 
026 * @author Kuali Rice Team (rice.collab@kuali.org)
027 *
028 * @deprecated Use {@link org.kuali.rice.krad.datadictionary.validation.constraint.ConfigurationBasedRegexPatternConstraint}.
029 */
030@Deprecated
031public class RegexValidationPattern extends CharacterLevelValidationPattern {
032
033    private static final long serialVersionUID = -5642894236634278352L;
034    private static final Logger LOG=Logger.getLogger(RegexValidationPattern.class);
035    /**
036     * Regular expression, e.g. "[a-zA-Z0-9]"
037     */
038    private String pattern;
039
040    private String validationErrorMessageKey;
041    /**
042     * This exports a representation of this instance by an ExportMap.
043     * 
044     * @see org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern#extendExportMap(org.kuali.rice.krad.datadictionary.exporter.ExportMap)
045     */
046    @Override
047        public void extendExportMap(ExportMap exportMap) {
048        if (LOG.isTraceEnabled()) {
049            String message=String.format("ENTRY %s",
050                    (exportMap==null)?"null":exportMap.toString());
051            LOG.trace(message);
052        }
053        
054        // Set element value
055        exportMap.set("type", "regex");
056        // Set attribute (of the above element) value
057        exportMap.set("pattern", getPattern());
058
059        if (LOG.isTraceEnabled()) {
060            String message=String.format("EXIT %s",
061                    (exportMap==null)?"null":exportMap.toString());
062            LOG.trace(message);
063        }
064        
065     }
066
067    /**
068     * This returns an instance of this class as string.
069     * 
070     * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getPatternXml()
071     */
072    public String getPatternXml() {
073        if (LOG.isTraceEnabled()) {
074            String message=String.format("ENTRY");
075            LOG.trace(message);
076        }
077        
078        StringBuffer xml = new StringBuffer("<regex ");
079        xml.append(pattern);
080        xml.append("/>");
081
082        if (LOG.isTraceEnabled()) {
083            String message=String.format("EXIT %s", xml.toString());
084            LOG.trace(message);
085        }
086        
087        return xml.toString();
088    }
089
090    /**
091     * This returns the specified regular expression defined in the data dictionary
092     * entry for validating the value of an attribute.
093     * 
094     * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
095     */
096    @Override
097        protected String getRegexString() {
098        if (LOG.isTraceEnabled()) {
099            String message=String.format("ENTRY %s",
100                    (pattern==null)?"null":pattern.toString());
101            LOG.trace(message);
102        }
103        
104        if (StringUtils.isEmpty(pattern)) {
105            throw new IllegalStateException(this.getClass().getName()+".pattern is empty");
106        }
107
108        if (LOG.isTraceEnabled()) {
109            String message=String.format("EXIT");
110            LOG.trace(message);
111        }
112        
113        return pattern;
114    }
115
116    /**
117     * @return the pattern
118     */
119    public final String getPattern() {
120        return this.pattern;
121    }
122
123    /**
124     * @param pattern the pattern to set
125     */
126    public final void setPattern(String pattern) {
127        this.pattern = pattern;
128    }
129
130        /**
131         * @return the validationErrorMessageKey
132         */
133    @Override
134        public String getValidationErrorMessageKey() {
135                return this.validationErrorMessageKey;
136        }
137
138        /**
139         * @param validationErrorMessageKey a message key from the application's message resource bundle signifying the error message
140         * to display if some validation does not match this pattern
141         */
142        public void setValidationErrorMessageKey(String validationErrorMessageKey) {
143                this.validationErrorMessageKey = validationErrorMessageKey;
144        }
145
146        /**
147         * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#completeValidation()
148         */
149        @Override
150        public void completeValidation() {
151                super.completeValidation();
152                if (StringUtils.isBlank(validationErrorMessageKey)) {
153                        throw new ValidationPatternException("Regex Validation Patterns must have a validation error message key defined");
154                }
155        }
156}