Coverage Report - org.kuali.rice.kns.datadictionary.validation.fieldlevel.FixedPointValidationPattern
 
Classes in this File Line Coverage Branch Coverage Complexity
FixedPointValidationPattern
0%
0/38
0%
0/14
1.583
 
 1  
 /**
 2  
  * Copyright 2005-2011 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  0
 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  0
         return precision;
 39  
     }
 40  
 
 41  
     /**
 42  
      * @param precision The precision to set.
 43  
      */
 44  
     public void setPrecision(int precision) {
 45  0
         this.precision = precision;
 46  0
     }
 47  
 
 48  
     /**
 49  
      * @return Returns the scale.
 50  
      */
 51  
     public int getScale() {
 52  0
         return scale;
 53  
     }
 54  
 
 55  
     /**
 56  
      * @param scale The scale to set.
 57  
      */
 58  
     public void setScale(int scale) {
 59  0
         this.scale = scale;
 60  0
     }
 61  
 
 62  
     /**
 63  
      * @return allowNegative
 64  
      */
 65  
     public boolean getAllowNegative() {
 66  0
         return allowNegative;
 67  
     }
 68  
 
 69  
     /**
 70  
      * @param allowNegative
 71  
      */
 72  
     public void setAllowNegative(boolean allowNegative) {
 73  0
         this.allowNegative = allowNegative;
 74  0
     }
 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  0
             final StringBuilder regex = new StringBuilder();
 84  
 
 85  0
         if (allowNegative) {
 86  0
             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  0
         regex.append("(");
 90  0
         regex.append("[0-9]{0," + (getPrecision() - getScale()) + "}");
 91  0
         regex.append("\\.");
 92  0
         regex.append("[0-9]{1," + getScale() + "}");
 93  0
         regex.append("|[0-9]{1," + (getPrecision() - getScale()) + "}");
 94  0
         regex.append(")");
 95  0
         return regex.toString();
 96  
     }
 97  
 
 98  
     /**
 99  
      * @see org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern#getPatternTypeName()
 100  
      */
 101  
     @Override
 102  
         protected String getPatternTypeName() {
 103  0
         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  0
         ExportMap exportMap = super.buildExportMap(exportKey);
 113  
 
 114  0
         if (allowNegative) {
 115  0
             exportMap.set("allowNegative", "true");
 116  
         }
 117  0
         exportMap.set("precision", Integer.toString(precision));
 118  0
         exportMap.set("scale", Integer.toString(scale));
 119  
 
 120  0
         return exportMap;
 121  
     }
 122  
 
 123  
         /**
 124  
          * @see org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern#getValidationErrorMessageKey()
 125  
          */
 126  
         @Override
 127  
         public String getValidationErrorMessageKey() {
 128  0
                 StringBuilder buf = new StringBuilder();
 129  0
                 buf.append("error.format.").append(getClass().getName());
 130  0
                 if (allowNegative) {
 131  0
                         buf.append(".allowNegative");
 132  
                 }
 133  0
                 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  0
                 return new String[] {attributeLabel, String.valueOf(precision), String.valueOf(scale)};
 144  
         }
 145  
         
 146  
         @Override
 147  
         public void completeValidation() throws ValidationPatternException {
 148  0
                 super.completeValidation();
 149  
                 
 150  0
             final boolean valid =
 151  
                     (getPrecision() >= 1) &&
 152  
                     (getScale() >= 0) &&
 153  
                     (getPrecision() >= getScale());
 154  
             
 155  0
             if (!valid) {
 156  0
                     throw new ValidationPatternException("The precision must be >= 1.  The scale must be >= 0.  The precision must be >= scale. Precision: " + getPrecision() + " Scale: " + getScale());
 157  
             }
 158  0
         }
 159  
 }