View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.datadictionary.validation.charlevel;
17  
18  import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
19  import org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern;
20  
21  /**
22   * Pattern for matching alphanumeric characters
23   * 
24   * Also, allows conditionally whitespace, underscore, period, parens, dollar signs, and forward slash.
25   */
26  public class AlphaNumericValidationPattern extends CharacterLevelValidationPattern {
27      protected boolean allowWhitespace = false;
28      protected boolean allowUnderscore = false;
29      protected boolean allowPeriod = false;
30  
31      protected boolean allowParenthesis = false;
32      protected boolean allowDollar = false;
33      protected boolean allowForwardSlash = false;
34      protected boolean lowerCase = false;
35      protected boolean allowDash = false;
36      
37      /**
38       * @return allowPeriod
39       */
40      public boolean getAllowPeriod() {
41          return allowPeriod;
42      }
43  
44      /**
45       * @param allowPeriod
46       */
47      public void setAllowPeriod(boolean allowPeriod) {
48          this.allowPeriod = allowPeriod;
49      }    
50      
51      /**
52  	 * @return the allowPeriod
53  	 */
54  	public boolean isAllowPeriod() {
55  		return allowPeriod;
56  	}
57      
58      /**
59  	 * @return the allowParenthesis
60  	 */
61  	public boolean isAllowParenthesis() {
62  		return allowParenthesis;
63  	}
64  
65  	/**
66  	 * @param allowParenthesis the allowParenthesis to set
67  	 */
68  	public void setAllowParenthesis(boolean allowParenthesis) {
69  		this.allowParenthesis = allowParenthesis;
70  	}
71  	
72  	/**
73  	 * @return the allowDollar
74  	 */
75  	public boolean isAllowDollar() {
76  		return allowDollar;
77  	}
78  
79  	/**
80  	 * @param allowDollar the allowDollar to set
81  	 */
82  	public void setAllowDollar(boolean allowDollar) {
83  		this.allowDollar = allowDollar;
84  	}
85  
86  	/**
87  	 * @return the allowforwardSlash
88  	 */
89  	public boolean isAllowForwardSlash() {
90  		return allowForwardSlash;
91  	}
92  
93  	/**
94  	 * @param allowForwardSlash the allowforwardSlash to set
95  	 */
96  	public void setAllowForwardSlash(boolean allowForwardSlash) {
97  		this.allowForwardSlash = allowForwardSlash;
98  	}
99      
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 }