001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.krad.datadictionary.validation;
017    
018    import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
019    import org.kuali.rice.krad.util.KRADConstants;
020    
021    import java.util.regex.Pattern;
022    
023    /**
024     * Abstraction of the regular expressions used to validate attribute values.
025     * 
026     * 
027     */
028    @Deprecated
029    abstract public class CharacterLevelValidationPattern extends ValidationPattern {
030        protected Pattern regexPattern;
031    
032        protected int maxLength = -1;
033        protected int exactLength = -1;
034    
035        /**
036         * Sets maxLength parameter for the associated regex.
037         * 
038         * @param maxLength
039         */
040        public void setMaxLength(int maxLength) {
041            if (this.exactLength != -1) {
042                throw new IllegalStateException("illegal attempt to set maxLength after mutually-exclusive exactLength has been set");
043            }
044    
045            this.maxLength = maxLength;
046        }
047    
048        /**
049         * @return current maxLength, or -1 if none has been set
050         */
051        public int getMaxLength() {
052            return maxLength;
053        }
054    
055    
056        /**
057         * Sets exactLength parameter for the associated regex.
058         * 
059         * @param exactLength
060         */
061        public void setExactLength(int exactLength) {
062            if (this.maxLength != -1) {
063                throw new IllegalStateException("illegal attempt to set exactLength after mutually-exclusive maxLength has been set");
064            }
065    
066            this.exactLength = exactLength;
067        }
068    
069        /**
070         * @return current exactLength, or -1 if none has been set
071         */
072        public int getExactLength() {
073            return exactLength;
074        }
075    
076    
077        /**
078         * @return regular expression Pattern generated using the individual ValidationPattern subclass
079         */
080        final public Pattern getRegexPattern() {
081            if ( regexPattern == null ) {
082                String regexString = getRegexString();
083        
084                StringBuffer completeRegex = new StringBuffer("^");
085                completeRegex.append(getRegexString());
086        
087                if (maxLength != -1) {
088                    completeRegex.append("{0," + maxLength + "}");
089                }
090                else if (exactLength != -1) {
091                    completeRegex.append("{" + exactLength + "}");
092                }
093                else {
094                    completeRegex.append("*");
095                }
096        
097                completeRegex.append("$");
098        
099                regexPattern = Pattern.compile(completeRegex.toString());
100            }
101            return regexPattern;
102        }
103    
104    
105        /**
106         * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#buildExportMap(java.lang.String)
107         */
108        final public ExportMap buildExportMap(String exportKey) {
109            ExportMap exportMap = new ExportMap(exportKey);
110    
111            if (getMaxLength() != -1) {
112                exportMap.set("maxLength", Integer.toString(getMaxLength()));
113            }
114            else if (getExactLength() != -1) {
115                exportMap.set("exactLength", Integer.toString(getExactLength()));
116            }
117    
118            extendExportMap(exportMap);
119    
120            return exportMap;
121        }
122    
123        /**
124         * Extends the given (parent class) exportMap as needed to represent subclass instances
125         * 
126         * @param exportMap
127         */
128        abstract public void extendExportMap(ExportMap exportMap);
129    
130            @Override
131            public String[] getValidationErrorMessageParameters(String attributeLabel) {
132                    if (getMaxLength() != -1) {
133                            return new String[] {attributeLabel, String.valueOf(getMaxLength())};
134                    }
135                    if (getExactLength() != -1) {
136                            return new String[] {attributeLabel, String.valueOf(getExactLength())};
137                    }
138                    return new String[] {attributeLabel};
139            }
140    
141            /**
142             * This overridden method ...
143             * 
144             * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getValidationErrorMessageKey()
145             */
146            @Override
147            public String getValidationErrorMessageKey() {
148                    StringBuilder buf = new StringBuilder();
149                    buf.append("error.format.").append(getClass().getName()).append(getValidationErrorMessageKeyOptions());
150                    if (getMaxLength() != -1) {
151                            buf.append(".maxLength");
152                    }
153                    if (getExactLength() != -1) {
154                            buf.append(".exactLength");
155                    }       
156                    return buf.toString();
157            }
158            
159            protected String getValidationErrorMessageKeyOptions() {
160                    return KRADConstants.EMPTY_STRING;
161            }
162    }