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