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.kuali.rice.krad.datadictionary.exporter.ExportMap;
019import org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern;
020
021/**
022 * Pattern for matching alphanumeric characters
023 * 
024 * Also, allows conditionally whitespace, underscore, period, parens, dollar signs, and forward slash.
025 */
026public class AlphaNumericValidationPattern extends CharacterLevelValidationPattern {
027    protected boolean allowWhitespace = false;
028    protected boolean allowUnderscore = false;
029    protected boolean allowPeriod = false;
030
031    protected boolean allowParenthesis = false;
032    protected boolean allowDollar = false;
033    protected boolean allowForwardSlash = false;
034    protected boolean lowerCase = false;
035    protected boolean allowDash = false;
036    
037    /**
038     * @return allowPeriod
039     */
040    public boolean getAllowPeriod() {
041        return allowPeriod;
042    }
043
044    /**
045     * @param allowPeriod
046     */
047    public void setAllowPeriod(boolean allowPeriod) {
048        this.allowPeriod = allowPeriod;
049    }    
050    
051    /**
052         * @return the allowPeriod
053         */
054        public boolean isAllowPeriod() {
055                return allowPeriod;
056        }
057    
058    /**
059         * @return the allowParenthesis
060         */
061        public boolean isAllowParenthesis() {
062                return allowParenthesis;
063        }
064
065        /**
066         * @param allowParenthesis the allowParenthesis to set
067         */
068        public void setAllowParenthesis(boolean allowParenthesis) {
069                this.allowParenthesis = allowParenthesis;
070        }
071        
072        /**
073         * @return the allowDollar
074         */
075        public boolean isAllowDollar() {
076                return allowDollar;
077        }
078
079        /**
080         * @param allowDollar the allowDollar to set
081         */
082        public void setAllowDollar(boolean allowDollar) {
083                this.allowDollar = allowDollar;
084        }
085
086        /**
087         * @return the allowforwardSlash
088         */
089        public boolean isAllowForwardSlash() {
090                return allowForwardSlash;
091        }
092
093        /**
094         * @param allowForwardSlash the allowforwardSlash to set
095         */
096        public void setAllowForwardSlash(boolean allowForwardSlash) {
097                this.allowForwardSlash = allowForwardSlash;
098        }
099    
100    /**
101     * @return allowWhitespace
102     */
103    public boolean getAllowWhitespace() {
104        return allowWhitespace;
105    }
106
107    /**
108     * @param allowWhitespace
109     */
110    public void setAllowWhitespace(boolean allowWhitespace) {
111        this.allowWhitespace = allowWhitespace;
112    }
113
114
115    /**
116     * @return allowUnderscore
117     */
118    public boolean getAllowUnderscore() {
119        return allowUnderscore;
120    }
121
122    /**
123     * @param allowUnderscore
124     */
125    public void setAllowUnderscore(boolean allowUnderscore) {
126        this.allowUnderscore = allowUnderscore;
127    }
128   
129    /**
130         * @return the lowerCase
131         */
132        public boolean isLowerCase() {
133                return this.lowerCase;
134        }
135
136        /**
137         * @param lowerCase the lowerCase to set
138         */
139        public void setLowerCase(boolean lowerCase) {
140                this.lowerCase = lowerCase;
141        }
142
143    /**
144     * @return allowDash
145     */
146    public boolean getAllowDash() {
147        return allowDash;
148    }
149
150    /**
151     * @param allowDash
152     */
153    public void setAllowDash(boolean allowDash) {
154        this.allowDash = allowDash;
155    }
156
157    /**
158     * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
159     */
160    protected String getRegexString() {
161        StringBuilder regexString = new StringBuilder("[A-Za-z0-9");
162        
163        /*
164         * This check must be first because we are removing the base 'A-Z' if lowerCase == true
165         */
166        if(lowerCase){
167                regexString = new StringBuilder("[a-z0-9");
168        }
169
170        if (allowWhitespace) {
171            regexString.append("\\s");
172        }
173        if (allowUnderscore) {
174            regexString.append("_");
175        }
176        if (allowPeriod) {
177            regexString.append(".");
178        }
179        if(allowParenthesis) {
180                regexString.append("(");
181                regexString.append(")");
182        }
183        if(allowDollar) {
184                regexString.append("$");
185        }
186        if(allowForwardSlash) {
187                regexString.append("/");
188        }
189        if (allowDash) {
190            regexString.append("-");
191        }
192        regexString.append("]");
193
194        return regexString.toString();
195    }
196
197
198    /**
199     * @see org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern#extendExportMap(org.kuali.bo.datadictionary.exporter.ExportMap)
200     */
201    public void extendExportMap(ExportMap exportMap) {
202        exportMap.set("type", "alphaNumeric");
203
204        if (lowerCase) {
205            exportMap.set("allowUpperCase", "true");
206        }
207        if (allowWhitespace) {
208            exportMap.set("allowWhitespace", "true");
209        }
210        if (allowUnderscore) {
211            exportMap.set("allowUnderscore", "true");
212        }
213        if (allowPeriod) {
214                exportMap.set("allowPeriod", "true");
215        }
216        if(allowParenthesis) {
217            exportMap.set("allowParenthesis", "true");
218
219        }
220        if(allowDollar) {
221            exportMap.set("allowDollar", "true");
222
223        }
224        if(allowForwardSlash) {
225            exportMap.set("allowForwardSlash", "true");
226
227        }
228        if (allowDash) {
229            exportMap.set("allowDash", "true");
230        }
231    }
232
233        @Override
234        protected String getValidationErrorMessageKeyOptions() {
235                final StringBuilder opts = new StringBuilder();
236
237                if (lowerCase) {
238                        opts.append(".lowerCase");
239                }
240                if (allowWhitespace) {
241                        opts.append(".allowWhitespace");
242                }
243                if (allowUnderscore) {
244                        opts.append(".allowUnderscore");
245                }
246                if (allowPeriod) {
247                        opts.append(".allowPeriod");
248                }
249                if(allowParenthesis) {
250                        opts.append(".allowParenthesis");
251                }
252                if(allowDollar) {
253                        opts.append(".allowDollar");
254                }
255                if(allowForwardSlash) {
256                        opts.append(".allowForwardSlash");
257                }
258        if (allowDash) {
259            opts.append(".allowDash");
260                }
261
262                return opts.toString();
263        }
264}