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;
017
018import org.kuali.rice.core.api.exception.RiceRuntimeException;
019import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
020import org.kuali.rice.krad.datadictionary.validation.constraint.ConfigurationBasedRegexPatternConstraint;
021import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
022
023import java.io.Serializable;
024import java.util.regex.Matcher;
025import java.util.regex.Pattern;
026
027/**
028 * Abstraction of the regular expressions used to validate attribute values.
029 * 
030                    The validationPattern element defines the allowable character-level
031                    or field-level values for an attribute.
032
033                    JSTL: validationPattern is a Map which is accessed using a key
034                    of "validationPattern". Each entry may contain some of the keys
035                    listed below.  The keys that may be present for a given attribute
036                    are dependent upon the type of validationPattern.
037
038                        * maxLength (String)
039                        * exactLength
040                        * type
041                        * allowWhitespace
042                        * allowUnderscore
043                        * allowPeriod
044                        * validChars
045                        * precision
046                        * scale
047                        * allowNegative
048
049                    The allowable keys (in addition to type) for each type are:
050                        ****Type****    ***Keys***
051                        alphanumeric    exactLength
052                                        maxLength
053                                        allowWhitespace
054                                        allowUnderscore
055                                        allowPeriod
056
057                        alpha           exactLength
058                                        maxLength
059                                        allowWhitespace
060
061                        anyCharacter    exactLength
062                                        maxLength
063                                        allowWhitespace
064
065                        charset         validChars
066
067                        numeric         exactLength
068                                        maxLength
069
070                        fixedPoint      allowNegative
071                                        precision
072                                        scale
073
074                        floatingPoint   allowNegative
075
076                        date            n/a
077                        emailAddress    n/a
078                        javaClass       n/a
079                        month           n/a
080                        phoneNumber     n/a
081                        timestamp       n/a
082                        year            n/a
083                        zipcode         n/a
084
085                    Note: maxLength and exactLength are mutually exclusive.
086                    If one is entered, the other may not be entered.
087
088                    Note:  See ApplicationResources.properties for
089                    exact regex patterns.
090                    e.g. validationPatternRegex.date for regex used in date validation.
091 */
092@Deprecated
093abstract public class ValidationPattern implements Serializable {
094// TODO: UNIT TEST: compile all patterns to test
095
096    /**
097     * @return regular expression Pattern generated by the individual ValidationPattern subclass
098     */
099    abstract public Pattern getRegexPattern();
100
101    /**
102     * @return String version of regular expression base, suitable for modification with length-specifiers and used internally by
103     *         getRegexPattern
104     */
105    abstract protected String getRegexString();
106
107
108    /**
109     * @return true if the given String matches this pattern
110     */
111    public boolean matches(String input) {
112        Pattern p = getRegexPattern();
113
114        Matcher m = p.matcher(input);
115
116        return m.matches();
117    }
118
119    /**
120     * @return ExportMap describing the subclass instance
121     */
122    abstract public ExportMap buildExportMap(String exportKey);
123    
124    abstract public String getValidationErrorMessageKey();
125    
126    public String[] getValidationErrorMessageParameters(String attributeLabel) {
127        return new String[] {attributeLabel};
128    }
129    
130    /**
131     * This method throws an exception if it is not configured properly
132     * 
133     */
134    public void completeValidation() throws ValidationPatternException {
135    }
136    
137    /** exception thrown when a ValidationPattern is in an incorrect state. */
138    public static class ValidationPatternException extends RiceRuntimeException {
139
140        private static final long serialVersionUID = 2012770642382150523L;
141        
142        public ValidationPatternException(String message) {
143            super(message);
144        }
145
146        public ValidationPatternException() {
147            super();
148        }
149
150        public ValidationPatternException(String message, Throwable cause) {
151            super(message, cause);
152        }
153
154        public ValidationPatternException(Throwable cause) {
155            super(cause);
156        }
157    }
158}