View Javadoc

1   /**
2    * Copyright 2005-2015 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 floating point numbers, optionally matching negative numbers
23   * 
24   * 
25   */
26  public class FloatingPointValidationPattern extends FieldLevelValidationPattern {
27      protected boolean allowNegative;
28  
29      /**
30       * @return allowNegative
31       */
32      public boolean getAllowNegative() {
33          return allowNegative;
34      }
35  
36      /**
37       * @param allowNegative
38       */
39      public void setAllowNegative(boolean allowNegative) {
40          this.allowNegative = allowNegative;
41      }
42  
43      /**
44       * Adds special handling to account for optional allowNegative
45       * 
46       * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
47       */
48      protected String getRegexString() {
49          StringBuffer regex = new StringBuffer();
50  
51          if (allowNegative) {
52              regex.append("-?");
53          }
54          regex.append(super.getRegexString());
55  
56          return regex.toString();
57      }
58  
59      /**
60       * @see org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern#getPatternTypeName()
61       */
62      protected String getPatternTypeName() {
63          return "floatingPoint";
64      }
65  
66  
67      /**
68       * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#buildExportMap(java.lang.String)
69       */
70      public ExportMap buildExportMap(String exportKey) {
71          ExportMap exportMap = super.buildExportMap(exportKey);
72  
73          if (allowNegative) {
74              exportMap.set("allowNegative", "true");
75          }
76  
77          return exportMap;
78      }
79      
80  	/**
81  	 * @see org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern#getValidationErrorMessageKey()
82  	 */
83  	@Override
84  	public String getValidationErrorMessageKey() {
85  		StringBuilder buf = new StringBuilder();
86  		buf.append("error.format.").append(getClass().getName());
87  		if (allowNegative) {
88  			buf.append(".allowNegative");
89  		}
90  		return buf.toString();
91  	}
92  }