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