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 java.util.ArrayList;
19 import java.util.List;
20
21 import org.kuali.rice.core.api.config.property.ConfigurationService;
22 import org.kuali.rice.krad.datadictionary.validator.ErrorReport;
23 import org.kuali.rice.krad.datadictionary.validator.TracerToken;
24 import org.kuali.rice.krad.service.KRADServiceLocator;
25 import org.kuali.rice.krad.uif.UifConstants;
26
27
28
29
30
31
32 public class FixedPointPatternConstraint extends ValidDataPatternConstraint {
33
34 protected boolean allowNegative;
35 protected int precision;
36 protected int scale;
37
38
39
40
41
42
43 @Override
44 protected String getRegexString() {
45 StringBuilder regex = new StringBuilder();
46
47 if (isAllowNegative()) {
48 regex.append("-?");
49 }
50
51 regex.append("(");
52 regex.append("[0-9]{0," + (getPrecision() - getScale()) + "}");
53 regex.append("\\.");
54 regex.append("[0-9]{1," + getScale() + "}");
55 regex.append("|[0-9]{1," + (getPrecision() - getScale()) + "}");
56 regex.append(")");
57 return regex.toString();
58 }
59
60
61
62
63 public boolean isAllowNegative() {
64 return this.allowNegative;
65 }
66
67
68
69
70 public void setAllowNegative(boolean allowNegative) {
71 this.allowNegative = allowNegative;
72 }
73
74
75
76
77 public int getPrecision() {
78 return this.precision;
79 }
80
81
82
83
84 public void setPrecision(int precision) {
85 this.precision = precision;
86 }
87
88
89
90
91 public int getScale() {
92 return this.scale;
93 }
94
95
96
97
98 public void setScale(int scale) {
99 this.scale = scale;
100 }
101
102
103
104
105
106
107 @Override
108 public List<String> getValidationMessageParams() {
109 if(validationMessageParams == null){
110 validationMessageParams = new ArrayList<String>();
111 ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
112 if (allowNegative) {
113 validationMessageParams.add(configService.getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX
114 + "positiveOrNegative"));
115 } else {
116 validationMessageParams.add(configService.getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX
117 + "positive"));
118 }
119
120 validationMessageParams.add(Integer.toString(precision));
121 validationMessageParams.add(Integer.toString(scale));
122 }
123 return validationMessageParams;
124 }
125
126
127
128
129
130
131
132
133 @Override
134 public ArrayList<ErrorReport> completeValidation(TracerToken tracer){
135 ArrayList<ErrorReport> reports=new ArrayList<ErrorReport>();
136 tracer.addBean("FixedPointPatternConstraint", getMessageKey());
137
138 if(getPrecision()<=getScale()){
139 ErrorReport error = new ErrorReport(ErrorReport.ERROR);
140 error.setValidationFailed("Precision should greater than Scale");
141 error.setBeanLocation(tracer.getBeanLocation());
142 error.addCurrentValue("precision ="+getPrecision());
143 error.addCurrentValue("scale = "+getScale());
144 reports.add(error);
145 }
146
147 reports.addAll(super.completeValidation(tracer.getCopy()));
148
149 return reports;
150 }
151
152 }