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 abstract public class CharacterLevelValidationPattern extends ValidationPattern {
30 protected Pattern regexPattern;
31
32 protected int maxLength = -1;
33 protected int exactLength = -1;
34
35
36
37
38
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
50
51 public int getMaxLength() {
52 return maxLength;
53 }
54
55
56
57
58
59
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
71
72 public int getExactLength() {
73 return exactLength;
74 }
75
76
77
78
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
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
125
126
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
143
144
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 }