001/** 002 * Copyright 2005-2018 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.kns.datadictionary.validation.fieldlevel; 017 018import org.kuali.rice.krad.datadictionary.exporter.ExportMap; 019import org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern; 020 021/** 022 * Validation pattern for matching fixed point numbers, optionally matching negative numbers 023 * 024 * @deprecated Use {@link org.kuali.rice.krad.datadictionary.validation.constraint.FixedPointPatternConstraint}. 025 */ 026@Deprecated 027public class FixedPointValidationPattern extends FieldLevelValidationPattern { 028 public static final String PATTERN_TYPE_PRECISION = "fixedPoint.precision"; 029 public static final String PATTERN_TYPE_SCALE = "fixedPoint.scale"; 030 031 protected boolean allowNegative; 032 protected int precision; 033 protected int scale; 034 035 /** 036 * @return Returns the precision. 037 */ 038 public int getPrecision() { 039 return precision; 040 } 041 042 /** 043 * @param precision The precision to set. 044 */ 045 public void setPrecision(int precision) { 046 this.precision = precision; 047 } 048 049 /** 050 * @return Returns the scale. 051 */ 052 public int getScale() { 053 return scale; 054 } 055 056 /** 057 * @param scale The scale to set. 058 */ 059 public void setScale(int scale) { 060 this.scale = scale; 061 } 062 063 /** 064 * @return allowNegative 065 */ 066 public boolean getAllowNegative() { 067 return allowNegative; 068 } 069 070 /** 071 * @param allowNegative 072 */ 073 public void setAllowNegative(boolean allowNegative) { 074 this.allowNegative = allowNegative; 075 } 076 077 /** 078 * Adds special handling to account for optional allowNegative and dynamic precision, scale 079 * 080 * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString() 081 */ 082 @Override 083 protected String getRegexString() { 084 final StringBuilder regex = new StringBuilder(); 085 086 if (allowNegative) { 087 regex.append("-?"); 088 } 089 // final patter will be: -?([0-9]{0,p-s}\.[0-9]{1,s}|[0-9]{1,p-s}) where p = precision, s=scale 090 regex.append("("); 091 regex.append("[0-9]{0," + (getPrecision() - getScale()) + "}"); 092 regex.append("\\."); 093 regex.append("[0-9]{1," + getScale() + "}"); 094 regex.append("|[0-9]{1," + (getPrecision() - getScale()) + "}"); 095 regex.append(")"); 096 return regex.toString(); 097 } 098 099 /** 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}