View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Validation pattern for matching fixed point numbers, optionally matching negative numbers
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       * @return Returns the precision.
36       */
37      public int getPrecision() {
38          return precision;
39      }
40  
41      /**
42       * @param precision The precision to set.
43       */
44      public void setPrecision(int precision) {
45          this.precision = precision;
46      }
47  
48      /**
49       * @return Returns the scale.
50       */
51      public int getScale() {
52          return scale;
53      }
54  
55      /**
56       * @param scale The scale to set.
57       */
58      public void setScale(int scale) {
59          this.scale = scale;
60      }
61  
62      /**
63       * @return allowNegative
64       */
65      public boolean getAllowNegative() {
66          return allowNegative;
67      }
68  
69      /**
70       * @param allowNegative
71       */
72      public void setAllowNegative(boolean allowNegative) {
73          this.allowNegative = allowNegative;
74      }
75  
76      /**
77       * Adds special handling to account for optional allowNegative and dynamic precision, scale
78       * 
79       * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
80       */
81      @Override
82  	protected String getRegexString() {    	
83      	final StringBuilder regex = new StringBuilder();
84  
85          if (allowNegative) {
86              regex.append("-?");
87          }
88          // final patter will be: -?([0-9]{0,p-s}\.[0-9]{1,s}|[0-9]{1,p-s}) where p = precision, s=scale
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       * @see org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern#getPatternTypeName()
100      */
101     @Override
102 	protected String getPatternTypeName() {
103         return "fixedPoint";
104     }
105 
106 
107     /**
108      * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#buildExportMap(java.lang.String)
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 	 * @see org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern#getValidationErrorMessageKey()
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 	 * This overridden method ...
138 	 * 
139 	 * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getValidationErrorMessageParameters(java.lang.String)
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 }