1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.datadictionary.validation.constraint;
17
18 import org.kuali.rice.core.api.config.property.ConfigurationService;
19 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
20 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21 import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
22 import org.kuali.rice.krad.service.KRADServiceLocator;
23 import org.kuali.rice.krad.uif.UifConstants;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28
29
30
31
32
33 @BeanTag(name = "fixedPointPatternConstraint-bean", parent = "FixedPointPatternConstraint")
34 public class FixedPointPatternConstraint extends ValidDataPatternConstraint {
35
36 protected boolean allowNegative;
37 protected int precision;
38 protected int scale;
39
40
41
42
43
44
45 @Override
46 protected String getRegexString() {
47 StringBuilder regex = new StringBuilder();
48
49 if (getPrecision() < 0 || getScale() < 0 || getPrecision() - getScale() < 0){
50 throw new RuntimeException("Precision and scale cannot be negative AND scale cannot be greater than "
51 + "precision for FixedPointPatternConstraints!");
52 }
53
54 if (isAllowNegative()) {
55 regex.append("-?");
56 }
57
58
59 regex.append("(");
60 if(getPrecision() - getScale() > 0){
61 regex.append("[0-9]{0," + (getPrecision() - getScale()) + "}");
62 }
63 regex.append("\\.");
64 regex.append("[0-9]{1," + getScale() + "}");
65 if(getPrecision() - getScale() > 0){
66 regex.append("|[0-9]{1," + (getPrecision() - getScale()) + "}");
67 }
68 regex.append(")");
69 return regex.toString();
70 }
71
72
73
74
75 @BeanTagAttribute(name = "allowNegative")
76 public boolean isAllowNegative() {
77 return this.allowNegative;
78 }
79
80
81
82
83 public void setAllowNegative(boolean allowNegative) {
84 this.allowNegative = allowNegative;
85 }
86
87
88
89
90 @BeanTagAttribute(name = "precision")
91 public int getPrecision() {
92 return this.precision;
93 }
94
95
96
97
98 public void setPrecision(int precision) {
99 this.precision = precision;
100 }
101
102
103
104
105 @BeanTagAttribute(name = "scale")
106 public int getScale() {
107 return this.scale;
108 }
109
110
111
112
113 public void setScale(int scale) {
114 this.scale = scale;
115 }
116
117
118
119
120
121
122 @Override
123 public List<String> getValidationMessageParams() {
124 if (validationMessageParams == null) {
125 validationMessageParams = new ArrayList<String>();
126 ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
127 if (allowNegative) {
128 validationMessageParams.add(configService.getPropertyValueAsString(
129 UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positiveOrNegative"));
130 } else {
131 validationMessageParams.add(configService.getPropertyValueAsString(
132 UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positive"));
133 }
134
135 validationMessageParams.add(Integer.toString(precision));
136 validationMessageParams.add(Integer.toString(scale));
137 }
138 return validationMessageParams;
139 }
140
141
142
143
144
145
146
147 @Override
148 public void completeValidation(ValidationTrace tracer) {
149 tracer.addBean("FixedPointPatternConstraint", getMessageKey());
150
151 if (getPrecision() <= getScale()) {
152 String currentValues[] = {"precision =" + getPrecision(), "scale = " + getScale()};
153 tracer.createError("Precision should greater than Scale", currentValues);
154 }
155
156 super.completeValidation(tracer.getCopy());
157 }
158
159 }