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