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.krad.datadictionary.validation;
17  
18  import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
19  import org.kuali.rice.krad.util.KRADConstants;
20  
21  import java.util.regex.Pattern;
22  
23  /**
24   * Abstraction of the regular expressions used to validate attribute values.
25   * 
26   * 
27   */
28  @Deprecated
29  abstract public class CharacterLevelValidationPattern extends ValidationPattern {
30      protected Pattern regexPattern;
31  
32      protected int maxLength = -1;
33      protected int exactLength = -1;
34  
35      /**
36       * Sets maxLength parameter for the associated regex.
37       * 
38       * @param maxLength
39       */
40      public void setMaxLength(int maxLength) {
41          if (this.exactLength != -1) {
42              throw new IllegalStateException("illegal attempt to set maxLength after mutually-exclusive exactLength has been set");
43          }
44  
45          this.maxLength = maxLength;
46      }
47  
48      /**
49       * @return current maxLength, or -1 if none has been set
50       */
51      public int getMaxLength() {
52          return maxLength;
53      }
54  
55  
56      /**
57       * Sets exactLength parameter for the associated regex.
58       * 
59       * @param exactLength
60       */
61      public void setExactLength(int exactLength) {
62          if (this.maxLength != -1) {
63              throw new IllegalStateException("illegal attempt to set exactLength after mutually-exclusive maxLength has been set");
64          }
65  
66          this.exactLength = exactLength;
67      }
68  
69      /**
70       * @return current exactLength, or -1 if none has been set
71       */
72      public int getExactLength() {
73          return exactLength;
74      }
75  
76  
77      /**
78       * @return regular expression Pattern generated using the individual ValidationPattern subclass
79       */
80      final public Pattern getRegexPattern() {
81          if ( regexPattern == null ) {
82              String regexString = getRegexString();
83      
84              StringBuffer completeRegex = new StringBuffer("^");
85              completeRegex.append(getRegexString());
86      
87              if (maxLength != -1) {
88                  completeRegex.append("{0," + maxLength + "}");
89              }
90              else if (exactLength != -1) {
91                  completeRegex.append("{" + exactLength + "}");
92              }
93              else {
94                  completeRegex.append("*");
95              }
96      
97              completeRegex.append("$");
98      
99              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 }