1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.datadictionary.validation.fieldlevel; |
17 | |
|
18 | |
import org.kuali.rice.krad.datadictionary.exporter.ExportMap; |
19 | |
import org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | 0 | public class FixedPointValidationPattern extends FieldLevelValidationPattern { |
27 | |
public static final String PATTERN_TYPE_PRECISION = "fixedPoint.precision"; |
28 | |
public static final String PATTERN_TYPE_SCALE = "fixedPoint.scale"; |
29 | |
|
30 | |
protected boolean allowNegative; |
31 | |
protected int precision; |
32 | |
protected int scale; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public int getPrecision() { |
38 | 0 | return precision; |
39 | |
} |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public void setPrecision(int precision) { |
45 | 0 | this.precision = precision; |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public int getScale() { |
52 | 0 | return scale; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public void setScale(int scale) { |
59 | 0 | this.scale = scale; |
60 | 0 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public boolean getAllowNegative() { |
66 | 0 | return allowNegative; |
67 | |
} |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public void setAllowNegative(boolean allowNegative) { |
73 | 0 | this.allowNegative = allowNegative; |
74 | 0 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
@Override |
82 | |
protected String getRegexString() { |
83 | 0 | final StringBuilder regex = new StringBuilder(); |
84 | |
|
85 | 0 | if (allowNegative) { |
86 | 0 | regex.append("-?"); |
87 | |
} |
88 | |
|
89 | 0 | regex.append("("); |
90 | 0 | regex.append("[0-9]{0," + (getPrecision() - getScale()) + "}"); |
91 | 0 | regex.append("\\."); |
92 | 0 | regex.append("[0-9]{1," + getScale() + "}"); |
93 | 0 | regex.append("|[0-9]{1," + (getPrecision() - getScale()) + "}"); |
94 | 0 | regex.append(")"); |
95 | 0 | return regex.toString(); |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
@Override |
102 | |
protected String getPatternTypeName() { |
103 | 0 | return "fixedPoint"; |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
@Override |
111 | |
public ExportMap buildExportMap(String exportKey) { |
112 | 0 | ExportMap exportMap = super.buildExportMap(exportKey); |
113 | |
|
114 | 0 | if (allowNegative) { |
115 | 0 | exportMap.set("allowNegative", "true"); |
116 | |
} |
117 | 0 | exportMap.set("precision", Integer.toString(precision)); |
118 | 0 | exportMap.set("scale", Integer.toString(scale)); |
119 | |
|
120 | 0 | return exportMap; |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
@Override |
127 | |
public String getValidationErrorMessageKey() { |
128 | 0 | StringBuilder buf = new StringBuilder(); |
129 | 0 | buf.append("error.format.").append(getClass().getName()); |
130 | 0 | if (allowNegative) { |
131 | 0 | buf.append(".allowNegative"); |
132 | |
} |
133 | 0 | return buf.toString(); |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
@Override |
142 | |
public String[] getValidationErrorMessageParameters(String attributeLabel) { |
143 | 0 | return new String[] {attributeLabel, String.valueOf(precision), String.valueOf(scale)}; |
144 | |
} |
145 | |
|
146 | |
@Override |
147 | |
public void completeValidation() throws ValidationPatternException { |
148 | 0 | super.completeValidation(); |
149 | |
|
150 | 0 | final boolean valid = |
151 | |
(getPrecision() >= 1) && |
152 | |
(getScale() >= 0) && |
153 | |
(getPrecision() >= getScale()); |
154 | |
|
155 | 0 | if (!valid) { |
156 | 0 | throw new ValidationPatternException("The precision must be >= 1. The scale must be >= 0. The precision must be >= scale. Precision: " + getPrecision() + " Scale: " + getScale()); |
157 | |
} |
158 | 0 | } |
159 | |
} |