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", 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 (isAllowNegative()) {
50 regex.append("-?");
51 }
52
53 regex.append("(");
54 regex.append("[0-9]{0," + (getPrecision() - getScale()) + "}");
55 regex.append("\\.");
56 regex.append("[0-9]{1," + getScale() + "}");
57 regex.append("|[0-9]{1," + (getPrecision() - getScale()) + "}");
58 regex.append(")");
59 return regex.toString();
60 }
61
62
63
64
65 @BeanTagAttribute(name = "allowNegative")
66 public boolean isAllowNegative() {
67 return this.allowNegative;
68 }
69
70
71
72
73 public void setAllowNegative(boolean allowNegative) {
74 this.allowNegative = allowNegative;
75 }
76
77
78
79
80 @BeanTagAttribute(name = "precision")
81 public int getPrecision() {
82 return this.precision;
83 }
84
85
86
87
88 public void setPrecision(int precision) {
89 this.precision = precision;
90 }
91
92
93
94
95 @BeanTagAttribute(name = "scale")
96 public int getScale() {
97 return this.scale;
98 }
99
100
101
102
103 public void setScale(int scale) {
104 this.scale = scale;
105 }
106
107
108
109
110
111
112 @Override
113 public List<String> getValidationMessageParams() {
114 if (validationMessageParams == null) {
115 validationMessageParams = new ArrayList<String>();
116 ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
117 if (allowNegative) {
118 validationMessageParams.add(configService.getPropertyValueAsString(
119 UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positiveOrNegative"));
120 } else {
121 validationMessageParams.add(configService.getPropertyValueAsString(
122 UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positive"));
123 }
124
125 validationMessageParams.add(Integer.toString(precision));
126 validationMessageParams.add(Integer.toString(scale));
127 }
128 return validationMessageParams;
129 }
130
131
132
133
134
135
136
137 @Override
138 public void completeValidation(ValidationTrace tracer) {
139 tracer.addBean("FixedPointPatternConstraint", getMessageKey());
140
141 if (getPrecision() <= getScale()) {
142 String currentValues[] = {"precision =" + getPrecision(), "scale = " + getScale()};
143 tracer.createError("Precision should greater than Scale", currentValues);
144 }
145
146 super.completeValidation(tracer.getCopy());
147 }
148
149 }