View Javadoc
1   /**
2    * Copyright 2005-2014 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   * @deprecated Use {@link org.kuali.rice.krad.datadictionary.validation.constraint.FixedPointPatternConstraint}.
25   */
26  @Deprecated
27  public class FixedPointValidationPattern extends FieldLevelValidationPattern {
28      public static final String PATTERN_TYPE_PRECISION = "fixedPoint.precision";
29      public static final String PATTERN_TYPE_SCALE = "fixedPoint.scale";
30  
31      protected boolean allowNegative;
32      protected int precision;
33      protected int scale;
34  
35      /**
36       * @return Returns the precision.
37       */
38      public int getPrecision() {
39          return precision;
40      }
41  
42      /**
43       * @param precision The precision to set.
44       */
45      public void setPrecision(int precision) {
46          this.precision = precision;
47      }
48  
49      /**
50       * @return Returns the scale.
51       */
52      public int getScale() {
53          return scale;
54      }
55  
56      /**
57       * @param scale The scale to set.
58       */
59      public void setScale(int scale) {
60          this.scale = scale;
61      }
62  
63      /**
64       * @return allowNegative
65       */
66      public boolean getAllowNegative() {
67          return allowNegative;
68      }
69  
70      /**
71       * @param allowNegative
72       */
73      public void setAllowNegative(boolean allowNegative) {
74          this.allowNegative = allowNegative;
75      }
76  
77      /**
78       * Adds special handling to account for optional allowNegative and dynamic precision, scale
79       * 
80       * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
81       */
82      @Override
83  	protected String getRegexString() {    	
84      	final StringBuilder regex = new StringBuilder();
85  
86          if (allowNegative) {
87              regex.append("-?");
88          }
89          // final patter will be: -?([0-9]{0,p-s}\.[0-9]{1,s}|[0-9]{1,p-s}) where p = precision, s=scale
90          regex.append("(");
91          regex.append("[0-9]{0," + (getPrecision() - getScale()) + "}");
92          regex.append("\\.");
93          regex.append("[0-9]{1," + getScale() + "}");
94          regex.append("|[0-9]{1," + (getPrecision() - getScale()) + "}");
95          regex.append(")");
96          return regex.toString();
97      }
98  
99      /**
100      * @see org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern#getPatternTypeName()
101      */
102     @Override
103 	protected String getPatternTypeName() {
104         return "fixedPoint";
105     }
106 
107 
108     /**
109      * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#buildExportMap(java.lang.String)
110      */
111     @Override
112 	public ExportMap buildExportMap(String exportKey) {
113         ExportMap exportMap = super.buildExportMap(exportKey);
114 
115         if (allowNegative) {
116             exportMap.set("allowNegative", "true");
117         }
118         exportMap.set("precision", Integer.toString(precision));
119         exportMap.set("scale", Integer.toString(scale));
120 
121         return exportMap;
122     }
123 
124 	/**
125 	 * @see org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern#getValidationErrorMessageKey()
126 	 */
127 	@Override
128 	public String getValidationErrorMessageKey() {
129 		StringBuilder buf = new StringBuilder();
130 		buf.append("error.format.").append(getClass().getName());
131 		if (allowNegative) {
132 			buf.append(".allowNegative");
133 		}
134 		return buf.toString();
135 	}
136 
137 	/**
138 	 * This overridden method ...
139 	 * 
140 	 * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getValidationErrorMessageParameters(java.lang.String)
141 	 */
142 	@Override
143 	public String[] getValidationErrorMessageParameters(String attributeLabel) {
144 		return new String[] {attributeLabel, String.valueOf(precision), String.valueOf(scale)};
145 	}
146 	
147 	@Override
148 	public void completeValidation() throws ValidationPatternException {
149 		super.completeValidation();
150 		
151     	final boolean valid =
152     		(getPrecision() >= 1) &&
153     		(getScale() >= 0) &&
154     		(getPrecision() >= getScale());
155     	
156     	if (!valid) {
157     		throw new ValidationPatternException("The precision must be >= 1.  The scale must be >= 0.  The precision must be >= scale. Precision: " + getPrecision() + " Scale: " + getScale());
158     	}
159 	}
160 }