View Javadoc

1   /**
2    * Copyright 2005-2012 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", 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 (isAllowNegative()) {
50              regex.append("-?");
51          }
52          // final patter will be: -?([0-9]{0,p-s}\.[0-9]{1,s}|[0-9]{1,p-s}) where p = precision, s=scale
53          regex.append("(");
54          regex.append("[0-9]{0," + (getPrecision() - getScale()) + "}");
55          regex.append("\\.");
56          regex.append("[0-9]{1," + getScale() + "}");
57          regex.append("|[0-9]{1," + (getPrecision() - getScale()) + "}");
58          regex.append(")");
59          return regex.toString();
60      }
61  
62      /**
63       * @return the allowNegative
64       */
65      @BeanTagAttribute(name = "allowNegative")
66      public boolean isAllowNegative() {
67          return this.allowNegative;
68      }
69  
70      /**
71       * @param allowNegative the allowNegative to set
72       */
73      public void setAllowNegative(boolean allowNegative) {
74          this.allowNegative = allowNegative;
75      }
76  
77      /**
78       * @return the precision
79       */
80      @BeanTagAttribute(name = "precision")
81      public int getPrecision() {
82          return this.precision;
83      }
84  
85      /**
86       * @param precision the precision to set
87       */
88      public void setPrecision(int precision) {
89          this.precision = precision;
90      }
91  
92      /**
93       * @return the scale
94       */
95      @BeanTagAttribute(name = "scale")
96      public int getScale() {
97          return this.scale;
98      }
99  
100     /**
101      * @param scale the scale to set
102      */
103     public void setScale(int scale) {
104         this.scale = scale;
105     }
106 
107     /**
108      * This overridden method ...
109      *
110      * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidDataPatternConstraint#getValidationMessageParams()
111      */
112     @Override
113     public List<String> getValidationMessageParams() {
114         if (validationMessageParams == null) {
115             validationMessageParams = new ArrayList<String>();
116             ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
117             if (allowNegative) {
118                 validationMessageParams.add(configService.getPropertyValueAsString(
119                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positiveOrNegative"));
120             } else {
121                 validationMessageParams.add(configService.getPropertyValueAsString(
122                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positive"));
123             }
124 
125             validationMessageParams.add(Integer.toString(precision));
126             validationMessageParams.add(Integer.toString(scale));
127         }
128         return validationMessageParams;
129     }
130 
131     /**
132      * Validates different requirements of component compiling a series of reports detailing information on errors
133      * found in the component.  Used by the RiceDictionaryValidator.
134      *
135      * @param tracer Record of component's location
136      */
137     @Override
138     public void completeValidation(ValidationTrace tracer) {
139         tracer.addBean("FixedPointPatternConstraint", getMessageKey());
140 
141         if (getPrecision() <= getScale()) {
142             String currentValues[] = {"precision =" + getPrecision(), "scale = " + getScale()};
143             tracer.createError("Precision should greater than Scale", currentValues);
144         }
145 
146         super.completeValidation(tracer.getCopy());
147     }
148 
149 }