View Javadoc

1   /**
2    * Copyright 2005-2013 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      
36      /**
37       * @return allowPeriod
38       */
39      public boolean getAllowPeriod() {
40          return allowPeriod;
41      }
42  
43      /**
44       * @param allowPeriod
45       */
46      public void setAllowPeriod(boolean allowPeriod) {
47          this.allowPeriod = allowPeriod;
48      }    
49      
50      /**
51  	 * @return the allowPeriod
52  	 */
53  	public boolean isAllowPeriod() {
54  		return allowPeriod;
55  	}
56      
57      /**
58  	 * @return the allowParenthesis
59  	 */
60  	public boolean isAllowParenthesis() {
61  		return allowParenthesis;
62  	}
63  
64  	/**
65  	 * @param allowParenthesis the allowParenthesis to set
66  	 */
67  	public void setAllowParenthesis(boolean allowParenthesis) {
68  		this.allowParenthesis = allowParenthesis;
69  	}
70  	
71  	/**
72  	 * @return the allowDollar
73  	 */
74  	public boolean isAllowDollar() {
75  		return allowDollar;
76  	}
77  
78  	/**
79  	 * @param allowDollar the allowDollar to set
80  	 */
81  	public void setAllowDollar(boolean allowDollar) {
82  		this.allowDollar = allowDollar;
83  	}
84  
85  	/**
86  	 * @return the allowforwardSlash
87  	 */
88  	public boolean isAllowForwardSlash() {
89  		return allowForwardSlash;
90  	}
91  
92  	/**
93  	 * @param allowforwardSlash the allowforwardSlash to set
94  	 */
95  	public void setAllowForwardSlash(boolean allowForwardSlash) {
96  		this.allowForwardSlash = allowForwardSlash;
97  	}
98      
99      /**
100      * @return allowWhitespace
101      */
102     public boolean getAllowWhitespace() {
103         return allowWhitespace;
104     }
105 
106     /**
107      * @param allowWhitespace
108      */
109     public void setAllowWhitespace(boolean allowWhitespace) {
110         this.allowWhitespace = allowWhitespace;
111     }
112 
113 
114     /**
115      * @return allowUnderscore
116      */
117     public boolean getAllowUnderscore() {
118         return allowUnderscore;
119     }
120 
121     /**
122      * @param allowWhitespace
123      */
124     public void setAllowUnderscore(boolean allowUnderscore) {
125         this.allowUnderscore = allowUnderscore;
126     }
127    
128     /**
129 	 * @return the lowerCase
130 	 */
131 	public boolean isLowerCase() {
132 		return this.lowerCase;
133 	}
134 
135 	/**
136 	 * @param lowerCase the lowerCase to set
137 	 */
138 	public void setLowerCase(boolean lowerCase) {
139 		this.lowerCase = lowerCase;
140 	}
141     
142     /**
143      * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
144      */
145     protected String getRegexString() {
146     	StringBuilder regexString = new StringBuilder("[A-Za-z0-9");
147     	
148     	/*
149     	 * This check must be first because we are removing the base 'A-Z' if lowerCase == true
150     	 */
151     	if(lowerCase){
152     		regexString = new StringBuilder("[a-z0-9");
153     	}
154 
155         if (allowWhitespace) {
156             regexString.append("\\s");
157         }
158         if (allowUnderscore) {
159             regexString.append("_");
160         }
161         if (allowPeriod) {
162             regexString.append(".");
163         }
164         if(allowParenthesis) {
165         	regexString.append("(");
166         	regexString.append(")");
167         }
168         if(allowDollar) {
169         	regexString.append("$");
170         }
171         if(allowForwardSlash) {
172         	regexString.append("/");
173         }
174         regexString.append("]");
175 
176         return regexString.toString();
177     }
178 
179 
180     /**
181      * @see org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern#extendExportMap(org.kuali.bo.datadictionary.exporter.ExportMap)
182      */
183     public void extendExportMap(ExportMap exportMap) {
184         exportMap.set("type", "alphaNumeric");
185 
186         if (lowerCase) {
187             exportMap.set("allowUpperCase", "true");
188         }
189         if (allowWhitespace) {
190             exportMap.set("allowWhitespace", "true");
191         }
192         if (allowUnderscore) {
193             exportMap.set("allowUnderscore", "true");
194         }
195         if (allowPeriod) {
196         	exportMap.set("allowPeriod", "true");
197         }
198         if(allowParenthesis) {
199             exportMap.set("allowParenthesis", "true");
200 
201         }
202         if(allowDollar) {
203             exportMap.set("allowDollar", "true");
204 
205         }
206         if(allowForwardSlash) {
207             exportMap.set("allowForwardSlash", "true");
208 
209         }
210     }
211 
212 	@Override
213 	protected String getValidationErrorMessageKeyOptions() {
214 		final StringBuilder opts = new StringBuilder();
215 
216 		if (lowerCase) {
217 			opts.append(".lowerCase");
218 		}
219 		if (allowWhitespace) {
220 			opts.append(".allowWhitespace");
221 		}
222 		if (allowUnderscore) {
223 			opts.append(".allowUnderscore");
224 		}
225 		if (allowPeriod) {
226 			opts.append(".allowPeriod");
227 		}
228 		if(allowParenthesis) {
229 			opts.append(".allowParenthesis");
230 		}
231 		if(allowDollar) {
232 			opts.append(".allowDollar");
233 		}
234 		if(allowForwardSlash) {
235 			opts.append(".allowForwardSlash");
236 		}
237 
238 		return opts.toString();
239 	}
240 }