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