View Javadoc

1   /**
2    * Copyright 2005-2014 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.service.KRADServiceLocator;
23  import org.kuali.rice.krad.uif.UifConstants;
24  
25  /**
26   * TODO delyea don't forget to fill this in.
27   * 
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  public class FixedPointPatternConstraint extends ValidDataPatternConstraint {
31  
32      protected boolean allowNegative;
33      protected int precision;
34      protected int scale;
35  
36      /**
37       * Overriding retrieval of
38       * 
39       * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersPatternConstraint#getRegexString()
40       */
41      @Override
42      protected String getRegexString() {
43          StringBuilder regex = new StringBuilder();
44  
45          if (isAllowNegative()) {
46              regex.append("-?");
47          }
48          // final patter will be: -?([0-9]{0,p-s}\.[0-9]{1,s}|[0-9]{1,p-s}) where p = precision, s=scale
49          regex.append("(");
50          regex.append("[0-9]{0," + (getPrecision() - getScale()) + "}");
51          regex.append("\\.");
52          regex.append("[0-9]{1," + getScale() + "}");
53          regex.append("|[0-9]{1," + (getPrecision() - getScale()) + "}");
54          regex.append(")");
55          return regex.toString();
56      }
57  
58      /**
59       * @return the allowNegative
60       */
61      public boolean isAllowNegative() {
62          return this.allowNegative;
63      }
64  
65      /**
66       * @param allowNegative the allowNegative to set
67       */
68      public void setAllowNegative(boolean allowNegative) {
69          this.allowNegative = allowNegative;
70      }
71  
72      /**
73       * @return the precision
74       */
75      public int getPrecision() {
76          return this.precision;
77      }
78  
79      /**
80       * @param precision the precision to set
81       */
82      public void setPrecision(int precision) {
83          this.precision = precision;
84      }
85  
86      /**
87       * @return the scale
88       */
89      public int getScale() {
90          return this.scale;
91      }
92  
93      /**
94       * @param scale the scale to set
95       */
96      public void setScale(int scale) {
97          this.scale = scale;
98      }
99  
100     /**
101      * This overridden method ...
102      * 
103      * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidDataPatternConstraint#getValidationMessageParams()
104      */
105     @Override
106     public List<String> getValidationMessageParams() {
107         if(validationMessageParams == null){
108             validationMessageParams = new ArrayList<String>();
109             ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
110             if (allowNegative) {
111                 validationMessageParams.add(configService.getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX
112                         + "positiveOrNegative"));
113             } else {
114                 validationMessageParams.add(configService.getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX
115                         + "positive"));
116             }
117     
118             validationMessageParams.add(Integer.toString(precision));
119             validationMessageParams.add(Integer.toString(scale));
120         }
121         return validationMessageParams;
122     }
123 
124 }