View Javadoc

1   /**
2    * Copyright 2005-2013 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.krad.datadictionary.validation.constraint;
17  
18  import org.kuali.rice.core.api.config.property.ConfigurationService;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21  import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
22  import org.kuali.rice.krad.service.KRADServiceLocator;
23  import org.kuali.rice.krad.uif.UifConstants;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /**
29   * TODO delyea don't forget to fill this in.
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  @BeanTag(name = "fixedPointPatternConstraint-bean", parent = "FixedPointPatternConstraint")
34  public class FixedPointPatternConstraint extends ValidDataPatternConstraint {
35  
36      protected boolean allowNegative;
37      protected int precision;
38      protected int scale;
39  
40      /**
41       * Overriding retrieval of
42       *
43       * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersPatternConstraint#getRegexString()
44       */
45      @Override
46      protected String getRegexString() {
47          StringBuilder regex = new StringBuilder();
48  
49          if (getPrecision() < 0 || getScale() < 0 || getPrecision() - getScale() < 0){
50              throw new RuntimeException("Precision and scale cannot be negative AND scale cannot be greater than "
51                      + "precision for FixedPointPatternConstraints!");
52          }
53  
54          if (isAllowNegative()) {
55              regex.append("-?");
56          }
57          // final pattern will be: -?([0-9]{0,p-s}\.[0-9]{1,s}|[0-9]{1,p-s}) where p = precision, s=scale
58  
59          regex.append("(");
60          if(getPrecision() - getScale() > 0){
61              regex.append("[0-9]{0," + (getPrecision() - getScale()) + "}");
62          }
63          regex.append("\\.");
64          regex.append("[0-9]{1," + getScale() + "}");
65          if(getPrecision() - getScale() > 0){
66              regex.append("|[0-9]{1," + (getPrecision() - getScale()) + "}");
67          }
68          regex.append(")");
69          return regex.toString();
70      }
71  
72      /**
73       * @return the allowNegative
74       */
75      @BeanTagAttribute(name = "allowNegative")
76      public boolean isAllowNegative() {
77          return this.allowNegative;
78      }
79  
80      /**
81       * @param allowNegative the allowNegative to set
82       */
83      public void setAllowNegative(boolean allowNegative) {
84          this.allowNegative = allowNegative;
85      }
86  
87      /**
88       * @return the precision
89       */
90      @BeanTagAttribute(name = "precision")
91      public int getPrecision() {
92          return this.precision;
93      }
94  
95      /**
96       * @param precision the precision to set
97       */
98      public void setPrecision(int precision) {
99          this.precision = precision;
100     }
101 
102     /**
103      * @return the scale
104      */
105     @BeanTagAttribute(name = "scale")
106     public int getScale() {
107         return this.scale;
108     }
109 
110     /**
111      * @param scale the scale to set
112      */
113     public void setScale(int scale) {
114         this.scale = scale;
115     }
116 
117     /**
118      * This overridden method ...
119      *
120      * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidDataPatternConstraint#getValidationMessageParams()
121      */
122     @Override
123     public List<String> getValidationMessageParams() {
124         if (validationMessageParams == null) {
125             validationMessageParams = new ArrayList<String>();
126             ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
127             if (allowNegative) {
128                 validationMessageParams.add(configService.getPropertyValueAsString(
129                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positiveOrNegative"));
130             } else {
131                 validationMessageParams.add(configService.getPropertyValueAsString(
132                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positive"));
133             }
134 
135             validationMessageParams.add(Integer.toString(precision));
136             validationMessageParams.add(Integer.toString(scale));
137         }
138         return validationMessageParams;
139     }
140 
141     /**
142      * Validates different requirements of component compiling a series of reports detailing information on errors
143      * found in the component.  Used by the RiceDictionaryValidator.
144      *
145      * @param tracer Record of component's location
146      */
147     @Override
148     public void completeValidation(ValidationTrace tracer) {
149         tracer.addBean("FixedPointPatternConstraint", getMessageKey());
150 
151         if (getPrecision() <= getScale()) {
152             String currentValues[] = {"precision =" + getPrecision(), "scale = " + getScale()};
153             tracer.createError("Precision should greater than Scale", currentValues);
154         }
155 
156         super.completeValidation(tracer.getCopy());
157     }
158 
159 }