1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
@Deprecated |
29 | 0 | abstract public class CharacterLevelValidationPattern extends ValidationPattern { |
30 | |
protected Pattern regexPattern; |
31 | |
|
32 | 0 | protected int maxLength = -1; |
33 | 0 | protected int exactLength = -1; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public void setMaxLength(int maxLength) { |
41 | 0 | if (this.exactLength != -1) { |
42 | 0 | throw new IllegalStateException("illegal attempt to set maxLength after mutually-exclusive exactLength has been set"); |
43 | |
} |
44 | |
|
45 | 0 | this.maxLength = maxLength; |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public int getMaxLength() { |
52 | 0 | return maxLength; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public void setExactLength(int exactLength) { |
62 | 0 | if (this.maxLength != -1) { |
63 | 0 | throw new IllegalStateException("illegal attempt to set exactLength after mutually-exclusive maxLength has been set"); |
64 | |
} |
65 | |
|
66 | 0 | this.exactLength = exactLength; |
67 | 0 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public int getExactLength() { |
73 | 0 | return exactLength; |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
final public Pattern getRegexPattern() { |
81 | 0 | if ( regexPattern == null ) { |
82 | 0 | String regexString = getRegexString(); |
83 | |
|
84 | 0 | StringBuffer completeRegex = new StringBuffer("^"); |
85 | 0 | completeRegex.append(getRegexString()); |
86 | |
|
87 | 0 | if (maxLength != -1) { |
88 | 0 | completeRegex.append("{0," + maxLength + "}"); |
89 | |
} |
90 | 0 | else if (exactLength != -1) { |
91 | 0 | completeRegex.append("{" + exactLength + "}"); |
92 | |
} |
93 | |
else { |
94 | 0 | completeRegex.append("*"); |
95 | |
} |
96 | |
|
97 | 0 | completeRegex.append("$"); |
98 | |
|
99 | 0 | regexPattern = Pattern.compile(completeRegex.toString()); |
100 | |
} |
101 | 0 | return regexPattern; |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
final public ExportMap buildExportMap(String exportKey) { |
109 | 0 | ExportMap exportMap = new ExportMap(exportKey); |
110 | |
|
111 | 0 | if (getMaxLength() != -1) { |
112 | 0 | exportMap.set("maxLength", Integer.toString(getMaxLength())); |
113 | |
} |
114 | 0 | else if (getExactLength() != -1) { |
115 | 0 | exportMap.set("exactLength", Integer.toString(getExactLength())); |
116 | |
} |
117 | |
|
118 | 0 | extendExportMap(exportMap); |
119 | |
|
120 | 0 | return exportMap; |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
abstract public void extendExportMap(ExportMap exportMap); |
129 | |
|
130 | |
@Override |
131 | |
public String[] getValidationErrorMessageParameters(String attributeLabel) { |
132 | 0 | if (getMaxLength() != -1) { |
133 | 0 | return new String[] {attributeLabel, String.valueOf(getMaxLength())}; |
134 | |
} |
135 | 0 | if (getExactLength() != -1) { |
136 | 0 | return new String[] {attributeLabel, String.valueOf(getExactLength())}; |
137 | |
} |
138 | 0 | return new String[] {attributeLabel}; |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
@Override |
147 | |
public String getValidationErrorMessageKey() { |
148 | 0 | StringBuilder buf = new StringBuilder(); |
149 | 0 | buf.append("error.format.").append(getClass().getName()).append(getValidationErrorMessageKeyOptions()); |
150 | 0 | if (getMaxLength() != -1) { |
151 | 0 | buf.append(".maxLength"); |
152 | |
} |
153 | 0 | if (getExactLength() != -1) { |
154 | 0 | buf.append(".exactLength"); |
155 | |
} |
156 | 0 | return buf.toString(); |
157 | |
} |
158 | |
|
159 | |
protected String getValidationErrorMessageKeyOptions() { |
160 | 0 | return KRADConstants.EMPTY_STRING; |
161 | |
} |
162 | |
} |